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

📄 robot.java

📁 很不错的一一个五子棋
💻 JAVA
字号:
import java.util.Random;

/**
 * 机器人类: Robot.class
 * @author obtuseSword
 * @date 2008-05-29
 */


public class Robot{

	/************* 公开方法:开始 ***************/
	
	// 构造函数(将棋盘“连接”到机器人上)
	public Robot(ChessBoard cb){
		chessBoard = cb;
	}

	// 机器人落子方法
	public void go(){
		// 如果游戏未开始或已结束,则保持不动
		if( chessBoard.judge() != ChessBoard.GameState.PLAYING )
			return;
		// 如果不是轮到机器人下,则保持不动
		if( chessBoard.getPlayingPlayer() == ChessBoard.PlayerType.WHITE_PLAYER )
			return;
		// 智能算法获取落子点
		double maxScore = - 1E99;
		int max_row = 0, max_col = 0;
		for(int i = 0; i < chessBoard.boardHeight; i++){
			for(int j = 0; j < chessBoard.boardWidth; j++){
				if( chessBoard.getChess(i, j) == ChessBoard.ChessType.NONE_CHESS ){
					double score = calScore(i, j);
					if( score > maxScore ){
						maxScore = score;
						max_row = i;
						max_col = j;
					}
				}
			}
		}
		// 机器人落子
		chessBoard.go(max_row, max_col);
	}

	/**----------- 公开方法:结束 -------------**/


	/************* 私有方法:开始 ***************/

