首页 > > 详细

解析 InvalidMoveException Java编程、Java解析、解析Java

import java.util.Scanner;
@SuppressWarnings("serial")
class InvalidMoveException extends Exception {}
public class CLIReversi {
@SuppressWarnings("resource")
public static void main(String args[]) throws Exception {
Scanner scanner = new Scanner(System.in);

// Welcome message
System.out.println("Welcome to Reversi-Tiny!");

// Specify which player is human
String player = "";
do {
System.out.println("Do you want to play as "
+ "player 1 or player 2? [Enter 1 or 2]");
player = scanner.nextLine();
} while (!player.equals("1") !player.equals("2"));
String computer = (player.equals("1")) ? "2": "1";

// Specify who play first
Boolean human_first = null; // note: nullable boolean
do {
System.out.println("Do you want to play first? [Y/n]");
String line = "";
line = scanner.nextLine();
if (line.equalsIgnoreCase("yes")
|| line.equalsIgnoreCase("y")) {
human_first = true;
} else if (line.equalsIgnoreCase("no")
|| line.equalsIgnoreCase("n")) {
human_first = false;
}
} while (human_first == null);

// Init board
char[] board = new char[16];
for (int i = 0; i 16) return false;
char char_at_pos = board.charAt(move_in_int - 1);
if (char_at_pos != '0') return false;
else return true;
} catch (Exception e) {
throw new InvalidMoveException();
}
}

private static State getOptimalMove (
State curr_state,
char player) {
if (curr_state.isTerminal()) return null;

State[] successors = curr_state.getSuccessors(player);
if (successors.length == 0) return null;
else {
State opt_state = null;
int opt_val;
if (player == '1') opt_val = Integer.MIN_VALUE;
else opt_val = Integer.MAX_VALUE;
for (State s : curr_state.getSuccessors(player)) {
if (player == '1') {
int curr_val = 0; // default - harmless
curr_val = Minimax.run_with_pruning(s, '2');

if (curr_val > opt_val) {
opt_val = curr_val;
opt_state = new State(s.getBoard().toCharArray());
}
} else {
int curr_val = 0;
curr_val = Minimax.run_with_pruning(s, '1');

if (curr_val < opt_val) {
opt_val = curr_val;
opt_state = new State(s.getBoard().toCharArray());
}
}
}
return opt_state;
}
}

private static void printPrettyBoard(State s) {
String b = s.getBoard();
System.out.println("-----------------");
System.out.printf("| %s | %s | %s | %s |\n",
b.charAt(0),b.charAt(1),b.charAt(2),b.charAt(3));
System.out.println("-----------------");
System.out.printf("| %s | %s | %s | %s |\n",
b.charAt(4),b.charAt(5),b.charAt(6),b.charAt(7));
System.out.println("-----------------");
System.out.printf("| %s | %s | %s | %s |\n",
b.charAt(8),b.charAt(9),b.charAt(10),b.charAt(11));
System.out.println("-----------------");
System.out.printf("| %s | %s | %s | %s |\n",
b.charAt(12),b.charAt(13),b.charAt(14),b.charAt(15));
System.out.println("-----------------");
}
}
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!