码迷,mamicode.com
首页 > 编程语言 > 详细

井字游戏 人机对战 java实现

时间:2018-12-30 13:58:02      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:this   sys   row   函数   cal   step   public   system.in   inpu   

package com.ecnu.Main;

/**
* 主函数触发游戏
*/
public class MainApplication {
public static void main(String[] args){
TicTacToeGame ticTacToeGame = new TicTacToeGame();
ticTacToeGame.start();
}
}

//TicTacToeGame 方法类

import java.util.Scanner;

public class TicTacToeGame {

private int stepCount = 0;
private int[][] gameBoard;
private Scanner scanner = new Scanner(System.in);
private final int humanFlag = 1;
private final int computerFlag = -1;
private final int emptyFlag = 0;

public void start() {
initGameBoard();
System.out.println("Game Board is ready!!! Game start!!!");
computerThink();

}

private void initGameBoard() {
this.gameBoard = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gameBoard[i][j] = emptyFlag;
}
}

showGameBoard();

}

private void computerThink() {
System.out.println("Computer:");
Move move = calculateTheBestMove();
int x = move.getX();
int y = move.getY();
gameBoard[y][x] = computerFlag;
stepCount++;
showGameBoard();

if(!isGameOver(x, y)){
humanAction();
}
}

private Move calculateTheBestMove(){
Move move = new Move();
Integer bestWeight = null;
Integer bestX = null;
Integer bestY = null;


for(int y=0; y<3; y++){
for(int x=0; x<3; x++){
if(gameBoard[y][x] == 0){
gameBoard[y][x] = computerFlag;
stepCount ++;
if(isWin(x,y)){
stepCount --;
move.setX(x);
move.setY(y);
move.setWeight(1000);
gameBoard[y][x] = emptyFlag;

return move;
}else if(isTie()){
stepCount --;
move.setX(x);
move.setY(y);
move.setWeight(0);
gameBoard[y][x] = emptyFlag;

return move;
}else{
Move worstMove = calculateTheWorstMove();
stepCount --;
gameBoard[y][x] = emptyFlag;
if(bestWeight == null || worstMove.getWeight()>= bestWeight){
bestX = x;
bestY = y;
bestWeight =worstMove.getWeight();
}
}

 

}
}
}

move.setWeight(bestWeight);
move.setX(bestX);
move.setY(bestY);
return move;
}

private Move calculateTheWorstMove(){
Move move = new Move();
Integer bestWeight = null;
Integer bestX = null;
Integer bestY = null;

for(int y=0; y<3; y++){
for(int x=0; x<3; x++){
if(gameBoard[y][x] == 0){
gameBoard[y][x] = humanFlag;
stepCount ++;
if(isWin(x,y)){
stepCount --;
move.setX(x);
move.setY(y);
move.setWeight(-1000);
gameBoard[y][x] = emptyFlag;
return move;
}else if(isTie()){
stepCount --;
move.setX(x);
move.setY(y);
move.setWeight(0);
gameBoard[y][x] = emptyFlag;
return move;
}else{
Move bestMove = calculateTheBestMove();
stepCount --;
gameBoard[y][x] = emptyFlag;
if(bestX == null || bestMove.getWeight() < bestWeight){
bestX = x;
bestY = y;
bestWeight = bestMove.getWeight();
}
}


}
}
}

move.setWeight(bestWeight);
move.setX(bestX);
move.setY(bestY);

return move;
}

private void humanAction() {
System.out.println("It is your turn now!");

boolean isHumanTurn = true;
int x = 0;
int y = 0;
while(isHumanTurn){
System.out.println("Please input the row number (1~3):");
y = scanner.nextInt() - 1;
System.out.println("Please input the column number (1~3):");
x = scanner.nextInt() - 1;

if (isInputValid(x, y)){
isHumanTurn = false;
gameBoard[y][x] = humanFlag;

}else{
System.out.println(String.format("You cannot place on row %d, column %d", y + 1, x + 1));
}

}
stepCount++;
showGameBoard();

if(!isGameOver(x, y)){

computerThink();
}

}


private boolean isWin(int x, int y) {

return (Math.abs(gameBoard[y][0] + gameBoard[y][1] + gameBoard[y][2]) == 3) ||
(Math.abs(gameBoard[0][x] + gameBoard[1][x] + gameBoard[2][x]) == 3) ||
(Math.abs(gameBoard[0][0] + gameBoard[1][1] + gameBoard[2][2]) == 3) ||
(Math.abs(gameBoard[2][0] + gameBoard[1][1] + gameBoard[0][2]) == 3);
}

private boolean isTie() {
return stepCount >= 9;
}

private boolean isInputValid(int x, int y){
return x>=0 && x<3 && y>=0 && y<3 && gameBoard[y][x] == 0;
}

private boolean isGameOver(int x, int y){
boolean isGameOver = true;
if(isWin(x, y)){
if(gameBoard[y][x] == -1){
System.out.println("Computer Win!!!!");

}else{
System.out.println("You Win!!!!");
}
}else if(isTie()){
System.out.println("Tie!!!");
}else{
isGameOver = false;
}

return isGameOver;
}


private void showGameBoard(){
for(int y=0; y<3; y++){
for(int x=0; x<3; x++){
if(gameBoard[y][x] == -1){
System.out.print("2 ");
}else {
System.out.print(gameBoard[y][x] + " ");
}
}
System.out.println();
}

System.out.println();

}

}

class Move{
private int x;
private int y;
private int weight;

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}
}

井字游戏 人机对战 java实现

标签:this   sys   row   函数   cal   step   public   system.in   inpu   

原文地址:https://www.cnblogs.com/cdlyy/p/10199120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!