game-client

Play TicTacToe and Reversi
Log | Files | Refs

Tournament.java (4430B)


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