Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace practica3
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool ganador = false;
- //si la variable turno es true, sera el turno del j1, y si el false, sera el turno del j2
- bool turno = true;
- int m, n, col;
- Console.WriteLine("Escribe las dimensiones del tablero (filas x columnas)");
- m = Convert.ToInt32(Console.ReadLine());
- n = Convert.ToInt32(Console.ReadLine());
- int[,] tablero = new int[m, n];
- //llenamos el tablero de 0 (espacio vacio)
- for (int x = 0; x < m; x++)
- {
- for (int y = 0; y < n; y++)
- {
- tablero[x, y] = 0;
- }
- }
- Dibuixa(ref m, ref n, ref tablero);
- do
- {
- Console.WriteLine("En que columna quieres poner la ficha");
- col = Convert.ToInt32(Console.ReadLine());
- Console.Clear();
- Jugada(ref turno, ref tablero, ref col);
- Dibuixa(ref m, ref n, ref tablero);
- EnRatlla(ref tablero, ganador);
- } while (ganador == false);
- }
- //FUNCIONES//
- //mostrar tablero con colores
- static void Dibuixa(ref int m, ref int n, ref int[,] tablero)
- {
- Console.Clear();
- for (int x = 0; x < m; x++)
- {
- for (int y = 0; y < n; y++)
- {
- if (tablero[x, y] == 1)
- {
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.Write(tablero[x, y]);
- Console.ForegroundColor = ConsoleColor.White;
- }
- else
- {
- if (tablero[x, y] == 2)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write(tablero[x, y]);
- Console.ForegroundColor = ConsoleColor.White;
- }
- else
- {
- Console.Write(tablero[x, y]);
- }
- }
- }
- Console.WriteLine("");
- }
- }
- //Realizar una jugada
- static void Jugada(ref bool turno, ref int[,] tablero, ref int col)
- {
- bool turnocompletado = false;
- for (int x = tablero.GetLength(0) - 1; turnocompletado == false; x--)
- {
- if (x < 0)
- {
- Console.WriteLine("Turno no valido, presione una tecla para continuar jugando");
- Console.ReadLine();
- break;
- }
- if (turno == true)
- {
- if (tablero[x, col] == 0)
- {
- tablero[x, col] = 1;
- turnocompletado = true;
- turno = false;
- }
- }
- else
- {
- if (tablero[x, col] == 0)
- {
- tablero[x, col] = 2;
- turnocompletado = true;
- turno = true;
- }
- }
- }
- }
- //Comprobar si hay algun 4 en ralla
- static void EnRatlla(ref int[,] tablero, bool ganador)
- {
- //Comprobar columnas
- for(int x = tablero.GetLength(0)-1; x >= 0; x--)
- {
- for (int y = 0; y < tablero.GetLength(1) - 1; y++)
- {
- //jugador 1
- if (tablero[x, y] == 1)
- {
- if (tablero[x - 1, y] == 1)
- {
- if (tablero[x - 2, y] == 1)
- {
- if (tablero[x - 3, y] == 1)
- {
- Console.WriteLine("4 en ralla! Gana el jugador 1");
- Console.ReadLine();
- ganador = true;
- }
- }
- }
- if (tablero[x, y] == 1)
- {
- if (tablero[x, y + 1] == 1)
- {
- if (tablero[x, y + 2] == 1)
- {
- if (tablero[x, y + 3] == 1)
- {
- Console.WriteLine("4 en ralla! Gana el jugador 1");
- Console.ReadLine();
- ganador = true;
- }
- }
- }
- }
- }
- //jugador 2
- if (tablero[x, y] == 2)
- {
- if (tablero[x - 1, y] == 2)
- {
- if (tablero[x - 2, y] == 2)
- {
- if (tablero[x - 3, y] == 2)
- {
- Console.WriteLine("4 en ralla! Gana el jugador 2");
- Console.ReadLine();
- ganador = true;
- }
- }
- }
- if (tablero[x, y] == 2)
- {
- if (tablero[x, y + 1] == 2)
- {
- if (tablero[x, y + 2] == 2)
- {
- if (tablero[x, y + 3] == 2)
- {
- Console.WriteLine("4 en ralla! Gana el jugador 2");
- Console.ReadLine();
- ganador = true;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement