-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
164 lines (145 loc) · 5.85 KB
/
Game.java
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.company;
import java.awt.*;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.*;
public class Game {
private static final int ROWS = 3;
private static final int COLS = 3;
private JFrame frame;
private JPanel panel;
private Container content;
private ArrayList<JButton> buttons;
private Hashtable<Integer, String> selectedButtons = new Hashtable<>();
private int numberPressed;
private boolean isX, isO;
public Game() {
makeFrame();
isX = false;
isO = false;
}
private void makeFrame() {
frame = new JFrame();
content = frame.getContentPane();
panel = new JPanel();
panel.setLayout(new GridLayout(ROWS, COLS));
makeButton();
content.add(panel);
frame.setSize(new Dimension(500, 500));
frame.setVisible(true);
}
private void makeButton() {
buttons = new ArrayList<>();
for (int x = 0; x < ROWS; x++)
for (int y = 0; y < COLS; y++) {
JButton button = new JButton("");
button.addActionListener(e -> {
play(button);
});
buttons.add(button);
panel.add(button);
}
}
private void play(JButton button) {
if (numberPressed != 1) {
JButton a = new JButton("X");
a.setFont(new Font("Calibri", Font.PLAIN, 100));
replace(button, a);
buttons.remove(button);
numberPressed++;
} else {
JButton b = new JButton("O");
b.setFont(new Font("Calibri", Font.PLAIN, 100));
replace(button, b);
buttons.remove(button);
numberPressed = 0;
}
if (!win() && buttons.isEmpty()) {
Object[] options = {"Restart", "Quit"};
int option = JOptionPane.showOptionDialog(frame, "NOBODY WINS!\nClick Restart to continue", "Sad", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
if (option == JOptionPane.YES_OPTION) {
makeFrame();
numberPressed = 0;
} else {
System.exit(0);
}
}
if(win() && isX) {
JOptionPane.showMessageDialog(frame, "Player 1, you win!", "Congratulations", JOptionPane.INFORMATION_MESSAGE);
}
if(win() && isO)
{
JOptionPane.showMessageDialog(frame, "Player 2, you win!", "Congratulations", JOptionPane.INFORMATION_MESSAGE);
}
frame.repaint();
frame.revalidate();}
private void replace(JButton a, JButton c) {
int index = panel.getComponentZOrder(a);
panel.remove(a);
panel.add(c, index);
selectedButtons.put(index, c.getText());
}
private boolean win() {
//check horizontal
if (selectedButtons.containsKey(0) && selectedButtons.containsKey(1) && selectedButtons.containsKey(2)) {
if (selectedButtons.get(0).equals(selectedButtons.get(1)) && selectedButtons.get(1).equals(selectedButtons.get(2))) {
choosePlayer(selectedButtons.get(0));
return true;} }
if (selectedButtons.containsKey(3) && selectedButtons.containsKey(4) && selectedButtons.containsKey(5)) {
if (selectedButtons.get(3).equals(selectedButtons.get(4)) && selectedButtons.get(4).equals(selectedButtons.get(5))) {
choosePlayer(selectedButtons.get(3));
return true;
}
}
if (selectedButtons.containsKey(6) && selectedButtons.containsKey(7) && selectedButtons.containsKey(8)) {
if (selectedButtons.get(6).equals(selectedButtons.get(7)) && selectedButtons.get(7).equals(selectedButtons.get(8))) {
choosePlayer(selectedButtons.get(6));
return true;
}
}
//check vertical
if (selectedButtons.containsKey(0) && selectedButtons.containsKey(3) && selectedButtons.containsKey(6)) {
if (selectedButtons.get(0).equals(selectedButtons.get(3)) && selectedButtons.get(3).equals(selectedButtons.get(6))) {
choosePlayer(selectedButtons.get(0));
return true;
}
}
if (selectedButtons.containsKey(1) && selectedButtons.containsKey(4) && selectedButtons.containsKey(7)) {
if (selectedButtons.get(1).equals(selectedButtons.get(4)) && selectedButtons.get(4).equals(selectedButtons.get(7))) {
choosePlayer(selectedButtons.get(1));
return true;
}
}
if (selectedButtons.containsKey(2) && selectedButtons.containsKey(5) && selectedButtons.containsKey(8)) {
if (selectedButtons.get(2).equals(selectedButtons.get(5)) && selectedButtons.get(5).equals(selectedButtons.get(8))) {
choosePlayer(selectedButtons.get(2));
return true;
}
}
//check diagonal
if (selectedButtons.containsKey(0) && selectedButtons.containsKey(4) && selectedButtons.containsKey(8)) {
if (selectedButtons.get(0).equals(selectedButtons.get(4)) && selectedButtons.get(4).equals(selectedButtons.get(8))) {
choosePlayer(selectedButtons.get(0));
return true;
}
}
if (selectedButtons.containsKey(2) && selectedButtons.containsKey(4) && selectedButtons.containsKey(6)) {
if (selectedButtons.get(2).equals(selectedButtons.get(4)) && selectedButtons.get(4).equals(selectedButtons.get(6))) {
choosePlayer(selectedButtons.get(2));
return true;
}
}
return false;
}
private void choosePlayer(String a )
{
if(a.equals("X"))
{
isX = true;
}
else
{
isO = true;
}
}
}