Skip to content

Commit aa35dbc

Browse files
committed
Improve hard computer skill
1 parent b01cac2 commit aa35dbc

3 files changed

Lines changed: 115 additions & 2 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package pl.nogacz.chess.application;
2+
3+
import pl.nogacz.chess.board.Board;
4+
import pl.nogacz.chess.board.Coordinates;
5+
import pl.nogacz.chess.pawns.Pawn;
6+
import pl.nogacz.chess.pawns.PawnClass;
7+
import pl.nogacz.chess.pawns.moves.PawnMoves;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
/**
13+
* @author Dawid Nogacz on 14.05.2019
14+
*/
15+
public class BoardPoint {
16+
public int calculateBoard() {
17+
HashMap<Coordinates, PawnClass> cacheBoard = new HashMap<>(Board.getBoard());
18+
19+
int whitePoint = 0;
20+
int blackPoint = 0;
21+
22+
for(Map.Entry<Coordinates, PawnClass> entry : cacheBoard.entrySet()) {
23+
if(entry.getValue().getColor().isWhite()) {
24+
whitePoint += calculatePawn(entry.getKey(), entry.getValue());
25+
} else {
26+
blackPoint += calculatePawn(entry.getKey(), entry.getValue());
27+
}
28+
}
29+
30+
return blackPoint - whitePoint;
31+
}
32+
33+
private int calculatePawn(Coordinates coordinates, PawnClass pawn) {
34+
int point = 1;
35+
36+
PawnMoves pawnMoves = new PawnMoves(pawn, coordinates);
37+
38+
if(pawnMoves.getPossibleCheck().size() > 0) {
39+
point += 100;
40+
} else if(pawnMoves.getPossibleKick().size() > 0) {
41+
point += 20;
42+
}
43+
44+
if(pawn.getColor().isWhite() && pawn.getPawn() == Pawn.PAWN && coordinates.getY() > 4) {
45+
point++;
46+
} else if(pawn.getColor().isBlack() && pawn.getPawn() == Pawn.PAWN && coordinates.getY() < 4) {
47+
point++;
48+
}
49+
50+
switch(pawn.getPawn()) {
51+
case QUEEN: point += 7; break;
52+
case KNIGHT: point += 3; break;
53+
case ROOK: point += 2; break;
54+
case BISHOP: point += 2; break;
55+
}
56+
57+
return point;
58+
}
59+
}

src/main/java/pl/nogacz/chess/application/Computer.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class Computer {
2424
private Set<Coordinates> possibleMoves = new HashSet<>();
2525
private Set<Coordinates> possibleKickAndNotIsEnemyKickMe = new HashSet<>();
2626

27+
private BoardPoint boardPoint = new BoardPoint();
28+
2729
public Computer() {
2830
if (isExists()) {
2931
load();
@@ -99,6 +101,7 @@ public void getGameData() {
99101
public Coordinates choosePawn() {
100102
switch (skill) {
101103
case 1: return choosePawnEasy();
104+
case 2: return choosePawnHard();
102105
default: return choosePawnNormal();
103106
}
104107
}
@@ -128,6 +131,59 @@ private Coordinates choosePawnNormal() {
128131
return (Coordinates) object[random.nextInt(object.length)];
129132
}
130133

134+
private Coordinates choosePawnHard() {
135+
int minNumber = -1000;
136+
137+
Set<Coordinates> cachePawn = new HashSet<>();
138+
cachePawn.addAll(possibleKick);
139+
cachePawn.addAll(possibleMoves);
140+
141+
Set<Coordinates> cachePossiblePawn = new HashSet<>();
142+
143+
for(Coordinates coordinates : cachePawn) {
144+
145+
PawnMoves moves = new PawnMoves(Board.getPawn(coordinates), coordinates);
146+
147+
Set<Coordinates> cacheMoves = new HashSet<>();
148+
cacheMoves.addAll(moves.getPossibleKick());
149+
cacheMoves.addAll(moves.getPossibleMoves());
150+
151+
for(Coordinates moveCoordinates : cacheMoves) {
152+
PawnClass oldPawn = Board.addPawnWithoutDesign(moveCoordinates, Board.getPawn(coordinates));
153+
154+
int point = boardPoint.calculateBoard();
155+
156+
if(point > minNumber) {
157+
minNumber = point;
158+
}
159+
160+
Board.removePawnWithoutDesign(moveCoordinates);
161+
162+
if (oldPawn != null) {
163+
Board.addPawnWithoutDesign(moveCoordinates, oldPawn);
164+
}
165+
}
166+
167+
for(Coordinates moveCoordinates : cacheMoves) {
168+
PawnClass oldPawn = Board.addPawnWithoutDesign(moveCoordinates, Board.getPawn(coordinates));
169+
170+
int point = boardPoint.calculateBoard();
171+
172+
if(point == minNumber) {
173+
cachePossiblePawn.add(coordinates);
174+
}
175+
176+
Board.removePawnWithoutDesign(moveCoordinates);
177+
178+
if (oldPawn != null) {
179+
Board.addPawnWithoutDesign(moveCoordinates, oldPawn);
180+
}
181+
}
182+
}
183+
184+
return (Coordinates) cachePossiblePawn.toArray()[random.nextInt(cachePossiblePawn.size())];
185+
}
186+
131187
public Coordinates chooseMove(Coordinates coordinates) {
132188
switch (skill) {
133189
case 1: return chooseMoveEasy(coordinates);

src/main/java/pl/nogacz/chess/application/GameLogic.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public boolean isMovePossible() {
6060
possibleMovesWhite.clear();
6161
}
6262

63-
System.out.println(pawnWhiteCount);
64-
6563
return possibleMovesWhite.size() > 0 && possibleMovesBlack.size() > 0;
6664
}
6765

0 commit comments

Comments
 (0)