zoukankan      html  css  js  c++  java
  • 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(" 
    ").
                    append("---+---+---
    ").
                    append(" ").append(position[3]).append(" ").append("|").append(" ").append(position[4]).append(" ").append("|").append(" ").append(position[5]).append(" 
    ").
                    append("---+---+---
    ").
                    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() {
    
        }
    
    
    }
  • 相关阅读:
    使用validwhen设计复杂的Struts表单验证
    http://wiki.jfrog.org/confluence/display/RTF/Understanding+Repositories
    我的JDK是1.5得啊,我的maven2也是2.0.9的最新版本的,这个是底层接口的泛型,又不能删除,请教用过的高手怎样解决啊?
    Oracle的rownum原理和使用
    Maven 搭建环境(http://dearshor.javaeye.com/blog/272274)
    使用nexus替代artifactory作为maven私服
    [转]关于struts中validate的几种情况
    用Artifactory管理内部Maven仓库
    容器
    天生一对"Maven2+Jetty" Maven2创建并管理WebApp,并使用Maven Jetty Plugin在Eclipse中调试
  • 原文地址:https://www.cnblogs.com/tuhooo/p/8269561.html
Copyright © 2011-2022 走看看