Advertisement
Atanasov_88

Untitled

Sep 28th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.42 KB | None | 0 0
  1. Game Rules
  2.  
  3. Traditionally the first player plays with "X". So you can decide who wants to go "X" and who wants go with "O".
  4. Only one player can play at a time.
  5. If any of the players have filled a square then the other player and the same player cannot override that square.
  6. There are only two conditions that may be match will be draw or may be win.
  7. The player that succeeds in placing three respective mark (X or O) in a horizontal, vertical or diagonal row wins the game.
  8. Winning condition
  9.  
  10. Whoever places three respective marks (X or O) horizontally vertically or diagonally will be the winner.
  11.  
  12. The code for the game is as follows:
  13.  
  14. using System;
  15. using System.Threading;
  16.  
  17. namespace TIC_TAC_TOE
  18. {
  19.     class Program
  20.     {
  21.         //making array and  
  22.         //by default I am providing 0-9 where no use of zero  
  23.         static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  24.         static int player = 1; //By default player 1 is set  
  25.         static int choice; //This holds the choice at which position user want to mark  
  26.  
  27.         // The flag veriable checks who has won if it's value is 1 then some one has won the match if -1 then Match has Draw if 0 then match is still running  
  28.         static int flag = 0;
  29.  
  30.         static void Main(string[] args)
  31.         {
  32.             do
  33.             {
  34.                 Console.Clear();// whenever loop will be again start then screen will be clear  
  35.                 Console.WriteLine("Player1:X and Player2:O");
  36.                 Console.WriteLine("\n");
  37.                 if (player % 2 == 0)//checking the chance of the player  
  38.                 {
  39.                     Console.WriteLine("Player 2 Chance");
  40.                 }
  41.                 else
  42.                 {
  43.                     Console.WriteLine("Player 1 Chance");
  44.                 }
  45.                 Console.WriteLine("\n");
  46.                 Board();// calling the board Function  
  47.                 choice = int.Parse(Console.ReadLine());//Taking users choice  
  48.  
  49.                 // checking that position where user want to run is marked (with X or O) or not  
  50.                 if (arr[choice] != 'X' && arr[choice] != 'O')
  51.                 {
  52.                     if (player % 2 == 0) //if chance is of player 2 then mark O else mark X  
  53.                     {
  54.                         arr[choice] = 'O';
  55.                         player++;
  56.                     }
  57.                     else
  58.                     {
  59.                         arr[choice] = 'X';
  60.                         player++;
  61.                     }
  62.                 }
  63.                 else //If there is any possition where user want to run and that is already marked then show message and load board again  
  64.                 {
  65.                     Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, arr[choice]);
  66.                     Console.WriteLine("\n");
  67.                     Console.WriteLine("Please wait 2 second board is loading again.....");
  68.                     Thread.Sleep(2000);
  69.                 }
  70.                 flag = CheckWin();// calling of check win  
  71.             } while (flag != 1 && flag != -1);// This loof will be run until all cell of the grid is not marked with X and O or some player is not win  
  72.  
  73.             Console.Clear();// clearing the console  
  74.             Board();// getting filled board again  
  75.  
  76.             if (flag == 1)// if flag value is 1 then some one has win or means who played marked last time which has win  
  77.             {
  78.                 Console.WriteLine("Player {0} has won", (player % 2) + 1);
  79.             }
  80.             else// if flag value is -1 the match will be draw and no one is winner  
  81.             {
  82.                 Console.WriteLine("Draw");
  83.             }
  84.             Console.ReadLine();
  85.         }
  86.         // Board method which creats board  
  87.         private static void Board()
  88.         {
  89.             Console.WriteLine("     |     |      ");
  90.             Console.WriteLine("  {0}  |  {1}  |  {2}", arr[1], arr[2], arr[3]);
  91.             Console.WriteLine("_____|_____|_____ ");
  92.             Console.WriteLine("     |     |      ");
  93.             Console.WriteLine("  {0}  |  {1}  |  {2}", arr[4], arr[5], arr[6]);
  94.             Console.WriteLine("_____|_____|_____ ");
  95.             Console.WriteLine("     |     |      ");
  96.             Console.WriteLine("  {0}  |  {1}  |  {2}", arr[7], arr[8], arr[9]);
  97.             Console.WriteLine("     |     |      ");
  98.         }
  99.  
  100.         //Checking that any player has won or not  
  101.         private static int CheckWin()
  102.         {
  103.             #region Horzontal Winning Condtion
  104.             //Winning Condition For First Row  
  105.             if (arr[1] == arr[2] && arr[2] == arr[3])
  106.             {
  107.                 return 1;
  108.             }
  109.             //Winning Condition For Second Row  
  110.             else if (arr[4] == arr[5] && arr[5] == arr[6])
  111.             {
  112.                 return 1;
  113.             }
  114.             //Winning Condition For Third Row  
  115.             else if (arr[6] == arr[7] && arr[7] == arr[8])
  116.             {
  117.                 return 1;
  118.             }
  119.             #endregion
  120.  
  121.             #region vertical Winning Condtion
  122.             //Winning Condition For First Column      
  123.             else if (arr[1] == arr[4] && arr[4] == arr[7])
  124.             {
  125.                 return 1;
  126.             }
  127.             //Winning Condition For Second Column  
  128.             else if (arr[2] == arr[5] && arr[5] == arr[8])
  129.             {
  130.                 return 1;
  131.             }
  132.             //Winning Condition For Third Column  
  133.             else if (arr[3] == arr[6] && arr[6] == arr[9])
  134.             {
  135.                 return 1;
  136.             }
  137.             #endregion
  138.  
  139.             #region Diagonal Winning Condition
  140.             else if (arr[1] == arr[5] && arr[5] == arr[9])
  141.             {
  142.                 return 1;
  143.             }
  144.             else if (arr[3] == arr[5] && arr[5] == arr[7])
  145.             {
  146.                 return 1;
  147.             }
  148.             #endregion
  149.  
  150.             #region Checking For Draw
  151.             // If all the cells or values filled with X or O then any player has won the match  
  152.             else if (arr[1] != '1' && arr[2] != '2' && arr[3] != '3' && arr[4] != '4' && arr[5] != '5' && arr[6] != '6' && arr[7] != '7' && arr[8] != '8' && arr[9] != '9')
  153.             {
  154.                 return -1;
  155.             }
  156.             #endregion
  157.  
  158.             else
  159.             {
  160.                 return 0;
  161.             }
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement