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