Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Main
  8. {
  9. class Program
  10. {
  11. static void Main()
  12. {
  13. string[,] possition_array = { { ".", ".", "." }, { ".", ".", "." }, { ".", ".", "." } };
  14. Console.WriteLine("Hello Players!");
  15. Console.WriteLine("Following is the board, you will enter numbers 1-9 to put down your mark.");
  16. Draw_board(possition_array);
  17. Console.WriteLine("Please enter wether you are playing as X or O; ");
  18. string Player1 = Console.ReadLine();
  19. switch (Player1)
  20. {
  21. case "X":
  22. int x = 0;
  23. Play(x, possition_array);
  24. break;
  25. case "O":
  26. int i = 1;
  27. Play(i, possition_array);
  28. break;
  29. default:
  30. Console.WriteLine("Please enter either X or O, as represented, to start the game");
  31. Main();
  32. break;
  33. }
  34. }
  35.  
  36.  
  37. static void Play(int x, string[,] possition_array)
  38. {
  39. Victory_check(possition_array);
  40. Draw_board(possition_array);
  41. string[] player_array = new string[] { "X", "O" };
  42. Console.WriteLine("Please enter where you wish to put your mark, ", player_array[x], ".");
  43. string Player_entry = Console.ReadLine();
  44. switch (Player_entry)
  45. {
  46. case "1":
  47. if (possition_array[0, 0] == ".")
  48. {
  49. possition_array[0, 0] = player_array[x];
  50. Player_change(x, possition_array);
  51. }
  52. else
  53. {
  54. Console.WriteLine("There's already a mark here, please try another number.");
  55. Play(x, possition_array);
  56. }
  57. break;
  58. case "2":
  59. if (possition_array[0, 1] == ".")
  60. {
  61. possition_array[0, 1] = player_array[x];
  62. Player_change(x, possition_array);
  63. }
  64. else
  65. {
  66. Console.WriteLine("There's already a mark here, please try another number.");
  67. Play(x, possition_array);
  68. }
  69. break;
  70. case "3":
  71. if (possition_array[0, 2] == ".")
  72. {
  73. possition_array[0, 2] = player_array[x];
  74. Player_change(x, possition_array);
  75. }
  76. else
  77. {
  78. Console.WriteLine("There's already a mark here, please try another number.");
  79. Play(x, possition_array);
  80. }
  81. break;
  82. case "4":
  83. if (possition_array[1, 0] == ".")
  84. {
  85. possition_array[1, 0] = player_array[x];
  86. Player_change(x, possition_array);
  87. }
  88. else
  89. {
  90. Console.WriteLine("There's already a mark here, please try another number.");
  91. Play(x, possition_array);
  92. }
  93. break;
  94. case "5":
  95. if (possition_array[1, 1] == ".")
  96. {
  97. possition_array[1, 1] = player_array[x];
  98. Player_change(x, possition_array);
  99. }
  100. else
  101. {
  102. Console.WriteLine("There's already a mark here, please try another number.");
  103. Play(x, possition_array);
  104. }
  105. break;
  106. case "6":
  107. if (possition_array[1, 2] == ".")
  108. {
  109. possition_array[1, 2] = player_array[x];
  110. Player_change(x, possition_array);
  111. }
  112. else
  113. {
  114. Console.WriteLine("There's already a mark here, please try another number.");
  115. Play(x, possition_array);
  116. }
  117. break;
  118. case "7":
  119. if (possition_array[2, 0] == ".")
  120. {
  121. possition_array[2, 0] = player_array[x];
  122. Player_change(x, possition_array);
  123. }
  124. else
  125. {
  126. Console.WriteLine("There's already a mark here, please try another number.");
  127. Play(x, possition_array);
  128. }
  129. break;
  130. case "8":
  131. if (possition_array[2, 1] == ".")
  132. {
  133. possition_array[2, 1] = player_array[x];
  134. Player_change(x, possition_array);
  135. }
  136. else
  137. {
  138. Console.WriteLine("There's already a mark here, please try another number.");
  139. Play(x, possition_array);
  140. }
  141. break;
  142. case "9":
  143. if (possition_array[2, 2] == ".")
  144. {
  145. possition_array[2, 2] = player_array[x];
  146. Player_change(x, possition_array);
  147. }
  148. else
  149. {
  150. Console.WriteLine("There's already a mark here, please try another number.");
  151. Play(x, possition_array);
  152. }
  153. break;
  154. default:
  155. Console.WriteLine("Please enter a number 1-9.");
  156. Play(x, possition_array);
  157. break;
  158.  
  159. }
  160.  
  161.  
  162. }
  163.  
  164. static void Victory_check(string[,] possition_array)
  165. {
  166. for (int i = 0; i < 3; i++)
  167. {
  168. if ((possition_array[i, 0] == "X") & (possition_array[i, 1] == "X") & (possition_array[i, 2] == "X"))
  169. {
  170. End("X", possition_array);
  171. }
  172. else if ((possition_array[i, 0] == "O") & (possition_array[i, 1] == "O") & (possition_array[i, 2] == "O"))
  173. {
  174. End("O", possition_array);
  175. }
  176. else if ((possition_array[0, i] == "X") & (possition_array[1, i] == "X") & (possition_array[2, i] == "X"))
  177. {
  178. End("X", possition_array);
  179. }
  180. else if ((possition_array[0, i] == "O") & (possition_array[1, i] == "O") & (possition_array[2, i] == "O"))
  181. {
  182. End("O", possition_array);
  183. }
  184. else if ((possition_array[0, 0] == "X") & (possition_array[1, 1] == "X") & (possition_array[2, 2] == "X"))
  185. {
  186. End("X", possition_array);
  187. }
  188. else if ((possition_array[0, 0] == "O") & (possition_array[1, 1] == "O") & (possition_array[2, 2] == "O"))
  189. {
  190. End("O", possition_array);
  191. }
  192. else
  193. {
  194. continue;
  195. }
  196. }
  197. }
  198.  
  199. static void Player_change(int x, string[,] possition_array)
  200. {
  201. if(x == 1)
  202. {
  203. Play(0, possition_array);
  204. }
  205. else
  206. {
  207. Play(1, possition_array);
  208. }
  209. }
  210.  
  211. static void Draw_board(string[,] possition_array)
  212. {
  213. for (int i = 0; i < 3; i++)
  214. {
  215. Console.WriteLine($"{possition_array[i, 0]} {possition_array[i, 1]} {possition_array[i, 2]}");
  216. }
  217. }
  218.  
  219. static void End(string Victor, string[,] possition_array)
  220. {
  221. Draw_board(possition_array);
  222. if (Victor == "X")
  223. {
  224. Console.WriteLine("Crosses won!");
  225. }
  226. else
  227. {
  228. Console.WriteLine("Circles won!");
  229. }
  230. Console.WriteLine("Well Played. Please press Enter to quit.");
  231. Console.ReadLine();
  232. }
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement