Programas en C#

Tuesday, March 20, 2007

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{

class Persona
{
public string Nombre;
public int Edad;
public string NIF;

public void Cumpleaños()
{
Edad++;
}
public Persona(string nombre, int edad, string nif)
{
Nombre = nombre;
Edad = edad;
NIF = nif;
}
}

class Trabajador : Persona
{

public int Sueldo;
public Trabajador(string nombre, int edad, string nif, int sueldo)
: base(nombre, edad, nif)
{
Sueldo = sueldo;
}

}




class Program
{


static void vertrabajador(Trabajador p)
{

Console.WriteLine("NOMBRE-->" + p.Nombre);
Console.WriteLine("EDAD---->" + p.Edad);
Console.WriteLine("NIF----->" + p.NIF);
Console.WriteLine("SUELDO-->" + p.Sueldo);
}

static void creartrabajadores(Trabajador[] ListaT)
{

for (int i = 0; i < ListaT.Length; i++)
{

Console.WriteLine("Escriba el nombre del trabajador");
string nombre = Console.ReadLine();
Console.WriteLine("Escriba la edad");
int edad = Int32.Parse(Console.ReadLine());
Console.WriteLine("Escriba el NIF");
string nif = Console.ReadLine();
Console.WriteLine("");
ListaT[i] = new Trabajador(nombre, edad, nif, 1000);
vertrabajador(ListaT[i]);
}
}

static void Main(string[] args)
{
Trabajador[] ListaT = new Trabajador[3];

int menu = 0;

while (menu != 4)
{
Console.WriteLine("1.Crear Objetos");
Console.WriteLine("2.Acceder Posición Objetos");
Console.WriteLine("3.Borrar Objetos");
Console.WriteLine("4.Salir");

menu = Int32.Parse(Console.ReadLine());
if (menu == 1) { creartrabajadores(ListaT); }

}

}
}
}

