62,616
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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
}
}
}