Реализовать ничью в игре Крестики-Нолики - Java

Узнай цену своей работы

Формулировка задачи:

Помогите сделать, чтоб в форме выводились не только победы крестиков и ноликов, но еще и ничья
Листинг программы
  1. import java.awt.BorderLayout;
  2. import java.awt.Container;
  3. import java.awt.Cursor;
  4. import java.awt.Dimension;
  5. import java.awt.GridLayout;
  6. import java.awt.Toolkit;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.ImageIcon;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JPanel;
  15. public class KrestikiNoliki {
  16. public static void main(String[] args) {
  17. KOFrame frame = new KOFrame();
  18. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. frame.show();
  20. }
  21. }
  22. class KOFrame extends JFrame {
  23. JLabel score;
  24. public KOFrame() {
  25. Toolkit kit = Toolkit.getDefaultToolkit();
  26. Dimension ss = kit.getScreenSize();
  27. int scrW = ss.width;
  28. int scrH = ss.height;
  29. int fW = 300;
  30. int fH = 350;
  31. // System.out.println((scrW - fW) / 2 +" " + (scrH - fH) / 2);
  32. JFrame frame = new JFrame();
  33. score = new JLabel();
  34. setSize(fW, fH);
  35. setLocation((scrW - fW) / 2, (scrH - fH) / 2);
  36. setTitle("Крестики-нолики Баранова Е.");
  37. setCursor(Cursor.HAND_CURSOR);
  38. KOPanel panel = new KOPanel(this);
  39. Container contain = getContentPane();
  40. contain.add(panel);
  41. ExitPanel exit_panel = new ExitPanel(panel);
  42. contain.add(exit_panel, "South");
  43. add(score, BorderLayout.NORTH);
  44. refreshScore(0, 0);
  45. }
  46. public void refreshScore(int x, int o) {
  47. score.setText(" Крестики: " + x
  48. + " Нолики: " + o);
  49. }
  50. }
  51. class ExitPanel extends JPanel {
  52. KOPanel koPanel;
  53. public ExitPanel(final KOPanel koPanel) {
  54. setLayout(new GridLayout(1, 2));
  55. KOButton exit = new KOButton(4, 4);
  56. KOButton restart = new KOButton(5, 5);
  57. this.koPanel = koPanel;
  58. add(restart);
  59. add(exit);
  60. exit.setText("Выход");
  61. restart.setText("Рестарт");
  62. exit.addActionListener(new ActionListener() {
  63. public void actionPerformed(ActionEvent event) {
  64. System.exit(0);
  65. }
  66. });
  67. restart.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent event) {
  69. koPanel.clear();
  70. koPanel.clearGameScreen();
  71. }
  72. });
  73. }
  74. }
  75. class KOPanel extends JPanel {
  76. char[][] game_arr = new char[3][3];
  77. {
  78. for (int i = 0; i < 3; ++i) {
  79. for (int j = 0; j < 3; ++j) {
  80. game_arr[i][j] = ' ';
  81. }
  82. }
  83. }
  84. KOButton[][] buttons = new KOButton[3][3];
  85. public int cout = 0;
  86. ImageIcon krest = new ImageIcon("krestik i nolik/x.jpg");
  87. ImageIcon nol = new ImageIcon("krestik i nolik/o.jpg");
  88. ImageIcon start = new ImageIcon("/start_image.jpg");
  89. KOFrame koFrame; // ссылка на главный фрейм
  90. int xCount = 0; // победы Х
  91. int oCount = 0; // победы О
  92. public KOPanel(KOFrame koFrame) {
  93. this.koFrame = koFrame;
  94. setLayout(new GridLayout(3, 3));
  95. {
  96. for (int i = 0; i < 3; ++i) {
  97. for (int j = 0; j < 3; ++j) {
  98. buttons[i][j] = new KOButton(i, j);
  99. buttons[i][j].addActionListener(new ActionListener() {
  100. public void actionPerformed(ActionEvent event) {
  101. KOButton sourceOfAction = (KOButton) event
  102. .getSource();
  103. if (cout % 2 == 0
  104. && game_arr[sourceOfAction.getRow()][sourceOfAction
  105. .getColumn()] == ' ') {
  106. game_arr[sourceOfAction.getRow()][sourceOfAction
  107. .getColumn()] = 'x';
  108. sourceOfAction.setIcon(krest);
  109. chek();
  110. cout++;
  111. } else if (cout % 2 == 1
  112. && game_arr[sourceOfAction.getRow()][sourceOfAction
  113. .getColumn()] == ' ') {
  114. game_arr[sourceOfAction.getRow()][sourceOfAction
  115. .getColumn()] = 'o';
  116. sourceOfAction.setIcon(nol);
  117. chek();
  118. cout++;
  119. }
  120. }
  121. });
  122. add(buttons[i][j]);
  123. }
  124. }
  125. }
  126. }
  127. public void chek() {
  128. String exit_option_string;
  129. int exit_option = 0;
  130. if ((game_arr[0][0] == 'x' && game_arr[0][1] == 'x' && game_arr[0][2] == 'x')
  131. || (game_arr[1][0] == 'x' && game_arr[1][1] == 'x' && game_arr[1][2] == 'x')
  132. || (game_arr[2][0] == 'x' && game_arr[2][1] == 'x' && game_arr[2][2] == 'x')
  133. || (game_arr[0][0] == 'x' && game_arr[1][0] == 'x' && game_arr[2][0] == 'x')
  134. || (game_arr[0][1] == 'x' && game_arr[1][1] == 'x' && game_arr[2][1] == 'x')
  135. || (game_arr[0][2] == 'x' && game_arr[1][2] == 'x' && game_arr[2][2] == 'x')
  136. || (game_arr[0][0] == 'x' && game_arr[1][1] == 'x' && game_arr[2][2] == 'x')
  137. || (game_arr[2][0] == 'x' && game_arr[1][1] == 'x' && game_arr[0][2] == 'x')) {
  138. JOptionPane.showMessageDialog(null, "Выиграли крестики!");
  139. clear();
  140. clearGameScreen();
  141. xCount++;
  142. koFrame.refreshScore(xCount, oCount);
  143. }
  144.  
  145. if ((game_arr[0][0] == 'o' && game_arr[0][1] == 'o' && game_arr[0][2] == 'o')
  146. || (game_arr[1][0] == 'o' && game_arr[1][1] == 'o' && game_arr[1][2] == 'o')
  147. || (game_arr[2][0] == 'o' && game_arr[2][1] == 'o' && game_arr[2][2] == 'o')
  148. || (game_arr[0][0] == 'o' && game_arr[1][0] == 'o' && game_arr[2][0] == 'o')
  149. || (game_arr[0][1] == 'o' && game_arr[1][1] == 'o' && game_arr[2][1] == 'o')
  150. || (game_arr[0][2] == 'o' && game_arr[1][2] == 'o' && game_arr[2][2] == 'o')
  151. || (game_arr[0][0] == 'o' && game_arr[1][1] == 'o' && game_arr[2][2] == 'o')
  152. || (game_arr[2][0] == 'o' && game_arr[1][1] == 'o' && game_arr[0][2] == 'o')) {
  153. JOptionPane.showMessageDialog(null, "Выиграли нолики!");
  154. clear();
  155. clearGameScreen();
  156. oCount++;
  157. koFrame.refreshScore(xCount, oCount);
  158. }
  159.  
  160. }
  161. public void clear() {
  162. for (int i = 0; i < 3; i++) {
  163. for (int j = 0; j < 3; j++) {
  164. game_arr[i][j] = ' ';
  165. }
  166. }
  167. }
  168. public void clearGameScreen() {
  169. for (int i = 0; i < 3; i++) {
  170. for (int j = 0; j < 3; j++) {
  171. buttons[i][j].setIcon(start);
  172. }
  173. }
  174. }
  175. }
  176. class KOButton extends JButton {
  177. public KOButton(int r, int c) {
  178. super();
  179. row = r;
  180. column = c;
  181. }
  182. public int getRow() {
  183. return row;
  184. }
  185. public int getColumn() {
  186. return column;
  187. }
  188. private int row, column;
  189. }

Решение задачи: «Реализовать ничью в игре Крестики-Нолики»

textual
Листинг программы
  1. if ((game_arr[0][0] == 'x' && game_arr[0][1] == 'x' && game_arr[0][2] == 'x')
  2.   || (game_arr[1][0] == 'x' && game_arr[1][1] == 'x' && game_arr[1][2] == 'x')
  3.   || (game_arr[2][0] == 'x' && game_arr[2][1] == 'x' && game_arr[2][2] == 'x')
  4.   || (game_arr[0][0] == 'x' && game_arr[1][0] == 'x' && game_arr[2][0] == 'x')
  5.   || (game_arr[0][1] == 'x' && game_arr[1][1] == 'x' && game_arr[2][1] == 'x')
  6.   || (game_arr[0][2] == 'x' && game_arr[1][2] == 'x' && game_arr[2][2] == 'x')
  7.   || (game_arr[0][0] == 'x' && game_arr[1][1] == 'x' && game_arr[2][2] == 'x')
  8.   || (game_arr[2][0] == 'x' && game_arr[1][1] == 'x' && game_arr[0][2] == 'x')) {

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 4.286 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут