Advertisement
Ardurum

4 en ralla

Feb 7th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.59 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, ganador);
  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, bool ganador)
  105.         {
  106.             //Comprobar columnas
  107.             for(int x = tablero.GetLength(0)-1; x >= 0; x--)
  108.             {
  109.                 for (int y = 0; y < tablero.GetLength(1) - 1; y++)
  110.                 {
  111.                     //jugador 1
  112.                     if (tablero[x, y] == 1)
  113.                     {
  114.                         if (tablero[x - 1, y] == 1)
  115.                         {
  116.                             if (tablero[x - 2, y] == 1)
  117.                             {
  118.                                 if (tablero[x - 3, y] == 1)
  119.                                 {
  120.                                     Console.WriteLine("4 en ralla! Gana el jugador 1");
  121.                                     Console.ReadLine();
  122.                                     ganador = true;
  123.                                 }
  124.                             }
  125.                         }
  126.                         if (tablero[x, y] == 1)
  127.                         {
  128.                             if (tablero[x, y + 1] == 1)
  129.                             {
  130.                                 if (tablero[x, y + 2] == 1)
  131.                                 {
  132.                                     if (tablero[x, y + 3] == 1)
  133.                                     {
  134.                                         Console.WriteLine("4 en ralla! Gana el jugador 1");
  135.                                         Console.ReadLine();
  136.                                         ganador = true;
  137.                                     }
  138.                                 }
  139.                             }
  140.                         }
  141.                     }
  142.                     //jugador 2
  143.                     if (tablero[x, y] == 2)
  144.                     {
  145.                         if (tablero[x - 1, y] == 2)
  146.                         {
  147.                             if (tablero[x - 2, y] == 2)
  148.                             {
  149.                                 if (tablero[x - 3, y] == 2)
  150.                                 {
  151.                                     Console.WriteLine("4 en ralla! Gana el jugador 2");
  152.                                     Console.ReadLine();
  153.                                     ganador = true;
  154.                                 }
  155.                             }
  156.                         }
  157.                         if (tablero[x, y] == 2)
  158.                         {
  159.                             if (tablero[x, y + 1] == 2)
  160.                             {
  161.                                 if (tablero[x, y + 2] == 2)
  162.                                 {
  163.                                     if (tablero[x, y + 3] == 2)
  164.                                     {
  165.                                         Console.WriteLine("4 en ralla! Gana el jugador 2");
  166.                                         Console.ReadLine();
  167.                                         ganador = true;
  168.                                     }
  169.                                 }
  170.                             }
  171.                         }
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.     }
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement