Guest User

GameEngine

a guest
Sep 14th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. import java.awt.Button;
  2. import java.awt.Color;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5. import javax.swing.JButton;
  6.  
  7. public class TicTacEngine implements ActionListener {
  8.  
  9. TicTac parent;
  10.  
  11. TicTacEngine (TicTac parent){
  12. this.parent=parent;
  13. }
  14.  
  15. public void actionPerformed (ActionEvent e){
  16. Button theButton = (Button) e.getSource();
  17.  
  18. if (theButton == parent.newGameButton){
  19. for (int i=0; i<9; i++) {
  20. parent.squares[i].setEnabled(true);
  21. parent.squares[i].setLabel("");
  22. parent.squares[i].setBackground(Color.ORANGE);
  23. }
  24.  
  25. parent.emptySquaresLeft=9;
  26. parent.score.setText("Твой ход!");
  27. parent.newGameButton.setEnabled(false);
  28. return;
  29. }
  30.  
  31. String winner="";
  32.  
  33. for (int i=0; i<9; i++) {
  34. if (theButton == parent.squares[i]){
  35. parent.squares[i].setLabel("X");
  36. winner = lookForWinner();
  37. parent.squares[i].setEnabled(false);
  38.  
  39. if (!"".equals(winner)) {
  40. endTheGame ();
  41. } else {
  42. computerMove();
  43. winner = lookForWinner();
  44.  
  45. if (!"".equals(winner)) {
  46. endTheGame();
  47. }
  48. }
  49. break;
  50. }
  51. }
  52. if (winner.equals("X")) {
  53. parent.score.setText("Ты победил, красавчик!");
  54. parent.won++;
  55. parent.wonScore.setText("Побед: "+parent.won);
  56. } else if (winner.equals("O")) {
  57. parent.score.setText("Лох, пидр!");
  58. parent.lose++;
  59. parent.loseScore.setText("Поражений: "+parent.lose);
  60. } else if (winner.equals("T")) {
  61. parent.score.setText("Победила дружба. Ыыыы.");
  62. }
  63. }
  64. String lookForWinner() {
  65. String theWinner="";
  66. parent.emptySquaresLeft--;
  67. if (parent.emptySquaresLeft == 0) {
  68. return "T";
  69. }
  70.  
  71. if (!parent.squares[0].getLabel().equals("")&&
  72. parent.squares[0].getLabel().equals(parent.squares[1].getLabel()) &&
  73. parent.squares[0].getLabel().equals(parent.squares[2].getLabel())) {
  74. theWinner = parent.squares [0].getLabel();
  75. highlightWinner(0,1,2);
  76.  
  77. } else if (!parent.squares[3].getLabel().equals("")&&
  78. parent.squares[3].getLabel().equals(parent.squares[4].getLabel()) &&
  79. parent.squares[3].getLabel().equals(parent.squares[5].getLabel())) {
  80. theWinner = parent.squares [3].getLabel();
  81. highlightWinner(3,4,5);
  82.  
  83. } else if (!parent.squares[6].getLabel().equals("")&&
  84. parent.squares[6].getLabel().equals(parent.squares[7].getLabel()) &&
  85. parent.squares[6].getLabel().equals(parent.squares[8].getLabel())) {
  86. theWinner = parent.squares[6].getLabel();
  87. highlightWinner(6,7,8);
  88.  
  89. } else if (!parent.squares[0].getLabel().equals("")&&
  90. parent.squares[0].getLabel().equals(parent.squares[3].getLabel()) &&
  91. parent.squares[0].getLabel().equals(parent.squares[6].getLabel())) {
  92. theWinner = parent.squares [3].getLabel();
  93. highlightWinner(0,3,6);
  94.  
  95. } else if (!parent.squares[1].getLabel().equals("")&&
  96. parent.squares[1].getLabel().equals(parent.squares[4].getLabel()) &&
  97. parent.squares[1].getLabel().equals(parent.squares[7].getLabel())) {
  98. theWinner = parent.squares [1].getLabel();
  99. highlightWinner(1,4,7);
  100.  
  101. } else if (!parent.squares[2].getLabel().equals("")&&
  102. parent.squares[2].getLabel().equals(parent.squares[5].getLabel()) &&
  103. parent.squares[2].getLabel().equals(parent.squares[8].getLabel())) {
  104. theWinner = parent.squares [2].getLabel();
  105. highlightWinner(2,5,8);
  106. } else if (!parent.squares[0].getLabel().equals("")&&
  107. parent.squares[0].getLabel().equals(parent.squares[4].getLabel()) &&
  108. parent.squares[0].getLabel().equals(parent.squares[8].getLabel())) {
  109. theWinner = parent.squares [0].getLabel();
  110. highlightWinner(0,4,8);
  111. } else if (!parent.squares[2].getLabel().equals("")&&
  112. parent.squares[2].getLabel().equals(parent.squares[4].getLabel()) &&
  113. parent.squares[2].getLabel().equals(parent.squares[6].getLabel())) {
  114. theWinner = parent.squares [2].getLabel();
  115. highlightWinner(2,4,6);
  116. }
  117. return theWinner;
  118. }
  119.  
  120. void computerMove() {
  121. int selectedSquare;
  122.  
  123. selectedSquare = findEmptySquare("O");
  124.  
  125. if (selectedSquare == -1)
  126. selectedSquare = findEmptySquare("X");
  127.  
  128. if ((selectedSquare == -1) && (parent.squares[4].getLabel().equals(""))) {
  129. selectedSquare=4;
  130. }
  131.  
  132. if (selectedSquare == -1) {
  133. selectedSquare = getRandomSquare();
  134. }
  135. parent.squares[selectedSquare].setLabel("O");
  136. parent.squares[selectedSquare].setEnabled(false);
  137. }
  138.  
  139. int findEmptySquare(String Player) {
  140. int weight[] =new int[9];
  141. for (int i=0; i<9; i++) {
  142. if (parent.squares[i].getLabel().equals("O"))
  143. weight[i]=-1;
  144. else if (parent.squares[i].getLabel().equals("X"))
  145. weight[i]=1;
  146. else
  147. weight[i]=0;
  148. }
  149.  
  150. int twoWeights = Player.equals("O") ? -2 : 2;
  151. if (weight[0] + weight[1] + weight[2] == twoWeights) {
  152. if (weight[0] == 0)
  153. return 0;
  154. else if (weight[1] == 0)
  155. return 1;
  156. else
  157. return 2;
  158. }
  159.  
  160. if (weight[3] + weight[4] + weight[5] == twoWeights) {
  161. if (weight[3] == 0)
  162. return 3;
  163. else if (weight[4] == 0)
  164. return 4;
  165. else
  166. return 5;
  167. }
  168.  
  169. if (weight[6] + weight[7] + weight[8] == twoWeights) {
  170. if (weight[6] == 0)
  171. return 6;
  172. else if (weight[7] == 0)
  173. return 7;
  174. else
  175. return 8;
  176. }
  177.  
  178. if (weight[0] + weight[3] + weight[6] == twoWeights) {
  179. if (weight[0] == 0)
  180. return 0;
  181. else if (weight[3] == 0)
  182. return 3;
  183. else
  184. return 6;
  185. }
  186.  
  187. if (weight[1] + weight[4] + weight[7] == twoWeights) {
  188. if (weight[1] == 0)
  189. return 1;
  190. else if (weight[4] == 0)
  191. return 4;
  192. else
  193. return 7;
  194. }
  195.  
  196. if (weight[2] + weight[5] + weight[8] == twoWeights) {
  197. if (weight[2] == 0)
  198. return 2;
  199. else if (weight[5] == 0)
  200. return 5;
  201. else
  202. return 8;
  203. }
  204.  
  205. if (weight[2] + weight[4] + weight[6] == twoWeights) {
  206. if (weight[2] == 0)
  207. return 2;
  208. else if (weight[4] == 0)
  209. return 4;
  210. else
  211. return 6;
  212. }
  213. if (weight[0] + weight[4] + weight[8] == twoWeights) {
  214. if (weight[0] == 0)
  215. return 0;
  216. else if (weight[4] == 0)
  217. return 4;
  218. else
  219. return 8;
  220. }
  221. return -1;
  222. }
  223.  
  224. int getRandomSquare() {
  225. boolean gotEmptySquare = false;
  226. int selectedSquare = -1;
  227. do {
  228. selectedSquare=(int) (Math.random()*9);
  229. if (parent.squares[selectedSquare].getLabel().equals("")) {
  230. gotEmptySquare = true;
  231. }
  232. } while (!gotEmptySquare );
  233.  
  234. return selectedSquare;
  235. }
  236.  
  237. void highlightWinner (int win1, int win2, int win3) {
  238. parent.squares[win1].setBackground(Color.CYAN);
  239. parent.squares[win2].setBackground(Color.CYAN);
  240. parent.squares[win3].setBackground(Color.CYAN);
  241. }
  242.  
  243. void endTheGame () {
  244. parent.newGameButton.setEnabled(true);
  245. for (int i=0; i<9; i++) {
  246. parent.squares[i].setEnabled(false);
  247. }
  248. }
  249. }
Add Comment
Please, Sign In to add comment