tic-tac-toe游戏代码
package com.p4f.tictactoe.demo;
import javax.swing.border.Border;
public class Board {
/**
* position
* 0 1 2
* 3 4 5
* 6 7 8
*/
private char[] position;
/**
* default constructor
*/
public Board() {
}
/**
* constructor with a string
* @param s board position
*/
public Board(String s) {
if(s == null) {
this.position = null;
} else {
this.position = s.toCharArray();
}
}
public boolean isWinForX() {
return isWinFor('X');
}
public boolean isWinForO() {
return isWinFor('O');
}
public boolean isDraw() {
// 如果既不是X赢也不是O赢那么就是平局了
return isFull() && !isWinForX() && !isWinForO();
}
/**
* 棋盘是不是满的
* @return true: 满的, false: 不是满的
*/
public boolean isFull() {
boolean t = true;
for(char c : position) {
if(c == ' ') {
return false;
}
}
return t;
}
public boolean isGameOver() {
// 如果不是平局X和O也没有赢
return isDraw() || isWinForX() || isWinForO();
}
/**
* 判断pos上是不是已经有子了
* @param pos 位置
* @return 有返回 true, 没有返回false
*/
public boolean isOccupied(int pos) {
return position[pos] != ' ';
}
public Board move(int pos, char c) {
if(isOccupied(pos)) {
return null;
} else {
char[] newCharArray = position.clone();
newCharArray[pos] = c;
return new Board(newCharArray.toString());
}
}
public void print() {
System.out.println(convertPosition(position));
}
public void printWithNumbers() {
char[] a = position.clone();
char[] temp = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
for(int i=0; i<=8; i++) {
if(a[i] == ' ') {
a[i] = temp[i];
}
}
System.out.println(convertPosition(a));
}
private String convertPosition(char[] position) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(" ").append(position[0]).append(" | ").append(position[1]).append(" | ").append(position[2]).append(" \n").
append("---+---+---\n").
append(" ").append(position[3]).append(" ").append("|").append(" ").append(position[4]).append(" ").append("|").append(" ").append(position[5]).append(" \n").
append("---+---+---\n").
append(" ").append(position[6]).append(" | ").append(position[7]).append(" | ").append(position[8]).append(" ");
return stringBuilder.toString();
}
public boolean isWinFor(char c) {
boolean t = false;
if(position[0] == c && position[1] == c && position[2] == c) {
t = true;
} else if(position[3] == c && position[4] == c && position[5] == c) {
t = true;
}else if(position[6] == c && position[7] == c && position[8] == c) {
t = true;
}else if(position[0] == c && position[3] == c && position[6] == c) {
t = true;
}else if(position[1] == c && position[4] == c && position[7] == c) {
t = true;
}else if(position[2] == c && position[5] == c && position[8] == c) {
t = true;
}else if(position[0] == c && position[4] == c && position[8] == c) {
t = true;
}else if(position[2] == c && position[4] == c && position[6] == c) {
t = true;
}
return t;
}
public static void main(String[] args) {
String s = "XXXOOO X ";
new Board(s).print();
new Board(s).printWithNumbers();
}
}
package com.p4f.tictactoe.demo;
public class TreeNode {
private Board board;
private char nextTurn;
private char winFor;
/**
* default constructor
*/
public TreeNode() {
}
/**
* constructor with board
*/
public TreeNode(Board board) {
this.board = board;
}
public void setNextTurn(char nextTurn) {
// 在当前棋盘的布局下, 谁来走这一步
this.nextTurn = nextTurn;
}
public char getNextTurn() {
// 返回当前落子的人是谁
return this.nextTurn;
}
// 如果设置当前棋盘下赢的人是谁
public void setWinFor(char winFor) {
this.winFor = winFor;
}
// 返回当前赢的人是谁
public char getWinFor() {
return this.winFor;
}
public void setNextMove(int nextMove) {
// 先判断有没有空位
// 如果有空位的话
// 按照以下次序
// 下一步可以赢的
// 如果
}
public int getNextMove() {
return 0;
}
/**
* 设置子节点
* @param pos 位置
* @param child 子节点
*/
public void setChild(int pos, TreeNode child) {
}
public TreeNode getChild(int pos) {
Board nextBoard = board.move(pos, nextTurn);
TreeNode child = new TreeNode(nextBoard);
return child;
}
public Board getBoard() {
return board;
}
public void setBoard(Board board) {
this.board = board;
}
}
package com.p4f.tictactoe.demo;
public class GameTree {
private TreeNode root; // 根节点
private char[] turn; // 下棋的顺序
public GameTree(String turns) {
this.turn = turns.toCharArray();
}
public void makeGameTreeAt(TreeNode node) {
if(node.getBoard().isDraw()) {
node.setWinFor(' ');
} else if(node.getBoard().isWinForX()) {
node.setWinFor('X');
} else if(node.getBoard().isWinForO()) {
node.setWinFor('O');
} else {
if(!node.getBoard().isFull()) {
for(int i=0; i<8; i++) {
if(!node.getBoard().isOccupied(i)) {
Board board = node.getBoard().move(i, node.getNextTurn());
TreeNode child = new TreeNode(board);
makeGameTreeAt(child);
}
}
}
}
}
public char getTurn(int n) {
return turn[n];
}
public char winner() {
return ' ';
}
public void print() {
}
public void print(TreeNode node) {
}
public void printNode() {
}
}
tic-tac-toe游戏代码的更多相关文章
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe
题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...
- 【leetcode】1275. Find Winner on a Tic Tac Toe Game
题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- ACM-Team Tic Tac Toe
我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...
- Epic - Tic Tac Toe
N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...
- java俄罗斯方块游戏代码
java俄罗斯方块游戏代码: package com; import java.awt.Color; import java.awt.Graphics; import java.awt.event.K ...
随机推荐
- C#SerialPort实现串口控制继电器
最近做了一个小系统,麻雀虽小五脏俱全呀,用到各种线程控制,串口控制等技术.其中串口控制最麻烦,因为继电器的响应很快,根据不同的转接口,返回的数据质量是不一样的,所以不能直接wirte,然后马上read ...
- Metesploit使用随笔
平时在工作中真正用到metesploit机会不多,偶尔也会用来做漏洞验证,但是每次使用的时候都需要花点时间回忆一下具体是怎么用的,因此索性记下来方便自己,以使用Nessus扫描YS的某个硬件设备发现的 ...
- access 数据更新语句
UPDATE YS_POINT AS a, YS_LINE AS b SET a.管线高程 = b.SELEV1WHERE (((a.物探点号)=[b].[起点号]));
- [转] c++ try catch 问题
windhaunting,原文地址 以前都是用try{} catch(…){}来捕获C++中一些意想不到的异常, 今天看了Winhack的帖子才知道,这种方法在VC中其实是靠不住的.例如下面的代码: ...
- easyui textbox获取焦点事件
$('#textboxid').textbox().next('span').find('input').focus(); $('#id').textbox('textbox').focus();
- [shell 编程] if [ $# -eq 0 ]该语句是什么含义?
$0: shell或shell脚本的名字$*:以一对双引号给出参数列表$@:将各个参数分别加双引号返回$#:参数的个数$_:代表上一个命令的最后一个参数$$:代表所在命令的PID$!:代表最后执行的后 ...
- solr6.6 索引 word文档
本文是solr6.6 导入 pdf/doc/txt/json/csv/xml文件 的继续,上篇在索引文件,唯独07格式的word文档不能正常抽取数据,进过研究测试终于,记录下属过程. 其它步骤基本和s ...
- Java笔记7:最简单的网络请求Demo
一.服务器端 1 新建一个工程,建立一个名为MyRequest的工程. 2 FileàProject StructureàModulesà点击最右侧的“+”àLibraryàJava 找到Tomcat ...
- Yii2系列教程:安装及Hello World
http://www.yiiframework.com/ 安装Yii2 打算从头开始,所以,连安装Yii2也稍微写一点吧.安装Yii2最好的方式就是使用composer: composer globa ...
- socket编程演示样例(多线程)
client代码例如以下: import java.io.*; import java.net.*; import java.util.Scanner; public class SimpleChat ...