📄 tictactoe.java
字号:
/* Written by: YG First written: 15/10/06 Last modified: 15/10/06*/import sheffield.*;public class TicTacToe { public static void main(String[] args) { EasyReader keyboard = new EasyReader(); // self introduction by the algorithm System.out.println("Hello. I'm Hal. I play a tic-tac-toe with you."); String playerName = keyboard.readString(" May I ask your name? "); System.out.println("Hello, " + playerName + ".\n"); // choose the grid dimension System.out.println("The grid dimension must be between 3 and 9."); int d = keyboard.readInt(" Please enter the dimension: "); // choose the play level System.out.println(); System.out.println("You can be a beginner (1) or a master (2)."); int l = keyboard.readInt(" Please choose the player level: "); // choose the player's mark System.out.println(); System.out.println("\'x\': plays first, \'o\': plays second."); String p = keyboard.readString(" Please choose your symbol: "); // create a grid TicTacGrid grid = new TicTacGrid(d, l, p); System.out.println(); System.out.print("You may choose a box using the combination of two "); System.out.print("characters (no space) -- a digit between 1 and "); System.out.print(d + " (x-axis, from left to right), followed by "); System.out.print("a lower case letter between \'a\' and \'"); System.out.print((char)((int)'a' + d - 1) + "\' "); System.out.println("(y-axis, from bottom to top)."); // the player has chosen 'o', so start by the algorithm if (p.equals("o")) { String s = grid.algorithmDoes(); System.out.println(" Hal: " + s); } // the game goes on int winner = -1; while (winner<0) { // move by the player boolean flag = false; do { String s = keyboard.readString(" " + playerName + ": "); flag = grid.playerDoes(s); } while (flag==false); // test if the game is over winner = grid.gameOver(); // move by the algorithm if (winner<0) { String s = grid.algorithmDoes(); System.out.println(" Hal: " + s); } // test if the game is over winner = grid.gameOver(); } // display the winner switch (winner) { case TicTacGrid.EMPTY: System.out.println("\ngame over -- draw."); break; case TicTacGrid.CROSS: System.out.println("\ngame over -- \'x\' won."); break; case TicTacGrid.CIRCLE: System.out.println("\ngame over -- \'o\' won."); break; default: System.out.println("\nunexpected error."); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -