Guest User

Untitled

a guest
Feb 8th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. //This program uses most of the things learned in Programming such as If/Else, loops, etc.
  2. using System;
  3. using System.Threading;
  4.  
  5. namespace TIC_TAC_TOE
  6. {
  7. class Program
  8. {
  9. static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  10. static int player = 1; //By default player 1 is set
  11. static int choice; //This holds the choice at which position user want to mark
  12.  
  13.  
  14. // 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
  15. static int flag = 0;
  16.  
  17.  
  18. static void Main(string[] args)
  19. {
  20. do
  21. {
  22. Console.Clear();// whenever loop will be again start then screen will be clear
  23.  
  24. Console.WriteLine("Welcome to Tic Tac Toe!");
  25. Console.WriteLine("--------------------------------------------------------------------------------");
  26. Console.ForegroundColor = ConsoleColor.Red;
  27. Console.WriteLine("Player1:X");
  28. Console.ResetColor();
  29.  
  30. Console.ForegroundColor = ConsoleColor.Green;
  31. Console.WriteLine("Player2:O");
  32. Console.ResetColor();
  33. Console.WriteLine("\n");
  34. if (player % 2 == 0)//checking the chance of the player
  35. {
  36. Console.ForegroundColor = ConsoleColor.Green;
  37. Console.WriteLine("Player 2's turn");
  38. Console.ResetColor();
  39. }
  40. else
  41. {
  42. Console.ForegroundColor = ConsoleColor.Red;
  43. Console.WriteLine("Player 1's turn");
  44. Console.ResetColor();
  45. }
  46. Console.WriteLine("\n");
  47. Board();// calling the board Function
  48.  
  49. choice = int.Parse(Console.ReadLine());//Taking users choice
  50.  
  51. // checking that position where user want to run is marked (with X or O) or not
  52. if (arr[choice] != 'X' && arr[choice] != 'O')
  53. {
  54.  
  55. if (player % 2 == 0) //if chance is of player 2 then mark O else mark X
  56. {
  57.  
  58. arr[choice] = 'O';
  59. player++;
  60. }
  61. else
  62. {
  63. arr[choice] = 'X';
  64. player++;
  65. }
  66. }
  67. else //If there is any possition where user want to run and that is already marked then show message and load board again
  68. {
  69. Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, arr[choice]);
  70. Console.WriteLine("\n");
  71. Console.WriteLine("Please wait 2 second board is loading again.....");
  72. Thread.Sleep(2000);
  73. }
  74. flag = CheckWin();// calling of check win
  75. } 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
  76.  
  77. Console.Clear();// clearing the console
  78. Board();// getting filled board again
  79.  
  80. if (flag == 1)// if flag value is 1 then some one has win or means who played marked last time which has win
  81. {
  82. Console.ForegroundColor = ConsoleColor.Cyan;
  83. Console.WriteLine("Player {0} has won", (player % 2) + 1);
  84. Console.ResetColor();
  85. }
  86. else// if flag value is -1 the match will be draw and no one is winner
  87. {
  88. Console.WriteLine("Draw");
  89. }
  90. Console.ReadLine();
  91. }
  92. // Board method which creates board
  93. private static void Board()
  94. {
  95.  
  96. Console.WriteLine(" | | ");
  97. Console.WriteLine(" {0} | {1} | {2}", arr[1], arr[2], arr[3]);
  98. Console.WriteLine("_____|_____|_____ ");
  99. Console.WriteLine(" | | ");
  100. Console.WriteLine(" {0} | {1} | {2}", arr[4], arr[5], arr[6]);
  101. Console.WriteLine("_____|_____|_____ ");
  102. Console.WriteLine(" | | ");
  103. Console.WriteLine(" {0} | {1} | {2}", arr[7], arr[8], arr[9]);
  104. Console.WriteLine(" | | ");
  105. Console.ResetColor();
  106. }
  107.  
  108. //Checking that any player has won or not
  109. private static int CheckWin()
  110. {
  111. //Horzontal Winning Condtion
  112. //Winning Condition For First Row
  113. if (arr[1] == arr[2] && arr[2] == arr[3])
  114. {
  115. return 1;
  116. }
  117. //Winning Condition For Second Row
  118. else if (arr[4] == arr[5] && arr[5] == arr[6])
  119. {
  120. return 1;
  121. }
  122. //Winning Condition For Third Row
  123. else if (arr[6] == arr[7] && arr[7] == arr[8])
  124. {
  125. return 1;
  126. }
  127.  
  128.  
  129. //Vertical Winning Condtion
  130. //Winning Condition For First Column
  131. else if (arr[1] == arr[4] && arr[4] == arr[7])
  132. {
  133. return 1;
  134. }
  135. //Winning Condition For Second Column
  136. else if (arr[2] == arr[5] && arr[5] == arr[8])
  137. {
  138. return 1;
  139. }
  140. //Winning Condition For Third Column
  141. else if (arr[3] == arr[6] && arr[6] == arr[9])
  142. {
  143. return 1;
  144. }
  145.  
  146.  
  147. //Diagonal Winning Condition
  148. else if (arr[1] == arr[5] && arr[5] == arr[9])
  149. {
  150. return 1;
  151. }
  152. else if (arr[3] == arr[5] && arr[5] == arr[7])
  153. {
  154. return 1;
  155. }
  156.  
  157.  
  158. // Checking For Draw
  159. // If all the cells or values filled with X or O then any player has won the match
  160. 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')
  161. {
  162. return -1;
  163. }
  164.  
  165.  
  166. else
  167. {
  168. return 0;
  169. }
  170. }
  171. }
  172. }
Add Comment
Please, Sign In to add comment