1. 程式人生 > >三子棋小遊戲

三子棋小遊戲

print spa clu 初始 main 被占用 size 是否 電腦

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<time.h>
  5 
  6 #define ROW 3
  7 #define COL 3
  8 
  9 char g_broad[ROW][COL];
 10 
 11 void Init(char broad[ROW][COL])
 12 {
 13     for (int row = 0;row < ROW;++row)
 14     {
 15         for
(int col = 0;col < COL;++col) 16 { 17 broad[row][col] = ; 18 } 19 } 20 } 21 22 void Print(char broad[ROW][COL]) 23 { 24 for(int row=0;row<ROW;++row) 25 { 26 printf("| %c | %c | %c |\n",broad[row][0],broad[row][1],broad[row][2]); 27 if
(row != ROW - 1) 28 { 29 printf(" ---|---|---\n"); 30 } 31 } 32 } 33 34 void Playermove(char broad[ROW][COL]) 35 { 36 printf("玩家下棋!\n"); 37 while(1) 38 { 39 int row = 0; 40 int col = 0; 41 printf("請輸入一組坐標:\n"); 42 scanf("
%d %d", &row, &col); 43 if (row < 0 || row >= ROW || col < 0 || col >= COL) 44 { 45 printf("您輸入的坐標非法!請重新輸入:\n"); 46 continue; 47 } 48 if (broad[row][col] != ) 49 { 50 printf("該位置已經有棋子,請重新輸入:\n"); 51 continue; 52 } 53 broad[row][col] = x; 54 break; 55 } 56 } 57 58 void Computermove(char broad[ROW][COL]) 59 { 60 printf("電腦下棋!\n"); 61 while (1) 62 { 63 int row = rand() % ROW; 64 int col = rand() % COL; 65 if (broad[row][col] != ) 66 { 67 //位置被占用,重新生成 68 continue; 69 } 70 broad[row][col] = o; 71 break; 72 } 73 74 } 75 76 int Isfull(char broad[ROW][COL]) 77 { 78 for (int row = 0;row < ROW;++row) 79 { 80 for (int col = 0;col < COL;++col) 81 { 82 if (broad[row][col] == ) 83 { 84 return 0; 85 } 86 } 87 } 88 return 1; 89 } 90 91 char Checkgame(char broad[ROW][COL]) 92 { 93 //先檢查所有的行 94 for (int row = 0;row < ROW;++row) 95 { 96 if (broad[row][0] == broad[row][1] 97 && broad[row][0] == broad[row][2] 98 && broad[row][0] != ) 99 { 100 return broad[row][0]; 101 } 102 } 103 //檢查所有列 104 for (int col = 0;col < COL;++col) 105 { 106 if (broad[0][col] == broad[1][col] 107 && broad[0][col] == broad[2][col] 108 && broad != ) 109 { 110 return broad[0][col]; 111 } 112 } 113 //檢查對角線 114 if (broad[0][0] == broad[1][1] 115 && broad[0][0] == broad[2][2] 116 && broad[0][0] != ) 117 { 118 return broad[0][0]; 119 } 120 if (broad[2][0] == broad[1][1] 121 && broad[2][0] == broad[0][2] 122 && broad[2][0] != ) 123 { 124 return broad[2][0]; 125 } 126 //判定是否和棋 127 if (Isfull(broad)) 128 { 129 return q; 130 } 131 return ; 132 } 133 134 void Game() 135 { 136 //1.先對棋盤進行初始化 137 Init(g_broad); 138 char winner = ; 139 while (1) 140 { 141 //2.打印棋盤(此時是一個空的棋盤) 142 Print(g_broad); 143 //3.玩家下棋,檢查遊戲是否結束 144 Playermove(g_broad); 145 //winner==x 玩家勝利;o電腦勝利;q和棋 ;‘ ‘勝負未分 146 winner = Checkgame(g_broad); 147 if (winner != ) 148 { 149 break; 150 } 151 //4.電腦下棋,檢查遊戲是否結束 152 Computermove(g_broad); 153 winner = Checkgame(g_broad); 154 if (winner != ) 155 { 156 break; 157 } 158 //循環第2步。 159 160 } 161 //在遊戲結束之前打印地圖 162 Print(g_broad); 163 if (winner == x) 164 { 165 printf("玩家勝利!\n"); 166 } 167 else if (winner == o) 168 { 169 printf("電腦勝利!\n"); 170 } 171 else if (winner == q) 172 { 173 printf("和棋!\n"); 174 } 175 else 176 { 177 printf("程序出錯!\n"); 178 } 179 180 } 181 182 int Menu() 183 { 184 printf("==============\n"); 185 printf("1.開始遊戲\n"); 186 printf("0.結束遊戲\n"); 187 printf("==============\n"); 188 printf("請輸入您的選擇:\n"); 189 int choice = 0; 190 scanf("%d",&choice); 191 return choice; 192 } 193 194 int main() 195 { 196 //這裏設置隨機種子 197 srand((unsigned int)time(0)); 198 while (1) 199 { 200 int choice = Menu(); 201 if (choice == 1) 202 { 203 Game(); 204 } 205 else 206 { 207 printf("Goodbye!\n"); 208 break; 209 } 210 } 211 return 0; 212 }

三子棋小遊戲