Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
chess
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
9731075
chess
Commits
4bb8935f
Commit
4bb8935f
authored
Apr 28, 2019
by
hosein
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pack completed
parent
1d44fbd4
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
184 additions
and
35 deletions
+184
-35
Game.java
src/pack/Game.java
+11
-1
Bishop.java
src/pack/Nuts/Bishop.java
+15
-1
King.java
src/pack/Nuts/King.java
+26
-2
Knight.java
src/pack/Nuts/Knight.java
+8
-0
Nut.java
src/pack/Nuts/Nut.java
+1
-0
Pawn.java
src/pack/Nuts/Pawn.java
+11
-0
Queen.java
src/pack/Nuts/Queen.java
+16
-15
Rook.java
src/pack/Nuts/Rook.java
+12
-3
Play.java
src/pack/Play.java
+9
-2
PlayGround.java
src/pack/PlayGround.java
+75
-11
No files found.
src/pack/Game.java
View file @
4bb8935f
...
...
@@ -125,11 +125,21 @@ public class Game {
i
.
nextLine
();
}
public
int
getTurn
(){
return
turn
;}
public
int
getTurn
()
{
return
turn
;
}
public
PlayGround
getPlayGround
()
{
return
playGround
;
}
public
boolean
isEnd
()
{
if
(!
playGround
.
getCheck
())
return
false
;
return
!
playGround
.
canKingScape
(
turn
);
}
}
src/pack/Nuts/Bishop.java
View file @
4bb8935f
...
...
@@ -31,6 +31,8 @@ public class Bishop extends Nut {
@Override
public
boolean
canMove
(
int
[]
destination
)
{
if
(!
isAlive
)
return
false
;
if
(
x
==
destination
[
0
]
&&
y
==
destination
[
1
])
return
false
;
if
(
x
==
destination
[
0
])
return
false
;
else
return
Math
.
abs
((
float
)
(
y
-
destination
[
1
])
/
(
float
)
(
x
-
destination
[
0
]))
==
1.0
;
}
...
...
@@ -45,10 +47,22 @@ public class Bishop extends Nut {
if
(
destination
[
1
]
<
y
)
Y
=
-
1
;
else
Y
=
1
;
for
(
int
i
=
x
+
X
,
j
=
y
+
Y
;
i
>
-
1
&&
i
!=
destination
[
0
]
&&
j
>
-
1
&&
j
!=
destination
[
1
];
i
+=
X
,
j
+=
Y
)
for
(
int
i
=
x
+
X
,
j
=
y
+
Y
;
i
>
-
1
&&
i
<
8
&&
i
!=
destination
[
0
]
&&
j
>
-
1
&&
j
<
8
&&
j
!=
destination
[
1
];
i
+=
X
,
j
+=
Y
)
if
(
ground
[
i
][
j
]
!=
0
)
return
false
;
return
true
;
}
public
int
[][]
getMoves
(
int
[][]
ground
)
{
int
[][]
moves
=
new
int
[
1
][
2
];
moves
[
0
][
0
]
=
-
1
;
return
moves
;
}
}
src/pack/Nuts/King.java
View file @
4bb8935f
...
...
@@ -24,17 +24,41 @@ public class King extends Nut {
return
Math
.
pow
(
x
-
destination
[
0
],
2
)
+
Math
.
pow
(
y
-
destination
[
1
],
2
)
<=
2
;
}
public
int
[]
getXY
(){
public
int
[]
getXY
()
{
int
[]
r
=
new
int
[
2
];
r
[
0
]
=
x
;
r
[
1
]
=
y
;
return
r
;
}
@Override
@Override
public
boolean
canRoute
(
int
[]
destination
,
int
[][]
ground
)
{
return
true
;
}
public
int
[][]
getMoves
(
int
[][]
ground
)
{
int
[][]
moves
=
new
int
[
8
][
2
];
int
i
,
I
,
j
,
J
;
i
=
I
=
j
=
J
=
0
;
if
(
x
>
0
)
i
=
-
1
;
if
(
y
>
0
)
j
=
-
1
;
if
(
x
<
7
)
I
=
2
;
if
(
y
<
7
)
J
=
2
;
int
k
=
0
;
for
(
int
q
=
x
+
i
,
w
;
q
<
x
+
I
;
q
++)
for
(
w
=
y
+
j
;
w
<
y
+
J
;
w
++)
if
(
ground
[
q
][
w
]
==
0
&&
(
q
!=
x
||
w
!=
y
))
{
moves
[
k
][
0
]
=
q
;
moves
[
k
][
1
]
=
w
;
k
++;
}
moves
[
k
][
0
]
=
-
1
;
return
moves
;
}
}
src/pack/Nuts/Knight.java
View file @
4bb8935f
...
...
@@ -34,6 +34,7 @@ public class Knight extends Nut {
@Override
public
boolean
canMove
(
int
[]
destination
)
{
if
(!
isAlive
)
return
false
;
return
Math
.
pow
(
destination
[
0
]
-
x
,
2
)
+
Math
.
pow
(
destination
[
1
]
-
y
,
2
)
==
5
;
}
...
...
@@ -42,5 +43,12 @@ public class Knight extends Nut {
return
true
;
}
public
int
[][]
getMoves
(
int
[][]
ground
)
{
int
[][]
moves
=
new
int
[
1
][
2
];
moves
[
0
][
0
]
=
-
1
;
return
moves
;
}
}
src/pack/Nuts/Nut.java
View file @
4bb8935f
...
...
@@ -19,5 +19,6 @@ public abstract class Nut {
isAlive
=
false
;
}
public
abstract
int
[][]
getMoves
(
int
[][]
ground
);
}
src/pack/Nuts/Pawn.java
View file @
4bb8935f
...
...
@@ -22,6 +22,7 @@ public class Pawn extends Nut {
@Override
public
boolean
canMove
(
int
[]
destination
)
{
if
(!
isAlive
)
return
false
;
if
(
id
>
0
)
{
if
(
x
==
6
&&
destination
[
0
]
-
x
==
-
2
&&
destination
[
1
]
==
y
)
return
true
;
return
destination
[
0
]
<
x
&&
destination
[
0
]
>=
x
-
1
&&
destination
[
1
]
<=
y
+
1
&&
destination
[
1
]
>=
y
-
1
;
...
...
@@ -44,6 +45,16 @@ public class Pawn extends Nut {
return
(
x
+
1
==
k
[
0
]
&&
y
-
1
==
k
[
1
])
||
(
x
+
1
==
k
[
0
]
&&
y
+
1
==
k
[
1
]);
}
@Override
public
int
[][]
getMoves
(
int
[][]
ground
)
{
int
[][]
moves
=
new
int
[
1
][
2
];
moves
[
0
][
0
]
=
-
1
;
return
moves
;
}
}
src/pack/Nuts/Queen.java
View file @
4bb8935f
...
...
@@ -10,7 +10,7 @@ public class Queen extends Nut {
y
=
3
;
}
else
{
x
=
0
;
y
=
4
;
y
=
3
;
}
}
...
...
@@ -21,13 +21,10 @@ public class Queen extends Nut {
@Override
public
boolean
canMove
(
int
[]
destination
)
{
if
(
x
==
destination
[
0
]
||
y
==
destination
[
1
])
return
true
;
else
{
if
(
x
==
destination
[
0
])
return
false
;
else
return
Math
.
abs
((
y
-
destination
[
1
])
/
(
x
-
destination
[
0
]))
==
1
;
}
if
(!
isAlive
)
return
false
;
if
(
x
==
destination
[
0
]
&&
y
==
destination
[
1
])
return
false
;
else
if
(
x
==
destination
[
0
]
||
y
==
destination
[
1
])
return
true
;
else
return
Math
.
abs
((
float
)(
y
-
destination
[
1
])
/
(
float
)
(
x
-
destination
[
0
]))
==
1
;
}
@Override
...
...
@@ -36,18 +33,19 @@ public class Queen extends Nut {
int
t
;
if
(
destination
[
1
]
-
y
>
0
)
t
=
1
;
else
t
=
-
1
;
for
(
int
i
=
y
;
i
!=
destination
[
1
];
i
+=
t
)
for
(
int
i
=
y
+
t
;
i
>
-
1
&&
i
<
8
&&
i
!=
destination
[
1
];
i
+=
t
)
if
(
ground
[
x
][
i
]
!=
0
)
return
false
;
return
true
;
}
else
if
(
destination
[
1
]
==
y
)
{
int
t
;
if
(
destination
[
0
]
-
x
>
0
)
t
=
1
;
if
(
destination
[
0
]
>
x
)
t
=
1
;
else
t
=
-
1
;
for
(
int
i
=
x
;
i
!=
destination
[
0
];
i
+=
t
)
for
(
int
i
=
x
+
t
;
i
>
-
1
&&
i
<
8
&&
i
!=
destination
[
0
];
i
+=
t
)
if
(
ground
[
i
][
y
]
!=
0
)
return
false
;
return
true
;
}
int
X
,
Y
;
if
(
destination
[
0
]
<
x
)
X
=
-
1
;
...
...
@@ -56,7 +54,7 @@ public class Queen extends Nut {
if
(
destination
[
1
]
<
y
)
Y
=
-
1
;
else
Y
=
1
;
for
(
int
i
=
x
+
X
,
j
=
y
+
Y
;
i
>
-
1
&&
i
!=
destination
[
0
]
&&
j
>
-
1
&&
j
!=
destination
[
1
];
i
+=
X
,
j
+=
Y
)
for
(
int
i
=
x
+
X
,
j
=
y
+
Y
;
i
>
-
1
&&
i
<
8
&&
i
!=
destination
[
0
]
&&
j
>
-
1
&&
j
<
8
&&
j
!=
destination
[
1
];
i
+=
X
,
j
+=
Y
)
if
(
ground
[
i
][
j
]
!=
0
)
return
false
;
...
...
@@ -65,8 +63,11 @@ public class Queen extends Nut {
}
public
int
[][]
getMoves
(
int
[][]
ground
)
{
int
[][]
moves
=
new
int
[
1
][
2
];
moves
[
0
][
0
]
=
-
1
;
return
moves
;
}
...
...
src/pack/Nuts/Rook.java
View file @
4bb8935f
...
...
@@ -32,6 +32,8 @@ public class Rook extends Nut {
@Override
public
boolean
canMove
(
int
[]
destination
)
{
if
(!
isAlive
)
return
false
;
if
(
x
==
destination
[
0
]
&&
y
==
destination
[
1
])
return
false
;
return
x
==
destination
[
0
]
||
y
==
destination
[
1
];
}
...
...
@@ -41,20 +43,27 @@ public class Rook extends Nut {
int
t
;
if
(
destination
[
1
]
-
y
>
0
)
t
=
1
;
else
t
=
-
1
;
for
(
int
i
=
y
;
i
!=
destination
[
1
];
i
+=
t
)
for
(
int
i
=
y
+
t
;
i
>
-
1
&&
i
<
8
&&
i
!=
destination
[
1
];
i
+=
t
)
if
(
ground
[
x
][
i
]
!=
0
)
return
false
;
}
else
{
int
t
;
if
(
destination
[
0
]
-
x
>
0
)
t
=
1
;
else
t
=
-
1
;
for
(
int
i
=
x
;
i
!=
destination
[
0
];
i
+=
t
)
for
(
int
i
=
x
+
t
;
i
>
-
1
&&
i
<
8
&&
i
!=
destination
[
0
];
i
+=
t
)
if
(
ground
[
i
][
y
]
!=
0
)
return
false
;
}
return
true
;
}
public
int
[][]
getMoves
(
int
[][]
ground
)
{
int
[][]
moves
=
new
int
[
1
][
2
];
moves
[
0
][
0
]
=
-
1
;
return
moves
;
}
}
src/pack/Play.java
View file @
4bb8935f
...
...
@@ -5,10 +5,17 @@ public class Play {
Game
game
=
new
Game
();
game
.
startGame
();
boolean
continue_
=
true
;
boolean
continue_
;
do
{
continue_
=
game
.
play
(
game
.
getInput
());
game
.
showPlayGround
();
game
.
getPlayGround
().
show
();
if
(
game
.
getPlayGround
().
getCheck
())
{
if
(
game
.
getTurn
()
==
1
)
System
.
out
.
println
(
"CHECKed!\nbe careful WHITEl!\n"
);
else
System
.
out
.
println
(
"CHECKed!\nbe careful BLACK!\n"
);
}
continue_
=
!
game
.
isEnd
();
}
while
(
continue_
);
}
...
...
src/pack/PlayGround.java
View file @
4bb8935f
...
...
@@ -39,13 +39,13 @@ public class PlayGround {
bRooks
[
0
]
=
new
Rook
(-
9
);
nutsID
.
put
(
bRooks
[
0
],
-
9
);
bRooks
[
1
]
=
new
Rook
(-
10
);
nutsID
.
put
(
w
Rooks
[
1
],
-
10
);
nutsID
.
put
(
b
Rooks
[
1
],
-
10
);
wKnights
=
new
Knight
[
2
];
wKnights
[
0
]
=
new
Knight
(
11
);
nutsID
.
put
(
wKnights
[
0
],
11
);
wKnights
[
1
]
=
new
Knight
(
12
);
nutsID
.
put
(
w
Rook
s
[
1
],
12
);
nutsID
.
put
(
w
Knight
s
[
1
],
12
);
bKnights
=
new
Knight
[
2
];
bKnights
[
0
]
=
new
Knight
(-
11
);
...
...
@@ -61,7 +61,7 @@ public class PlayGround {
bBishops
=
new
Bishop
[
2
];
bBishops
[
0
]
=
new
Bishop
(-
13
);
nutsID
.
put
(
bBishops
[
1
],
-
13
);
nutsID
.
put
(
bBishops
[
0
],
-
13
);
bBishops
[
1
]
=
new
Bishop
(-
14
);
nutsID
.
put
(
bBishops
[
1
],
-
14
);
...
...
@@ -96,7 +96,6 @@ public class PlayGround {
ground
[
0
][
6
]
=
-
11
;
ground
[
0
][
7
]
=
-
9
;
ground
[
7
][
0
]
=
9
;
ground
[
7
][
1
]
=
11
;
ground
[
7
][
2
]
=
13
;
...
...
@@ -120,11 +119,10 @@ public class PlayGround {
return
2
;
//4 check
ifMoveWillCheck
(
getDestination
((
string
[
0
].
charAt
(
0
))
,
(
short
)
(
string
[
0
].
charAt
(
1
))),
getDestination
(
string
[
1
].
charAt
(
0
),
(
short
)
(
string
[
1
].
charAt
(
1
))),
turn
);
//4 check
ifMoveWillCheck
(
getDestination
((
string
[
0
].
charAt
(
0
)),
(
short
)
(
string
[
0
].
charAt
(
1
))),
getDestination
(
string
[
1
].
charAt
(
0
),
(
short
)
(
string
[
1
].
charAt
(
1
))),
turn
);
if
(
getCheck
())
return
4
;
//5 on mohre on harekato nemitone bere
for
(
Nut
i
:
nutsID
.
keySet
())
{
if
(
nutsID
.
get
(
i
)
==
id1
)
{
...
...
@@ -153,8 +151,19 @@ public class PlayGround {
int
id1
=
ground
[
origin
[
0
]][
origin
[
1
]],
id2
=
ground
[
destination
[
0
]][
destination
[
1
]];
ground
[
origin
[
0
]][
origin
[
1
]]
=
0
;
ground
[
destination
[
0
]][
destination
[
1
]]
=
id1
;
if
(
id1
==
16
)
wKing
.
move
(
destination
,
ground
);
else
if
(
id1
==
-
16
)
bKing
.
move
(
destination
,
ground
);
isCheck
(
turn
);
getCheck
();
if
(
id1
==
16
)
wKing
.
move
(
origin
,
ground
);
else
if
(
id1
==
-
16
)
bKing
.
move
(
origin
,
ground
);
ground
[
origin
[
0
]][
origin
[
1
]]
=
id1
;
ground
[
destination
[
0
]][
destination
[
1
]]
=
id2
;
}
...
...
@@ -224,12 +233,12 @@ public class PlayGround {
(
wKnights
[
0
].
canMove
(
bKing
.
getXY
()))
||
(
wKnights
[
1
].
canMove
(
bKing
.
getXY
()))
||
(
wBishops
[
0
].
canMove
(
bKing
.
getXY
())
&&
wBishops
[
0
].
canRoute
(
bKing
.
getXY
(),
ground
))
||
(
wBishops
[
1
].
canMove
(
bKing
.
getXY
())
&&
(
wBishops
[
1
].
canRoute
(
bKing
.
getXY
(),
ground
)
)))
{
(
wBishops
[
1
].
canMove
(
bKing
.
getXY
())
&&
wBishops
[
1
].
canRoute
(
bKing
.
getXY
(),
ground
)))
{
check
=
true
;
return
;
}
check
=
false
;
}
check
=
false
;
}
public
boolean
getCheck
()
{
...
...
@@ -321,4 +330,59 @@ public class PlayGround {
}
}
\ No newline at end of file
public
boolean
canKingScape
(
int
turn
)
{
boolean
previousCheck
=
getCheck
();
if
(
turn
>
0
)
{
int
[][]
moves
=
wKing
.
getMoves
(
ground
);
for
(
int
i
=
0
;
moves
[
i
][
0
]
!=
-
1
;
i
++)
{
ifMoveWillCheck
(
wKing
.
getXY
(),
moves
[
i
],
turn
);
if
(!
getCheck
())
{
check
=
previousCheck
;
return
true
;
}
}
for
(
Nut
n
:
nutsID
.
keySet
())
if
(
nutsID
.
get
(
n
)
>
0
)
{
moves
=
n
.
getMoves
(
ground
);
for
(
int
i
=
0
;
moves
[
i
][
0
]
!=
-
1
;
i
++)
{
ifMoveWillCheck
(
wKing
.
getXY
(),
moves
[
i
],
turn
);
if
(!
getCheck
())
{
check
=
previousCheck
;
return
true
;
}
}
}
}
else
{
int
[][]
moves
=
bKing
.
getMoves
(
ground
);
for
(
int
i
=
0
;
moves
[
i
][
0
]
!=
-
1
;
i
++)
{
ifMoveWillCheck
(
bKing
.
getXY
(),
moves
[
i
],
turn
);
if
(!
getCheck
())
{
check
=
previousCheck
;
return
true
;
}
}
for
(
Nut
n
:
nutsID
.
keySet
())
if
(
nutsID
.
get
(
n
)
>
0
)
{
moves
=
n
.
getMoves
(
ground
);
for
(
int
i
=
0
;
moves
[
i
][
0
]
!=
-
1
;
i
++)
{
ifMoveWillCheck
(
bKing
.
getXY
(),
moves
[
i
],
turn
);
if
(!
getCheck
())
{
check
=
previousCheck
;
return
true
;
}
}
}
}
check
=
previousCheck
;
return
false
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment