Repository navigation
Expand file tree
/
Copy pathGame.java
More file actions
177 lines (142 loc) · 4.42 KB
/
Copy pathGame.java
File metadata and controls
177 lines (142 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import java.awt.Graphics;//
import java.awt.Color;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.border.MatteBorder;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.CompoundBorder;
public class Game extends JFrame implements MouseListener{
private JPanel contentPane;
private JLabel DebugText;
private JLabel MilesEdgeworth;
private JTextPane textPane;
private String debugMessage;
private Chess[][] chessBoard;
//my code
private int state;
private int nowcamp;
private int stateX, stateY;
private int lastStep;/////
//my code
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game frame = new Game();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Game() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 10, 875, 700);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.addMouseListener(this);
setContentPane(contentPane);
contentPane.setLayout(null);
DebugText = new JLabel("Debug Test :");
DebugText.setBounds(5, 642, 824, 15);
DebugText.setVerticalAlignment(SwingConstants.BOTTOM);
contentPane.add(DebugText);
JLabel chessBoardpic = new JLabel("");
chessBoardpic.setBounds(5, 5, 605, 637);
chessBoardpic.setIcon(new ImageIcon("ChessBoard.png"));
contentPane.add(chessBoardpic);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 255, 0)));
panel.setBounds(620, 21, 229, 360);
contentPane.add(panel);
panel.setLayout(null);
MilesEdgeworth = new JLabel("");
MilesEdgeworth.setBounds(10, 10, 209, 245);
panel.add(MilesEdgeworth);
textPane = new JTextPane();
textPane.setForeground(new Color(0, 128, 0));
textPane.setBackground(Color.DARK_GRAY);
textPane.setBounds(10, 265, 209, 85);
panel.add(textPane);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(620, 391, 229, 230);
contentPane.add(panel_1);
//my code ><
chessBoard = new Chess[8][8];
// chess[][] = new King("wKing",,,0);
state = 0;//狀態初始化
nowcamp = 0;//陣營初始化
stateX = -1;
stateY = -1;
//my code ><
}
private Point determineGrid(int x,int y){
int chessBoardX=24,chessBoardY=41;
int gridWidth=70,gridHeight=70;
int gx = (x - chessBoardX)/gridWidth;
int gy = (y - chessBoardY)/gridHeight;
Point xy = new Point(gx,gy);
return xy;
}
public void mouseClicked(MouseEvent e) {
Point p = determineGrid(e.getX(),e.getY());
//my code:)
if(state == 0){
boolean[][] boardAvailable;
if(p.x <=7 && p.y <= 7 && chessBoard[p.x][p.y] != null && chessBoard[p.x][p.y].camp() == nowcamp){
boardAvailable = chessBoard[p.x][p.y].getReachableGrid(chessBoard);
for(int i = 0; i < 8; i ++){
for(int j = 0; j < 8; j ++){
if(boardAvailable[i][j] = true){
//加辨識符號到圖上
}
}
}
stateX = p.x;
stateY = p.y;
state = 1;//進入下一個狀態
}
}else if(state == 1){
if(chessBoard[stateX][stateY].isReachable(chessBoard,p.x,p.y)){
if(chessBoard[stateX][stateY].isCritical()){//吃國王
// gameover
state = 2;//結束狀態
}
chessBoard[stateX][stateY] = chessBoard[p.x][p.y];
chessBoard[p.x][p.y] = null;//
state = 0;//回到上一個狀態
}else if(stateX == p.x && stateY == p.y){
state = 0;//回到上一個狀態
}
}
//my code
//DEBUG
debugMessage = new String("Clicked. X is: " + e.getX() + " Y is: " + e.getY() + ". Grid is :[" + p.x + "," + p.y +"]");
DebugText.setText(debugMessage);
//DEBUG
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}