nicatronTg

Untitled

Apr 19th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // a 3x3 array of characters represents the board
  2. char[][] board = new char[3][3];
  3. // how big is each box, and where did you click
  4. int boxSize, clickX, clickY;
  5. // is it X's turn (true) or O's turn (false)
  6. boolean turnX = true;
  7. int backgroundColor = 155;
  8. int strokeColor = 0;
  9. boolean xWon = false;
  10. boolean oWon = false;
  11.  
  12. void setup() {
  13. size(300,300);
  14. boxSize = width/3;
  15. strokeWeight(2.0);
  16. stroke(strokeColor);
  17. background(backgroundColor);
  18. resetBoard();
  19. }
  20.  
  21. void checkWinHorizontally()
  22. {
  23. for (int i = 0; i < 3; i++) {
  24. int tempX = 0;
  25. int tempY = 0;
  26. for (int j = 0; j < 3; j++) {
  27. if (board[j][i] == 'X') {
  28. tempX++;
  29. } else if (board[j][i] == 'O') {
  30. tempY++;
  31. }
  32.  
  33. if (j == 2) {
  34. if (tempX == 3)
  35. {
  36. print("Win: X," + i);
  37. xWon = true;
  38. } else if (tempY == 3) {
  39. print("Win: Y," + i);
  40. oWon = true;
  41. }
  42. tempX = 0;
  43. tempY = 0;
  44. }
  45. }
  46. }
  47. }
  48.  
  49. void checkWinVertically()
  50. {
  51. for (int i = 0; i < 3; i++) {
  52. int tempX = 0;
  53. int tempY = 0;
  54. for (int j = 0; j < 3; j++) {
  55. if (board[i][j] == 'X') {
  56. tempX++;
  57. } else if (board[i][j] == 'O') {
  58. tempY++;
  59. }
  60.  
  61. if (j == 2) {
  62. if (tempX == 3)
  63. {
  64. print("Win: X," + i);
  65. xWon = true;
  66. } else if (tempY == 3) {
  67. print("Win: Y," + i);
  68. xWon = false;
  69. }
  70. tempX = 0;
  71. tempY = 0;
  72. }
  73. }
  74. }
  75. }
  76.  
  77. // set all the squares to '?' (no player has used them)
  78. void resetBoard() {
  79. for (int i = 0; i < 3; i++) {
  80. for (int j = 0; j < 3; j++) {
  81. board[i][j] = '?';
  82. }
  83. }
  84. }
  85.  
  86. // draw an X
  87. void drawX(int x, int y) {
  88. if (xWon) {
  89. stroke(255);
  90. } else {
  91. stroke(strokeColor);
  92. }
  93. line(x - boxSize/4, y - boxSize/4, x + boxSize/4, y + boxSize/4);
  94. line(x - boxSize/4, y + boxSize/4, x + boxSize/4, y - boxSize/4);
  95. stroke(strokeColor);
  96. }
  97.  
  98. // draw an O
  99. void drawO(int x, int y) {
  100. if (oWon) {
  101. fill(255);
  102. } else {
  103. fill(155);
  104. }
  105. ellipse(x, y, boxSize/2, boxSize/2);
  106. }
  107.  
  108. void drawBoard() {
  109. // draw the grid lines
  110. line(boxSize, 0, boxSize, height);
  111. line(2*boxSize, 0, 2*boxSize, height);
  112. line(0, boxSize, width, boxSize);
  113. line(0, 2*boxSize, width, 2*boxSize);
  114. // populate the board
  115. for (int i = 0; i < 3; i++) {
  116. for (int j = 0; j < 3; j++) {
  117. if (board[i][j] == 'X') {
  118. drawX(boxSize*i + boxSize/2, boxSize*j + boxSize/2);
  119. }
  120. else if (board[i][j] == 'O') {
  121. drawO(boxSize*i + boxSize/2, boxSize*j + boxSize/2);
  122. }
  123. }
  124. }
  125. }
  126.  
  127. // when a square is clicked, save the mouse coords and update
  128. void mousePressed() {
  129. if ((xWon == true) || (oWon == true))
  130. {
  131. board = new char[3][3];
  132. resetBoard();
  133. xWon = false;
  134. oWon = false;
  135. return;
  136. }
  137. clickX = mouseX;
  138. clickY = mouseY;
  139. updateBoard();
  140. checkWinVertically();
  141. checkWinHorizontally();
  142. }
  143.  
  144. // update the board[][] array with the clicked square
  145. void updateBoard() {
  146.  
  147. int i, j;
  148.  
  149. // which 3rd of the board did the X coord land on
  150. if (clickX < boxSize) i = 0;
  151. else if (clickX < 2*boxSize) i = 1;
  152. else if (clickX < width) i = 2;
  153. else i = -1;
  154.  
  155. // which 3rd of the board did the Y coord land on
  156. if (clickY < boxSize) j = 0;
  157. else if (clickY < 2*boxSize) j = 1;
  158. else if (clickY < width) j = 2;
  159. else j = -1;
  160.  
  161. // if the click was inside the grid and the space hasn't been used
  162. if (i >= 0 && j >= 0 & board[i][j] == '?') {
  163. // set the square based on whose turn it is
  164. if (turnX) {
  165. board[i][j] = 'X';
  166. }
  167. else {
  168. board[i][j] = 'O';
  169. }
  170. turnX = !turnX;
  171. }
  172. }
  173.  
  174. void draw() {
  175. drawBoard();
  176. }
Advertisement
Add Comment
Please, Sign In to add comment