博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tic-tac-toe游戏代码
阅读量:5287 次
发布时间:2019-06-14

本文共 6053 字,大约阅读时间需要 20 分钟。

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() {    }}

转载于:https://www.cnblogs.com/tuhooo/p/8269561.html

你可能感兴趣的文章
axure学习点
查看>>
WPF文本框只允许输入数字[转]
查看>>
dom4j 通用解析器,解析成List<Map<String,Object>>
查看>>
TYVJ.1864.[Poetize I]守卫者的挑战(概率DP)
查看>>
0925 韩顺平java视频
查看>>
iOS-程序启动原理和UIApplication
查看>>
mysql 8.0 zip包安装
查看>>
awk 统计
查看>>
模板设计模式的应用
查看>>
Matlab parfor-loop并行运算
查看>>
2012-01-12 16:01 hibernate注解以及简单实例
查看>>
Ztree异步树加载
查看>>
关于IE和火狐,谷歌,Safari对Html标签Object和Embed的支持问题
查看>>
poj3320 Jessica's Reading Problem(尺取思路+STL)
查看>>
分布式计算开源框架Hadoop介绍
查看>>
坏的事情不都会带来坏的结果
查看>>
RPC的基础:调研EOS插件http_plugin
查看>>
第二次团队冲刺第二天
查看>>
11)Java abstract class 和 interface
查看>>
使用xrdp或Xmanager 远程连接 CentOS6
查看>>