0
|
1 /*
|
|
2 * Ristipolku
|
|
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
|
|
4 *
|
2
|
5 * Ohjelmointiprojekti Java-kurssille .
|
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 {
|
1
|
55 board[x][y].paint(g, sx + (x * scale), sy + (y * scale), scale - scale/10);
|
2
|
56 }
|
|
57 }
|
1
|
58
|
2
|
59 private boolean isEmpty(int x, int y)
|
|
60 {
|
|
61 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null);
|
|
62 }
|
1
|
63
|
2
|
64 private Piece getPiece(int x, int y)
|
|
65 {
|
|
66 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize)
|
|
67 return board[x][y];
|
|
68 else
|
|
69 return null;
|
|
70 }
|
1
|
71
|
2
|
72 public Point resolvePath(int x, int y, int inpoint)
|
|
73 {
|
|
74 }
|
|
75
|
|
76 public void pieceRotate(boolean dir)
|
|
77 {
|
|
78 current.rotate(dir);
|
|
79 }
|
1
|
80
|
2
|
81 public void pieceFinishTurn()
|
|
82 {
|
|
83 if (current != null)
|
|
84 current.setType(PieceType.NORMAL);
|
1
|
85
|
2
|
86 if (isEmpty(cx, cy))
|
|
87 {
|
|
88 current = new Piece(PieceType.ACTIVE);
|
|
89 lauta.setPiece(cx, cy, current);
|
|
90 }
|
1
|
91 }
|
|
92 }
|
|
93
|
|
94
|
|
95 public class gfxtest extends JApplet
|
|
96 implements KeyListener
|
|
97 {
|
2
|
98 GameBoard lauta;
|
1
|
99
|
2
|
100 public void init()
|
|
101 {
|
|
102 getContentPane().setBackground(Color.white);
|
|
103 lauta = new GameBoard();
|
|
104 addKeyListener(this);
|
|
105 }
|
0
|
106
|
2
|
107 public void paint(Graphics g)
|
|
108 {
|
|
109 Graphics2D g2 = (Graphics2D) g;
|
0
|
110 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
111 RenderingHints.VALUE_ANTIALIAS_ON);
|
2
|
112 lauta.paint(g2, 15, 15, 60);
|
0
|
113 }
|
|
114
|
|
115
|
2
|
116 public void keyPressed(KeyEvent e)
|
|
117 {
|
|
118 }
|
1
|
119
|
2
|
120 public void keyReleased(KeyEvent e)
|
|
121 {
|
|
122 }
|
1
|
123
|
2
|
124 public void keyTyped(KeyEvent e)
|
|
125 {
|
|
126 switch (event.getKeyCode())
|
|
127 {
|
|
128 case VK_LEFT:
|
|
129 case VK_UP:
|
|
130 lauta.pieceRotate(false);
|
|
131 break;
|
0
|
132
|
2
|
133 case VK_RIGHT:
|
|
134 case VK_DOWN:
|
|
135 lauta.pieceRotate(true);
|
|
136 break;
|
|
137
|
|
138 case VK_ENTER:
|
|
139 lauta.pieceFinishTurn();
|
|
140 break;
|
|
141 }
|
|
142 }
|
0
|
143 }
|