关于 悔棋 的方法
之前写了一个面试题,是井字棋游戏。和五子棋差不多。是用OOD 设计的。
大致实现就是人和电脑下棋,电脑不用AI算法实现,只是随机下棋,谁的棋子先连成三个,谁就赢了。
想实现一个方法时悔棋的方法,大概知道使用stack去实现,悔棋的时候出栈就好。可实现有点麻烦对我现在的水平。
希望大家不要见笑。
大家对我的代码和思想有一些建议和意见,比如还需要加一些什么功能会更完善,我也很乐意听取。谢谢
粘代码
棋盘类
玩家 父类
电脑 子类
人 子类
main
再次感谢大家~~
大致实现就是人和电脑下棋,电脑不用AI算法实现,只是随机下棋,谁的棋子先连成三个,谁就赢了。
想实现一个方法时悔棋的方法,大概知道使用stack去实现,悔棋的时候出栈就好。可实现有点麻烦对我现在的水平。
希望大家不要见笑。
大家对我的代码和思想有一些建议和意见,比如还需要加一些什么功能会更完善,我也很乐意听取。谢谢
粘代码
棋盘类
public class ChessBoard {
public static final int ROW=3; //constant row
public static final int COL =3; //constant column
public static final int board[][] = new int[ROW][COL]; // two-dimensional array as board
public static final int EMPTY = 0; //three cell
public static final int CIRCLE = 1;
public static final int CROSS = 2;
public void initGame(){ //initialize the board
for(int i = 0; i <ROW; i ++){
for(int j = 0; j < COL; j++){
board[i][j] = EMPTY;
}
}
}
public void printBoard(){ //print board
System.out.println("---------------------------");
for(int i = 0; i < ROW; i ++){
for(int j = 0; j < COL; j ++){
if(board[i][j] == 0){
System.out.print("___|");
}
else if(board[i][j] == CIRCLE){
System.out.print(" O |");
}
else
System.out.print(" X |");
}
System.out.println("\t");
}
System.out.println("\n");
}
public boolean isDraw(){ //if all the cell are not empty, the status is Draw
for(int i = 0; i < ROW; i ++){
for(int j = 0; j < COL; j ++){
if(board[i][j] == EMPTY){
return false;
}
}
}
return true;
}
public boolean isWin(int currentRow, int currentCol, int currentFlag){ //check if one player is win
if ((board[currentRow][0] == currentFlag // 3-in-the-row
&& board[currentRow][1] == currentFlag
&& board[currentRow][2] == currentFlag
|| board[0][currentCol] == currentFlag // 3-in-the-column
&& board[1][currentCol] == currentFlag
&& board[2][currentCol] == currentFlag
|| currentRow == currentCol // 3-in-the-diagonal
&& board[0][0] == currentFlag
&& board[1][1] == currentFlag
&& board[2][2] == currentFlag
|| currentRow + currentCol == 2 // 3-in-the-opposite-diagonal
&& board[0][2] == currentFlag
&& board[1][1] == currentFlag
&& board[2][0] == currentFlag) )
return true;
else
return false;
}
public void upadateBoard(int currentRow, int currentCol, int currentFlag){ //update the board
if(isDraw()&&!(isWin(currentRow, currentCol, currentFlag))){ //check if the status is Draw
printBoard();
System.out.println("No Winner, DRAW ! ");
System.exit(0);
}
else if(isWin(currentRow, currentCol, currentFlag)){ //check if the status is win
if (currentFlag ==1){ //human win
printBoard();
System.out.println("You Win ! ");
System.exit(0);
}
else
{ //computer win
printBoard();
System.out.println("Computer Win ! ");
System.exit(0);
}
}
else{ //otherwise still play
board[currentRow][currentCol] = currentFlag;
}
Controller.currentFlag = (currentFlag ==1)? 2:1; //judge what is the next player
}
}
玩家 父类
public abstract class Player { //super class Player
public abstract void play() throws Exception;
public abstract int getRow(); //get the player's current row
public abstract int getCol(); //get the palyer's current column
}电脑 子类
import java.util.Random;
public class ComputerPlayer extends Player { //subclass ComputerPlayer
private Random rand; //random pick up the row and column
private int Crow; //current row
private int Ccol; //current column
public ComputerPlayer(){ //constructor
super();
}
public void play() { //play
System.out.println("Computer auto play ");
rand = new Random();
while(true){
Crow = rand.nextInt(ChessBoard.board.length); //row range(0-2);
Ccol = rand.nextInt(ChessBoard.board.length); //column range(0-2)
if(ChessBoard.board[Crow][Ccol] == ChessBoard.EMPTY){ //check if the cell is empty
ChessBoard.board[Crow][Ccol] = ChessBoard.CROSS;
break;
}
//otherwise still play
}
}
public int getRow(){ //get the current Row
return Crow;
}
public int getCol(){ //get the current Column
return Ccol;
}
}
人 子类
import java.util.*;
public class HumanPlayer extends Player { //subclass HumanPlayer
private int Hrow; //current row
private int Hcol; //current column
private Scanner scanner; //input the number of row and column
public HumanPlayer(){ //constructor
super();
}
public void play() throws Exception{ //status play
while(true){
System.out.println("Please enter the row[1-3], col[1-3]");
scanner = new Scanner(System.in);
try{
Hrow = scanner.nextInt() -1;
Hcol = scanner.nextInt() -1;
if(Hrow >=ChessBoard.ROW||Hrow <0||Hcol >=ChessBoard.COL||Hcol <0){ //input number out of bound
throw new Exception("Input out of bound, please try again");
}
else if(ChessBoard.board[Hrow][Hcol] ==ChessBoard.EMPTY){ //check if the cell is empty
ChessBoard.board[Hrow][Hcol] = ChessBoard.CIRCLE;
break;
}
else
{ //check if the chessboard has occupied
System.out.println("The ChessBoard has occupied, Please try again");
}
}catch (InputMismatchException e){ //the input must a number
scanner.next();
System.out.println("Input is invalid, please enter digital only");
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}
public int getRow(){ //get the current row
return Hrow;
}
public int getCol(){ //get the current column
return Hcol;
}
}
main
import java.util.*;
public class Controller { //control the game
public static int currentFlag; //current flag, 1 represents human player, 2 represents computer player
public static int row; //current row
public static int col; //current column
public static Random readFlag; //pick up which player is first player
public static void main(String [] args) throws Exception{
ChessBoard pad = new ChessBoard(); //instance the chessBoard
Player human = new HumanPlayer(); //instance the human player
Player computer = new ComputerPlayer(); //instance the computer player
System.out.println("Welcome to the Tic-Tac-Toe Game. Good Luck ! ");
pad.printBoard(); //print the board
pad.initGame(); //start the game
readFlag = new Random(); //pick up the order of the player, it is random
currentFlag = readFlag.nextInt(2)+1;
while(!(pad.isDraw()&& !(pad.isWin(row, col, currentFlag)))){ //check if the status is draw, or isWin, or still play
if(currentFlag ==1){ // human player
human.play();
row = human.getRow();
col = human.getCol();
}
else if(currentFlag ==2){ //computer player
computer.play();
row = computer.getRow();
col = computer.getCol();
}
pad.upadateBoard(row, col, currentFlag); //update the board
pad.printBoard(); //print the chessboard
}
}
}
再次感谢大家~~

...全文
相关推荐
微信小程序五子棋游戏的悔棋实现方法【附demo源码下载】
`drawChessboard()` 和 `drawAll()` 方法分别用于清空棋盘和重新绘制所有棋子。
【原创】Java中国象棋(有悔棋功能)
在Java中,我们可以创建多个类来表示不同的棋子,每个类包含相应的移动和攻击方法。悔棋功能的实现是这个项目的关键部分。这通常通过记录每一步棋的状态来完成。
MFC 五子棋 悔棋 人机对弈
CAIPlayer类可以有一个方法,如MakeMove(),根据评估结果生成AI的下一步。此外,UI设计也是关键。
java五子棋 可悔棋
变换背景颜色是通过改变GUI组件的背景色来实现的,Java提供了丰富的色彩类和方法来处理颜色的变化。玩家可以选择不同的颜色,程序会相应地更新界面的背景。
java 划线棋 有计时 悔棋等功能
棋盘类应该包含以下方法:1. 初始化棋盘:设置所有位置为空位。2. 获取当前位置的状态:检查给定位置是否已被占据或为空。3.


Java SE
