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