コード例 #1
0
  /**
   * The heuristic evaluation function that determines the chances of winning...
   *
   * @param brd board to be evaluated
   * @return score that indicates our chances of winning
   */
  protected static int evalBoard(BreakthroughState brd) {
    int score = 0;
    for (int r = 0; r < BreakthroughState.N; r++) {
      for (int c = 0; c < BreakthroughState.N; c++) {
        if (brd.board[r][c] == BreakthroughState.homeSym) {
          score += (r + 1);
        } else if (brd.board[r][c] == BreakthroughState.awaySym) {
          score -= (BreakthroughState.N - r);
        }
      }
    }

    if (Math.abs(score) > MAX_EVAL_SCORE) {
      System.err.println("Problem with eval");
      System.exit(0);
    }
    return score;
  }
コード例 #2
0
 public static void main(String args[]) {
   System.out.println("----- Client Start -----");
   Console in = System.console();
   // get a name
   String username = in.readLine("Enter your name: ");
   // get server hostname
   String hostname = in.readLine("Server hostname: ");
   // c or j
   String mode = in.readLine("[C]reate or [J]oin?: ");
   // get room name
   String roomname = in.readLine("Name of game-room: ");
   // set up the demopanel and its enclosing frame
   DemoPanel ui = new DemoPanel();
   JFrame frame = new JFrame("ateam-tanks");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(500, 500);
   frame.setLayout(new BorderLayout());
   frame.add(ui, BorderLayout.CENTER);
   frame.addKeyListener(ui);
   frame.setVisible(true);
   // init and start game client
   GameClient c1 = new GameClient(username, 8887);
   // create RealWindow
   RealWindow win = new RealWindow(c1, ui);
   // start server communications
   try {
     sleep(1000);
     System.out.println("Connecting to server..");
     c1.push(new event.client.JoinServerEvent(hostname));
     sleep(1000);
     if (mode.equals("C") || mode.equals("c")) {
       c1.push(new event.client.FwdUserEvent(new event.user.CreateRoomEvent(roomname, mkList())));
       in.readLine("Press Enter to start the game");
       c1.push(new event.client.FwdUserEvent(new event.user.StartGameEvent()));
     } else if (mode.equals("J") || mode.equals("j")) {
       c1.push(new event.client.FwdUserEvent(new event.user.JoinRoomEvent(roomname)));
     }
   } catch (InterruptedException e) {
   }
 }
コード例 #3
0
  public GameMove getMove(GameState state, String lastMove) {
    DomineeringState board = (DomineeringState) state;
    homeSym = board.homeSym;
    awaySym = board.awaySym;
    emptySym = board.emptySym;
    isHome = state.who == GameState.Who.HOME; // HOME: true, AWAY: false

    if (isHome) {
      MAX_DEPTH = 6;
      if (board.numMoves >= 6) MAX_DEPTH = 8;
      if (board.numMoves >= 12) MAX_DEPTH = 10;
      if (board.numMoves >= 14) MAX_DEPTH = 12;
    } else {
      MAX_DEPTH = 7;
      if (board.numMoves >= 6) MAX_DEPTH = 9;
      if (board.numMoves >= 12) MAX_DEPTH = 11;
      if (board.numMoves >= 14) MAX_DEPTH = 13;
    }

    Move[] stack = new Move[MAX_DEPTH + 1];
    init(stack);

    if (isHome == true) {
      if (board.numMoves == 0) {
        Move[] temp = new Move[4];
        temp[0] = new Move(1, 2, 1, 3);
        temp[1] = new Move(1, 4, 1, 5);
        temp[2] = new Move(6, 2, 6, 3);
        temp[3] = new Move(6, 4, 6, 5);
        Random rnd = new Random();
        return temp[rnd.nextInt(4)];
      } else if (board.numMoves == 2) {
        Map map = new HashMap();
        Scanner sc;
        try {
          sc = new Scanner(new File("home3.txt"));
          while (sc.hasNextLine()) {
            String s = sc.nextLine();
            String[] sp = s.split("\\s+");
            long key = Long.parseLong(sp[0]);
            Move value =
                new Move(
                    Integer.parseInt(sp[1]),
                    Integer.parseInt(sp[2]),
                    Integer.parseInt(sp[3]),
                    Integer.parseInt(sp[4]));
            map.put(key, value);
          }
          long hash = new Board(board.board, isHome, "alphabetatt").getHashKey();
          Move rtn = (Move) map.get(hash);
          if (rtn != null) return rtn;
        } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    } else {
      if (board.numMoves == 1) {
        Map map = new HashMap();
        Scanner sc;
        try {
          sc = new Scanner(new File("away2.txt"));
          while (sc.hasNextLine()) {
            String s = sc.nextLine();
            String[] sp = s.split("\\s+");
            long key = Long.parseLong(sp[0]);
            Move value =
                new Move(
                    Integer.parseInt(sp[1]),
                    Integer.parseInt(sp[2]),
                    Integer.parseInt(sp[3]),
                    Integer.parseInt(sp[4]));
            map.put(key, value);
          }
          long hash = new Board(board.board, isHome, "alphabetatt").getHashKey();
          Move rtn = (Move) map.get(hash);
          if (rtn != null) return rtn;
        } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }

    long start = System.currentTimeMillis();
    alphabetaTT(board.board, 0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, isHome, stack);
    long end = System.currentTimeMillis();

    total += (end - start);
    System.out.printf(
        "%s %3d Time: %-4d Total:%d\n", stack[0], (int) stack[0].score, (end - start), total);
    tt.print();

    System.out.println("Move: " + board.numMoves);

    return stack[0];
  }