Skip to content

Commit 325f89c

Browse files
add other changes.
1 parent 7c1b918 commit 325f89c

17 files changed

+363
-452
lines changed

src/com/gipf/client/game/GameController.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public GameController(Controller controller) {
2525
public void input(String received) {
2626
if (received.startsWith("/i")) {
2727
// "/i Board.BLACK_VALUE thisStonesAccount opponentStonesAccount boardString";
28-
String info = received.split("/i ")[1];
28+
String info = received.split("/i ")[1];
2929
String boardString = received.substring(3 + info.split(" ")[0].length() + 1 + info.split(" ")[1].length() + 1 + info.split(" ")[2].length() + 1);
3030
this.initCall(Integer.parseInt(info.split(" ")[0]), Integer.parseInt(info.split(" ")[1]), Integer.parseInt(info.split(" ")[2]), boardString);
3131
} else if (received.startsWith("/u")) {
@@ -85,9 +85,10 @@ private Player returnOpponent(Player player) {
8585
if (player.equals(this.controller.getGame().getPlayerOne())) return this.controller.getGame().getPlayerTwo();
8686
else return this.controller.getGame().getPlayerOne();
8787
}
88-
88+
8989
public void setPlayer(Player player, boolean copyData) {
9090
if (copyData) {
91+
player.setName(this.thisPlayer.getName());
9192
player.setStoneAccount(this.thisPlayer.getStoneAccount());
9293
player.setStoneColor(this.thisPlayer.getStoneColor());
9394
this.thisPlayer = player;
@@ -169,7 +170,7 @@ private Point readPoint(String pointString) {
169170

170171
return new Point(x, y);
171172
}
172-
173+
173174
public Controller getController() {
174175
return this.controller;
175176
}

src/com/gipf/client/game/player/Player.java

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public class Player {
44

5+
private String name;
56
private int stoneAccount;
67
private int stoneColor;
78

@@ -10,11 +11,13 @@ public Player() {
1011
}
1112

1213
public Player(int stoneColor) {
14+
this.name = "Player " + String.valueOf(stoneColor);
1315
this.stoneAccount = 15;
1416
this.stoneColor = stoneColor;
1517
}
1618

17-
public Player(int stones, int stoneColor) {
19+
public Player(String name, int stones, int stoneColor) {
20+
this.name = name;
1821
this.stoneAccount = stones;
1922
this.stoneColor = stoneColor;
2023
}
@@ -23,6 +26,14 @@ public void update(String state) {
2326

2427
}
2528

29+
public String getName() {
30+
return this.name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
2637
public int getStoneAccount() {
2738
return this.stoneAccount;
2839
}
@@ -32,23 +43,20 @@ public void setStoneAccount(int stones) {
3243
}
3344

3445
public void setStoneColor(int color) {
46+
this.name = "Player " + String.valueOf(color);
3547
this.stoneColor = color;
3648
}
37-
38-
public void addStones(int stones) {
39-
this.stoneAccount += stones;
40-
}
4149

4250
public int getStoneColor() {
4351
return this.stoneColor;
4452
}
4553

4654
public Player copy() {
47-
return new Player(this.stoneAccount, this.stoneColor);
55+
return new Player(this.name, this.stoneAccount, this.stoneColor);
4856
}
4957

5058
public String toString() {
51-
return "[Player: stone color: " + this.stoneColor + " stone account: " + this.stoneAccount + "]";
59+
return this.name;
5260
}
5361

5462
public boolean equals(Object o) {
+11-40
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,39 @@
11
package com.gipf.client.game.player.bot;
22

3-
import java.util.ArrayList;
4-
53
import com.gipf.client.game.GameController;
64
import com.gipf.client.game.player.Player;
7-
import com.gipf.client.game.player.bot.action.Action;
85
import com.gipf.client.player.bot.algorithm.Algorithm;
9-
import com.gipf.client.player.bot.evaluation.Evaluator;
10-
import com.gipf.client.player.bot.generator.TreeGenerator;
6+
import com.gipf.client.player.bot.evaluation.EvaluationFunction;
117

128
public class Bot extends Player {
139

1410
private GameController gameController;
1511
private Algorithm algorithm;
16-
private Evaluator evaluator;
17-
private TreeGenerator treeGenerator;
18-
private BotLogic botLogic;
19-
20-
private ArrayList<Action> upcomingActions;
2112

22-
public Bot(Algorithm algorithm, Evaluator evaluator) {
13+
public Bot(Algorithm algorithm) {
2314
this.algorithm = algorithm;
24-
this.evaluator = evaluator;
25-
this.treeGenerator = new TreeGenerator();
26-
this.botLogic = new BotLogic(this);
27-
this.upcomingActions = new ArrayList<Action>();
2815
}
2916

3017
public void update(String state) {
31-
if (state.equals("move")) {
32-
BotMoveThread botThread = new BotMoveThread(this, this.gameController, this.algorithm, this.evaluator);
33-
botThread.start();
34-
} else if (state.equals("remove")) {
35-
BotRemoveThread botThread = new BotRemoveThread(this, this.gameController, this.algorithm, this.evaluator, this.upcomingActions);
18+
if (state.equals("move")) {
19+
BotMoveThread botThread = new BotMoveThread(this, this.gameController, this.algorithm);
3620
botThread.start();
37-
this.upcomingActions.clear();
21+
} else if (state.equals("remove")) { //TODO
22+
3823
}
3924
}
4025

4126
public void setGameController(GameController gameController) {
4227
this.gameController = gameController;
28+
this.algorithm.setGame(gameController.getController().getGame());
4329
}
4430

4531
public void setAlgorithm(Algorithm algorithm) {
32+
algorithm.setGame(this.algorithm.getGame());
4633
this.algorithm = algorithm;
4734
}
4835

49-
public void setEvaluator(Evaluator evaluator) {
50-
this.evaluator = evaluator;
51-
}
52-
53-
public TreeGenerator getGenerator() {
54-
return this.treeGenerator;
55-
}
56-
57-
public void setUpcomingActions(ArrayList<Action> actions) {
58-
this.upcomingActions = actions;
59-
}
60-
61-
public BotLogic getLogic() {
62-
return this.botLogic;
63-
}
64-
65-
public Evaluator getEvaluator() {
66-
return this.evaluator;
36+
public void setEvaluationFunction(EvaluationFunction function) {
37+
this.algorithm.setEvaluationFunction(function);
6738
}
68-
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,5 @@
11
package com.gipf.client.game.player.bot;
22

3-
import java.util.ArrayList;
3+
public class BotLogic {
44

5-
import com.gipf.client.game.GameController;
6-
import com.gipf.client.game.player.bot.action.Action;
7-
import com.gipf.client.game.player.bot.tree.Node;
8-
import com.gipf.client.game.player.bot.tree.Tree;
9-
import com.gipf.client.player.bot.algorithm.Algorithm;
10-
import com.gipf.client.player.bot.evaluation.Evaluator;
11-
import com.gipf.client.utils.Point;
12-
13-
public class BotRemoveThread extends Thread {
14-
15-
private ArrayList<Action> actions;
16-
private GameController gameController;
17-
private Bot bot;
18-
private Algorithm algorithm;
19-
private Evaluator evaluator;
20-
21-
public BotRemoveThread(Bot bot, GameController gameController, Algorithm algorithm, Evaluator evaluator, ArrayList<Action> actions) {
22-
this.gameController = gameController;
23-
this.actions = actions;
24-
this.bot = bot;
25-
this.evaluator = evaluator;
26-
this.algorithm = algorithm;
27-
}
28-
29-
public void run() {
30-
31-
if (this.actions.size() == 0) {
32-
Node root = this.evaluator.evalToNode(this.gameController.getController().getGame().copy());
33-
this.bot.getLogic().performLogic(this.bot, root);
34-
35-
this.actions = algorithm.calculateBestActions(new Tree(root), this.bot);
36-
}
37-
38-
// visualising removal
39-
try {
40-
Thread.sleep(500);
41-
} catch (InterruptedException e) {
42-
Thread.currentThread().interrupt();
43-
}
44-
45-
// remove
46-
for (Action a : this.actions) {
47-
for (Point p : a.getPoints()) {
48-
this.gameController.getController().getGamePanel().getButtons()[p.getX()][p.getY()].doClick();
49-
try {
50-
Thread.sleep(250);
51-
} catch (InterruptedException e) {
52-
Thread.currentThread().interrupt();
53-
}
54-
55-
}
56-
this.gameController.getController().getGamePanel().getCheckButton().doClick();
57-
}
58-
59-
// ending thread
60-
try {
61-
this.join();
62-
Thread.currentThread().interrupt();
63-
} catch (InterruptedException e) {
64-
Thread.currentThread().interrupt();
65-
}
66-
}
67-
68-
public Bot getBot() {
69-
return this.bot;
70-
}
71-
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,42 @@
11
package com.gipf.client.game.player.bot;
22

3-
import java.util.ArrayList;
4-
53
import com.gipf.client.game.GameController;
6-
import com.gipf.client.game.player.bot.action.Action;
7-
import com.gipf.client.game.player.bot.tree.Node;
8-
import com.gipf.client.game.player.bot.tree.Tree;
94
import com.gipf.client.player.bot.algorithm.Algorithm;
10-
import com.gipf.client.player.bot.evaluation.Evaluator;
5+
import com.gipf.client.player.bot.generator.GameState;
6+
import com.gipf.client.utils.Point;
117

128
public class BotMoveThread extends Thread {
139

14-
public static long runTime = 0;
15-
public static int runs = 0;
16-
1710
private GameController gameController;
18-
private Evaluator evaluator;
1911
private Algorithm algorithm;
2012
private Bot bot;
2113

22-
public BotMoveThread(Bot bot, GameController gameController, Algorithm algorithm, Evaluator evaluator) {
14+
public BotMoveThread(Bot bot, GameController gameController, Algorithm algorithm) {
2315
this.gameController = gameController;
2416
this.algorithm = algorithm;
25-
this.evaluator = evaluator;
2617
this.bot = bot;
2718
}
2819

2920
public void run() {
30-
long start = System.nanoTime();
21+
long start = System.currentTimeMillis();
3122

3223
// computation for move
33-
Node root = this.evaluator.evalToNode(this.gameController.getController().getGame().copy());
34-
this.bot.getGenerator().generateTreeLayer(root, this.bot, this.bot.getLogic(), true);
35-
ArrayList<Action> actions = this.algorithm.calculateBestActions(new Tree(root), this.bot);
36-
37-
ArrayList<Node> search = new Tree(root).bfSearch(root);
38-
39-
if (actions.size() > 1) {
40-
ArrayList<Action> upcomingActions = new ArrayList<Action>();
41-
for (int i = 1; i < actions.size(); i++)
42-
upcomingActions.add(actions.get(i));
43-
this.bot.setUpcomingActions(upcomingActions);
44-
}
45-
46-
long end = System.nanoTime();
47-
48-
runTime += end - start;
49-
runs++;
50-
System.out.println("average runtime: " + runTime / (runs * 1000000.0) + "ms at " + runs + " runs, current run time: " + (end - start) + " miliseconds = " + ((end - start) / 1000000.0) + " ms with " + search.size() + " nodes.");
51-
System.out.println();
52-
// used for making bots move visible
53-
if (end - start < 166700000) {
54-
try {
55-
Thread.sleep((166700000 - (end - start)) / 1000000);
56-
} catch (InterruptedException e) {
57-
Thread.currentThread().interrupt();
58-
}
59-
}
24+
Point[] move = this.algorithm.returnBestMove(new GameState(this.gameController.getController().getGame(), null, null), this.bot);
25+
26+
long end = System.currentTimeMillis();
27+
28+
// used for making bots move visible
29+
// if (end - start < 500) {
30+
// try {
31+
// Thread.sleep(500 - (end - start));
32+
// } catch (InterruptedException e) {
33+
// Thread.currentThread().interrupt();
34+
// }
35+
// }
6036

6137
// do move
62-
this.gameController.getController().getGamePanel().getButtons()[actions.get(0).getPoints()[0].getX()][actions.get(0).getPoints()[0].getY()].doClick();
63-
64-
// visualise direction choice
65-
if (end - start < 250000000) {
66-
try {
67-
Thread.sleep((250000000 - (end - start)) / 1000000);
68-
} catch (InterruptedException e) {
69-
Thread.currentThread().interrupt();
70-
}
71-
}
72-
this.gameController.getController().getGamePanel().getButtons()[actions.get(0).getPoints()[1].getX()][actions.get(0).getPoints()[1].getY()].doClick();
38+
this.gameController.getController().getGamePanel().getButtons()[move[0].getX()][move[0].getY()].doClick();
39+
this.gameController.getController().getGamePanel().getButtons()[move[1].getX()][move[1].getY()].doClick();
7340

7441
// ending thread
7542
try {
@@ -79,4 +46,4 @@ public void run() {
7946
Thread.currentThread().interrupt();
8047
}
8148
}
82-
}
49+
}

0 commit comments

Comments
 (0)