-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.java
162 lines (142 loc) · 4.37 KB
/
TicTacToe.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
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class TicTacToe implements ActionListener{
JFrame frame = new JFrame();
JPanel title = new JPanel();
JPanel ButtonPanel = new JPanel();
JLabel textfield = new JLabel();
JButton[] buttons = new JButton[9];
Random computer = new Random();
boolean player;
TicTacToe(){
textfield.setBackground(new Color(224,238,238));
textfield.setForeground(new Color(139,71,93));
textfield.setFont(new Font("Serif",Font.BOLD,60));
textfield.setHorizontalAlignment(JLabel.CENTER);
textfield.setOpaque(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.getContentPane().setBackground(new Color(50,50,50));
frame.setLayout(new BorderLayout());
frame.setVisible(true);
title.setBounds(0,0,800,100);
title.setLayout(new BorderLayout());
ButtonPanel.setBounds(100,100,300,150);
ButtonPanel.setLayout(new GridLayout(3,3));
ButtonPanel.setBackground(new Color(224,238,238));
for(int x=0;x<9;x++) {
buttons[x] = new JButton();
ButtonPanel.add(buttons[x]);
buttons[x].setFont(new Font("Serif",Font.BOLD,90));
buttons[x].setFocusable(false);
buttons[x].addActionListener(this);
}
frame.setBounds(450,130,600,600);
frame.add(ButtonPanel, BorderLayout.CENTER);
frame.add(title,BorderLayout.NORTH);
title.add(textfield);
turn();
}
public void turn() {
if(computer.nextInt(2)==0) {
player=true;
textfield.setText("X turn!");
}
else {
player=false;
textfield.setText("O turn!");
}
}
@Override
public void actionPerformed(ActionEvent e) {
for(int x=0;x<9;x++) {
if(e.getSource()==buttons[x]) {
if(player) {
if(buttons[x].getText()=="") {
buttons[x].setForeground(new Color(205,38,38));
buttons[x].setText("X");
player=false;
textfield.setText("O turn!");
winner_control();
}
}
else {
if(buttons[x].getText()=="") {
buttons[x].setForeground(new Color(58,95,205));
buttons[x].setText("O");
player=true;
textfield.setText("X turn!");
winner_control();
}
}
}
}
}
public void winner_control() {
// Player X win conditions;
if((buttons[0].getText()=="X")&&(buttons[1].getText()=="X")&&(buttons[2].getText()=="X")) {
xWins(0,1,2);
}
if((buttons[3].getText()=="X")&&(buttons[4].getText()=="X")&&(buttons[5].getText()=="X")) {
xWins(3,4,5);
}
if((buttons[6].getText()=="X")&&(buttons[7].getText()=="X")&&(buttons[8].getText()=="X")) {
xWins(6,7,8);
}
if((buttons[0].getText()=="X")&&(buttons[3].getText()=="X")&&(buttons[6].getText()=="X")) {
xWins(0,3,6);
}
if((buttons[1].getText()=="X")&&(buttons[4].getText()=="X")&&(buttons[7].getText()=="X")) {
xWins(1,4,7);
}
if((buttons[2].getText()=="X")&&(buttons[5].getText()=="X")&&(buttons[8].getText()=="X")) {
xWins(2,5,8);
}
if((buttons[0].getText()=="X")&&(buttons[4].getText()=="X")&&(buttons[8].getText()=="X")) {
xWins(0,4,8);
}
if((buttons[2].getText()=="X")&&(buttons[4].getText()=="X")&&(buttons[6].getText()=="X")) {
xWins(2,4,6);
}
// Player O win conditions;
if((buttons[0].getText()=="O")&&(buttons[1].getText()=="O")&&(buttons[2].getText()=="O")) {
oWins(0,1,2);
}
if((buttons[3].getText()=="O")&&(buttons[4].getText()=="O")&&(buttons[5].getText()=="O")) {
oWins(3,4,5);
}
if((buttons[6].getText()=="O")&&(buttons[7].getText()=="O")&&(buttons[8].getText()=="O")) {
oWins(6,7,8);
}
if((buttons[0].getText()=="O")&&(buttons[3].getText()=="O")&&(buttons[6].getText()=="O")) {
oWins(0,3,6);
}
if((buttons[1].getText()=="O")&&(buttons[4].getText()=="O")&&(buttons[7].getText()=="O")) {
oWins(1,4,7);
}
if((buttons[2].getText()=="O")&&(buttons[5].getText()=="O")&&(buttons[8].getText()=="O")) {
oWins(2,5,8);
}
if((buttons[0].getText()=="O")&&(buttons[4].getText()=="O")&&(buttons[8].getText()=="O")) {
oWins(0,4,8);
}
if((buttons[2].getText()=="O")&&(buttons[4].getText()=="O")&&(buttons[6].getText()=="O")) {
oWins(2,4,6);
}
}
//Winner conditions;
public void xWins(int a, int b, int c) {
for(int x=0;x<9;x++) {
buttons[x].setEnabled(false);
}
textfield.setText("Winner X");
}
public void oWins(int a, int b, int c) {
for(int x=0;x<9;x++) {
buttons[x].setEnabled(false);
}
textfield.setText("Winner O");
}
}