package li.seiji;

import li.seiji.minichess.Game;
import li.seiji.minichess.InvalidMoveException;
import li.seiji.minichess.Player;
import li.seiji.minichess.board.Board;
import li.seiji.minichess.board.GameState;
import li.seiji.minichess.board.State;
import li.seiji.minichess.evaluator.DefaultBoardEvaluator;
import li.seiji.minichess.evaluator.ExtendedBoardEvaluator;
import li.seiji.minichess.imcs.Client;
import li.seiji.minichess.imcs.IMCSGame;
import li.seiji.minichess.logging.ConsoleLogger;
import li.seiji.minichess.player.*;

import java.io.IOException;
import java.util.Optional;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws InvalidMoveException, IOException, InterruptedException {
        warmup();
        Scanner scanner = new Scanner(System.in);

        while(true) {
            try {
                System.out.print("What to do: ");
                String[] cmd = scanner.nextLine().split(" ");

                if(cmd.length == 2 && cmd[0] == "accept") {
                    System.out.println("Accepting game: " + args[1]);
                    playAccept(args[1]);
                } else if(cmd.length == 1 && cmd[0] == "offer") {
                    System.out.println("Offering game...");
                    playOffer();
                }

            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }




    private static void playOffer() throws IOException, InvalidMoveException {
        IMCSPlayer netPlayer = new IMCSPlayer("imcs.svcs.cs.pdx.edu",  3589, "lazycat", "31337");
        char ownPlayer = netPlayer.offer();

        ThreadedIterativeNegamaxAlphaBetaPlayer player = new ThreadedIterativeNegamaxAlphaBetaPlayer(25);
        Game game;

        if(ownPlayer == 'W') {
            game = new Game(player, netPlayer);
        } else {
            game = new Game(netPlayer, player);
        }
        game.setLogger(new ConsoleLogger());
        game.run();
    }

    private static void playAccept(String gameId) throws IOException, InvalidMoveException {
        IMCSPlayer netPlayer = new IMCSPlayer("imcs.svcs.cs.pdx.edu",  3589, "lazycat", "31337");
        char ownPlayer = netPlayer.accept(gameId);

        ThreadedIterativeNegamaxAlphaBetaPlayer player = new ThreadedIterativeNegamaxAlphaBetaPlayer(25);
        Game game;

        if(ownPlayer == 'W') {
            game = new Game(player, netPlayer);
        } else {
            game = new Game(netPlayer, player);
        }
        game.setLogger(new ConsoleLogger());
        game.run();
    }


    private static void warmup() throws InvalidMoveException {
        System.out.println("Warming up");
        Board board = new Board();

        ThreadedIterativeNegamaxAlphaBetaPlayer playerWhite = new ThreadedIterativeNegamaxAlphaBetaPlayer(25);
        ThreadedIterativeNegamaxAlphaBetaPlayer playerBlack = new ThreadedIterativeNegamaxAlphaBetaPlayer(25);
        playerWhite.start(Player.WHITE);
        playerBlack.start(Player.BLACK);

        for(int i = 0; i < 8; ++i) {
            if(i % 2 == 0) {
                board.move(playerWhite.getMove(board));
            } else {
                board.move(playerBlack.getMove(board));
            }
        }

        playerWhite.end();
        playerBlack.end();
    }

}

@@ -1009,20 +1009,24 @@
 & cmd%5B0%5D
- == 
+.equals(
 %22accept%22
@@ -1026,16 +1026,17 @@
 accept%22)
+)
  %7B%0A     
@@ -1090,20 +1090,19 @@
 me: %22 + 
-args
+cmd
 %5B1%5D);%0A  
@@ -1130,20 +1130,19 @@
 yAccept(
-args
+cmd
 %5B1%5D);%0A  
@@ -1194,20 +1194,24 @@
 d%5B0%5D
- == 
+.equals(
 %22offer%22)
  %7B%0A 
@@ -1206,16 +1206,17 @@
 %22offer%22)
+)
  %7B%0A     
@@ -3071,17 +3071,17 @@
  0; i %3C 
-8
+4
 ; ++i) %7B

Fix player