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
|
2
|
18
|
1
|
19 class PathInfo
|
|
20 {
|
2
|
21 public int in, inX, inY, out, outX, outY;
|
1
|
22
|
2
|
23 public PathInfo(int in, int inX, int inY, int out, int outX, int outY)
|
|
24 {
|
|
25 this.in = in;
|
|
26 this.inX = inX;
|
|
27 this.inY = inY;
|
|
28
|
|
29 this.out = out;
|
|
30 this.outX = outX;
|
|
31 this.outY = outY;
|
|
32 }
|
1
|
33 }
|
0
|
34
|
|
35
|
1
|
36 class GameBoard
|
0
|
37 {
|
2
|
38 public static final int boardSize = 9;
|
|
39 public static final int boardMiddle = 4;
|
|
40 Piece[][] board;
|
|
41 Piece current;
|
7
|
42
|
|
43 int moveX, moveY, movePoint;
|
|
44
|
2
|
45 public GameBoard()
|
|
46 {
|
|
47 board = new Piece[boardSize][boardSize];
|
|
48
|
|
49 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
|
|
50
|
|
51 moveX = boardMiddle;
|
|
52 moveY = boardMiddle - 1;
|
7
|
53 movePoint = 0;
|
2
|
54 }
|
0
|
55
|
7
|
56 public void paint(Graphics2D g, int sx, int sy, float scale)
|
2
|
57 {
|
|
58 for (int y = 0; y < boardSize; y++)
|
|
59 for (int x = 0; x < boardSize; x++)
|
|
60 if (board[x][y] != null)
|
|
61 {
|
6
|
62 AffineTransform save = g.getTransform();
|
|
63
|
4
|
64 board[x][y].paint(g,
|
|
65 sx + (x * scale),
|
|
66 sy + (y * scale),
|
|
67 scale - scale / 10);
|
6
|
68
|
|
69 g.setTransform(save);
|
2
|
70 }
|
|
71 }
|
1
|
72
|
2
|
73 private boolean isEmpty(int x, int y)
|
|
74 {
|
|
75 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null);
|
|
76 }
|
1
|
77
|
2
|
78 private Piece getPiece(int x, int y)
|
|
79 {
|
|
80 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize)
|
|
81 return board[x][y];
|
|
82 else
|
|
83 return null;
|
|
84 }
|
7
|
85
|
2
|
86 public void pieceRotate(boolean dir)
|
|
87 {
|
|
88 current.rotate(dir);
|
|
89 }
|
1
|
90
|
7
|
91 public PathInfo resolvePath(int startX, int startY, int startPoint)
|
|
92 {
|
|
93 int x = startX, y = startY;
|
|
94 int point;
|
|
95
|
|
96 Piece curr = getPiece(startX, startY);
|
|
97 if (curr == null)
|
|
98 return null;
|
|
99
|
|
100 while (curr != null)
|
|
101 {
|
|
102
|
|
103 }
|
|
104
|
|
105 return new PathInfo(startPoint, startX, startY, point, x, y);
|
|
106 }
|
|
107
|
2
|
108 public void pieceFinishTurn()
|
|
109 {
|
|
110 if (current != null)
|
7
|
111 {
|
6
|
112 current.setType(PieceType.LOCKED);
|
7
|
113 PathInfo i = resolvePath(moveX, moveY, movePoint);
|
|
114
|
|
115 if (i != null)
|
|
116 {
|
|
117
|
|
118 }
|
|
119 }
|
|
120
|
|
121 if (isEmpty(moveX, moveY))
|
2
|
122 {
|
|
123 current = new Piece(PieceType.ACTIVE);
|
7
|
124 lauta.setPiece(moveX, moveY, current);
|
2
|
125 }
|
1
|
126 }
|
|
127 }
|
|
128
|
|
129
|
4
|
130 public class Ristipolku extends JApplet
|
|
131 implements KeyListener
|
1
|
132 {
|
4
|
133 GameBoard lauta = null;
|
|
134 BufferedImage lautaBG = null;
|
1
|
135
|
2
|
136 public void init()
|
|
137 {
|
|
138 getContentPane().setBackground(Color.white);
|
4
|
139
|
|
140 try
|
|
141 {
|
7
|
142 lautaBG = ImageIO.read(new File("background.jpg"));
|
4
|
143 }
|
7
|
144
|
4
|
145 catch (IOException e)
|
|
146 {
|
|
147 JOptionPane.showMessageDialog(null,
|
|
148 "Could not load background image.",
|
|
149 "Initialization error",
|
|
150 JOptionPane.ERROR_MESSAGE);
|
|
151 }
|
|
152
|
2
|
153 lauta = new GameBoard();
|
|
154 addKeyListener(this);
|
|
155 }
|
0
|
156
|
2
|
157 public void paint(Graphics g)
|
|
158 {
|
|
159 Graphics2D g2 = (Graphics2D) g;
|
4
|
160
|
0
|
161 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
162 RenderingHints.VALUE_ANTIALIAS_ON);
|
4
|
163
|
7
|
164 lauta.paint(g2, 100, 150, 60);
|
0
|
165 }
|
|
166
|
2
|
167 public void keyPressed(KeyEvent e)
|
|
168 {
|
|
169 }
|
1
|
170
|
2
|
171 public void keyReleased(KeyEvent e)
|
|
172 {
|
|
173 }
|
1
|
174
|
2
|
175 public void keyTyped(KeyEvent e)
|
|
176 {
|
7
|
177 switch (e.getKeyCode())
|
2
|
178 {
|
7
|
179 case KeyEvent.VK_LEFT:
|
|
180 case KeyEvent.VK_UP:
|
2
|
181 lauta.pieceRotate(false);
|
|
182 break;
|
0
|
183
|
7
|
184 case KeyEvent.VK_RIGHT:
|
|
185 case KeyEvent.VK_DOWN:
|
2
|
186 lauta.pieceRotate(true);
|
|
187 break;
|
|
188
|
7
|
189 case KeyEvent.VK_ENTER:
|
2
|
190 lauta.pieceFinishTurn();
|
|
191 break;
|
|
192 }
|
|
193 }
|
0
|
194 }
|