Labels:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int dim = 10;
int[,] tabla = new int[dim,dim];
int[,] tabla1 = new int[dim,dim];
int menu = 0;
while (menu != 5)
{
Console.WriteLine(" ------------------");
Console.WriteLine("| 1.Crear Tabla |");
Console.WriteLine("| 2.Jugar |");
Console.WriteLine("| 3.Crear Tabla 3 |");
Console.WriteLine("| 4.Solucion |");
Console.WriteLine("| 5.Salir |");
Console.WriteLine(" ------------------");
menu = Int32.Parse(Console.ReadLine());
if (menu == 1)
{
crear3(tabla);
crear2(tabla);
crear(tabla);

}
if (menu == 2)
{
jugar(tabla1, tabla);
}
if (menu == 3)
{
crear3(tabla);
}
if (menu == 4)
{
mostrartabla(tabla);

}
}

}
public static void crear(int[,] tabla)
{
int fila = 0, columna = 0;
int barcos = 0;
Random x = new Random();



while (barcos < 5)
{
fila = x.Next(5);
columna = x.Next(5);

if (tabla[fila, columna] == 0)
{
tabla[fila, columna] = 1;
barcos++;
}

}
}
public static void crear2(int[,] tabla)
{
int dim = 10;
int f = 0, c = 0,f1=0,c1=0,r=0;
int barcos = 0;
Random x = new Random();


while (barcos < 4)
{

f = x.Next(dim);
c = x.Next(dim);
r = x.Next(3) - 1;

f1 = f + r ;
r = x.Next(3) - 1;
c1 = c + r ;

while ((f1 > 9) || (c1 > 9) || (f1 < 0) || (c1 < 0) || ((f1 == f) && (c1 == c)) || (tabla[f1, c1] == 1))
{
f = x.Next(dim);
c = x.Next(dim);
r = x.Next(3) - 1;

f1 = f + r ;
r = x.Next(3) - 1;
c1 = c + r ;


}
tabla[f, c] = 1;
tabla[f1, c1] = 1;
barcos++;



}
}
public static void crear3(int[,] tabla)
{

int dim = 10;
int f = 0, c = 0;
int barcos = 0, dir = 0;
Random x = new Random();



while (barcos < 3)
{

f = x.Next(dim);
c = x.Next(dim);
dir = x.Next(3);



if ((dir == 0) && (f>=0)&&(c<=9)&&(f+1<=9)&&(f+2<=9)&&(c>=0)&&(c+1>=0)&&(c+1<=9)&&(c+2>=0)&&(c+2<=9)&&(tabla[f, c] == 0) && (tabla[f + 1, c + 1] == 0) && (tabla[f + 2, c + 2] == 0))
{
tabla[f, c] = 1;
tabla[f + 1, c + 1] = 1;
tabla[f + 2, c + 2] = 1;
barcos++;

}
if ((dir == 1) && (f >= 0) && (c <= 9) && (f + 1 >= 0) && (f + 1 <= 9) && (f + 2 >= 0) && (f + 2 <=9)&&(c>=0) && (tabla[f, c] == 0) && (tabla[f + 1, c] == 0) && (tabla[f + 2, c] == 0))
{
tabla[f, c] = 1;
tabla[f + 1, c] = 1;
tabla[f + 2, c] = 1;
barcos++;
}
if ((dir == 2) && (f >= 0) && (c <= 9) && (f - 1 >= 0) && (f - 1 <= 9) && (f - 2 >= 0) && (f - 2 <= 9) && (c >= 0) && (c - 1 >= 0)&&(c - 1 <= 9)&& (c - 2 >= 0)&&(c - 2 <= 9)&&(tabla[f, c] == 0) && (tabla[f - 1, c - 1] == 0) && (tabla[f - 2, c - 2] == 0))
{
tabla[f, c] = 1;
tabla[f-1, c ] = 1;
tabla[f-2, c ] = 1;
barcos++;
}
if ((dir == 3) && (f >= 0) && (c <= 9) && (c + 1 >= 0) && (c + 1 <= 9) && (c + 2 >= 0) && (c + 2 <= 9) && (c >= 0) && (c + 1 >= 0) && (c + 1 <= 9) &&(tabla[f, c] == 0) && (tabla[f, c + 1] == 0) && (tabla[f, c + 2] == 0))
{
tabla[f, c] = 1;
tabla[f, c + 1] = 1;
tabla[f, c + 2] = 1;
barcos++;
}


}
}

public static void mostrar(int[,] tabla1, int[,] tabla)
{

Console.WriteLine("------MOSTRAR TABLA------");

for (int f = 0; f < tabla.GetLength(0); f++)
{
for (int c = 0; c < tabla.GetLength(1); c++)
{
if (tabla1[f, c] == 1)
Console.Write(tabla[f, c]);
else
Console.Write("X");


}

Console.WriteLine();



}
}
public static void jugar(int[,] tabla1, int[,] tabla)
{
int fila = 0; int columna = 0;
int barcos = 0;
int intentos = 0;
while (barcos < 6)
{
Console.Write("Posición fila?");
fila = Int32.Parse(Console.ReadLine());
Console.Write("Posición columna?");
columna = Int32.Parse(Console.ReadLine());


tabla1[fila, columna] = 1;

if (tabla[fila, columna] == 1)
{
barcos++;
}


mostrar(tabla1, tabla);
intentos++;
Console.WriteLine("-------------------------");
Console.WriteLine("Llevas | {0} | Intentos",intentos);
Console.WriteLine("-------------------------");
Console.WriteLine();
}
Console.WriteLine("Ha Hundido la flota");

}
public static void mostrartabla(int[,] t)
{

Console.WriteLine("MOSTRAR TABLA");

for (int f = 0; f < t.GetLength(0); f++)
{
for (int c = 0; c < t.GetLength(1); c++)
{
Console.Write(t[f, c]);
}
Console.WriteLine();
}
}
}
}

Labels: