-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimGame.java~
238 lines (177 loc) · 6.96 KB
/
SimGame.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* File: SimGame.java
* Purpose: This is a version of the Sim Pencil Game (see Wiki for details);
* it draws the game on an 800x800 window and allows the user to
* Date: 4/21/14
* Class: CS 112
*
* Author: Wayne Snyder
*/
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class SimGame{
private static final int numCircles = 6;
private static Graph G = new Graph(numCircles);
private static JFrame frame;
private static Color bg;
private static JCanvas canvas;
private static int centerX;
private static int centerY;
private static int radius;
private static int diameter;
private static int[] circlesX; // location of vertices in camvas
private static int[] circlesY;
private static int[] xl; // location of vertex labels
private static int[] yl;
private static int fontHeight = 14;
private static int fontWidth = 10;
public static void initDisplay() {
frame=new JFrame("Sim Game");
bg = new Color(255,255,220);
canvas=new JCanvas();
frame.setSize(600,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(bg);
frame.add(canvas);
frame.setVisible(true);
centerX = 300;
centerY = 300;
radius = 180;
diameter = 20;
circlesX = new int[numCircles];
circlesY = new int[numCircles];
xl = new int[numCircles]; // location of vertex labels
yl = new int[numCircles];
for(int i = 0; i < numCircles; ++i) {
circlesX[i] = (int)(centerX - (diameter/2) + radius*Math.cos(i*2*3.1416/numCircles));
circlesY[i] = (int)(centerY - (diameter/2) + radius*Math.sin(-i*2*3.1416/numCircles));
xl[i] = (int)(centerX - (diameter/2.0) - (fontWidth/2) + (radius+30)*Math.cos(i*2*3.1416/numCircles));
yl[i] = (int)(centerY - (diameter/2.0) + (fontHeight/2) + (radius+30)*Math.sin(-i*2*3.1416/numCircles));
}
canvas.setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
canvas.setStroke(new BasicStroke(2));
}
// update the display of the graph with whatever move has been made
// edges with weight -1 (machine) will be drawn as blue, weight 1 (human)
// as red
public static void drawDisplay() {
canvas.startBuffer();
canvas.clear();
canvas.setPaint(Color.BLACK);
for(int i = 0; i < numCircles; ++i) {
canvas.drawOval(circlesX[i]- (diameter/2) ,circlesY[i]- (diameter/2) ,diameter, diameter);
canvas.drawString(i + "", xl[i], yl[i]);
}
for(int r = 0; r < numCircles; ++r) {
for(int c = 0; c < r; ++c) {
int e = G.getEdge(r,c);
if(e != 0) {
if( e == 1 )
canvas.setPaint(Color.RED);
else
canvas.setPaint(Color.BLUE);
canvas.drawLine(circlesX[r],circlesY[r],
circlesX[c],circlesY[c] );
}
}
}
canvas.endBuffer();
}
// since users will not hit the mark precisely, find the closest
// vertex to the point where they clicked or released the mouse
private static int closestVertex(int x, int y) {
double smallestDistance = 10000000.0;
int closestVertex = -1;
for(int i = 0; i < numCircles; ++i) {
double distance =
Math.sqrt((circlesX[i]-x)*(circlesX[i]-x)
+(circlesY[i]-y)*(circlesY[i]-y));
if(distance < smallestDistance) {
smallestDistance = distance;
closestVertex = i;
}
}
return closestVertex;
}
private static boolean completeGraph(Graph G) {
for(int r = 0; r < numCircles; ++r) {
for(int c = 0; c < r; ++c) {
if(!G.isEdge(r,c))
return false;
}
}
return true;
}
public static void main(String args[]){
initDisplay();
drawDisplay();
canvas.setPaint(Color.BLACK);
canvas.drawString("You go first: drag your mouse between two vertices to move.", 100, 550);
// comment-in the second line and comment-out the first to use your Player class
RandomPlayer P = new RandomPlayer();
// Player P = new Player();
JEventQueue events=new JEventQueue();
events.listenTo(canvas,"canvas");
int x0 = 0;
int y0 = 0;
int round = 0;
while(!completeGraph(G)) {
EventObject event=events.waitEvent();
if(events.isMouseEvent(event)){
int x=events.getMouseX(event);
int y=events.getMouseY(event);
if(events.isMousePressed(event)){
x0=x;
y0=y;
}
if(events.isMouseReleased(event)) {
// draw a red edge between vertices closest to
// begin and end of mouse drag
int source = closestVertex(x0,y0);
int target = closestVertex(x,y);
if(source != target && !G.isEdge(source,target)) {
G.addEdge(source, target,1);
}
else if(source == target) {
canvas.setPaint(Color.BLACK);
canvas.drawString("You may not draw an edge from a vertex to itself... try again!", 100, 530);
continue;
}
else {
canvas.setPaint(Color.BLACK);
canvas.drawString("You may not redraw a blue edge... try again!", 100, 550);
continue;
}
drawDisplay();
if(G.isCycleOfLength(3,1)) {
canvas.setPaint(Color.BLACK);
canvas.drawString("Blue Player Wins!", 100, 550);
break;
}
canvas.sleep(1000);
// get move (edge) from player and make a move as blue
Move m = P.chooseMove(G);
System.out.println(m);
G.addEdge(m.source, m.target, -1);
drawDisplay();
if(G.isCycleOfLength(3,-1)) {
canvas.setPaint(Color.BLACK);
canvas.drawString("Red Player Wins!", 100, 550);
break;
}
}
}
}
}
}
class Move {
int source;
int target;
public Move(int s, int t) {
source = s; target = t;
}
public String toString() {
return "(" + source + "," + target + ")";
}
}