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 井字棋 人机_井字游戏 人机对战 java实现相关推荐

  1. java 井字棋 人机_一个井字棋tictactoe游戏的java实现 | Soo Smart!

    这是一个井字棋游戏的java实现.摘录于stackoverflow. 游戏规则很简单,只要一方棋子在水平线,垂直线或者对角线任意一条线上排列成功即为获胜. 作者原先的代码存在着一些问题: 代码如下: ...

  2. python井字棋游戏开发(人人对战,人机对战,包含源码,逻辑思维流程图)

    需求分析 井字棋是比较便捷休闲娱乐的一种迷你棋,玩法比较简单,只需要一个九宫格棋盘就可以实现两人对战,规则为谁先连成三个棋子的一条线即可获胜.本游戏,需要满足两个主要功能:1.能实现玩家对战:2.能实 ...

  3. python井字棋。分与人机和朋友下的那种

    python井字棋快来看看孩子的头发 怎么用python做井字棋游戏,废话不多说上代码! 相关说明 怎么用python做井字棋游戏,废话不多说上代码! 觉得有帮助送我上去!!!!!!!!!!!!!!! ...

  4. java井字棋ai_JavaScript实现一个带AI的井字棋游戏源码

    JavaScript实现一个带AI的井字棋游戏源码 发布时间:2020-09-05 00:56:12 来源:脚本之家 阅读:100 作者:小楼夜听雨QAQ 最近有一门课结束了,需要做一个井字棋的游戏, ...

  5. java井字棋编程的收获_Java 井字棋小结

    1.井字棋获胜的四种情况 横行全为同一符号 竖行全为同一符号 斜对角线为同一符号 反对角线为同一符号 2.编程思路 构建数组(二维)读入数据 分四个部分,分别判断横行,竖行,对角线,反对角线是否满足条 ...

  6. java井字棋ai_简单的井字棋 AI DEMO | Minimax 算法

    在"类与对象"实训课上,有一道附加题让我们用 OOP 做一个的井字棋模拟程序,要求中电脑是随机落子的,这样显然不是很优雅.回忆起以前学的对抗搜索(这里叫 MaxMin 算法),我继 ...

  7. java 井字棋 人机_井字棋(人机对战版)

    1 #include 2 #include 3 4 const int ROW = 3;5 const int COL = 3;6 intchessboard[ROW][COL];7 intscore ...

  8. C++实现的基于α-β剪枝算法的井字棋游戏

    "井字棋"游戏(又叫"三子棋"),是一款十分经典的益智小游戏,操作简单,娱乐性强.两个玩家,一个打圈(O),一个打叉(X),轮流在3乘3的格上打自己的符号,最先 ...

  9. [文档和源码分享]C++实现的基于α-β剪枝算法的井字棋游戏

    "井字棋"游戏(又叫"三子棋"),是一款十分经典的益智小游戏,操作简单,娱乐性强.两个玩家,一个打圈(O),一个打叉(X),轮流在3乘3的格上打自己的符号,最先 ...

最新文章

  1. TypeError: cannot concatenate ‘str‘ and ‘list‘ objects
  2. SAP库存管理预留功能评测
  3. 是否应该饮用酵素和自制的葡萄酒
  4. bzoj4006 [JLOI2015]管道连接
  5. CF 554B 找相同行
  6. 服务器邮件删除了怎么恢复,Exchange邮件误删除不用急 DPM轻松恢复
  7. MIPS汇编程序设计实验
  8. 未来简史--读书语句摘录及感悟
  9. Failure to find xxx:jar:0.0.1 in https://repo.maven.apache.org/maven2 was cached in the local re
  10. C语言怎样判断乘法越界,如何判断C语言算术运算的越界问题
  11. python 双精度浮点_Python双精度浮点数运算并分行显示操作示例
  12. python数据可视化库 动态的_Python数据可视化:Pandas库,要是一行代码就能完成...
  13. php基础教程推荐,php基础教程-绝对推荐
  14. Unity制作2D战棋小游戏
  15. DELL PowerEdge 远程开机
  16. 全球与中国市场聚乙烯醇缩丁醛(PVB)树脂发展规模分析与前景战略研究报告2022年版
  17. PTA题目 谁是赢家
  18. 什么是Microsoft Flow?
  19. C# 极品飞车9(改钱辅助) / nfsMWAssist
  20. asp编程工具_asp.net core 成为构建企业首选

热门文章

  1. 《那些年啊,那些事——一个程序员的奋斗史》转载1到7
  2. QuickStart系列:docker部署之Gitlab本地代码仓库
  3. May 18:PHP 输出语句
  4. C语言第三次博客作业---单层循环结构
  5. 一个极其高效的虚拟机内存冗余消除机制:UKSM
  6. 转载 Spark性能优化指南——基础篇
  7. Jquery 学习之基础一
  8. [转载]聊一聊人员培养
  9. php 安装xdebug扩展
  10. hdu 1159(最长公共子序列)