井字游戏 人机对战 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=" ...
随机推荐
- iOS 限制TextField输入长度
1 #import "MyInfoEditTableViewCell.h" @interface MyInfoEditTableViewCell()<UITextFieldD ...
- [题解](排列/逆序对)luogu_P1338末日的传说
首先我们要考虑怎么排能使逆序对数最多:显然是下降序列时,会产生n*(n-1)/2数量的逆序对 那么我们肯定是要尽量把序列的尾端安排成下降序列,前面的尽量不动,中间可能有一段排列自适应到m的逆序对数 然 ...
- Xml2Object
<dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- 深刻理解Linux进程间通信(IPC)
https://www.ibm.com/developerworks/cn/linux/l-ipc/ linux下进程间通信的几种主要手段简介: 管道(Pipe)及有名管道(named pipe):管 ...
- Hadoop实战:微博数据分析
项目需求 自定义输入格式,将明星微博数据排序后按粉丝数 关注数 微博数 分别输出到不同文件中. 数据集 下面是部分数据,猛戳此链接下载完整数据集 数据格式: 明星 明星微博名称 粉丝数 ...
- asp.net mvc网站的发布与IIS配置
一.安装IIS (如果服务器上已经安装了就跳过) 控制面板——程序——程序功能——打开或关闭windows功能——勾选Inertnet信息服务下面所有选项——确定 二.获取发布文件(如果已经发不好就跳 ...
- (三)Redis两种持久化方案
Redis的持久化策略:2种 RDB方式的持久化是通过快照(snapshotting)完成的,当符合一定条件时Redis会自动将内存中的数据进行快照并持久化到硬盘.RDB是Redis默认采用的持久化方 ...
- Python3基础(6)面向对象编程、异常处理
---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...
- SpringBoot | 第二十五章:日志管理之自定义Appender
前言 前面两章节我们介绍了一些日志框架的常见配置及使用实践.一般上,在开发过程中,像log4j2.logback日志框架都提供了很多Appender,基本上可以满足大部分的业务需求了.但在一些特殊需求 ...