sábado, 22 de octubre de 2011

Tres en Raya - C Sharp

Aca dejo el Código fuente de este pequeño juego pero vastante jodido por dentro.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int n = 0;
            string ingresar = "";
            bool validar = false;
            bool salir = false;
            const int ARRAY1_L = 10;
            int[] array1 = new int[ARRAY1_L];
            array1[n] = 0;
            char[] array2 = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            Console.SetCursorPosition(20, 0);
            Console.WriteLine(" TRES EN RAYA ");
            Console.WriteLine();
            Console.SetCursorPosition(20, 1);
            int[,] board = new int[3, 3];
            Console.SetCursorPosition(0, 0);

            Console.WriteLine();
            Console.WriteLine("   |  | ");
            Console.WriteLine("   |  | ");
            Console.WriteLine("  1| 2| 3 ");
            Console.WriteLine("------------ ");
            Console.WriteLine("   |  | ");
            Console.WriteLine("   |  | ");
            Console.WriteLine("  4| 5| 6 ");
            Console.WriteLine("------------ ");
            Console.WriteLine("   |  | ");
            Console.WriteLine("   |  | ");
            Console.WriteLine("  7| 8| 9 ");
            Console.WriteLine();
            Console.WriteLine();

            while (!salir)
            {
                for (i = 0; i < 9; i++)
                {
                  
                    Console.SetCursorPosition(0, 13);
                    Console.WriteLine(" Ingresa numeros del 1 al 9: ");

                    while (!validar)
                    {
                        ingresar = Console.ReadLine();

                        if (!int.TryParse(ingresar, out n) || n < 0 || n > 10)
                        {
                            Console.WriteLine("Solo se permiten números del 1 al 9: ");
                        }
                        else
                        {
                            if (array1[n] == n)
                            {
                                Console.WriteLine(" Espacio utilizado, use otro : ");
                            }
                            else
                            {
                                validar = true;
                            }
                        }
                    }
                    array1[n] = n;
                    validar = false;

                    if (n == 1)
                    {
                        Console.SetCursorPosition(2, 2);
                    }
                    if (n == 2)
                    {
                        Console.SetCursorPosition(5, 2);
                    }
                    if (n == 3)
                    {
                        Console.SetCursorPosition(8, 2);
                    }
                    if (n == 4)
                    {
                        Console.SetCursorPosition(2, 6);
                    }
                    if (n == 5)
                    {
                        Console.SetCursorPosition(5, 6);
                    }
                    if (n == 6)
                    {
                        Console.SetCursorPosition(8, 6);
                    }
                    if (n == 7)
                    {
                        Console.SetCursorPosition(2, 10);
                    }
                    if (n == 8)
                    {
                        Console.SetCursorPosition(5, 10);
                    }
                    if (n == 9)
                    {
                        Console.SetCursorPosition(8, 10);
                    }

                    if (i % 2 == 0)
                    {
                        Console.WriteLine("X");
                        array2[n - 1] = 'X';
                    }
                    else
                    {
                        Console.WriteLine("0");
                        array2[n - 1] = '0';
                    }
                    if (
                    (array2[0] == array2[2] && array2[0] == array2[1]) ||
                    (array2[0] == array2[4] && array2[0] == array2[8]) ||
                    (array2[0] == array2[3] && array2[0] == array2[6]) ||

                    (array2[3] == array2[4] && array2[3] == array2[5]) ||
                    (array2[6] == array2[7] && array2[6] == array2[8]) ||
                    (array2[2] == array2[4] && array2[2] == array2[6]) ||

                    (array2[2] == array2[5] && array2[2] == array2[8]) ||
                    (array2[1] == array2[4] && array2[1] == array2[7])
                    )
                    {
                        salir = true;
                    }

                    if (salir == true)
                    {
                        Console.SetCursorPosition(0, 20);
                        Console.WriteLine("El Ganador es {0}", i % 2 == 0 ? "X" : "O");
                        break;
                    }
                }
            }
            Console.WriteLine("Presione cualquier tecla para salir");
            Console.ReadKey();
        }
    }
}

0 comentarios:

Publicar un comentario

Ahora estamos en www.adminpc.org

Twitter Delicious Facebook Digg Stumbleupon Favorites More