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

📄 othelloboard.java

📁 JCreator下开发
💻 JAVA
字号:
//棋盘类,实现电脑走棋和玩家落子
import java.io.*;
public class OthelloBoard
{
	private int[][] board;
	public OthelloBoard()//构造方法
	{
		board = new int[8][8];  //定义与棋盘上的位置相对应的二维矩阵
		initializeBoard();
	}
	private void initializeBoard()//初始化棋盘
	{
		for (int row = 0; row < 8; ++row)
			for (int col = 0; col < 8; ++col)
				board[row][col] = Cyberthello.EMPTY;
		board[3][3] = Cyberthello.WHITE;
		board[3][4] = Cyberthello.BLACK;
		board[4][3] = Cyberthello.BLACK;
		board[4][4] = Cyberthello.WHITE;
	}
	public OthelloBoard(OthelloBoard b)//拷贝构造
	{
		board = new int[8][8];
		for (int row = 0; row < 8; ++row)
			for (int col = 0; col < 8; ++col)
				board[row][col] = b.board[row][col];
	}
	public int getSquareContents(int row, int col)
	{
		return board[row][col];
	}
	public int getBlackScore()//黑子数
	{
		int score = 0;

		for (int row = 0; row < 8; ++row)
			for (int col = 0; col < 8; ++col)
				if (board[row][col] == Cyberthello.BLACK)
					++score;
		return score;
	}
	public int getWhiteScore()//白子数
	{
		int score = 0;

		for (int row = 0; row < 8; ++row)
			for (int col = 0; col < 8; ++col)
				if (board[row][col] == Cyberthello.WHITE)
					++score;
		return score;
	}
	public OthelloBoard tryMove(Move m, int moveColor)//虚拟落子,以便判断到底落在哪个位置才能吃到更多的子
	{
		OthelloBoard newBoard = new OthelloBoard(this);
		boolean okMove = newBoard.move(m, moveColor);
		if (!okMove)
			System.out.println("Invalid move in tryMove: " + m);
		return newBoard;
	}
	public boolean move(Move m, int moveColor)
	{
		return makeOrTestMove(m.row, m.col, moveColor, true);
	}
	public boolean isValidMove(Move m, int moveColor)
	{
		return makeOrTestMove(m.row, m.col, moveColor, false);
	}
	public boolean hasMove(int moveColor)//有路可走
	{
		for (int row = 0; row < 8; ++row)
			for (int col = 0; col < 8; ++col)
			{
				if (makeOrTestMove(row, col, moveColor, false))
					return true;
			}
		return false;
	}
	private boolean makeOrTestMove(int row, int col, int moveColor, boolean makeMove)//测试该位置是否可以落子
	{
		if (col < 0 || col >= 8 || row < 0 || row >= 8)
			return false;
		if (board[row][col] != Cyberthello.EMPTY)
			return false;
		boolean moveIsLegal = false;
		int oppColor = -moveColor;
		int r, c;
        //遍历八个方位,看看该位置到底能不能落子并且吃子
		if (row - 1 >= 0 && board[row - 1][col] == oppColor)//从位置col,row开始向上遍历
		{
			boolean found = false;
			boolean blankFound = false;
			for (r = row - 2; r >= 0 && !found && !blankFound; --r)
			{
				if (board[r][col] == moveColor)
					found = true;
				else if (board[r][col] == Cyberthello.EMPTY)
					blankFound = true;
			}
			if (found)//找到相同颜色的棋子
			{
				if (makeMove)//可以落子,把中间的棋子都吃掉
				{
					for (r = row - 1; board[r][col] == oppColor; --r)//把相反颜色的棋子都吃掉
						board[r][col] = moveColor;
					moveIsLegal = true;
				}
				else
					return true;
			}
		}
        //向下遍历,方法同上
		if (row + 1 < 8 && board[row + 1][col] == oppColor)
		{
			boolean found = false;
			boolean blankFound = false;

			for (r = row + 2; r < 8 && !found && !blankFound; ++r)
			{
				if (board[r][col] == moveColor)
					found = true;
				else if (board[r][col] == Cyberthello.EMPTY)
					blankFound = true;
			}
			if (found)
			{
				if (makeMove)
				{
					for (r = row + 1; board[r][col] == oppColor; ++r)
						board[r][col] = moveColor;
					moveIsLegal = true;
				}
				else
					return true;
			}
		}
		//左
		if (col - 1 >= 0 && board[row][col - 1] == oppColor)
		{
			boolean found = false;
			boolean blankFound = false;
			for (c = col - 2; c >= 0 && !found && !blankFound; --c)
			{
				if (board[row][c] == moveColor)
					found = true;
				else if (board[row][c] == Cyberthello.EMPTY)
					blankFound = true;
			}
	        if (found)
			{
				if (makeMove)
				{
					for (c = col - 1; board[row][c] == oppColor; --c)
						board[row][c] = moveColor;
					moveIsLegal = true;
				}
				else
					return true;
			}
		}
		// 右
		if (col + 1 < 8 && board[row][col + 1] == oppColor)
		{
			boolean found = false;
			boolean blankFound = false;
			for (c = col + 2; c < 8 && !found && !blankFound; ++c)
			{
				if (board[row][c] == moveColor)
					found = true;
				else if (board[row][c] == Cyberthello.EMPTY)
					blankFound = true;
			}
			if (found)
			{
				if (makeMove)
				{
					for (c = col + 1; board[row][c] == oppColor; ++c)
						board[row][c] = moveColor;
					moveIsLegal = true;
				}
				else
					return true;
			}
		}
		//左上
		if (col - 1 >= 0 && row - 1 >= 0 && board[row - 1][col - 1] == oppColor)
		{
			boolean found = false;
			boolean blankFound = false;
			for (c = col - 2, r = row - 2; c >= 0 && r >= 0 && !found && !blankFound; --c, --r)
			{
				if (board[r][c] == moveColor)
					found = true;
				else if (board[r][c] == Cyberthello.EMPTY)
					blankFound = true;
			}
			if (found)
			{
				if (makeMove)
				{
					for (c = col - 1, r = row - 1; board[r][c] == oppColor; --c, --r)
						board[r][c] = moveColor;
					moveIsLegal = true;
				}
				else
					return true;
			}
		}
		// 右上
		if (col + 1 < 8 && row - 1 >= 0 && board[row - 1][col + 1] == oppColor)
		{
			boolean found = false;
			boolean blankFound = false;
			for (c = col + 2, r = row - 2; c < 8 && r >= 0 && !found && !blankFound; ++c, --r)
			{
				if (board[r][c] == moveColor)
					found = true;
				else if (board[r][c] == Cyberthello.EMPTY)
					blankFound = true;
			}
			if (found)
			{
				if (makeMove)
				{
					for (c = col + 1, r = row - 1; board[r][c] == oppColor; ++c, --r)
						board[r][c] = moveColor;
					moveIsLegal = true;
				}
				else
					return true;
			}
		}
		// 右下
		if (col + 1 < 8 && row + 1 < 8 && board[row + 1][col + 1] == oppColor)
		{
			boolean found = false;
			boolean blankFound = false;

			for (c = col + 2, r = row + 2; c < 8 && r < 8 && !found && !blankFound; ++c, ++r)
			{
				if (board[r][c] == moveColor)
					found = true;
				else if (board[r][c] == Cyberthello.EMPTY)
					blankFound = true;
			}
			if (found)
			{
				if (makeMove)
				{
					for (c = col + 1, r = row + 1; board[r][c] == oppColor; ++c, ++r)
						board[r][c] = moveColor;
					moveIsLegal = true;
				}
				else
					return true;
			}
		}
		// 左下
		if (col - 1 >= 0 && row + 1 < 8 && board[row + 1][col - 1] == oppColor)
		{
			boolean found = false;
			boolean blankFound = false;

			for (c = col - 2, r = row + 2; c >= 0 && r < 8 && !found && !blankFound; --c, ++r)
			{
				if (board[r][c] == moveColor)
					found = true;
				else if (board[r][c] == Cyberthello.EMPTY)
					blankFound = true;
			}
			if (found)
			{
				if (makeMove)
				{
					for (c = col - 1, r = row + 1; board[r][c] == oppColor; --c, ++r)
						board[r][c] = moveColor;
					moveIsLegal = true;
				}
				else
					return true;
			}
		}
        //全部遍历完之后,如果可以落子,那么在这个位置落子
		if (makeMove && moveIsLegal)
			board[row][col] = moveColor;
		return moveIsLegal;
	}
	public boolean blackHasMove()//黑子有路可走?
	{
		return hasMove(Cyberthello.BLACK);
	}
	public boolean whiteHasMove()//白子有路可走?
	{
		return hasMove(Cyberthello.WHITE);
	}
	public void set(int[] a)
	{
		if (a.length != 64)
			throw new RuntimeException("Pass OthelloBoard.set " +
						" an array of length 64");
		for (int row = 0; row < 8; ++row)
			for (int col = 0; col < 8; ++col)
				board[row][col] = a[row*8+col];
	}
}

⌨️ 快捷键说明

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