还是TicTacToe 2.0

  基本内容已经搞定了,AI明天写。

  1.0的AI就是完全靠随机,只要玩家先走,电脑就基本没机会赢,这样就不叫游戏了,所以必须得改进。

 1 public class Game {

 2     String chessBoard;

 3     String[][] pieces = new String[3][3];

 4     

 5     /** 初始化棋盘样式和棋子数组。*/

 6     public Game() {

 7         chessBoard = 

 8                 "--------------   \n" +

 9                 "| %s | %s | %s | \n" +

10                 "--------------   \n" +

11                 "| %s | %s | %s | \n" +

12                 "--------------   \n" +

13                 "| %s | %s | %s | \n" +

14                 "--------------   \n";

15         

16         for (String[] s1: pieces) {

17             for (int i = 0; i < s1.length; i++)

18                 s1[i] = " ";

19         }

20     }

21     

22     /** 在棋盘上落子。*/

23     int changeBoard(int row, int column, String chessMan) {

24         if (this.pieces[row][column] == " ") {

25             this.pieces[row][column] = chessMan;

26             return 1;

27         }

28         return -1;

29     }

30     

31     /** 棋子数组降维。*/

32     String[] pieceList() {

33         String[] temp = new String[9];

34         for (int i = 0, j = 0; i < 3; i++, j += 3)

35             System.arraycopy(this.pieces[i], 0, temp, j, 3);

36         return temp;

37     }

38     

39     /** 打印棋盘。*/

40     void printChessBoard() {

41         System.out.printf(this.chessBoard, (Object[])this.pieceList());

42     }

43     

44     /** 判断平局。*/

45     boolean isFilled() {

46         for (String[] s1: this.pieces) {

47             for (String s2: s1) {

48                 if (s2 == " ")

49                     return false;

50             }

51         }

52         return true;

53     }

54     /** 判断胜利。*/

55     boolean checkWin() {

56         for (int i = 0; i < 3; i++) {

57             if ((this.pieces[i][0] == this.pieces[i][1] && 

58                 this.pieces[i][1] == this.pieces[i][2] && 

59                 this.pieces[i][1] != " ")

60                 ||

61                 (this.pieces[0][i] == this.pieces[1][i] && 

62                 this.pieces[1][i] == this.pieces[2][i] &&

63                 this.pieces[1][i] != " "))

64                 return true;

65         }

66         if ((this.pieces[0][0] == this.pieces[1][1] &&

67             this.pieces[1][1] == this.pieces[2][2] &&

68             this.pieces[1][1] != " ")

69             ||

70             (this.pieces[2][0] == this.pieces[1][1] &&

71             this.pieces[1][1] == this.pieces[0][2] &&

72             this.pieces[1][1] != " "))

73             return true;

74         

75         return false;

76     }

77 }
Game类
 1 public class Player {

 2     

 3     /** 玩家落子。*/

 4     void move(Game game) {

 5         

 6         while (true) {

 7             java.util.Scanner input = new java.util.Scanner(System.in);

 8             

 9             System.out.print("请输入行数(1-3):");

10             String row = input.next();

11             if (!isValid(row))

12                 continue;

13             System.out.print("请输入列数(1-3):");

14             String column = input.next();

15             if (!isValid(column))

16                 continue;

17             

18             int r = Integer.parseInt(row) - 1;

19             int c = Integer.parseInt(column) - 1;

20             

21             if (game.changeBoard(r, c, "○") == -1) {

22                 System.out.println("这里有子了。\n");

23                 continue;

24             }

25             

26             return;

27         }

28     }

29     

30     /** 检查玩家输入。*/

31     boolean isValid(String userInput) {

32         if (userInput.matches("[123]{1}"))

33             return true;

34         else

35             return false;

36     }

37 }
Player类
 1 public class Computer {

 2     

 3     /** 电脑落子。*/

 4     void move(Game game) {

 5         int row, column;

 6         

 7         while (true) {

 8             row = (int) (Math.random() * 3);

 9             column = (int) (Math.random() * 3);

10             if (game.changeBoard(row, column, "×") == -1)

11                 continue;

12             break;

13         }

14     }

15     

16     /** 找出并返回棋盘上的关键点。*/

17     boolean hasCriticalPoint(Game game) {

18         return false;

19     }

20 }
Computer类
 1 public class Run {

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

 3         Game game = new Game();

 4         Player player = new Player();

 5         Computer computer = new Computer();

 6         

 7         while (true) {

 8             game.printChessBoard();

 9             

10             player.move(game);

11             System.out.println();

12             if (game.checkWin()) {

13                 game.printChessBoard();

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

15                 break;

16             }

17             if (game.isFilled()) {

18                 game.printChessBoard();

19                 System.out.println("平局了。");

20                 break;

21             }

22             computer.move(game);

23             if (game.checkWin()) {

24                 game.printChessBoard();

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

26                 break;

27             }

28             if (game.isFilled()) {

29                 game.printChessBoard();

30                 System.out.println("平局了。");

31                 break;

32             }

33                 

34             System.out.println();

35         }

36     }

37     

38     public static void trace(String toShow) {

39         System.out.printf("===Trace:%s===\n", toShow);

40     }

41 }
Run类加主方法

你可能感兴趣的:(c)