Advertisement
Ardurum

5 en ralla

Feb 7th, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using System;
  2.  
  3. namespace practica3
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             bool ganador = false;
  10.             //si la variable turno es true, sera el turno del j1, y si el false, sera el turno del j2
  11.             bool turno = true;
  12.             int m, n, col;
  13.             Console.WriteLine("Escribe las dimensiones del tablero (filas x columnas)");
  14.             m = Convert.ToInt32(Console.ReadLine());
  15.             n = Convert.ToInt32(Console.ReadLine());
  16.             int[,] tablero = new int[m, n];
  17.             //llenamos el tablero de 0 (espacio vacio)
  18.             for (int x = 0; x < m; x++)
  19.             {
  20.                 for (int y = 0; y < n; y++)
  21.                 {
  22.                     tablero[x, y] = 0;
  23.                 }
  24.  
  25.             }
  26.             Dibuixa(ref m, ref n, ref tablero);
  27.             do
  28.             {
  29.                 Console.WriteLine("En que columna quieres poner la ficha");
  30.                
  31.                 col = Convert.ToInt32(Console.ReadLine());
  32.                 Console.Clear();
  33.                 Jugada(ref turno, ref tablero, ref col);
  34.                 Dibuixa(ref m, ref n, ref tablero);
  35.                 EnRatlla(ref tablero);
  36.             } while (ganador == false);
  37.         }
  38.         //FUNCIONES//
  39.  
  40.         //mostrar tablero con colores
  41.         static void Dibuixa(ref int m, ref int n, ref int[,] tablero)
  42.         {
  43.             Console.Clear();
  44.             for (int x = 0; x < m; x++)
  45.             {
  46.                 for (int y = 0; y < n; y++)
  47.                 {
  48.                     if (tablero[x, y] == 1)
  49.                     {
  50.                         Console.ForegroundColor = ConsoleColor.Blue;
  51.                         Console.Write(tablero[x, y]);
  52.                         Console.ForegroundColor = ConsoleColor.White;
  53.                     }
  54.                     else
  55.                     {
  56.                         if (tablero[x, y] == 2)
  57.                         {
  58.                             Console.ForegroundColor = ConsoleColor.Red;
  59.                             Console.Write(tablero[x, y]);
  60.                             Console.ForegroundColor = ConsoleColor.White;
  61.                         }
  62.                         else
  63.                         {
  64.                             Console.Write(tablero[x, y]);
  65.                         }
  66.                     }
  67.                 }
  68.                 Console.WriteLine("");
  69.             }
  70.         }
  71.         //Realizar una jugada
  72.         static void Jugada(ref bool turno, ref int[,] tablero, ref int col)
  73.         {
  74.             bool turnocompletado = false;
  75.             for (int x = tablero.GetLength(0) - 1; turnocompletado == false; x--)
  76.             {
  77.                 if (x < 0)
  78.                 {
  79.                     Console.WriteLine("Turno no valido, presione una tecla para continuar jugando");
  80.                     Console.ReadLine();
  81.                     break;
  82.                 }
  83.                 if (turno == true)
  84.                 {
  85.                     if (tablero[x, col] == 0)
  86.                     {
  87.                         tablero[x, col] = 1;
  88.                         turnocompletado = true;
  89.                         turno = false;
  90.                     }
  91.                 }
  92.                 else
  93.                 {
  94.                     if (tablero[x, col] == 0)
  95.                     {
  96.                         tablero[x, col] = 2;
  97.                         turnocompletado = true;
  98.                         turno = true;
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.         //Comprobar si hay algun 4 en ralla
  104.         static void EnRatlla(ref int[,] tablero)
  105.         {
  106.             int checkrow, checkcol;
  107.             //Console.WriteLine("Escribe la fila y columna que quieres comprobar");
  108.             //checkrow = Convert.ToInt32(Console.ReadLine());
  109.             //checkcol = Convert.ToInt32(Console.ReadLine());
  110.             //comprobar fila
  111.             for(int x = 0; x < tablero.GetLength(0)-5; x++)
  112.             {
  113.                 for(int y = 0; y < tablero.GetLength(1)-5; y++)
  114.                 {
  115.                     if (tablero[x, y] == 1)
  116.                     {
  117.                         if (tablero[x + 1,y] == 1)
  118.                         {
  119.                             if (tablero[x + 2, y] == 1)
  120.                             {
  121.                                 if (tablero[x + 3, y] == 1)
  122.                                 {
  123.                                     Console.WriteLine("4 en ralla! Gana el jugador 1");
  124.                                     Console.ReadLine();
  125.                                 }
  126.                             }
  127.                         }
  128.                         if (tablero[x, y] == 1)
  129.                         {
  130.                             if (tablero[x, y+1] == 1)
  131.                             {
  132.                                 if (tablero[x, y+2] == 1)
  133.                                 {
  134.                                     if (tablero[x, y+3] == 1)
  135.                                     {
  136.                                         Console.WriteLine("4 en ralla! Gana el jugador 1");
  137.                                         Console.ReadLine();
  138.                                     }
  139.                                 }
  140.                             }
  141.                         }
  142.                     }
  143.             }
  144.  
  145.         }
  146.     }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement