⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample.java

📁 IBM othello比赛平台
💻 JAVA
字号:
/**
 *      A sample code for the first IBM China Research Lab Programming Contest
 *             
 *  This code segment implement the interface of  com.ibm.crl.contest.AbstractPlayer.
 *  It gives the most simple action in the Othello game, which:
 *  1.  Print basic informations at the beginning of the game.
 *  2.  For each turn, it will print the current chessboard, pause for 1 seconds, and select a
 *  random position.
 */
package samples;

import java.awt.Point;
import java.util.Hashtable;
import java.util.Random;

import com.ibm.crl.contest.AbstractPlayer;
import com.ibm.crl.contest.ChessContext;

/**
 * @author Wang Rui IBM China Research Lab E-Mail: wangrcrl@cn.ibm.com
 */
public class Sample extends AbstractPlayer {
	java.util.Random random = new Random();

	ChessContext chessContext;

	public int init(ChessContext chessContext, byte[][] chessboard,
			int timeLimit, int memoryLimit, boolean color) {
		this.chessContext = chessContext;
		System.out.println("The chessboard size is ("
				+ chessContext.getChessboardWidth() + ","
				+ chessContext.getChessboardHeight() + "). I'm the "
				+ (color ? "black" : "white") + " side.");
		printChessboard(chessboard, null);
		return 0;
	}

	/**
	 * Print chessboard
	 * 
	 * @param cb
	 * @param availables
	 */
	private void printChessboard(byte[][] cb, Point[] availables) {
		java.util.Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>();
		if (availables != null) {
			for (int i = 0; i < availables.length; i++) {
				Point point = availables[i];
				hashtable.put(point.x + "_" + point.y, i);
			}
		}
		for (int y = 0; y < chessContext.getChessboardHeight(); y++) {
			for (int x = 0; x < chessContext.getChessboardWidth(); x++) {
				byte b = cb[x][y];
				String s;
				if (hashtable.containsKey(x + "_" + y)) {
					s = String.format("%-2d", hashtable.get(x + "_" + y));
				} else {
					switch (b) {
					case AVAILABLE:
						s = ". ";
						break;
					case BLACK:
						s = "b ";
						break;
					case WHITE:
						s = "w ";
						break;
					default:
						s = "X ";
					}
				}
				System.out.print(s);
			}
			System.out.println();
		}
	}

	public int myTurn(byte[][] chessboard, Point[] availables, int curStep,
			Point lastMove) {
		System.out
				.println("My turn, available positions: " + availables.length);
		if (availables.length == 0)
			return -1;
		printChessboard(chessboard, availables);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		int n = random.nextInt(availables.length);
		Point point = availables[n];
		System.out.println("take " + point.x + "," + point.y + " (" + n + ")");
		return n;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -