三子棋游戏
问题描述:
3*3的棋盘中,只要一条线上出现三个一样的棋子就获胜(玩家或电脑);如果棋盘已经放满还未出现三个棋子一条线则打成平手。
具体细节:
-
初始化棋盘(用空格初始化)
//初始化棋盘 void initChess(char chessbox[ROW][COL]){
1for (int row = 0; row < ROW; row++){ 2 3 4 for (int col = 0; col < COL; col++){ 5 6 7 chessbox[row][col] = ' '; 8 } 9}}
-
打印棋盘
//打印棋盘 void printChess(char chessbox[ROW][COL]){
1system("cls"); 2printf("+---+---+---+\n"); 3for (int row = 0; row < ROW; row++){ 4 5 6 printf("| %c | %c | %c |\n", 7 chessbox[row][0], chessbox[row][1], 8 chessbox[row][2]); 9 printf("+---+---+---+\n"); 10}}
-
电脑落子(用o表示电脑落子)
//电脑落子(用o表示) void computerMove(char chessbox[ROW][COL]){
1srand(time(0)); 2while (1){ 3 4 5 int row = rand() % 3; 6 int col = rand() % 3; 7 if (chessbox[row][col] == ' '){ 8 9 10 chessbox[row][col] = 'o'; 11 break; 12 } 13 14}}
-
玩家落子
//玩家落子(用x表示) void playerMove(char chessbox[ROW][COL]){
1int row, col; 2while (1){ 3 4 5 printf("请输入您的落子地点:"); 6 scanf("%d %d", &row, &col); 7 if (row >= 3 || col >= 3){ 8 9 10 printf("您输入的落子位置有误,请重新输入:"); 11 continue; 12 } 13 if (chessbox[row][col] == ' '){ 14 15 16 chessbox[row][col] = 'x'; 17 break; 18 } 19 printf("该位置已有棋子,请重新输入:"); 20}}
-
三个棋子一条线
-
在一行或一列实现三个棋子一条线
//行 for (int row = 0; row < ROW; row++){
1 if (chessbox[row][0] != ' ' 2 &&chessbox[row][0] == chessbox[row][1] 3 && chessbox[row][0] == chessbox[row][2]){ 4 5 6 return chessbox[row][0]; 7 } 8} 9//列 10for (int col = 0; col < COL; col++){ 11 12 13 if (chessbox[0][col] != ' ' 14 &&chessbox[0][col] == chessbox[1][col] 15 && chessbox[0][col] == chessbox[2][col]){ 16 17 18 return chessbox[0][col]; 19 } 20} -
对角线实现三个棋子一条线
if (chessbox[0][0] != ' ' &&chessbox[0][0] == chessbox[1][1] && chessbox[0][0] == chessbox[2][2]){
1 return chessbox[0][0]; 2} 3if (chessbox[2][0] != ' ' 4 &&chessbox[2][0] == chessbox[1][1] 5 && chessbox[2][0] == chessbox[0][2]){ 6 7 8 return chessbox[2][0]; 9}
- 和棋
-
棋盘放满还未获胜,则为和棋,打成了平手。
在这里插入代码片 //和棋 if(isFull(checkbox)){
1 return 'a'; 2 }
- 输赢约定:
-
返回x代表玩家获胜
if (isWinner(chessbox) == 'x'){
1 printf("恭喜您赢啦!\n"); 2 break; 3 } -
返回o代表电脑获胜
if (isWinner(chessbox) == 'o'){
1 printf("很遗憾,您输了!\n"); 2 break; 3 } -
返回a代表和棋(打成平手)
if (isWinner(chessbox) == 'a'){
1 printf("你和电脑同一水平呦!\n"); 2 break; 3 }
- 判断棋盘是否放满:
-
返回1代表棋盘已满
-
返回0代表棋盘未满
//判断棋盘是否摆满 //1表示满;0表示不满。 int isFullChess(char chessbox[ROW][COL]){
1for (int row = 0; row < ROW; row++){ 2 3 4 for (int col = 0; col < COL; col++){ 5 6 7 //找到空格,说明未满 8 if (chessbox[row][col] == ' '){ 9 10 11 return 0; 12 } 13 } 14} 15return 1;}
源代码:
1#define _CRT_SECURE_NO_WARNINGS 2#include<stdio.h> 3#include<stdlib.h> 4#include<math.h> 5#define ROW 3 6#define COL 3 7//玩家落子(用x表示) 8void playerMove(char chessbox[ROW][COL]){ 9 10 11 int row, col; 12 while (1){ 13 14 15 printf("请输入您的落子地点:"); 16 scanf("%d %d", &row, &col); 17 if (row >= 3 || col >= 3){ 18 19 20 printf("您输入的落子位置有误,请重新输入:"); 21 continue; 22 } 23 if (chessbox[row][col] == ' '){ 24 25 26 chessbox[row][col] = 'x'; 27 break; 28 } 29 printf("该位置已有棋子,请重新输入:"); 30 } 31} 32//电脑落子(用o表示) 33void computerMove(char chessbox[ROW][COL]){ 34 35 36 srand(time(0)); 37 while (1){ 38 39 40 int row = rand() % 3; 41 int col = rand() % 3; 42 if (chessbox[row][col] == ' '){ 43 44 45 chessbox[row][col] = 'o'; 46 break; 47 } 48 49 } 50} 51//初始化棋盘 52void initChess(char chessbox[ROW][COL]){ 53 54 55 for (int row = 0; row < ROW; row++){ 56 57 58 for (int col = 0; col < COL; col++){ 59 60 61 chessbox[row][col] = ' '; 62 } 63 } 64} 65//打印棋盘 66void printChess(char chessbox[ROW][COL]){ 67 68 69 system("cls"); 70 printf("+---+---+---+\n"); 71 for (int row = 0; row < ROW; row++){ 72 73 74 printf("| %c | %c | %c |\n", 75 chessbox[row][0], chessbox[row][1], 76 chessbox[row][2]); 77 printf("+---+---+---+\n"); 78 } 79} 80//判断棋盘是否摆满,摆满返回1;未满返回0 81int isFullChess(char chessbox[ROW][COL]){ 82 83 84 for (int row = 0; row < ROW; row++){ 85 86 87 for (int col = 0; col < COL; col++){ 88 89 90 //找到空格,说明未满 91 if (chessbox[row][col] == ' '){ 92 93 94 return 0; 95 } 96 } 97 } 98 return 1; 99} 100//约定:返回x代表玩家赢了;返回o代表电脑赢了;返回a代表和棋 101char isWinner(char chessbox[ROW][COL]){ 102 103 104 //行 105 for (int row = 0; row < ROW; row++){ 106 107 108 if (chessbox[row][0] != ' ' 109 &&chessbox[row][0] == chessbox[row][1] 110 && chessbox[row][0] == chessbox[row][2]){ 111 112 113 return chessbox[row][0]; 114 } 115 } 116 //列 117 for (int col = 0; col < COL; col++){ 118 119 120 if (chessbox[0][col] != ' ' 121 &&chessbox[0][col] == chessbox[1][col] 122 && chessbox[0][col] == chessbox[2][col]){ 123 124 125 return chessbox[0][col]; 126 } 127 } 128 //对角线 129 if (chessbox[0][0] != ' ' 130 &&chessbox[0][0] == chessbox[1][1] 131 && chessbox[0][0] == chessbox[2][2]){ 132 133 134 return chessbox[0][0]; 135 } 136 if (chessbox[2][0] != ' ' 137 &&chessbox[2][0] == chessbox[1][1] 138 && chessbox[2][0] == chessbox[0][2]){ 139 140 141 return chessbox[2][0]; 142 } 143 //和棋 144 if (isFullChess(chessbox)){ 145 146 147 return 'a'; 148 } 149 return 0; 150} 151//开始一局游戏 152void game(){ 153 154 155 156 char chessbox[ROW][COL] = { 157 158 0 }; 159 initChess(chessbox); 160 printf("游戏开始啦!\n"); 161 printChess(chessbox); 162 while (1){ 163 164 165 //玩家落子 166 playerMove(chessbox); 167 //打印棋牌 168 printChess(chessbox); 169 //判断胜负 170 if (isWinner(chessbox) == 'x'){ 171 172 173 printf("恭喜您赢啦!\n"); 174 break; 175 } 176 if (isWinner(chessbox) == 'a'){ 177 178 179 printf("你和电脑同一水平呦!\n"); 180 break; 181 } 182 //电脑落子 183 computerMove(chessbox); 184 //打印棋牌 185 printChess(chessbox); 186 //判断胜负 187 if (isWinner(chessbox) == 'o'){ 188 189 190 printf("很遗憾,您输了!\n"); 191 break; 192 } 193 if (isWinner(chessbox) == 'a'){ 194 195 196 printf("你和电脑同一水平呦!\n"); 197 break; 198 } 199 } 200} 201int menu(){ 202 203 204 printf("===============\n"); 205 printf("1.开始游戏\n"); 206 printf("0.结束游戏\n"); 207 printf("===============\n"); 208 int choice; 209 printf("请输入您的选择:"); 210 scanf("%d", &choice); 211 return choice; 212} 213 214int main() 215{ 216 217 218 219ile (1){ 220 221 222 int choice=menu(); 223 if (choice == 1){ 224 225 226 game(); 227 continue; 228 } 229 else if (choice == 0){ 230 231 232 break; 233 }else{ 234 235 236 printf("输入有误,请您重新输入!\n"); 237 continue; 238 } 239 } 240 system("pause"); 241 return 0; 242} 243
运行结果:



