我问你,这beyond代码违反了什么原则

封装/

  1 package TicTacToe;
  2 
  3 import java.util.Scanner;
  4 import java.util.Random;
  5 
  6 public class Run {
  7     
  8     // 数组输入和随机三个必需对象。
  9     static Scanner input = new Scanner(System.in);
 10     String[] panel = new String[9];
 11     Random generator = new Random();
 12     
 13     void drawPanel() {
 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     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     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     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     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         Run game = new Run();
 88         System.out.println("初始化棋盘中……");
 89         for (int i=0; i<9; i++) {
 90             game.panel[i] = "□";
 91         }
 92         System.out.println("初始化完毕。");
 93         
 94         while (true) {
 95             System.out.println(); // 打印一个空行。
 96             game.drawPanel();     // 绘制棋盘。
 97             
 98             System.out.print("落子(1-9):");
 99             int playermove = input.nextInt();
100             if (game.playerMove(playermove) == 0) {
101                 continue;
102             }
103             
104             System.out.println();
105             game.drawPanel();
106             if (game.checkWin() == 1) {
107                 System.out.println("你赢了!");
108                 break;
109             }
110             if (game.checkFull() == 1) {
111                 System.out.println("平局了!");
112                 break;
113             }
114             
115             System.out.println("
电脑的回合:");
116             game.comMove();
117             if (game.checkWin() == 1) {
118                 game.drawPanel();
119                 System.out.println("电脑赢了!");
120                 break;
121             }
122             if (game.checkFull() == 1) {
123                 System.out.println("平局了!");
124                 break;
125             }
126         }
127     }
128 }
折叠代码
原文地址:https://www.cnblogs.com/chihane/p/3424282.html