C語言簡易三子棋

Duikerdd發表於2018-10-18

   這是本人依據現學知識寫的簡易三子棋,也不是那麼簡潔明瞭,望大佬指點

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #define ROW 3
  5 #define COL 3
  6 char chess_board[ROW][COL];
  7 int PlayGame(int choice){
  8     printf("************************
");
  9     printf("*  歡迎來到三子棋遊戲  *
");
 10     printf("*       請您選擇       *
");
 11     printf("*      1.開始遊戲      *
");
 12     printf("*      2.離開遊戲      *
");
 13     printf("************************
");
 14     while (1){
 15         scanf("%d", &choice);
 16         if (choice == 1)
 17         {
 18             return 1;
 19             break;
 20         }
 21         else if (choice == 2)
 22         {
 23             return 0;
 24             break;
 25         }
 26         else
 27         {
 28             printf("輸入非法,請重新輸入!");
 29             continue;
 30         }
 31     }
 32 }
 33 int Init(char a[ROW][COL]){
 34     for (int row = 0; row <ROW; row++){
 35         for (int col = 0; col < COL; col++)
 36         {
 37             a[row][col] = `  `;
 38         }
 39     }
 40     return 0;
 41 }
 42 void Print(char a[ROW][COL]){
 43     printf("           
");
 44     for (int i = 0; i < ROW; i++){
 45         printf("  %c | %c  | %c 
", a[i][0], a[i][1], a[i][2]);
 46         if (i != 3){
 47             printf("----|----|----
");
 48         }
 49     }
 50 }
 51 void PlayerMove(char a[ROW][COL]){
 52     int row = -1;
 53     int col = -1;
 54     while (1){
 55         printf("請您選擇落子點座標:");
 56         scanf("%d %d", &row, &col);
 57         if (row<0 || row>2 || col<0 || col>2){
 58             printf("輸入非法,請重新輸入!");
 59             continue;
 60         }
 61         else if (a[row][col] == `x` || a[row][col] == `o`)
 62         {
 63             printf("已有棋子,請重新選擇座標!");
 64             continue;
 65         }
 66         else
 67         {
 68             a[row][col] = `o`;
 69             break;
 70         }
 71 
 72     }
 73 }
 74 int IsFull(char a[ROW][COL]){
 75     int sum = 0;
 76     for (int row = 0; row < ROW; row++)
 77     {
 78         for (int col = 0; col < COL; col++)
 79         {
 80             if (a[row][col] != ` `)
 81             {
 82 
 83                 sum++;
 84             }
 85         }
 86     }
 87     if (sum == 9){
 88         return 1;
 89     }
 90 }
 91 char Checkwinner(char a[ROW][COL]){
 92     //1.判斷三子 橫 豎 斜
 93     for (int row = 0; row < ROW; row++){
 94         if (a[row][0] == a[row][1] && a[row][0] == a[row][2] && a[row][0] != ` `)
 95         {
 96             return `p`;
 97         }
 98     }
 99     for (int col = 0; col < COL; col++){
100         if (a[0][col] == a[1][col] && a[0][col] == a[2][col] && a[0][col] != ` `)
101         {
102             return `p`;
103         }
104     }
105     if (a[0][0] == a[1][1] && a[0][0] == a[2][2] && a[0][0] != ` `)
106     {
107         return `p`;
108     }
109     if (a[0][2] == a[1][1] && a[0][2] == a[2][0] && a[0][2] != ` `){
110         return `p`;
111     }
112 }
113 
114 
115 
116 
117 void ComputeMove(char a[ROW][COL]){
118     printf("請電腦落子!
");
119     while (1){
120         int col = rand() % 3;
121         int row = rand() % 3;
122         if (a[row][col] == ` `)
123         {
124             a[row][col] = `x`;
125             break;
126         }
127         else continue;
128     }
129 }
130 void Game(){
131     Init(chess_board);//2.棋盤初始化
132     Print(chess_board);//3.列印棋盤
133     while (1){
134 
135         PlayerMove(chess_board);//4.使用者落子
136         Print(chess_board);
137         if (Checkwinner(chess_board) == `p`)
138         {
139             printf("玩家獲勝!
");
140             break;
141         }
142         if (IsFull(chess_board) == 1)//2.判斷是否和棋
143         {
144             printf("平局!");
145             break;
146         }
147         ComputeMove(chess_board);//6.電腦隨機落子
148         Print(chess_board);//3.列印棋盤
149         if (Checkwinner(chess_board) == `p`)
150         {
151             printf("電腦獲勝!
");
152             break;
153         }
154 
155         if (IsFull(chess_board) == 1)//2.判斷是否和棋
156         {
157             printf("平局!
");
158             break;
159         }
160     }
161 }
162 
163 
164 int main(){
165     int choice = 0;
166     if (PlayGame(choice) == 1) {   //1.選擇是否開始遊戲  a)開始  b)離開
167          Game();
168 
169     }
170     system("pause");
171 }

 

相關文章