Tournament.java (3484B) download
1package nl.isygameclient;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.List;
6import java.util.Map;
7import java.util.Random;
8import nl.isygameclient.models.Ai;
9import nl.isygameclient.models.Game;
10import nl.isygameclient.models.Player;
11import nl.isygameclient.models.PlayerManager;
12import nl.isygameclient.models.games.othello.Othello;
13import nl.isygameclient.network.Event;
14import nl.isygameclient.network.EventType;
15import nl.isygameclient.network.GameClient;
16import nl.isygameclient.util.Vector2D;
17
18public class Tournament {
19 public static final int[][] HEURISTIC = {
20 { 10, 2, 5, 5, 5, 5, 2, 10 },
21 { 2, 2, 1, 1, 1, 1, 2, 2 },
22 { 5, 1, 1, 1, 1, 1, 1, 5 },
23 { 5, 1, 1, 1, 1, 1, 1, 5 },
24 { 5, 1, 1, 1, 1, 1, 1, 5 },
25 { 5, 1, 1, 1, 1, 1, 1, 5 },
26 { 2, 2, 1, 1, 1, 1, 2, 2 },
27 { 10, 2, 5, 5, 5, 5, 2, 10 }
28 };
29
30 public static void main(String[] args) throws IOException {
31 // GameClient client = new GameClient("145.33.225.170", 7789);
32 GameClient client = new GameClient("localhost", 7789);
33
34 client.login("ITV2D1" + new Random().nextInt(0, 9));
35
36 Player dummyPlayer = new Player("network", "black") {
37 public Vector2D<Integer, Integer> onPlayerTurn() { return null; }
38 };
39
40 Player randomPlayer = new Player("network", "black") {
41 public Vector2D<Integer, Integer> onPlayerTurn() { return null; }
42 };
43
44 PlayerManager playerManager = new PlayerManager(0, List.of(randomPlayer, dummyPlayer));
45 Othello othello = new Othello(playerManager);
46 Random random = new Random();
47
48 String firstPlayer = null;
49
50 while (true) {
51 Event event = client.event(-1);
52
53 switch (event.type) {
54 case MATCH:
55 @SuppressWarnings("unchecked")
56 var data = (Map<String, String>) event.data;
57
58 String opponent = data.get("opponent");
59 System.out.println("playing against " + opponent);
60
61 firstPlayer = null;
62 playerManager.restart();
63 othello.getBoard().clear();
64
65 break;
66 case YOURTURN:
67 if (firstPlayer == null) {
68 firstPlayer = "self";
69 othello.initializeBoard();
70 }
71 var turns = othello.getValidMoves(randomPlayer);
72 System.out.println(turns);
73 var turn = turns.get(random.nextInt(0, turns.size()));
74
75 System.out.println(turn);
76 client.send("move " + (turn.getY() * 8 + turn.getX()));
77 // othello.move(ai, turn);
78
79 break;
80 case MOVE:
81 if (firstPlayer == null) {
82 firstPlayer = "other";
83 playerManager.nextPlayer();
84 othello.initializeBoard();
85 }
86 @SuppressWarnings("unchecked")
87 var moveMap = (Map<String, String>) event.data;
88
89 int move = Integer.parseInt(moveMap.get("move"));
90 System.out.printf("%s did move %dx%d\n", moveMap.get("player"), move % 8, move / 8);
91 othello.move(moveMap.get("player").equals(client.getName()) ? randomPlayer : dummyPlayer, new Vector2D<>(move % 8, move / 8));
92
93 break;
94 case WIN:
95 case LOSS:
96 default:
97 System.out.printf("%s -> %s\n", client.getName(), event.type);
98 for (int y = 0; y < 8; y++) {
99 System.out.print("|");
100 for (int x = 0; x < 8; x++) {
101 Player p = othello.getBoard().get(new Vector2D<Integer, Integer>(x, y));
102 if (p == dummyPlayer) {
103 System.out.print("N|");
104 } else if (p == randomPlayer) {
105 System.out.print("A|");
106 } else {
107 System.out.print(" |");
108 }
109 }
110 System.out.println();
111 } // outcome = event;
112 // return false;
113 }
114 }
115 // client.close();
116 }
117}