井字游戏 人机对战 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;
}
}
井字游戏 人机对战 java实现的更多相关文章
- java 五子棋之人机对战思路详解
最近做了五子棋,记录下自己完成五子棋的人机对战的思路. 首先,思路是这样的:每当人手动下一颗棋子(黑子)的时候,应当遍历它周围棋子的情况,并赋予周围棋子一定的权值,当在机器要下棋子(白子)守护之前,会 ...
- HTML5+JS 《五子飞》游戏实现(八)人机对战
要想实现人机对战,就必须让电脑自动下棋,而且要知道自动去查找对方的棋子,看看有没有可以挑一对的,有没有可以夹一个的,这样下起来才有意思. 当电脑用户下完棋后,电脑应立即搜索用户的棋子,然后如果没有被吃 ...
- 完全自制的五子棋人机对战游戏(VC++实现)
五子棋工作文档 1说明: 这个程序在创建初期的时候是有一个写的比较乱的文档的,但是很可惜回学校的时候没有带回来……所以现在赶紧整理一下,不然再过一段时间就忘干净了. 最初这个程序是受老同学所托做的,一 ...
- 介绍一款Android小游戏--交互式人机对战五子棋
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6589025 学习Android系统开发之余,编 ...
- Python:游戏:五子棋之人机对战
本文代码基于 python3.6 和 pygame1.9.4. 五子棋比起我之前写的几款游戏来说,难度提高了不少.如果是人与人对战,那么,电脑只需要判断是否赢了就可以.如果是人机对战,那你还得让电脑知 ...
- "人机"对战:电脑太简单了,我是射手 skr~skr~skr
9月17日,2018 世界人工智能大会在上海拉开帷幕.在 SAIL 榜单入围项目中,我看到了小爱同学.小马智行.微软小冰.腾讯觅影等等,这不仅让我大开了眼界,也不禁让我感慨 AI 的发展神速.犹记得去 ...
- python3 井字棋 GUI - 人机对战、机器对战 (threading、tkinter库)
python3 井字棋 GUI - 人机对战.机器对战 功能 GUI界面 人机对战(可选择机器先走) 机器对战(50局) 流程图 内核 棋盘 [0][1][2] [3][4][5] [6][7][8] ...
- 基于Qt Creator实现中国象棋人机对战, c++实现
GitHub地址: https://github.com/daleyzou/wobuku 这是自己大一学完c++后,在课程实践中写过的一个程序,实现象棋人机对战的算法还是有点难的, 自己当时差不多也是 ...
- js实现五子棋人机对战源码
indexhtml <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
随机推荐
- JMeter - 如何创建可重用和模块化测试脚本
概述: 我的应用程序几乎没有业务关键流程,我们可以从中提出不同的业务工作流程.当我试图在JMeter中提出性能测试脚本时,我需要找到一些方法来创建可重用/模块化的测试脚本.这样我就可以创建不同的工作流 ...
- Java面向对象_常用类库api——二叉树数据结构实现
二叉树是每个节点最多有两个子树的有序树.通常子树被称为"左子树"和"右子树". 二叉树算法的排序规则: 1.选择第一个元素作为根节点 2.之后如果元素大于根节点 ...
- Java面向对象_静态代理模式
概念:为其它对象提供一种代理以控制对这个对象的访问.代理模式说白了就是"真实对象"的代表,在访问对象时引入一定程度的间接性,因为这种间接性可以附加多种用途. public clas ...
- VMware安装CentOS7的详细过程
原文:https://www.jianshu.com/p/ce08cdbc4ddb?utm_source=tuicool&utm_medium=referral 本篇文章主要介绍了VMware ...
- CXF 发布rest服务
1.1 什么是rest服务 REST 是一种软件架构模式,只是一种风格,rest服务采用HTTP 做传输协议,REST 对于HTTP 的利用实现精确的资源定位. Rest要求对资源定位更加准 ...
- ACdream 1427—— Nice Sequence——————【线段树单点更新,区间查询】
Nice Sequence Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Su ...
- window mysql5.7 zip 安装
第一步 ? 1 2 3 4 5 6 7 8 9 10 11 12 my-default.ini 添加配置: #绑定IPv4和3306端 bind-address = 127.0.0.1 port = ...
- MongoDB-2.6.0 (OpenLogic CentOS7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: mongodb basic software database linux open source 服务优惠价: 按服务商许可协议 云服务器费用:查 ...
- Linux最常用命令实战
1.改变机器的名称: vim /etc/hostname Master 在文件中修改机器名称为我们想要的名称(相当于域名) 可以通过shutdown -h now 关闭 2.查看当前机器IP: ifc ...
- linux 命令——53 route(转)
Linux系统的route 命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需 要一台连接两个网络的路由器 ...