1092 java概論

題目說明

Write a java program to develop the Tic-Tac-Toe game using AWT button class.
The program should have the ability to decide the winner and the loser.

參考解法

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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class hw1 extends Frame implements ActionListener{
static hw1 frm = new hw1();
static Dialog dlg = new Dialog(frm);
static JButton table[] = new JButton[9];
static int temp[] = new int[9];
static int turn = 0;
static int count = 0;

static public int check(){
int o = 0;
int x = 0;

if (temp[0] == 1 && temp[1] == 1 && temp[2] == 1)//o
return 1;
else if (temp[3] == 1 && temp[4] == 1 && temp[5] == 1)
return 1;
else if (temp[6] == 1 && temp[7] == 1 && temp[8] == 1)
return 1;
else if (temp[0] == 1 && temp[3] == 1 && temp[6] == 1)
return 1;
else if (temp[1] == 1 && temp[4] == 1 && temp[7] == 1)
return 1;
else if (temp[2] == 1 && temp[5] == 1 && temp[8] == 1)
return 1;

else if (temp[0] == 2 && temp[1] == 2 && temp[2] == 2)//x
return 2;
else if (temp[3] == 2 && temp[4] == 2 && temp[5] == 2)
return 2;
else if (temp[6] == 2 && temp[7] == 2 && temp[8] == 2)
return 2;
else if (temp[0] == 2 && temp[3] == 2 && temp[6] == 2)
return 2;
else if (temp[1] == 2 && temp[4] == 2 && temp[7] == 2)
return 2;
else if (temp[2] == 2 && temp[5] == 2 && temp[8] == 2)
return 2;

else if (temp[0] == 1 && temp[4] == 1 && temp[8] == 1)//ox
return 1;
else if (temp[0] == 2 && temp[4] == 2 && temp[8] == 2)
return 2;
else if (temp[2] == 1 && temp[4] == 1 && temp[6] == 1)
return 1;
else if (temp[2] == 2 && temp[4] == 2 && temp[6] == 2)
return 2;

return 0;
}

public void actionPerformed(ActionEvent e){
System.out.println("ON");
JDialog.setDefaultLookAndFeelDecorated(true);
JButton btn = (JButton) e.getSource();
count++;

for (int i = 0; i < 9; i++)
{
if (btn == table[i])
{
if (turn == 0 && temp[i] == 0)
{
temp[i] = 1;
table[i].setText("O");
if (check() == 1)
JOptionPane.showMessageDialog(frm, "O is win! ");
turn = 1;
}
else if (turn == 1 && temp[i] == 0)
{
temp[i] = 2;
table[i].setText("X");
if (check() == 2)
JOptionPane.showMessageDialog(frm, "X is win! ");
turn = 0;
}
else
{
JOptionPane.showMessageDialog(frm, "It has been chosen.");
}
}
}

if (count == 9)
JOptionPane.showMessageDialog(frm, "It's a tie. Game over.");

}

public static void main(String args[]) {
for (int i = 0; i < 9; i++)
{
temp[i] = 0;
}

frm.setSize(605, 625);
frm.setLayout(null);

for (int i = 0; i < 9; i++)
{
table[i] = new JButton();
}

int x = 0, y = 20;
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 3; i++)
{
table[j * 3 + i].setBounds(x, y, 200, 200);
table[j * 3 + i].addActionListener(frm);
x += 200;
}
x = 0;
y += 200;
}

for (int i = 0; i < 9; i++)
{
frm.add(table[i]);
}

frm.setTitle("TIC-TAC-TOE");
frm.setVisible(true);

frm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
};
}