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实现的更多相关文章

  1. java 五子棋之人机对战思路详解

    最近做了五子棋,记录下自己完成五子棋的人机对战的思路. 首先,思路是这样的:每当人手动下一颗棋子(黑子)的时候,应当遍历它周围棋子的情况,并赋予周围棋子一定的权值,当在机器要下棋子(白子)守护之前,会 ...

  2. HTML5+JS 《五子飞》游戏实现(八)人机对战

    要想实现人机对战,就必须让电脑自动下棋,而且要知道自动去查找对方的棋子,看看有没有可以挑一对的,有没有可以夹一个的,这样下起来才有意思. 当电脑用户下完棋后,电脑应立即搜索用户的棋子,然后如果没有被吃 ...

  3. 完全自制的五子棋人机对战游戏(VC++实现)

    五子棋工作文档 1说明: 这个程序在创建初期的时候是有一个写的比较乱的文档的,但是很可惜回学校的时候没有带回来……所以现在赶紧整理一下,不然再过一段时间就忘干净了. 最初这个程序是受老同学所托做的,一 ...

  4. 介绍一款Android小游戏--交互式人机对战五子棋

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6589025 学习Android系统开发之余,编 ...

  5. Python:游戏:五子棋之人机对战

    本文代码基于 python3.6 和 pygame1.9.4. 五子棋比起我之前写的几款游戏来说,难度提高了不少.如果是人与人对战,那么,电脑只需要判断是否赢了就可以.如果是人机对战,那你还得让电脑知 ...

  6. "人机"对战:电脑太简单了,我是射手 skr~skr~skr

    9月17日,2018 世界人工智能大会在上海拉开帷幕.在 SAIL 榜单入围项目中,我看到了小爱同学.小马智行.微软小冰.腾讯觅影等等,这不仅让我大开了眼界,也不禁让我感慨 AI 的发展神速.犹记得去 ...

  7. python3 井字棋 GUI - 人机对战、机器对战 (threading、tkinter库)

    python3 井字棋 GUI - 人机对战.机器对战 功能 GUI界面 人机对战(可选择机器先走) 机器对战(50局) 流程图 内核 棋盘 [0][1][2] [3][4][5] [6][7][8] ...

  8. 基于Qt Creator实现中国象棋人机对战, c++实现

    GitHub地址: https://github.com/daleyzou/wobuku 这是自己大一学完c++后,在课程实践中写过的一个程序,实现象棋人机对战的算法还是有点难的, 自己当时差不多也是 ...

  9. js实现五子棋人机对战源码

    indexhtml <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

随机推荐

  1. POJ1028 Web Navigation

    题目来源:http://poj.org/problem?id=1028 题目大意: 模拟实现一个浏览器的“前进”和“回退”功能.由一个forward stack和一个backward stack实现. ...

  2. 6、kvm克隆虚拟机

    kvm克隆有两种方法可以使用先暂停kvm虚拟机 virsh suspend privi-server 方法一:对虚拟机本身直接clone virt-clone -o privi-server -n p ...

  3. C. Glass Carving 正着做或者倒着做都可以

    http://codeforces.com/problemset/problem/527/C 这题总体思路就是,每画一条线,然后就找到x间距的最max值和y间距的最max值,相乘就是当前的ans 那么 ...

  4. [转]gbk和utf8的区别

    转自:百度经验 GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符仅能存储简体中文字符. UTF-8编码:它是一种全国家通过的一种编码,如果 ...

  5. linux增加/删除虚拟IP地址

    网卡上增加一个IP: ifconfig eth0:1 192.168.0.1 netmask 255.255.255.0 删除网卡的第二个IP地址: ip addr del 192.168.0.1 d ...

  6. InteliJ idea import project 找不到文件结构解决办法

    一.按下列步骤操作:       1. 关闭IDEA,        2.然后删除项目文件夹下的.idea文件夹       3.重新用IDEA工具打开项目: 二.import新项目之后,可能需要等1 ...

  7. 记录下laravel 5.2的auth/logout路由工作不正常的问题

  8. java基础知识——Java的定义,特点和技术平台

    (作者声明:对于Java编程语言,很多人只知道怎么用,却对其了解甚少.我也是其中一员.所以菜鸟的我,去查询了教科书以及大神的总结,主要参考了<Java核心技术>这本神作.现在分享给大家!) ...

  9. Today is the first day of the rest of your life.

    Today is the first day of the rest of your life. 今天是你余下人生的第一天.

  10. 【web】movie review——静态页面训练、css训练

    实现样式要求: image: banner.png: generaloverview.png: background.png: bannerbackground.png: rottenbig.png: ...