井字游戏 人机对战 java实现
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;
}
}
最新文章
- 使用Spring AsyncRestTemplate对象进行异步请求调用
- EntityFramework 开始小试
- 加密解密(10)常见HASH算法:MD5(128bit),SHA1(160bit)
- poj 1192最优连通子集(简单树形dp)
- vim技巧和坑
- 【集训笔记】母函数【母函数模板】【HDOJ1028【HDOJ1085
- ACdream 之ACfun 题解
- USB OTG简要
- Java解析JSON文件的方法 (二)
- java 中 针对数组进行的工具类
- maya cmds pymel 选择 uv area(uv 面积) 为0 的面
- Echarts 报错:Uncaught Error: [MODULE_MISS]";echarts/config"; is not exists!
- 嵌入式开发 迅为4418开发板Qt移植移动4G模块第一部分
- 现阶段如何开始使用v-ray for unrealengine
- 点击倒计时60S获取验证码
- python中的re模块——正则表达式
- js计算器---转
- Hyper-V虚拟机上安装一个图形界面的Linux系统
- 《DSP using MATLAB》Problem 6.7
- mysql日期时间函数使用总结