Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. namespace TIC_TAC_TOE
  2. {
  3. class Program
  4. {
  5. static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  6. static int player = 1; //By default player 1 is set
  7. static int choice; //This holds the choice at which position user want to mark
  8.  
  9.  
  10. // The flag variable 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
  11. static int flag = 0;
  12.  
  13.  
  14. static void Main(string[] args)
  15. {
  16. do
  17. {
  18. Console.Clear();// whenever loop will be again start then screen will be clear
  19.  
  20. Console.WriteLine("Welcome to Tic Tac Toe!");
  21. Console.WriteLine("--------------------------------------------------------------------------------");
  22. Console.ForegroundColor = ConsoleColor.Red;
  23. Console.WriteLine("Player1:X");
  24. Console.ResetColor();
  25.  
  26. Console.ForegroundColor = ConsoleColor.Green;
  27. Console.WriteLine("Player2:O");
  28. Console.ResetColor();
  29. Console.WriteLine("\n");
  30. if (player % 2 == 0)//checking the chance of the player
  31. {
  32. Console.ForegroundColor = ConsoleColor.Green;
  33. Console.WriteLine("Player 2's turn");
  34. Console.ResetColor();
  35. }
  36. else
  37. {
  38. Console.ForegroundColor = ConsoleColor.Red;
  39. Console.WriteLine("Player 1's turn");
  40. Console.ResetColor();
  41. }
  42. Console.WriteLine("\n");
  43. Board();// calling the board Function
  44.  
  45. choice = int.Parse(Console.ReadLine());//Taking users choice
  46.  
  47. // checking that position where user want to run is marked (with X or O) or not
  48. if (arr[choice] != 'X' && arr[choice] != 'O')
  49. {
  50.  
  51. if (player % 2 == 0) //if chance is of player 2 then mark O else mark X
  52. {
  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 loop 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.ForegroundColor = ConsoleColor.Cyan;
  79. Console.WriteLine("Player {0} has won", (player % 2) + 1);
  80. Console.ResetColor();
  81. }
  82. else// if flag value is -1 the match will be draw and no one is winner
  83. {
  84. Console.WriteLine("Draw");
  85. }
  86. Console.ReadLine();
  87. }
  88. // Board method which creates board
  89. private static void Board()
  90. {
  91.  
  92. Console.WriteLine(" | | ");
  93. Console.WriteLine(" {0} | {1} | {2}", arr[1], arr[2], arr[3]);
  94. Console.WriteLine("_____|_____|_____ ");
  95. Console.WriteLine(" | | ");
  96. Console.WriteLine(" {0} | {1} | {2}", arr[4], arr[5], arr[6]);
  97. Console.WriteLine("_____|_____|_____ ");
  98. Console.WriteLine(" | | ");
  99. Console.WriteLine(" {0} | {1} | {2}", arr[7], arr[8], arr[9]);
  100. Console.WriteLine(" | | ");
  101. Console.ResetColor();
  102. }
  103.  
  104. //Checking that any player has won or not
  105. private static int CheckWin()
  106. {
  107. //Horzontal Winning Condtion
  108. //Winning Condition For First Row
  109. if (arr[1] == arr[2] && arr[2] == arr[3])
  110. {
  111. return 1;
  112. }
  113. //Winning Condition For Second Row
  114. else if (arr[4] == arr[5] && arr[5] == arr[6])
  115. {
  116. return 1;
  117. }
  118. //Winning Condition For Third Row
  119. else if (arr[6] == arr[7] && arr[7] == arr[8])
  120. {
  121. return 1;
  122. }
  123.  
  124.  
  125. //Vertical Winning Condtion
  126. //Winning Condition For First Column
  127. else if (arr[1] == arr[4] && arr[4] == arr[7])
  128. {
  129. return 1;
  130. }
  131. //Winning Condition For Second Column
  132. else if (arr[2] == arr[5] && arr[5] == arr[8])
  133. {
  134. return 1;
  135. }
  136. //Winning Condition For Third Column
  137. else if (arr[3] == arr[6] && arr[6] == arr[9])
  138. {
  139. return 1;
  140. }
  141.  
  142.  
  143. //Diagonal Winning Condition
  144. else if (arr[1] == arr[5] && arr[5] == arr[9])
  145. {
  146. return 1;
  147. }
  148. else if (arr[3] == arr[5] && arr[5] == arr[7])
  149. {
  150. return 1;
  151. }
  152.  
  153.  
  154. // Checking For Draw
  155. // If all the cells or values filled with X or O then any player has won the match
  156. 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')
  157. {
  158. return -1;
  159. }
  160.  
  161.  
  162. else
  163. {
  164. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement