-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.java
More file actions
145 lines (130 loc) · 3.58 KB
/
Window.java
File metadata and controls
145 lines (130 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Window extends JFrame
{
private JButton buttonGrid[][];
private String player;
private boolean isSingle;
public Window(boolean isSingle)
{
super("XO Game");
buttonGrid = new JButton[3][3];
player = "X";
this.isSingle = isSingle;
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new GridLayout(3, 3));
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
buttonGrid[row][col] = new JButton("");
buttonGrid[row][col].addActionListener(buttonListener);
buttonGrid[row][col].setFont(new Font("Arial", Font.PLAIN, 60));
buttonGrid[row][col].setFocusPainted(false);
buttonGrid[row][col].setContentAreaFilled(false);
gamePanel.add(buttonGrid[row][col]);
}
}
setSize(500, 500);
setContentPane(gamePanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
/**
* Listener for executing moves as buttons are pressed
*/
ActionListener buttonListener = (ActionEvent e) ->
{
for (int row = 0; row < buttonGrid.length; row++)
{
for (int col = 0; col < buttonGrid[row].length; col++)
{
if (buttonGrid[row][col] == e.getSource())
{
JButton button = (JButton) e.getSource();
button.setText(player);
button.setEnabled(false);
EndState playerWon = isVictory(buttonGrid);
if (playerWon != null)
{
endScreen();
}
player = player.equals("X") ? "O" : "X";
if (isSingle && playerWon == null)
{
int bestMove[] = Minimax.computerMove(buttonGrid);
buttonGrid[bestMove[0]][bestMove[1]].setText(player);
buttonGrid[bestMove[0]][bestMove[1]].setEnabled(false);
EndState computerWon = isVictory(buttonGrid);
if (computerWon != null)
{
endScreen();
}
player = player.equals("X") ? "O" : "X";
}
break;
}
}
}
};
/**
* Updates the screen to the finished state
*/
public void endScreen()
{
for (JButton buttonRow[] : buttonGrid)
{
for (JButton btn : buttonRow)
{
btn.setEnabled(false);
}
}
}
/**
* Checks the current state of the game
* @param board the board being checked for it's state
* @return EndState object showing who won and where it has happen
*/
public static EndState isVictory(JButton board[][])
{
// Horizontal / Vertical
for (int i = 0; i < board.length; i++)
{
if (board[i][0].getText().equals(board[i][1].getText()) && board[i][1].getText().equals(board[i][2].getText()) && !board[i][0].getText().equals(""))
{
return new EndState(board[i][0].getText(), "H", i);
}
else if (board[0][i].getText().equals(board[1][i].getText()) && board[1][i].getText().equals(board[2][i].getText()) && !board[0][i].getText().equals(""))
{
return new EndState(board[0][i].getText(), "V", i);
}
}
// Diagnal
String middle = board[1][1].getText();
if (board[0][0].getText().equals(middle) && board[2][2].getText().equals(middle) && !board[1][1].getText().equals(""))
{
return new EndState(board[1][1].getText(), "DD");
}
if (board[0][2].getText().equals(middle) && board[2][0].getText().equals(middle) && !board[1][1].getText().equals(""))
{
return new EndState(board[1][1].getText(), "DU");
}
// Next Turn Possible ?
for (int i = 0; i < board.length; i++)
{
for (int j = 0; j < board[i].length; j++)
{
if (board[i][j].getText().equals(""))
{
return null;
}
}
}
return new EndState("T");
}
}