Advertisement
WadeRollins2710

Bingo with C#

Oct 1st, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.46 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3.  
  4. namespace Bingo
  5. {
  6.     public static class global
  7.     {
  8.         public static string Mode;
  9.         public static int i, j;
  10.         public static int[,] COM_Board= new int [6,6];
  11.         public static int[,] Human_Board = new int[6,6];
  12.     }
  13.  
  14.     class Program
  15.     {
  16.         public static void PrintHumanBoard()
  17.         {
  18.             for (int i=1;i<=5;i++)
  19.             {
  20.                 for (int j = 1; j <= 5; j++)
  21.                     if (global.Human_Board[i, j] == 0)
  22.                     {
  23.                         Console.ForegroundColor = ConsoleColor.Yellow;
  24.                         if (global.Human_Board[i, j] < 10)
  25.                             Console.Write("{0}  ", global.Human_Board[i, j]);
  26.                         else Console.Write("{0} ", global.Human_Board[i, j]);
  27.                         Console.ForegroundColor = ConsoleColor.White;
  28.                     }
  29.                     else
  30.                     {
  31.                         if (global.Human_Board[i, j] < 10)
  32.                             Console.Write("{0}  ", global.Human_Board[i, j]);
  33.                         else Console.Write("{0} ", global.Human_Board[i, j]);
  34.                     }
  35.                 Console.WriteLine();
  36.             }
  37.         }
  38.         public static void PrintCOMBoard()
  39.         {
  40.             for (int i = 1; i <= 5; i++)
  41.             {
  42.                 for (int j = 1; j <= 5; j++)
  43.                     if (global.COM_Board[i, j] == 0)
  44.                     {
  45.                         Console.ForegroundColor = ConsoleColor.Yellow;
  46.                         if (global.COM_Board[i, j] < 10)
  47.                             Console.Write("{0}  ", global.COM_Board[i, j]);
  48.                         else Console.Write("{0} ", global.COM_Board[i, j]);
  49.                         Console.ForegroundColor = ConsoleColor.White;
  50.                     }
  51.                     else
  52.                     {
  53.                         if (global.COM_Board[i, j] < 10)
  54.                             Console.Write("{0}  ", global.COM_Board[i, j]);
  55.                         else Console.Write("{0} ", global.COM_Board[i, j]);
  56.                     }
  57.                 Console.WriteLine();
  58.             }
  59.         }
  60.         public static void Sout(string S)
  61.         {
  62.             Console.WriteLine(S);
  63.             Task.Delay(1000);
  64.         }
  65.         public static void Info()
  66.         {
  67.             Sout("Made by Tran Viet Anh");
  68.             Sout("Do not copy");
  69.         }
  70.         public static void Init_Board()
  71.         {
  72.             Console.Write("Choose mode: ");
  73.             global.Mode = Console.ReadLine();
  74.             global.Mode = global.Mode.ToUpper();
  75.             while ((global.Mode!="EASY") && (global.Mode!="NORMAL"))
  76.             {
  77.                 Console.Write("Mode inputed is invalid, please input again: ");
  78.                 global.Mode = Console.ReadLine();
  79.                 global.Mode = global.Mode.ToUpper();
  80.             }
  81.             Console.Write("Make board by manual or auto? ");
  82.             string Choice = Console.ReadLine();
  83.             Choice = Choice.ToUpper();
  84.             while (Choice=="INFO")
  85.             {
  86.                 Info();
  87.                 Console.Write("Input command again: ");
  88.                 Choice = Console.ReadLine();
  89.                 Choice = Choice.ToUpper();
  90.             }
  91.             while ((Choice!="AUTO") && (Choice!="MANUAL"))
  92.             {
  93.                 Console.Write("Command inputed is invalid, please input again: ");
  94.                 Choice = Console.ReadLine();
  95.                 Choice = Choice.ToUpper();
  96.             }
  97.             if (Choice=="AUTO")
  98.             {
  99.                 int value = 0;
  100.                 for (int i=1;i<=5;i++)
  101.                     for (int j=1;j<=5;j++)
  102.                     {
  103.                         value++;
  104.                         global.Human_Board[i, j] = value;
  105.                     }
  106.                 Random Rnd = new Random();
  107.                 for (int i=1;i<=5;i++)
  108.                     for (int j=1;j<=5;j++)
  109.                     {
  110.                         int x = Rnd.Next(5) + 1;
  111.                         int y = Rnd.Next(5) + 1;
  112.                         //Swap
  113.                         int Mid = global.Human_Board[i, j];
  114.                         global.Human_Board[i, j] = global.Human_Board[x, y];
  115.                         global.Human_Board[x, y] = Mid;
  116.                     }
  117.                 value = 0;
  118.                 for (int i = 1; i <= 5; i++)
  119.                     for (int j = 1; j <= 5; j++)
  120.                     {
  121.                         value++;
  122.                         global.COM_Board[i, j] = value;
  123.                     }
  124.                 for (int i = 1; i <= 5; i++)
  125.                     for (int j = 1; j <= 5; j++)
  126.                     {
  127.                         int x = Rnd.Next(5) + 1;
  128.                         int y = Rnd.Next(5) + 1;
  129.                         //Swap
  130.                         int Mid = global.COM_Board[i, j];
  131.                         global.COM_Board[i, j] = global.COM_Board[x, y];
  132.                         global.COM_Board[x, y] = Mid;
  133.                     }
  134.                 PrintHumanBoard();
  135.                 PrintCOMBoard();
  136.             }
  137.         }
  138.         public static void Tie()
  139.         {
  140.             Console.Clear();
  141.             PrintHumanBoard();
  142.             Console.WriteLine();
  143.             PrintCOMBoard();
  144.             Console.Write("TIE");
  145.             Console.ReadLine();
  146.         }
  147.         public static void ComputerWon()
  148.         {
  149.             Console.Clear();
  150.             PrintHumanBoard();
  151.             Console.WriteLine();
  152.             PrintCOMBoard();
  153.             Console.WriteLine("COM WON!");
  154.             Console.ReadLine();
  155.         }
  156.         public static void HumanWon()
  157.         {
  158.             Console.Clear();
  159.             PrintHumanBoard();
  160.             Console.WriteLine();
  161.             PrintCOMBoard();
  162.             Console.WriteLine("HUMAN WON!");
  163.             Console.ReadLine();
  164.         }
  165.         public static int HumanDiagonalScore()
  166.         {
  167.             int count = 0, c = 0;
  168.             for (int i = 1; i <= 5; i++)
  169.                 if (global.COM_Board[i, i] == 0) c++;
  170.             if (c == 5) count++;
  171.             for (int i = 1; i <= 5; i++)
  172.                 if (global.COM_Board[i, 5 - i + 1] == 0) c++;
  173.             if (c == 5) count++;
  174.             return count;
  175.         }
  176.         public static int HumanLineScore()
  177.         {
  178.             int Score=0;
  179.             for (int i=1;i<=5;i++)
  180.             {
  181.                 int LineScore = 0;
  182.                 for (int j = 1; j <= 5; j++)
  183.                     if (global.Human_Board[i, j] == 0) LineScore++;
  184.                 if (LineScore == 5) Score++;
  185.             }
  186.             return Score;
  187.         }
  188.         public static int HumanColumnScore()
  189.         {
  190.             int Score = 0;
  191.             for (int i = 1; i <= 5; i++)
  192.             {
  193.                 int LineScore = 0;
  194.                 for (int j = 1; j <= 5; j++)
  195.                     if (global.Human_Board[i, j] == 0) LineScore++;
  196.                 if (LineScore == 5) Score++;
  197.             }
  198.             return Score;
  199.         }
  200.         public static int COMDiagonalScore()
  201.         {
  202.             int count = 0, c = 0;
  203.             for (int i = 1; i <= 5; i++)
  204.                 if (global.COM_Board[i, i] == 0) c++;
  205.             if (c == 5) count++;
  206.             for (int i = 1; i <= 5; i++)
  207.                 if (global.COM_Board[i, 5-i+1] == 0) c++;
  208.             if (c == 5) count++;
  209.             return count;
  210.         }
  211.         public static int COMColumnScore()
  212.         {
  213.             int Count = 0;
  214.             for (int j = 1; j <= 5; j++)
  215.             {
  216.                 int c = 0;
  217.                 for (int i = 1; i <= 5; i++)
  218.                     if (global.COM_Board[i, j] == 0) c++;
  219.                 if (c == 5) Count++;
  220.             }
  221.             return Count;
  222.         }
  223.         public static int COMLineScore()
  224.         {
  225.             int Count = 0;
  226.             for (int i = 1; i <= 5; i++)
  227.             {
  228.                 int c = 0;
  229.                 for (int j = 1; j <= 5; j++)
  230.                     if (global.COM_Board[i, j] == 0) c++;
  231.                 if (c == 5) Count++;
  232.             }
  233.             return Count;
  234.         }
  235.         public static bool COMVictoryCondition()
  236.         {
  237.             int Score = 0;
  238.             Score = Score + COMLineScore();
  239.             Score = Score + COMColumnScore();
  240.             Score = Score + COMDiagonalScore();
  241.             if (Score >= 5) return true;
  242.             else return false;
  243.         }
  244.         public static bool HumanVictoryCondition()
  245.         {
  246.             int Score = 0;
  247.             Score = Score + HumanLineScore();
  248.             Score = Score + HumanColumnScore();
  249.             Score = Score + HumanDiagonalScore();
  250.             if (Score >= 5) return true;
  251.             else return false;
  252.         }
  253.         public static bool ValidChoice(int Choice)
  254.         {
  255.             if ((Choice < 1) || (Choice > 25)) return false;
  256.             else
  257.             {
  258.                 bool B = false;
  259.                 for (int i=1;i<=5;i++)
  260.                     for (int j=1;j<=5;j++)
  261.                         if (global.Human_Board[i,j]==Choice)
  262.                         {
  263.                             B = true;
  264.                             break;
  265.                         }
  266.                 return B;
  267.             }
  268.         }
  269.         public static bool ValidCOMChoice(int Value)
  270.         {
  271.             bool B = false;
  272.             for (int i=1;i<=5;i++)
  273.                 for (int j=1;j<=5;j++)
  274.                     if (global.COM_Board[i,j]==Value)
  275.                     {
  276.                         B = true;
  277.                         break;
  278.                     }
  279.             return B;
  280.         }
  281.         public static int EasyBot()
  282.         {
  283.             int COMChoice;
  284.             Random Rnd = new Random();
  285.             COMChoice = Rnd.Next(25) + 1;
  286.             while (!ValidCOMChoice(COMChoice))
  287.                 COMChoice = Rnd.Next(25) + 1;
  288.             return COMChoice;
  289.         }
  290.         public static void Play()
  291.         {
  292.             int Choice = 0;
  293.             while (!(HumanVictoryCondition()) && !(COMVictoryCondition()))
  294.             {
  295.                 Console.Clear();
  296.                 PrintHumanBoard();
  297.                 Console.WriteLine();
  298.                 Console.Write("Input Choice: ");
  299.                 Choice = int.Parse(Console.ReadLine());
  300.                 while (!(ValidChoice(Choice)))
  301.                 {
  302.                     Console.WriteLine("Invalid value, input again: ");
  303.                     Choice = int.Parse(Console.ReadLine());
  304.                 }
  305.                 for (int i = 1; i <= 5; i++)
  306.                     for (int j = 1; j <= 5; j++)
  307.                         if (global.Human_Board[i, j] == Choice) global.Human_Board[i, j] = 0;
  308.                 for (int i = 1; i <= 5; i++)
  309.                     for (int j = 1; j <= 5; j++)
  310.                         if (global.COM_Board[i, j] == Choice) global.COM_Board[i, j] = 0;
  311.                 if ((HumanVictoryCondition()) == (COMVictoryCondition()))
  312.                 {
  313.                     if (HumanVictoryCondition() == true)
  314.                         Tie();
  315.                 }
  316.                 else
  317.                     if (HumanVictoryCondition() == true)
  318.                         HumanWon();
  319.                     else ComputerWon();
  320.                 if (global.Mode == "EASY") Choice = EasyBot();
  321.                 Console.Write("COM Choice: ");
  322.                 System.Threading.Thread.Sleep(1000);
  323.                 Console.WriteLine(Choice);
  324.                 System.Threading.Thread.Sleep(1000);
  325.                 for (int i = 1; i <= 5; i++)
  326.                     for (int j = 1; j <= 5; j++)
  327.                         if (global.Human_Board[i, j] == Choice) global.Human_Board[i, j] = 0;
  328.                 for (int i = 1; i <= 5; i++)
  329.                     for (int j = 1; j <= 5; j++)
  330.                         if (global.COM_Board[i, j] == Choice) global.COM_Board[i, j] = 0;
  331.                 if ((HumanVictoryCondition()) == (COMVictoryCondition()))
  332.                 {
  333.                     if (HumanVictoryCondition() == true)
  334.                         Tie();
  335.                 }
  336.                 else
  337.                     if (HumanVictoryCondition() == true)
  338.                     HumanWon();
  339.                 else ComputerWon();
  340.             }
  341.         }
  342.         static void Main(string[] args)
  343.         {
  344.             Console.Clear();
  345.             Console.ForegroundColor = ConsoleColor.White;
  346.             Init_Board();
  347.             Play();
  348.         }
  349.     }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement