关于 悔棋 的方法

Sally_simple 2014-01-08 08:39:28
之前写了一个面试题,是井字棋游戏。和五子棋差不多。是用OOD 设计的。
大致实现就是人和电脑下棋,电脑不用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

}

}
}




再次感谢大家~~
...全文
320 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
harrisonkao 2014-01-09
  • 打赏
  • 举报
回复
居然不是用坐标来做啊,看的眼花缭乱
linuxca 2014-01-08
  • 打赏
  • 举报
回复
Sally_simple 2014-01-08
  • 打赏
  • 举报
回复
引用 4 楼 u013413091 的回复:
可以把棋子涌动给记录下来啊,比如说写个ChessMove类,包含信息有移动棋子的前后位置,前后状态等,这样,开个对象数组不久记录下来了么,到时候想悔棋就恢复就行,而且井字棋的话是一个一个下棋,比移动棋子还简单,
其实想能想出来,就是实现的时候,写不出来~哎~你能大概实现以下嘛,Thanks
Sally_simple 2014-01-08
  • 打赏
  • 举报
回复
引用 3 楼 Gaowen_HAN 的回复:
楼主的头像真好看,给分吗~
哈哈~谢啦
SparkFour 2014-01-08
  • 打赏
  • 举报
回复
可以把棋子涌动给记录下来啊,比如说写个ChessMove类,包含信息有移动棋子的前后位置,前后状态等,这样,开个对象数组不久记录下来了么,到时候想悔棋就恢复就行,而且井字棋的话是一个一个下棋,比移动棋子还简单,
fearlessMore 2014-01-08
  • 打赏
  • 举报
回复
楼主的头像真好看,给分吗~
chengxu2011 2014-01-08
  • 打赏
  • 举报
回复
可以用备忘录模式么?
  • 打赏
  • 举报
回复

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