Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 19th, 2013  |  syntax: Java  |  size: 3.63 KB  |  views: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.         public void buttonClicked (Button buttonObj){
  2. if (!buttonObj.getLabel().equals("--")) //Cannot override an x or an o (click twice)
  3. return;
  4. String symbol;
  5. if (count%2==0){   // x's turn
  6. symbol = "X";
  7. } else {        // y's turn
  8. symbol = "O";
  9. }
  10. count++;  // getting ready for the next turn
  11. for(int i=0;i<myButtons.length; i++){
  12. for(int j=0;j<myButtons[i].length; j++){
  13. if (buttonObj == myButtons[i][j]){
  14. if (symbol.equals("X"))
  15. myButtons[i][j].setBackground(Color.red);
  16. else
  17. myButtons[i][j].setBackground(Color.blue);
  18. myButtons[i][j].setLabel(symbol);
  19. //return;  // get out of method
  20. }
  21. }
  22. }
  23.  
  24.  
  25. if (checkWinner()==-1)  // add code to pop-up a messageBox that there was a winner
  26. messageBox("No one wins");
  27. else if (checkWinner()==0)
  28. messageBox("O wins");
  29. else if (checkWinner () == 1)
  30. messageBox("X wins");
  31. // add code to pop-up a messageBox that there was a winner and who it was (X or O)
  32. // you will need else if / else if to check
  33. // if there is no winner than there should be no messageBox and the game continues
  34. // as usual.
  35. }
  36. /**
  37. * Method to check if there is a winner.
  38. * @return 0 - O wins
  39. * @return 1 - X wins
  40. * @return -1 - no win
  41. */
  42. public int checkWinner(){
  43. int blank = 0;
  44. for (int i=0; i<myButtons.length; i++){
  45. for(int j=0; j<myButtons[0].length; j++)
  46. if(!myButtons[i][j].getLabel().equals("--"))
  47. blank++;
  48. }
  49. if (blank != 9){
  50. for(int i=0;i<myButtons.length; i++){
  51. for(int j=0;j<myButtons[i].length; j++){
  52. if (i == 1){
  53. if (myButtons[i][j].getLabel().equals("X")){
  54. if (myButtons[i+1][j].getLabel().equals("X")){
  55. if (myButtons[i+2][j].getLabel().equals("X"))
  56. return 1;
  57. }
  58. else if (myButtons[i][j+1].getLabel().equals("X")){
  59. if (myButtons[i][j+2].getLabel().equals("X"))
  60. return 1;}
  61. else if (myButtons[i+1][j+1].getLabel().equals("X")){
  62. if (myButtons[i+2][j+2].getLabel().equals("X"))
  63. return 1;}
  64. }else if (myButtons[i][j].getLabel().equals("O")){
  65. if (myButtons[i+1][j].getLabel().equals("O")){
  66. if (myButtons[i+2][j].getLabel().equals("O"))
  67. return 0;}
  68. else if (myButtons[i][j+1].getLabel().equals("O")){
  69. if (myButtons[i][j+2].getLabel().equals("O"))
  70. return 0;}
  71. else if (myButtons[i+1][j+1].getLabel().equals("O")){
  72. if (myButtons[i+2][j+2].getLabel().equals("O"))
  73. return 0;}
  74. }
  75. } else if (i == 2){
  76. if (myButtons[i][j].getLabel().equals("X")){
  77. if (myButtons[i-1][j].getLabel().equals("X")){
  78. if (myButtons[i+1][j].getLabel().equals("X"))
  79. return 1;
  80. }
  81. else if (myButtons[i][j-1].getLabel().equals("X")){
  82. if (myButtons[i][j+1].getLabel().equals("X"))
  83. return 1;}
  84. else if (myButtons[i+1][j+1].getLabel().equals("X") && (j == 2)){
  85. if (myButtons[i-1][j-1].getLabel().equals("X"))
  86. return 1;}
  87. }else if (myButtons[i][j].getLabel().equals("O")){
  88. if (myButtons[i-1][j].getLabel().equals("O")){
  89. if (myButtons[i+1][j].getLabel().equals("O"))
  90. return 0;}
  91. else if (myButtons[i][j-1].getLabel().equals("O")){
  92. if (myButtons[i][j+1].getLabel().equals("O"))
  93. return 0;}
  94. else if (myButtons[i+1][j+1].getLabel().equals("O") && (j == 2)){
  95. if (myButtons[i-1][j-1].getLabel().equals("O"))
  96. return 0;}
  97. }
  98. } else if (i == 3){
  99. if (myButtons[i][j].getLabel().equals("X")){
  100. if (myButtons[i-1][j].getLabel().equals("X")){
  101. if (myButtons[i-2][j].getLabel().equals("X"))
  102. return 1;
  103. }
  104. else if (myButtons[i][j+1].getLabel().equals("X")){
  105. if (myButtons[i][j+2].getLabel().equals("X"))
  106. return 1;}
  107. }else if (myButtons[i][j].getLabel().equals("O")){
  108. if (myButtons[i-1][j].getLabel().equals("O")){
  109. if (myButtons[i-2][j].getLabel().equals("O"))
  110. return 0;}
  111. else if (myButtons[i][j+1].getLabel().equals("O")){
  112. if (myButtons[i][j+2].getLabel().equals("O"))
  113. return 0;}
  114. }
  115. }      
  116. }
  117. }
  118. }      
  119. if (blank==9)
  120. return -1; // no winner
  121. else
  122. return 2; // game not over yet
  123. }
clone this paste RAW Paste Data