-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGomoku.java
More file actions
143 lines (124 loc) · 4.03 KB
/
Copy pathGomoku.java
File metadata and controls
143 lines (124 loc) · 4.03 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
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
/**
* <code>Gomoku</code> - a simple GUI for playing basic Gomoku. See
* http://en.wikipedia.org/wiki/Gomoku
*
* Usage: java Gomoku [<grid size>]
* @author Todd W. Neller
* @version 1.0
*/
public class Gomoku
{
/**
* <code>main</code> - Create the JFrame and start a new embedded
* Gomoku game. The grid size is 19 by default, but may be
* supplied in the first command line argument.
*
* @param args a <code>String[]</code> value - command line
* arguments
*/
public static void main(String[] args) {
int size = 19;
if (args.length > 0)
size = Integer.parseInt(args[0]);
JFrame frame = new JFrame();
final int FRAME_WIDTH = 600;
final int FRAME_HEIGHT = 650;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Gomoku");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GomokuPanel panel = new GomokuPanel(size);
frame.add(panel);
frame.setVisible(true);
}
}
class GomokuPanel extends JPanel
{
private final int MARGIN = 5;
private final double PIECE_FRAC = 0.9;
private int size = 19;
private GomokuState state;
public GomokuPanel()
{
this(19);
}
public GomokuPanel(int size)
{
super();
this.size = size;
state = new GomokuState(size);
addMouseListener(new GomokuListener());
}
class GomokuListener extends MouseAdapter
{
public void mouseReleased(MouseEvent e)
{
double panelWidth = getWidth();
double panelHeight = getHeight();
double boardWidth = Math.min(panelWidth, panelHeight) - 2 * MARGIN;
double squareWidth = boardWidth / size;
double pieceDiameter = PIECE_FRAC * squareWidth;
double xLeft = (panelWidth - boardWidth) / 2 + MARGIN;
double yTop = (panelHeight - boardWidth) / 2 + MARGIN;
int col = (int) Math.round((e.getX() - xLeft) / squareWidth - 0.5);
int row = (int) Math.round((e.getY() - yTop) / squareWidth - 0.5);
if (row >= 0 && row < size && col >= 0 && col < size
&& state.getPiece(row, col) == GomokuState.NONE
&& state.getWinner() == GomokuState.NONE) {
state.playPiece(row, col);
repaint();
int winner = state.getWinner();
if (winner != GomokuState.NONE)
JOptionPane.showMessageDialog(null,
(winner == GomokuState.BLACK) ? "Black wins!"
: "White wins!");
}
}
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double panelWidth = getWidth();
double panelHeight = getHeight();
g2.setColor(new Color(0.925f, 0.670f, 0.34f)); // light wood
g2.fill(new Rectangle2D.Double(0, 0, panelWidth, panelHeight));
double boardWidth = Math.min(panelWidth, panelHeight) - 2 * MARGIN;
double squareWidth = boardWidth / size;
double gridWidth = (size - 1) * squareWidth;
double pieceDiameter = PIECE_FRAC * squareWidth;
boardWidth -= pieceDiameter;
double xLeft = (panelWidth - boardWidth) / 2 + MARGIN;
double yTop = (panelHeight - boardWidth) / 2 + MARGIN;
g2.setColor(Color.BLACK);
for (int i = 0; i < size; i++) {
double offset = i * squareWidth;
g2.draw(new Line2D.Double(xLeft, yTop + offset,
xLeft + gridWidth, yTop + offset));
g2.draw(new Line2D.Double(xLeft + offset, yTop,
xLeft + offset, yTop + gridWidth));
}
for (int row = 0; row < size; row++)
for (int col = 0; col < size; col++) {
int piece = state.getPiece(row, col);
if (piece != GomokuState.NONE) {
Color c = (piece == GomokuState.BLACK) ? Color.BLACK : Color.WHITE;
g2.setColor(c);
double xCenter = xLeft + col * squareWidth;
double yCenter = yTop + row * squareWidth;
Ellipse2D.Double circle
= new Ellipse2D.Double(xCenter - pieceDiameter / 2,
yCenter - pieceDiameter / 2,
pieceDiameter,
pieceDiameter);
g2.fill(circle);
g2.setColor(Color.black);
g2.draw(circle);
}
}
}
}