呵呵

  TicTacToe。

  对的我还有进步空间一定是这样。

  1 package What;

  2 

  3 import java.util.Scanner;

  4 import java.util.Random;

  5 

  6 public class Run {

  7     

  8     // 数组输入和随机三个必需对象。

  9     static String[] panel = new String[9];

 10     static Scanner input = new Scanner(System.in);

 11     static Random generator = new Random();

 12     

 13     static void drawPanel(String[] panel) {

 14         /* 绘制棋盘,接受一个数组,按照数字组内容绘制

 15          * 方法也是有点二逼那种。

 16          */

 17         

 18         System.out.println(panel[0] + " " + panel[1] + " " + panel[2]);

 19         System.out.println(panel[3] + " " + panel[4] + " " + panel[5]);

 20         System.out.println(panel[6] + " " + panel[7] + " " + panel[8]);

 21     }

 22     

 23     static int playerMove(int i) {

 24         // 玩家落子。简单检查格子是否已经被占用。

 25         if (panel[i-1] == "○" || panel[i-1] == "×") {

 26             System.out.println("这个地方已经落子了,不能睁大你的狗眼仔细看看么?");

 27             return 0;

 28         } else {

 29             panel[i-1] = "○";

 30             return 1;

 31         }

 32     }

 33     

 34     static void comMove() {

 35         // 电脑落子。随机生成0-8的整数来落子,如果已经有子就continue掉重新生成随机数。

 36         while (true) {

 37             int location = generator.nextInt(9);

 38             if (panel[location] == "○" || panel[location] == "×") {

 39                 continue;

 40             } else {

 41                 panel[location] = "×";

 42                 break;

 43             }

 44         }

 45     }

 46     

 47     static int checkWin() {

 48         /*  检查获胜,方法简直不能更2B……

 49          *  呜呜呜我想要列表嵌套……

 50          */

 51         if (panel[0] == panel[1] & panel[1]== panel[2] & panel[0] != "□") {

 52             return 1;

 53         } else if (panel[3] == panel[4] & panel[4]== panel[5] & panel[3] != "□") {

 54             return 1;

 55         } else if (panel[6] == panel[7] & panel[7]== panel[8] & panel[6] != "□") {

 56             return 1;

 57         } else if (panel[0] == panel[3] & panel[3]== panel[6] & panel[0] != "□") {

 58             return 1;

 59         } else if (panel[1] == panel[4] & panel[4]== panel[7] & panel[1] != "□") {

 60             return 1;

 61         } else if (panel[2] == panel[5] & panel[5]== panel[8] & panel[2] != "□") {

 62             return 1;

 63         } else if (panel[0] == panel[4] & panel[4]== panel[8] & panel[0] != "□") {

 64             return 1;

 65         } else if (panel[2] == panel[4] & panel[4]== panel[6] & panel[2] != "□") {

 66             return 1;

 67         } else {

 68             return 0;

 69         }

 70     }

 71     

 72     static int checkFull() {

 73         /* 检查棋盘是否已经被占满。

 74          * 迭代棋盘内容,如果有空格子就立刻返回0。

 75          */

 76         for (int i=0; i<9; i++) {

 77             if (panel[i] == "□") {

 78                 return 0;

 79             }

 80         }

 81         return 1;

 82     }

 83     

 84     

 85     public static void main(String[] args) {

 86         //没脸写注释,虽然也不难看懂不是不难看是不难看懂其实挺难看的。

 87         System.out.println("初始化棋盘中……");

 88         for (int i=0; i<9; i++) {

 89             panel[i] = "□";

 90         }

 91         System.out.println("初始化完毕。");

 92         

 93         while (true) {

 94             System.out.println(); // 打印一个空行。

 95             drawPanel(panel);     // 绘制棋盘。

 96             

 97             System.out.print("落子(1-9):");

 98             int playermove = input.nextInt();

 99             if (playerMove(playermove) == 0) {

100                 continue;

101             }

102             

103             System.out.println();

104             drawPanel(panel);

105             if (checkWin() == 1) {

106                 System.out.println("你赢了!");

107                 break;

108             }

109             if (checkFull() == 1) {

110                 System.out.println("平局了!");

111                 break;

112             }

113             

114             System.out.println("\n电脑的回合:");

115             comMove();

116             if (checkWin() == 1) {

117                 drawPanel(panel);

118                 System.out.println("电脑赢了!");

119                 break;

120             }

121             if (checkFull() == 1) {

122                 System.out.println("平局了!");

123                 break;

124             }

125         }

126     }

127 }
折叠代码

 

你可能感兴趣的:(呵呵)