Advertisement
jyoung12387

Tic Tac Toe - Example

Mar 4th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.87 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. namespace tictactoe
  5. {
  6.     class Program
  7.     {
  8.         // the playfield
  9.         static char[,] playField =
  10.         {
  11.             {'1','2','3' },  //row 0
  12.             {'4','5','6' },  //row 1
  13.             {'7','8','9' }   //row 2
  14.         };
  15.  
  16.  
  17.  
  18.         static int turns = 0;
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             int player = 2; //player 1 starts
  23.             int input = 0;
  24.             bool inputCorrect = true;
  25.  
  26.  
  27.             //Run code as long as program runs
  28.             do
  29.             {
  30.  
  31.                 if (player == 2)
  32.                 {
  33.                     player = 1;
  34.                     EnterXorO(player, input);
  35.  
  36.                 }
  37.                 else if (player == 1)
  38.                 {
  39.                     player = 2;
  40.                     EnterXorO(player, input);
  41.  
  42.                 }
  43.  
  44.                 setField();
  45.  
  46.                 #region
  47.  
  48.                 //check winning condition
  49.                 char[] playerChars = { 'X', 'O' };
  50.  
  51.                 foreach(char playerChar in playerChars)
  52.                 {
  53.                     if(((playField[0,0] == playerChar) && (playField[0,1] == playerChar) && (playField[0,2] == playerChar))
  54.                         || ((playField[1, 0] == playerChar) && (playField[1, 1] == playerChar) && (playField[1, 2] == playerChar))    
  55.                         || ((playField[2, 0] == playerChar) && (playField[2, 1] == playerChar) && (playField[2, 2] == playerChar))  
  56.                         || ((playField[0, 0] == playerChar) && (playField[1, 0] == playerChar) && (playField[2, 0] == playerChar))  
  57.                         || ((playField[0, 1] == playerChar) && (playField[1, 1] == playerChar) && (playField[2, 1] == playerChar))
  58.                         || ((playField[0, 2] == playerChar) && (playField[1, 2] == playerChar) && (playField[2, 2] == playerChar))
  59.                         || ((playField[0, 0] == playerChar) && (playField[1, 1] == playerChar) && (playField[2, 2] == playerChar))
  60.                         || ((playField[2, 0] == playerChar) && (playField[1, 1] == playerChar) && (playField[0, 2] == playerChar))
  61.  
  62.                         )
  63.                     {
  64.                         if(playerChar =='X')
  65.                         {
  66.                             Console.WriteLine("Player 2 has won!");
  67.                         }
  68.                         else
  69.                         {
  70.                             Console.WriteLine("Player 1 has won!");
  71.                         }
  72.  
  73.                         Console.WriteLine("Please press any key to reset game.");
  74.                         Console.ReadKey();
  75.  
  76.                         ResetField();
  77.  
  78.                         break;
  79.                     }
  80.                     else if (turns ==10)
  81.                     {
  82.                         Console.WriteLine("\nDraw!.");
  83.                         Console.WriteLine("Please press any key to reset game.");
  84.                         Console.ReadKey();
  85.                         ResetField();
  86.                         break;
  87.                     }
  88.  
  89.  
  90.                 }
  91.  
  92.  
  93.  
  94.                 #endregion
  95.  
  96.                 #region
  97.                 //test if field is already taken
  98.                 do
  99.                 {
  100.                     Console.Write("\nPlayer {0}: Choose your field! ", player);
  101.                     try
  102.                     {
  103.                         input = Convert.ToInt32(Console.ReadLine());
  104.  
  105.                     }
  106.                     catch
  107.                     {
  108.                         Console.WriteLine("Please enter a number!");
  109.                     }
  110.  
  111.                     if ((input == 1) && (playField[0, 0] == '1'))
  112.                         inputCorrect = true;
  113.                     else if ((input == 2) && (playField[0, 1] == '2'))
  114.                         inputCorrect = true;
  115.                     else if ((input == 3) && (playField[0, 2] == '3'))
  116.                         inputCorrect = true;
  117.                     else if ((input == 4) && (playField[1, 0] == '4'))
  118.                         inputCorrect = true;
  119.                     else if ((input == 5) && (playField[1, 1] == '5'))
  120.                         inputCorrect = true;
  121.                     else if ((input == 6) && (playField[1, 2] == '6'))
  122.                         inputCorrect = true;
  123.                     else if ((input == 7) && (playField[2, 0] == '7'))
  124.                         inputCorrect = true;
  125.                     else if ((input == 8) && (playField[2, 1] == '8'))
  126.                         inputCorrect = true;
  127.                     else if ((input == 9) && (playField[2, 2] == '9'))
  128.                         inputCorrect = true;
  129.                     else
  130.                     {
  131.                         Console.WriteLine("\nIncorrect input! Please use another field!");
  132.                         inputCorrect = false;
  133.                     }
  134.  
  135.                 } while (!inputCorrect);
  136.                 #endregion
  137.  
  138.             } while (true);
  139.  
  140.  
  141.         }
  142.  
  143.         public static void ResetField()
  144.         {
  145.             char[,] playFieldInitial =
  146.             {
  147.             {'1','2','3' },  //row 0
  148.             {'4','5','6' },  //row 1
  149.             {'7','8','9' }   //row 2
  150.             };
  151.  
  152.  
  153.             playField = playFieldInitial;
  154.             setField();
  155.             turns = 0;
  156.  
  157.         }
  158.  
  159.         public static void setField()
  160.         {
  161.             Console.Clear();
  162.             Console.WriteLine("     |     |     ");
  163.             Console.WriteLine("  {0}  |  {1}  |  {2}  ", playField[0, 0], playField[0, 1], playField[0, 2]);
  164.             Console.WriteLine("_____|_____|_____");
  165.             Console.WriteLine("     |     |     ");
  166.             Console.WriteLine("  {0}  |  {1}  |  {2}  ", playField[1, 0], playField[1, 1], playField[1, 2]);
  167.             Console.WriteLine("_____|_____|_____");
  168.             Console.WriteLine("     |     |     ");
  169.             Console.WriteLine("  {0}  |  {1}  |  {2}  ", playField[2, 0], playField[2, 1], playField[2, 2]);
  170.             Console.WriteLine("     |     |     ");
  171.             turns++;
  172.         }
  173.  
  174.         public static void EnterXorO(int player, int input)
  175.         {
  176.             char playerSign = ' ';
  177.  
  178.             if (player == 1)
  179.                 playerSign = 'X';
  180.             else if (player == 2)
  181.                 playerSign = 'O';
  182.  
  183.  
  184.             switch (input)
  185.             {
  186.                 case 1: playField[0, 0] = playerSign; break;
  187.                 case 2: playField[0, 1] = playerSign; break;
  188.                 case 3: playField[0, 2] = playerSign; break;
  189.                 case 4: playField[1, 0] = playerSign; break;
  190.                 case 5: playField[1, 1] = playerSign; break;
  191.                 case 6: playField[1, 2] = playerSign; break;
  192.                 case 7: playField[2, 0] = playerSign; break;
  193.                 case 8: playField[2, 1] = playerSign; break;
  194.                 case 9: playField[2, 2] = playerSign; break;
  195.             }
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement