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