Реализовать ничью в игре Крестики-Нолики - Java
Формулировка задачи:
Помогите сделать, чтоб в форме выводились не только победы крестиков и ноликов, но еще и ничья
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class KrestikiNoliki {
public static void main(String[] args) {
KOFrame frame = new KOFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class KOFrame extends JFrame {
JLabel score;
public KOFrame() {
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension ss = kit.getScreenSize();
int scrW = ss.width;
int scrH = ss.height;
int fW = 300;
int fH = 350;
// System.out.println((scrW - fW) / 2 +" " + (scrH - fH) / 2);
JFrame frame = new JFrame();
score = new JLabel();
setSize(fW, fH);
setLocation((scrW - fW) / 2, (scrH - fH) / 2);
setTitle("Крестики-нолики Баранова Е.");
setCursor(Cursor.HAND_CURSOR);
KOPanel panel = new KOPanel(this);
Container contain = getContentPane();
contain.add(panel);
ExitPanel exit_panel = new ExitPanel(panel);
contain.add(exit_panel, "South");
add(score, BorderLayout.NORTH);
refreshScore(0, 0);
}
public void refreshScore(int x, int o) {
score.setText(" Крестики: " + x
+ " Нолики: " + o);
}
}
class ExitPanel extends JPanel {
KOPanel koPanel;
public ExitPanel(final KOPanel koPanel) {
setLayout(new GridLayout(1, 2));
KOButton exit = new KOButton(4, 4);
KOButton restart = new KOButton(5, 5);
this.koPanel = koPanel;
add(restart);
add(exit);
exit.setText("Выход");
restart.setText("Рестарт");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
restart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
koPanel.clear();
koPanel.clearGameScreen();
}
});
}
}
class KOPanel extends JPanel {
char[][] game_arr = new char[3][3];
{
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
game_arr[i][j] = ' ';
}
}
}
KOButton[][] buttons = new KOButton[3][3];
public int cout = 0;
ImageIcon krest = new ImageIcon("krestik i nolik/x.jpg");
ImageIcon nol = new ImageIcon("krestik i nolik/o.jpg");
ImageIcon start = new ImageIcon("/start_image.jpg");
KOFrame koFrame; // ссылка на главный фрейм
int xCount = 0; // победы Х
int oCount = 0; // победы О
public KOPanel(KOFrame koFrame) {
this.koFrame = koFrame;
setLayout(new GridLayout(3, 3));
{
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
buttons[i][j] = new KOButton(i, j);
buttons[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
KOButton sourceOfAction = (KOButton) event
.getSource();
if (cout % 2 == 0
&& game_arr[sourceOfAction.getRow()][sourceOfAction
.getColumn()] == ' ') {
game_arr[sourceOfAction.getRow()][sourceOfAction
.getColumn()] = 'x';
sourceOfAction.setIcon(krest);
chek();
cout++;
} else if (cout % 2 == 1
&& game_arr[sourceOfAction.getRow()][sourceOfAction
.getColumn()] == ' ') {
game_arr[sourceOfAction.getRow()][sourceOfAction
.getColumn()] = 'o';
sourceOfAction.setIcon(nol);
chek();
cout++;
}
}
});
add(buttons[i][j]);
}
}
}
}
public void chek() {
String exit_option_string;
int exit_option = 0;
if ((game_arr[0][0] == 'x' && game_arr[0][1] == 'x' && game_arr[0][2] == 'x')
|| (game_arr[1][0] == 'x' && game_arr[1][1] == 'x' && game_arr[1][2] == 'x')
|| (game_arr[2][0] == 'x' && game_arr[2][1] == 'x' && game_arr[2][2] == 'x')
|| (game_arr[0][0] == 'x' && game_arr[1][0] == 'x' && game_arr[2][0] == 'x')
|| (game_arr[0][1] == 'x' && game_arr[1][1] == 'x' && game_arr[2][1] == 'x')
|| (game_arr[0][2] == 'x' && game_arr[1][2] == 'x' && game_arr[2][2] == 'x')
|| (game_arr[0][0] == 'x' && game_arr[1][1] == 'x' && game_arr[2][2] == 'x')
|| (game_arr[2][0] == 'x' && game_arr[1][1] == 'x' && game_arr[0][2] == 'x')) {
JOptionPane.showMessageDialog(null, "Выиграли крестики!");
clear();
clearGameScreen();
xCount++;
koFrame.refreshScore(xCount, oCount);
}
if ((game_arr[0][0] == 'o' && game_arr[0][1] == 'o' && game_arr[0][2] == 'o')
|| (game_arr[1][0] == 'o' && game_arr[1][1] == 'o' && game_arr[1][2] == 'o')
|| (game_arr[2][0] == 'o' && game_arr[2][1] == 'o' && game_arr[2][2] == 'o')
|| (game_arr[0][0] == 'o' && game_arr[1][0] == 'o' && game_arr[2][0] == 'o')
|| (game_arr[0][1] == 'o' && game_arr[1][1] == 'o' && game_arr[2][1] == 'o')
|| (game_arr[0][2] == 'o' && game_arr[1][2] == 'o' && game_arr[2][2] == 'o')
|| (game_arr[0][0] == 'o' && game_arr[1][1] == 'o' && game_arr[2][2] == 'o')
|| (game_arr[2][0] == 'o' && game_arr[1][1] == 'o' && game_arr[0][2] == 'o')) {
JOptionPane.showMessageDialog(null, "Выиграли нолики!");
clear();
clearGameScreen();
oCount++;
koFrame.refreshScore(xCount, oCount);
}
}
public void clear() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
game_arr[i][j] = ' ';
}
}
}
public void clearGameScreen() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setIcon(start);
}
}
}
}
class KOButton extends JButton {
public KOButton(int r, int c) {
super();
row = r;
column = c;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
private int row, column;
}Решение задачи: «Реализовать ничью в игре Крестики-Нолики»
textual
Листинг программы
if ((game_arr[0][0] == 'x' && game_arr[0][1] == 'x' && game_arr[0][2] == 'x')
|| (game_arr[1][0] == 'x' && game_arr[1][1] == 'x' && game_arr[1][2] == 'x')
|| (game_arr[2][0] == 'x' && game_arr[2][1] == 'x' && game_arr[2][2] == 'x')
|| (game_arr[0][0] == 'x' && game_arr[1][0] == 'x' && game_arr[2][0] == 'x')
|| (game_arr[0][1] == 'x' && game_arr[1][1] == 'x' && game_arr[2][1] == 'x')
|| (game_arr[0][2] == 'x' && game_arr[1][2] == 'x' && game_arr[2][2] == 'x')
|| (game_arr[0][0] == 'x' && game_arr[1][1] == 'x' && game_arr[2][2] == 'x')
|| (game_arr[2][0] == 'x' && game_arr[1][1] == 'x' && game_arr[0][2] == 'x')) {