	// 落子在棋盘某处的进攻型得分
	private double attack(int row,int col){
		double s1, s2, s3, s4;
		int len, space, k, k1, countercheck;
		// 横向得分
		len = space = k = k1 = 1;
		countercheck = 0;
		while( col-k >= 0 ){
			if( chessBoard.getChess(row, col-k) == ChessBoard.ChessType.BLACK_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( col-k1 >= 0 ){
			if( chessBoard.getChess(row, col-k1) != ChessBoard.ChessType.WHITE_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		k = k1 = 1;
		while( col+k < chessBoard.boardWidth ){
			if( chessBoard.getChess(row, col+k) == ChessBoard.ChessType.BLACK_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( col+k1 < chessBoard.boardWidth ){
			if( chessBoard.getChess(row, col+k1) != ChessBoard.ChessType.WHITE_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		if( space < 5 )
			s1 = 1.0;
		else{
			s1 = Math.pow(2*len, 2*len);
			for(int r = 0; r < countercheck; r++){
				if( len < 5 )
					s1 = Math.sqrt(s1);
			}
			if( len > 4 )
				s1 *= s1;
		}
		// 纵向得分
		len = space = k = k1 = 1;
		countercheck = 0;
		while( row-k >= 0 ){
			if( chessBoard.getChess(row-k, col) == ChessBoard.ChessType.BLACK_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row-k1 >= 0 ){
			if( chessBoard.getChess(row-k1, col) != ChessBoard.ChessType.WHITE_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		k = k1 = 1;
		while( row+k < chessBoard.boardHeight ){
			if( chessBoard.getChess(row+k, col) == ChessBoard.ChessType.BLACK_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row+k1 < chessBoard.boardHeight ){
			if( chessBoard.getChess(row+k1, col) != ChessBoard.ChessType.WHITE_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		if( space < 5 )
			s2 = 1.0;
		else{
			s2 = Math.pow(2*len, 2*len);
			for(int r = 0; r < countercheck; r++){
				if( len < 5 )
					s2 = Math.sqrt(s2);
			}
			if( len > 4 )
				s2 *= s2;
		}
		// 左斜向得分
		len = space = k = k1 = 1;
		countercheck = 0;
		while( row+k < chessBoard.boardHeight && col-k >= 0 ){
			if( chessBoard.getChess(row+k, col-k) == ChessBoard.ChessType.BLACK_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row+k1 < chessBoard.boardHeight && col-k1 >= 0 ){
			if( chessBoard.getChess(row+k1, col-k1) != ChessBoard.ChessType.WHITE_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		k = k1 = 1;
		while( row-k >=0 && col+k < chessBoard.boardWidth ){
			if( chessBoard.getChess(row-k, col+k) == ChessBoard.ChessType.BLACK_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row-k1 >=0 && col+k1 < chessBoard.boardWidth ){
			if( chessBoard.getChess(row-k1, col+k1) != ChessBoard.ChessType.WHITE_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		if( space < 5 )
			s3 = 1.0;
		else{
			s3 = Math.pow(2*len, 2*len);
			for(int r = 0; r < countercheck; r++){
				if( len < 5 )
					s3 = Math.sqrt(s3);
			}
			if( len > 4 )
				s3 *= s3;
		}
		// 右斜向得分
		len = space = k = k1 = 1;
		countercheck = 0;
		while( row+k < chessBoard.boardHeight && col+k < chessBoard.boardWidth ){
			if( chessBoard.getChess(row+k, col+k) == ChessBoard.ChessType.BLACK_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row+k1 < chessBoard.boardHeight && col+k1 < chessBoard.boardWidth ){
			if( chessBoard.getChess(row+k1, col+k1) != ChessBoard.ChessType.WHITE_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		k = k1 = 1;
		while( row-k >=0 && col-k >= 0 ){
			if( chessBoard.getChess(row-k, col-k) == ChessBoard.ChessType.BLACK_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row-k1 >=0 && col-k1 >= 0 ){
			if( chessBoard.getChess(row-k1, col-k1) != ChessBoard.ChessType.WHITE_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		if( space < 5 )
			s4 = 1.0;
		else{
			s4 = Math.pow(2*len, 2*len);
			for(int r = 0; r < countercheck; r++){
				if( len < 5 )
					s4 = Math.sqrt(s4);
			}
			if( len > 4 )
				s4 *= s4;
		}
		// 返回总得分
		return Math.sqrt(Math.sqrt(s1*s2*s3*s4));
	}

	// 落子在棋盘某处的防守型得分
	private double defend(int row,int col){
		double s1, s2, s3, s4;
		int len, space, k, k1, countercheck;
		// 横向得分
		len = space = k = k1 = 1;
		countercheck = 0;
		while( col-k >= 0 ){
			if( chessBoard.getChess(row, col-k) == ChessBoard.ChessType.WHITE_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( col-k1 >= 0 ){
			if( chessBoard.getChess(row, col-k1) != ChessBoard.ChessType.BLACK_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		k = k1 = 1;
		while( col+k < chessBoard.boardWidth ){
			if( chessBoard.getChess(row, col+k) == ChessBoard.ChessType.WHITE_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( col+k1 < chessBoard.boardWidth ){
			if( chessBoard.getChess(row, col+k1) != ChessBoard.ChessType.BLACK_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		if( space < 5 )
			s1 = 1.0;
		else{
			s1 = Math.pow(2*len, 2*len);
			for(int r = 0; r < countercheck; r++){
				if( len < 5 )
					s1 = Math.sqrt(s1);
			}
			if( len > 4 )
				s1 = Math.exp(s1);
		}
		// 纵向得分
		len = space = k = k1 = 1;
		countercheck = 0;
		while( row-k >= 0 ){
			if( chessBoard.getChess(row-k, col) == ChessBoard.ChessType.WHITE_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row-k1 >= 0 ){
			if( chessBoard.getChess(row-k1, col) != ChessBoard.ChessType.BLACK_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		k = k1 = 1;
		while( row+k < chessBoard.boardHeight ){
			if( chessBoard.getChess(row+k, col) == ChessBoard.ChessType.WHITE_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row+k1 < chessBoard.boardHeight ){
			if( chessBoard.getChess(row+k1, col) != ChessBoard.ChessType.BLACK_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		if( space < 5 )
			s2 = 1.0;
		else{
			s2 = Math.pow(2*len, 2*len);
			for(int r = 0; r < countercheck; r++){
				if( len < 5 )
					s2 = Math.sqrt(s2);
			}
			if( len > 4 )
				s2 = Math.exp(s2);
		}
		// 左斜向得分
		len = space = k = k1 = 1;
		countercheck = 0;
		while( row+k < chessBoard.boardHeight && col-k >= 0 ){
			if( chessBoard.getChess(row+k, col-k) == ChessBoard.ChessType.WHITE_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row+k1 < chessBoard.boardHeight && col-k1 >= 0 ){
			if( chessBoard.getChess(row+k1, col-k1) != ChessBoard.ChessType.BLACK_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		k = k1 = 1;
		while( row-k >=0 && col+k < chessBoard.boardWidth ){
			if( chessBoard.getChess(row-k, col+k) == ChessBoard.ChessType.WHITE_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row-k1 >=0 && col+k1 < chessBoard.boardWidth ){
			if( chessBoard.getChess(row-k1, col+k1) != ChessBoard.ChessType.BLACK_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		if( space < 5 )
			s3 = 1.0;
		else{
			s3 = Math.pow(2*len, 2*len);
			for(int r = 0; r < countercheck; r++){
				if( len < 5 )
					s3 = Math.sqrt(s3);
			}
			if( len > 4 )
				s3 = Math.exp(s3);
		}
		// 右斜向得分
		len = space = k = k1 = 1;
		countercheck = 0;
		while( row+k < chessBoard.boardHeight && col+k < chessBoard.boardWidth ){
			if( chessBoard.getChess(row+k, col+k) == ChessBoard.ChessType.WHITE_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row+k1 < chessBoard.boardHeight && col+k1 < chessBoard.boardWidth ){
			if( chessBoard.getChess(row+k1, col+k1) != ChessBoard.ChessType.BLACK_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		k = k1 = 1;
		while( row-k >=0 && col-k >= 0 ){
			if( chessBoard.getChess(row-k, col-k) == ChessBoard.ChessType.WHITE_CHESS ){
				k++; len++;
			}else
				break;
		}
		while( row-k1 >=0 && col-k1 >= 0 ){
			if( chessBoard.getChess(row-k1, col-k1) != ChessBoard.ChessType.BLACK_CHESS ){
				k1++; space++;
			}else
				break;
		}
		if( k1 == k )
			countercheck++;
		if( space < 5 )
			s4 = 1.0;
		else{
			s4 = Math.pow(2*len, 2*len);
			for(int r = 0; r < countercheck; r++){
				if( len < 5 )
					s4 = Math.sqrt(s4);
			}
			if( len > 4 )
				s4 = Math.exp(s4);
		}
		// 返回总得分
		return Math.sqrt(Math.sqrt(s1*s2*s3*s4));
	}

	// 计算落子在棋盘某处的得分
	private double calScore(int row,int col){
		Random random = new Random();
		double A = attack(row, col) + 3.14159 * random.nextDouble();
		double D = defend(row, col) + 3.14159 * random.nextDouble();
		return Math.sqrt(A * D);
	}
	
	/**----------- 私有方法:结束 -------------**/


	/************* 私有字段:开始 ***************/

	// 当前机器人所在的棋盘
	private ChessBoard chessBoard;
	
	/**----------- 私有字段:结束 -------------**/
	
}

⌨️ 快捷键说明

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