Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TicTacToe_Game
  4. {
  5. public class Game
  6. {
  7. //The game's board itself.
  8. //If the element is 0, the tile is unmarked.
  9. //If the element is 1, the tile is marked by player1.
  10. //If the element is 2, the tile is marked by player2.
  11. private byte[,] board = new byte[3,3];
  12. //Used to check which player's turn it is.
  13. private bool player1_turn = true;
  14. private bool gameWon = false;
  15.  
  16. public Game() {
  17. //Prepares the game board.
  18. for (int i = 0; i < 3; ++i) {
  19. for (int n = 0; n < 3; ++n) {
  20. board[i, n] = 0;
  21. }
  22. }
  23. }
  24.  
  25. public byte[,] GetBoard() {
  26. return board;
  27. }
  28.  
  29. /// <summary>
  30. /// Resets the current game.
  31. /// </summary>
  32. public void ResetGame() {
  33. player1_turn = true;
  34. gameWon = false;
  35. //Resets the game board.
  36. for (int i = 0; i < 3; ++i)
  37. {
  38. for (int n = 0; n < 3; ++n)
  39. {
  40. board[i, n] = 0;
  41. }
  42. }
  43. }
  44.  
  45. private byte UpdateBoard(byte x, byte y) {
  46. //1 for win, 2 for no win, 0 for error.
  47. byte isWinningMove = 2;
  48.  
  49. byte marking;
  50. if (player1_turn)
  51. {
  52. marking = 1;
  53. }
  54. else {
  55. marking = 2;
  56. }
  57.  
  58. //If the marked tile was not occupied.
  59. if (board[x, y] == 0)
  60. {
  61. board[x, y] = marking;
  62.  
  63. int counter = 0;
  64.  
  65. //Check each horizontal row for wins.
  66. for (int i = 0; i < 3 && isWinningMove != 1; ++i)
  67. {
  68. for (int n = 0; n < 3; ++n)
  69. {
  70. if (board[i, n] == marking) {
  71. ++counter;
  72. }
  73. }
  74. //If there is a row of equivalent markings.
  75. if (counter == 3)
  76. {
  77. //The player wins.
  78. isWinningMove = 1;
  79. }
  80. counter = 0;
  81. }
  82.  
  83. //Check each verticle column for wins.
  84. for (int i = 0; i < 3 && isWinningMove != 1; ++i)
  85. {
  86. for (int n = 0; n < 3; ++n)
  87. {
  88. if (board[n, i] == marking)
  89. {
  90. ++counter;
  91. }
  92. }
  93. //If there is a column of equivalent markings.
  94. if (counter == 3)
  95. {
  96. //The player wins.
  97. isWinningMove = 1;
  98. }
  99. counter = 0;
  100. }
  101.  
  102. //Check for diagonals.
  103. if ((board[0, 0] + board[1, 1] + board[2, 2]) == (marking * 3))
  104. {
  105. isWinningMove = 1;
  106. }
  107. else if ((board[0, 2] + board[1, 1] + board[2, 0]) == (marking * 3)) {
  108. isWinningMove = 1;
  109. }
  110. }
  111. //If the requested tile to be mark was already occupied.
  112. else {
  113. //Returns an error code.
  114. isWinningMove = 0;
  115. }
  116.  
  117. return isWinningMove;
  118. }
  119.  
  120. public bool NewMove(byte row, byte column) {
  121. //If the user made a move when the game is already over.
  122. if (gameWon) {
  123. //Resets the current game first.
  124. ResetGame();
  125. }
  126.  
  127. //If the new move is not a valid move.
  128. if (row > 2 || column > 2) {
  129. throw new InvalidOperationException();
  130. }
  131.  
  132. byte status = UpdateBoard(row, column);
  133. bool result;
  134. if (status == 0)
  135. {
  136. throw new InvalidOperationException();
  137. }
  138. else if (status == 1)
  139. {
  140. gameWon = true;
  141. result = true;
  142. }
  143. else {
  144. result = false;
  145. }
  146.  
  147. //Changes the player turn for the next move.
  148. if (player1_turn)
  149. {
  150. player1_turn = false;
  151. }
  152. else
  153. {
  154. player1_turn = true;
  155. }
  156.  
  157. return result;
  158. }
  159.  
  160. public bool NewMove(byte tileNumber)
  161. {
  162. //If the user made a move when the game is already over.
  163. if (gameWon)
  164. {
  165. //Resets the current game first.
  166. ResetGame();
  167. }
  168.  
  169. if (tileNumber == 0 || tileNumber > 9) {
  170. throw new InvalidOperationException();
  171. }
  172.  
  173. byte row = 0, column = 0;
  174.  
  175. for (byte i = 1, x = 0, y = 0; i < 10; ++i) {
  176. if (i == tileNumber) {
  177. row = x; column = y;
  178. break;
  179. }
  180. ++y;
  181. if (y == 3) {
  182. ++x;
  183. y = 0;
  184. }
  185. }
  186.  
  187. /*
  188. Tile number is visualized as this way:
  189. 1 2 3
  190. 4 5 6
  191. 7 8 9
  192. */
  193.  
  194. byte status = UpdateBoard(row, column);
  195. bool result;
  196. if (status == 0)
  197. {
  198. throw new InvalidOperationException();
  199. }
  200. else if (status == 1)
  201. {
  202. gameWon = true;
  203. result = true;
  204. }
  205. else
  206. {
  207. result = false;
  208. }
  209.  
  210. //Changes the player turn for the next move.
  211. if (player1_turn)
  212. {
  213. player1_turn = false;
  214. }
  215. else {
  216. player1_turn = true;
  217. }
  218.  
  219. return result;
  220. }
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement