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游戏代码的更多相关文章

  1. POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...

  2. 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 ...

  3. 【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 ...

  4. 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 ...

  5. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  6. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  7. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

  8. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

  9. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  10. java俄罗斯方块游戏代码

    java俄罗斯方块游戏代码: package com; import java.awt.Color; import java.awt.Graphics; import java.awt.event.K ...

随机推荐

  1. 关于 xk 的位数。

    关于 xk 的位数. 如果x大于0小于l,那么位数=1+小数部分×k, 如果x≥l,那么位数=trunc(ln(x)/ln(10)×k)+1+小数部分×k. trunc//向下取整

  2. Asp.Net MVC part3 路由Route

    路由Route路由规则Route:可以查看源代码了解一下构造方法,需要指定路由格式.默认值.处理器三个值路由数据RouteData:当前请求上下文匹配路由规则而得到的一个对象,可以在Action中通过 ...

  3. SQL Server on Linux: How? Introduction: SQL Server Blog

    SQL Server Blog Official News from Microsoft’s Information Platform https://blogs.technet.microsoft. ...

  4. fullPage全屏滚动的实现

    fullPage.js 是一个基于 jQuery 的插件,它能够很方便.很轻松的制作出全屏网站. 用法: 1.引入jquery 2.引入fullPage 3.每个section代表一屏 4.js启动: ...

  5. iOS:实现邮件和短信发送的简单示例

    发送邮件1.导入库文件:MessageUI.framework2.引入头文件3.实现代理<MFMailComposeViewControllerDelegate> 和 <UINavi ...

  6. Excel的列数以数字格式查看

    1.Excel中的列数默认是以字母形式显示的,当我们有大量数据并想知道任一数据是第多少行多少列时这样就不方便了,我们可以通过如下设置来达到让EXCEL以数字形式显示行数和列数的效果. 2.点击文件-- ...

  7. 数据结构之---C语言实现最小生成树之prim(普里姆)算法

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  8. SurfaceView的经典写法

    package com.example.test; import android.content.Context; import android.graphics.Canvas; import and ...

  9. 道具搜索框(|=, & , ^=)实现的列子

    需求: 勾上界面上多选框筛选出符合的道具 思路: 1. 使用组合数字让一个数字包含多这个搜索条件,比如2代表搜索衣服和武器, 数字按照2的n次幂的值递增,通过|,&,^运算符实现一个数字包含多 ...

  10. jQuery unbind() 方法

    jQuery 中的 unbind() 方法是 bind() 方法的反向操作,从每一个匹配的元素中删除绑定的事件. 语法结构: unbind([type][, data]); type是事件类型,dat ...