0
|
1 /*
|
|
2 * Ristipolku
|
|
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
|
|
4 *
|
4
|
5 * Ohjelmointiprojekti 2010-2011 Java-kurssille T740306.
|
0
|
6 */
|
|
7 import java.awt.*;
|
|
8 import java.awt.geom.*;
|
1
|
9 import java.awt.event.*;
|
7
|
10 import java.awt.image.*;
|
|
11 import java.awt.event.*;
|
|
12 import javax.swing.*;
|
6
|
13 import javax.imageio.*;
|
1
|
14 import java.util.*;
|
6
|
15 import java.io.*;
|
1
|
16 import game.*;
|
|
17
|
8
|
18 import javax.sound.sampled.*;
|
|
19
|
2
|
20
|
1
|
21 class PathInfo
|
|
22 {
|
2
|
23 public int in, inX, inY, out, outX, outY;
|
1
|
24
|
2
|
25 public PathInfo(int in, int inX, int inY, int out, int outX, int outY)
|
|
26 {
|
|
27 this.in = in;
|
|
28 this.inX = inX;
|
|
29 this.inY = inY;
|
|
30
|
|
31 this.out = out;
|
|
32 this.outX = outX;
|
|
33 this.outY = outY;
|
|
34 }
|
1
|
35 }
|
0
|
36
|
8
|
37 /*
|
|
38 class AnimatedElement
|
|
39 {
|
|
40 float x, y, stime, value;
|
|
41 Interpolate lerp;
|
|
42 boolean active;
|
|
43
|
|
44 public AnimatedElement(float x, float y, )
|
|
45 {
|
|
46 stime = 0;
|
|
47 this.x = x;
|
|
48 this.y = y;
|
|
49
|
|
50 }
|
|
51
|
|
52 public animate(float time)
|
|
53 {
|
|
54 if (!active)
|
|
55 {
|
|
56 active = true;
|
|
57 stime = time;
|
|
58 }
|
|
59
|
|
60 float t = (time - stime) / 10.0f;
|
|
61 if (t < 100)
|
|
62 value = lerp.getValue(t);
|
|
63 else
|
|
64 {
|
|
65
|
|
66 }
|
|
67 }
|
|
68
|
|
69 public paint(Graphics2D g, );
|
|
70 {
|
|
71 }
|
|
72 }
|
|
73 */
|
0
|
74
|
1
|
75 class GameBoard
|
0
|
76 {
|
2
|
77 public static final int boardSize = 9;
|
|
78 public static final int boardMiddle = 4;
|
|
79 Piece[][] board;
|
|
80 Piece current;
|
8
|
81 public boolean flagGameOver;
|
7
|
82
|
|
83 int moveX, moveY, movePoint;
|
|
84
|
2
|
85 public GameBoard()
|
|
86 {
|
|
87 board = new Piece[boardSize][boardSize];
|
|
88
|
|
89 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
|
|
90
|
|
91 moveX = boardMiddle;
|
|
92 moveY = boardMiddle - 1;
|
7
|
93 movePoint = 0;
|
8
|
94
|
|
95
|
|
96
|
|
97 flagGameOver = false;
|
2
|
98 }
|
0
|
99
|
7
|
100 public void paint(Graphics2D g, int sx, int sy, float scale)
|
2
|
101 {
|
|
102 for (int y = 0; y < boardSize; y++)
|
|
103 for (int x = 0; x < boardSize; x++)
|
|
104 if (board[x][y] != null)
|
|
105 {
|
6
|
106 AffineTransform save = g.getTransform();
|
|
107
|
4
|
108 board[x][y].paint(g,
|
|
109 sx + (x * scale),
|
|
110 sy + (y * scale),
|
|
111 scale - scale / 10);
|
6
|
112
|
|
113 g.setTransform(save);
|
2
|
114 }
|
|
115 }
|
1
|
116
|
2
|
117 private boolean isEmpty(int x, int y)
|
|
118 {
|
|
119 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null);
|
|
120 }
|
1
|
121
|
2
|
122 private Piece getPiece(int x, int y)
|
|
123 {
|
|
124 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize)
|
|
125 return board[x][y];
|
|
126 else
|
|
127 return null;
|
|
128 }
|
7
|
129
|
2
|
130 public void pieceRotate(boolean dir)
|
|
131 {
|
|
132 current.rotate(dir);
|
|
133 }
|
1
|
134
|
8
|
135 public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark)
|
7
|
136 {
|
|
137 int x = startX, y = startY;
|
8
|
138 int point = -1;
|
7
|
139
|
|
140 Piece curr = getPiece(startX, startY);
|
|
141 if (curr == null)
|
|
142 return null;
|
|
143
|
|
144 while (curr != null)
|
|
145 {
|
8
|
146 // curr.(true);
|
|
147 // elements.spawn("", );
|
7
|
148 }
|
|
149
|
|
150 return new PathInfo(startPoint, startX, startY, point, x, y);
|
|
151 }
|
|
152
|
2
|
153 public void pieceFinishTurn()
|
|
154 {
|
|
155 if (current != null)
|
7
|
156 {
|
6
|
157 current.setType(PieceType.LOCKED);
|
8
|
158 PathInfo i = resolvePath(moveX, moveY, movePoint, true);
|
7
|
159
|
|
160 if (i != null)
|
|
161 {
|
|
162 }
|
|
163 }
|
|
164
|
8
|
165 current = new Piece(PieceType.ACTIVE);
|
7
|
166 if (isEmpty(moveX, moveY))
|
2
|
167 {
|
8
|
168 board[moveX][moveY] = current;
|
|
169 }
|
|
170 else
|
|
171 {
|
|
172 PathInfo i = resolvePath(moveX, moveY, movePoint, true);
|
|
173 if (i != null)
|
|
174 board[moveX][moveY] = current;
|
|
175 else
|
|
176 flagGameOver = true;
|
2
|
177 }
|
1
|
178 }
|
|
179 }
|
|
180
|
|
181
|
8
|
182 public class Ristipolku extends JPanel
|
|
183 implements Runnable, KeyListener
|
1
|
184 {
|
8
|
185 Thread animThread;
|
|
186 boolean animEnable = false;
|
4
|
187 GameBoard lauta = null;
|
|
188 BufferedImage lautaBG = null;
|
8
|
189 float clock;
|
1
|
190
|
2
|
191 public void init()
|
|
192 {
|
8
|
193 clock = 0;
|
4
|
194
|
|
195 try
|
|
196 {
|
8
|
197 lautaBG = ImageIO.read(new File("board.png"));
|
4
|
198 }
|
7
|
199
|
4
|
200 catch (IOException e)
|
|
201 {
|
|
202 JOptionPane.showMessageDialog(null,
|
|
203 "Could not load background image.",
|
|
204 "Initialization error",
|
|
205 JOptionPane.ERROR_MESSAGE);
|
|
206 }
|
|
207
|
8
|
208 startThreads();
|
|
209
|
2
|
210 lauta = new GameBoard();
|
|
211 addKeyListener(this);
|
8
|
212
|
|
213 startThreads();
|
|
214 }
|
|
215
|
|
216 public void startThreads()
|
|
217 {
|
|
218 if (animThread == null)
|
|
219 {
|
|
220 animThread = new Thread(this);
|
|
221 animEnable = true;
|
|
222 animThread.start();
|
|
223 }
|
|
224 }
|
|
225
|
|
226 public void stopThreads()
|
|
227 {
|
|
228 if (animThread != null)
|
|
229 {
|
|
230 animThread.interrupt();
|
|
231 animEnable = false;
|
|
232 animThread = null;
|
|
233 }
|
2
|
234 }
|
0
|
235
|
2
|
236 public void paint(Graphics g)
|
|
237 {
|
|
238 Graphics2D g2 = (Graphics2D) g;
|
4
|
239
|
0
|
240 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
241 RenderingHints.VALUE_ANTIALIAS_ON);
|
4
|
242
|
7
|
243 lauta.paint(g2, 100, 150, 60);
|
0
|
244 }
|
|
245
|
2
|
246 public void keyPressed(KeyEvent e)
|
|
247 {
|
|
248 }
|
1
|
249
|
2
|
250 public void keyReleased(KeyEvent e)
|
|
251 {
|
|
252 }
|
1
|
253
|
2
|
254 public void keyTyped(KeyEvent e)
|
|
255 {
|
7
|
256 switch (e.getKeyCode())
|
2
|
257 {
|
7
|
258 case KeyEvent.VK_LEFT:
|
|
259 case KeyEvent.VK_UP:
|
2
|
260 lauta.pieceRotate(false);
|
|
261 break;
|
0
|
262
|
7
|
263 case KeyEvent.VK_RIGHT:
|
|
264 case KeyEvent.VK_DOWN:
|
2
|
265 lauta.pieceRotate(true);
|
|
266 break;
|
|
267
|
7
|
268 case KeyEvent.VK_ENTER:
|
2
|
269 lauta.pieceFinishTurn();
|
|
270 break;
|
|
271 }
|
|
272 }
|
8
|
273
|
|
274 public void run()
|
|
275 {
|
|
276 while (animEnable)
|
|
277 {
|
|
278 clock++;
|
|
279
|
|
280 Thread.sleep(100);
|
|
281 }
|
|
282 }
|
0
|
283 }
|