-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnight.java
158 lines (145 loc) · 3.56 KB
/
Knight.java
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
/**
A class that represents the chess piece Knight.
@author Dyland Xue
@version 2011-05-17
*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class Knight implements Piece
{
private int color;
private String name;
private ImageIcon ico;
private ChessBoard myBoard;
/**
default constructor
@param c color of this piece
*/
public Knight(int c)
{
color = c;
if(color == ChessBoard.WHITE)
{
name = "N";
ico = new ImageIcon(Toolkit.getDefaultToolkit().getImage("white_knight.png"));
}
else if(color == ChessBoard.BLACK)
{
name = "n";
ico = new ImageIcon(Toolkit.getDefaultToolkit().getImage("black_knight.png"));
}
}
/**
constructor with reference to a chessboard
@param c color of this piece
@param cb reference to a chessboard
*/
public Knight(int c, ChessBoard cb)
{
this(c);
myBoard = cb;
}
/**
checks if the move from c1 to c2 is possible for this piece
@param c1 starting coord
@param c2 ending coord
@return true if possible, otherwise false
*/
public boolean soFarSoGood(Coord c1, Coord c2)
{
int ri = c1.getRow();
int ci = c1.getCol();
int rt = c2.getRow();
int ct = c2.getCol();
if(myBoard.get(c2) != null && myBoard.get(c2).getColor() == color)
{
return false;
}
else
{
return
((rt == ri + 2 || rt == ri - 2) && (ct == ci + 1 || ct == ci - 1))
||
((rt == ri + 1 || rt == ri - 1) && (ct == ci + 2 || ct == ci - 2));
//rt == ri +- 2 and ct == ci +- 1
//or
//rt == ri +- 1 and ct == ci +-2
}
}
/**
checks if the move from c1 to c2 satisfies chess rules or not
@param c1 starting coord
@param c2 ending coord
@return true if move is valid, otherwise false
*/
public boolean moveIsValid(Coord c1, Coord c2)
{
if(!soFarSoGood(c1, c2))
{
return false;
}
else
{
Piece temp = myBoard.get(c2);
myBoard.set(c2, this);
myBoard.set(c1, null);
myBoard.next();
if(myBoard.getJudge().isCheck())
{
myBoard.set(c2, temp);
myBoard.set(c1, this);
myBoard.next();
myBoard.getJudge().check = false;
return false;
}
else
{
myBoard.set(c2, temp);
myBoard.set(c1, this);
myBoard.next();
return true;
}
}
}
/**
sets a chessboard reference
@param cb the chessboard this piece is in
*/
public void setBoard(ChessBoard cb)
{
myBoard = cb;
}
/**
gets the color of this piece, either white or black
@return myColor
*/
public int getColor()
{
return color;
}
/**
gets the initial of this piece, used in notation
@return name
*/
public String getInitial()
{
return "N";
}
/**
gets the icon of this piece, used to represent itself in the GUI
@return ico
*/
public ImageIcon getIcon()
{
return ico;
}
/**
gets the value of this piece
@return VALUE_N
*/
public double getVal()
{
return XMQ.VALUE_N;
}
}