Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. import javax.swing.JLabel;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Button;
  5. import java.awt.Color;
  6. import java.awt.Label;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14.  
  15. public class TicTac {
  16.  
  17. JPanel windowContent;
  18. JButton[] squares = new JButton[9];
  19. JButton newGameButton;
  20. JLabel wonScore;
  21. JLabel loseScore;
  22. JLabel score;
  23. JPanel panel1;
  24. JPanel panel2;
  25. JPanel panel3;
  26. int emptySquaresLeft=9;
  27. int won = 0;
  28. int lose = 0;
  29.  
  30. public static void main () {
  31.  
  32. JPanel windowContent = new JPanel();
  33. BorderLayout bl = new BorderLayout();
  34. windowContent.setLayout(bl);
  35. JLabel wonScore = new JLabel();
  36. JLabel loseScore = new JLabel();
  37. JPanel panel1 = new JPanel();
  38. windowContent.add("North", panel1);
  39. JButton newGameButton = new JButton();
  40. newGameButton.setBackground(Color.GREEN);
  41.  
  42. wonScore = new JLabel ("Побед: 0");
  43. loseScore = new JLabel ("Поражений: 0");
  44.  
  45. panel1.setLayout(bl);
  46. panel1.add(newGameButton);
  47. panel1.add(wonScore);
  48. panel1.add(loseScore);
  49.  
  50. JLabel score = new JLabel ("Твой ход!");
  51. windowContent.add(score, "South");
  52.  
  53. JPanel panel2 = new JPanel();
  54. windowContent.add("Center", panel2);
  55. JButton[] squares = new JButton[9];
  56.  
  57. for (int i=0; i<10; i++) {
  58. squares[i] = new JButton (""+i);
  59. squares[i].addActionListener(actionPerformed); /*!!!! Cant be resolved to a variable !!!*/
  60. squares[i].setBackground(Color.ORANGE);
  61. panel2.add(squares[i]);
  62. }
  63. }
  64.  
  65. public void actionPerformed (ActionEvent e){
  66. JButton theButton = (JButton) e.getSource();
  67.  
  68. if (theButton == newGameButton){
  69. for (int i=0; i<9; i++) {
  70. squares[i].setEnabled(true);
  71. squares[i].setLabel("");
  72. squares[i].setBackground(Color.ORANGE);
  73. }
  74.  
  75. emptySquaresLeft=9;
  76. score.setText("Твой ход!");
  77. newGameButton.setEnabled(false);
  78. return;
  79. }
  80.  
  81. String winner="";
  82.  
  83. for (int i=0; i<9; i++) {
  84. if (theButton == squares[i]){
  85. squares[i].setLabel("X");
  86. winner = lookForWinner();
  87. squares[i].setEnabled(false);
  88.  
  89. if (!"".equals(winner)) {
  90. endTheGame ();
  91. } else {
  92. computerMove();
  93. winner = lookForWinner();
  94.  
  95. if (!"".equals(winner)) {
  96. endTheGame();
  97. }
  98. }
  99. break;
  100. }
  101. }
  102. if (winner.equals("X")) {
  103. score.setText("Ты победил!");
  104. won++;
  105. wonScore.setText("Побед: "+won);
  106. } else if (winner.equals("O")) {
  107. score.setText("Лох!");
  108. lose++;
  109. loseScore.setText("Поражений: "+lose);
  110. } else if (winner.equals("T")) {
  111. score.setText("Победила дружба..");
  112. }
  113. }
  114. String lookForWinner() {
  115. String theWinner="";
  116. emptySquaresLeft--;
  117. if (emptySquaresLeft == 0) {
  118. return "T";
  119. }
  120.  
  121. if (!squares[0].getLabel().equals("")&&
  122. squares[0].getLabel().equals(squares[1].getLabel()) &&
  123. squares[0].getLabel().equals(squares[2].getLabel())) {
  124. theWinner = squares [0].getLabel();
  125. highlightWinner(0,1,2);
  126.  
  127. } else if (!squares[3].getLabel().equals("")&&
  128. squares[3].getLabel().equals(squares[4].getLabel()) &&
  129. squares[3].getLabel().equals(squares[5].getLabel())) {
  130. theWinner = squares [3].getLabel();
  131. highlightWinner(3,4,5);
  132.  
  133. } else if (!squares[6].getLabel().equals("")&&
  134. squares[6].getLabel().equals(squares[7].getLabel()) &&
  135. squares[6].getLabel().equals(squares[8].getLabel())) {
  136. theWinner = squares [6].getLabel();
  137. highlightWinner(6,7,8);
  138.  
  139. } else if (!squares[0].getLabel().equals("")&&
  140. squares[0].getLabel().equals(squares[3].getLabel()) &&
  141. squares[0].getLabel().equals(squares[6].getLabel())) {
  142. theWinner = squares [3].getLabel();
  143. highlightWinner(0,3,6);
  144.  
  145. } else if (!squares[1].getLabel().equals("")&&
  146. squares[1].getLabel().equals(squares[4].getLabel()) &&
  147. squares[1].getLabel().equals(squares[7].getLabel())) {
  148. theWinner = squares [1].getLabel();
  149. highlightWinner(1,4,7);
  150.  
  151. } else if (!squares[2].getLabel().equals("")&&
  152. squares[2].getLabel().equals(squares[5].getLabel()) &&
  153. squares[2].getLabel().equals(squares[8].getLabel())) {
  154. theWinner = squares [2].getLabel();
  155. highlightWinner(2,5,8);
  156. } else if (!squares[0].getLabel().equals("")&&
  157. squares[0].getLabel().equals(squares[4].getLabel()) &&
  158. squares[0].getLabel().equals(squares[8].getLabel())) {
  159. theWinner = squares [0].getLabel();
  160. highlightWinner(0,4,8);
  161. } else if (!squares[2].getLabel().equals("")&&
  162. squares[2].getLabel().equals(squares[4].getLabel()) &&
  163. squares[2].getLabel().equals(squares[6].getLabel())) {
  164. theWinner = squares [2].getLabel();
  165. highlightWinner(2,4,6);
  166. }
  167. return theWinner;
  168. }
  169. void computerMove() {
  170. int selectedSquare;
  171.  
  172. selectedSquare = findEmptySquare("O");
  173.  
  174. if (selectedSquare == -1)
  175. selectedSquare = findEmptySquare("X");
  176.  
  177. if ((selectedSquare == -1) && (squares[4].getLabel().equals(""))) {
  178. selectedSquare=4;
  179. }
  180.  
  181. if (selectedSquare == -1) {
  182. selectedSquare = getRandomSquare();
  183. }
  184. squares[selectedSquare].setLabel("O");
  185. squares[selectedSquare].setEnabled(false);
  186. }
  187.  
  188. int findEmptySquare(String Player) {
  189. int weight[] =new int[9];
  190. for (int i=0; i<9; i++) {
  191. if (squares[i].getLabel().equals("O"))
  192. weight[i]=-1;
  193. else if (squares[i].getLabel().equals("X"))
  194. weight[i]=1;
  195. else
  196. weight[i]=0;
  197. }
  198.  
  199. int twoWeights = Player.equals("O") ? -2 : 2;
  200. if (weight[0] + weight[1] + weight[2] == twoWeights) {
  201. if (weight[0] == 0)
  202. return 0;
  203. else if (weight[1] == 0)
  204. return 1;
  205. else
  206. return 2;
  207. }
  208.  
  209. if (weight[3] + weight[4] + weight[5] == twoWeights) {
  210. if (weight[3] == 0)
  211. return 3;
  212. else if (weight[4] == 0)
  213. return 4;
  214. else
  215. return 5;
  216. }
  217.  
  218. if (weight[6] + weight[7] + weight[8] == twoWeights) {
  219. if (weight[6] == 0)
  220. return 6;
  221. else if (weight[7] == 0)
  222. return 7;
  223. else
  224. return 8;
  225. }
  226.  
  227. if (weight[0] + weight[3] + weight[6] == twoWeights) {
  228. if (weight[0] == 0)
  229. return 0;
  230. else if (weight[3] == 0)
  231. return 3;
  232. else
  233. return 6;
  234. }
  235.  
  236. if (weight[1] + weight[4] + weight[7] == twoWeights) {
  237. if (weight[1] == 0)
  238. return 1;
  239. else if (weight[4] == 0)
  240. return 4;
  241. else
  242. return 7;
  243. }
  244.  
  245. if (weight[2] + weight[5] + weight[8] == twoWeights) {
  246. if (weight[2] == 0)
  247. return 2;
  248. else if (weight[5] == 0)
  249. return 5;
  250. else
  251. return 8;
  252. }
  253.  
  254. if (weight[2] + weight[4] + weight[6] == twoWeights) {
  255. if (weight[2] == 0)
  256. return 2;
  257. else if (weight[4] == 0)
  258. return 4;
  259. else
  260. return 6;
  261. }
  262. if (weight[0] + weight[4] + weight[8] == twoWeights) {
  263. if (weight[0] == 0)
  264. return 0;
  265. else if (weight[4] == 0)
  266. return 4;
  267. else
  268. return 8;
  269. }
  270. return -1;
  271. }
  272.  
  273. int getRandomSquare() {
  274. boolean gotEmptySquare = false;
  275. int selectedSquare = -1;
  276. do {
  277. selectedSquare=(int) (Math.random()*9);
  278. if (squares[selectedSquare].getLabel().equals("")) {
  279. gotEmptySquare = true;
  280. }
  281. } while (!gotEmptySquare );
  282.  
  283. return selectedSquare;
  284. }
  285.  
  286. void highlightWinner (int win1, int win2, int win3) {
  287. squares[win1].setBackground(Color.CYAN);
  288. squares[win2].setBackground(Color.CYAN);
  289. squares[win3].setBackground(Color.CYAN);
  290. }
  291.  
  292. void endTheGame () {
  293. newGameButton.setEnabled(true);
  294. for (int i=0; i<9; i++) {
  295. squares[i].setEnabled(false);
  296. }
  297.  
  298. JFrame frame = new JFrame("TicTac");
  299. frame.setContentPane(windowContent);
  300. frame.pack();
  301. frame.setVisible(true);
  302. }
  303. public static void main(String[] args) {
  304. TicTac TicTac =new TicTac();
  305. }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement