Java browser: Test.java

Content of Test.java extracted from reversi.jar

/*
Test program for Reversi
 
Usage:  java Test
 
version 0.1, 10-12-2006
16-12-2017 version 0.2: refactored
 
Requirement: Java 1.5 or later
 
*/
 
import java.util.ArrayList;
 
public class Test {
  static final int level_black = 4;
  static final int level_white = 4;
 
  public static void main(String[] args) {
    ReversiBoard board = new ReversiBoard();
    Strategy normal =
        new Strategy() {
          public int strategy(TKind me, TKind opponent, TKind[][] board) {
            int tstrat = 0;
            for (int i = 0; i < 8; i++)
              if (board[i][0] == opponent) tstrat++;
              else if (board[i][0] == me) tstrat--;
            for (int i = 0; i < 8; i++)
              if (board[i][7] == opponent) tstrat++;
              else if (board[i][7] == me) tstrat--;
            for (int i = 0; i < 8; i++)
              if (board[0][i] == opponent) tstrat++;
              else if (board[0][i] == me) tstrat--;
            for (int i = 0; i < 8; i++)
              if (board[7][i] == opponent) tstrat++;
              else if (board[7][i] == me) tstrat--;
            return tstrat;
          }
        };
    Strategy corners =
        new Strategy() {
          public int strategy(TKind me, TKind opponent, TKind[][] board) {
            int tstrat = 0;
            for (int i = 0; i < 8; i++)
              if (board[i][0] == opponent) tstrat++;
              else if (board[i][0] == me) tstrat--;
            for (int i = 0; i < 8; i++)
              if (board[i][7] == opponent) tstrat++;
              else if (board[i][7] == me) tstrat--;
            for (int i = 0; i < 8; i++)
              if (board[0][i] == opponent) tstrat++;
              else if (board[0][i] == me) tstrat--;
            for (int i = 0; i < 8; i++)
              if (board[7][i] == opponent) tstrat++;
              else if (board[7][i] == me) tstrat--;
            if (board[0][0] == opponent) tstrat += 4;
			else if (board[0][0] == me) tstrat -= 4;
            if (board[0][7] == opponent) tstrat += 4;
			else if (board[0][7] == me) tstrat -= 4;
            if (board[7][0] == opponent) tstrat += 4;
			else if (board[7][0] == me) tstrat -= 4;
            if (board[7][7] == opponent) tstrat += 4;
			else if (board[7][7] == me) tstrat -= 4;
            return tstrat;
          }
        };
    Strategy none =
        new Strategy() {
          public int strategy(TKind me, TKind opponent, TKind[][] board) {
            return 0;
          }
        };
    Move move = new Move();
    ArrayList<Integer> table = new ArrayList<>();
    int n = 1000;
    int win = 0;
    int drawn = 0;
    for (int i = 0; i < n; i++) {
      board.clear();
      while (board.userCanMove(TKind.black) || board.userCanMove(TKind.white)) {
        if (board.findMove(TKind.black, level_black, move, corners)) board.move(move, TKind.black);
        if (board.findMove(TKind.white, level_white, move, normal)) board.move(move, TKind.white);
      }
      System.out.print(
          "Iteration#="
              + i
              + " Result:"
              + board.getCounter(TKind.black)
              + "-"
              + board.getCounter(TKind.white));
      if (board.getCounter(TKind.black) > board.getCounter(TKind.white)) win++;
      if (board.getCounter(TKind.black) == board.getCounter(TKind.white)) drawn++;
      System.out.println("  Win#=" + win + "  Drawn#=" + drawn);
    }
    System.out.println("Total#=" + n + "  Win#=" + win + "  Drawn#=" + drawn);
  }
 
  public static double sd(ArrayList<Integer> table) {
 
    double mean = mean(table);
    double temp = 0;
 
    for (int i = 0; i < table.size(); i++) {
      temp += Math.pow(table.get(i) - mean, 2);
    }
 
    return Math.sqrt(temp / (double) (table.size()));
  }
 
  public static double mean(ArrayList<Integer> table) {
    int total = 0;
 
    for (int i = 0; i < table.size(); i++) {
      total += table.get(i);
    }
    return (double) total / (double) table.size();
  }
}
 
Share Share on Facebook Share on Twitter Bookmark on Reddit Share via mail
Privacy Policy Creative Commons Attribution-Share Alike Trovami