2.4游戏的整个代码
test.c:
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("**********************************\n"); printf("****** 1.play ******\n"); printf("****** 2.exit ******\n"); printf("**********************************\n"); } //1.初始化棋盘 //2.打印棋盘 //3.玩家下棋 //4.电脑下棋 //5.判断 void game() { char Board[ROW][COL] = { 0 }; //初始化棋盘 InitBoard(Board, ROW, COL); //打印棋盘 DisplayBoard(Board, ROW, COL); //下棋 int ret = 0; while (1) { //玩家下棋 * printf("玩家下棋:\n"); Player_move(Board, ROW, COL); DisplayBoard(Board, ROW, COL); ret = Is_win(Board, ROW, COL); if (ret != 'C') { break; } //电脑下棋 # printf("电脑下棋\n"); Computer_move(Board, ROW, COL); DisplayBoard(Board, ROW, COL); ret = Is_win(Board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家胜利!!!\n"); } else if (ret == '#') { printf("电脑胜利!!!\n"); } else { printf("平局\n"); } } void test() { int input = 0; srand((unsigned)time(NULL)); menu(); do { printf("请选择:\n"); scanf("%d", &input); switch (input) { case 1: printf("开始游戏\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("输入错误,请重新选择\n"); break; } } while (input); } int main() { test(); return 0; }
game.h:
#pragma once #define ROW 3 #define COL 3 #include<stdio.h> #include<stdlib.h> #include<time.h> //初始化棋盘声明 void InitBoard(char Board[ROW][COL], int row, int col); //打印棋盘声明 void DisplayBoard(char Board[ROW][COL], int row, int col); //玩家下棋声明 void Player_move(char Board[ROW][COL], int row, int col); //电脑下棋声明 void Computer_move(char Board[ROW][COL], int row, int col); //胜利声明 char Is_win(char Board[ROW][COL], int row, int col); //平局声明 int Is_full(char Board[ROW][COL], int row, int col);
game.c:
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void InitBoard(char Board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { Board[i][j] = ' '; } } } void DisplayBoard(char Board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < ROW; i++) { //打印行 for (j = 0; j < COL; j++) { printf(" %c ", Board[i][j]); if (j < COL - 1) printf("|"); } printf("\n"); //打印列 for (j = 0; j < COL; j++) { printf("---"); if (j < COL - 1) printf("|"); } printf("\n"); } } void Player_move(char Board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (Board[x - 1][y - 1] == ' ') { Board[x - 1][y - 1] = '*'; break; } else { printf("输过啦!!!\n"); } } else { printf("输出去啦!!!!\n请重新输入:\n"); } } } void Computer_move(char Board[ROW][COL], int row, int col) { while (1) { int x = rand() % ROW; int y = rand() % COL; if (Board[x][y] == ' ') { Board[x][y] = '#'; break; } } } int Is_full(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 1; } //玩家赢返回 * //电脑赢返回 # //平局 返回 Q //继续 返回 C char Is_win(char Board[ROW][COL], int row, int col) { int i = 0; //三行 for (i = 0; i < row; i++) { if (Board[i][0] == Board[i][1] && Board[i][1] == Board[i][2] && Board[i][1] != ' ') { return Board[i][1]; } } //三列 for (i = 0; i < col; i++) { if (Board[0][i] == Board[1][i] && Board[1][i] == Board[2][i] && Board[1][i] != ' ') { return Board[1][i]; } } //对角线的判断 if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[1][1] != ' ') { return Board[1][1]; } if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][0] && Board[1][1] != ' ') { return Board[1][1]; } //判断平局 if (1 == Is_full(Board, row, col)) { return 'Q'; } //继续 return 'C'; }
2.5三子棋的扩展
大家可以发现,目前我们的电脑走时是靠随机数的生成,所以电脑就表现得十分笨拙,所以我们可以自己去思考一下,比如如果电脑发现自己有两个连在一起了,就直接放在第三个取得胜利,有还有如果发现玩家要连在一起了,就去阻断玩家等。这是个思考题哈!