//game.c voidInPut(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] =' '; } } }
2.打印棋盘
这时我们尝试打印棋盘,发现看不见棋盘,因为数组是空格。所以我们要在屏幕上打印分割线来显示棋盘。所以数字间用分割线来显示,定义显示棋盘函数void DisplayBoard(char board[ROW][COL], int row, int col)
int i = 0; for (i = 0; i < row; i++) { //1.打印数据 int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); //数据 if (j < col - 1) printf("|"); //分隔线 } printf("\n"); //2.打印分割行 if (i < col - 1) { int i = 0; for (i = 0; i < col; i++) { printf("---"); if (i < col - 1) printf("|"); } printf("\n"); } } }
voidComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑下棋:>\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
4.判断输赢
定义char IsWin(char board[ROW][COL], int row, int col);函数在玩家和电脑每次下完后都来判断输赢,故使用while循环;
//game.h #pragma once #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 //行 #define COL 3 //列 //打印菜单 voidmanu(); //初始化菜单 voidInPut(char board[ROW][COL],int row,int col); //打印菜单 voidDisplayBoard(char board[ROW][COL], int row, int col); //玩家下棋 voidPlayerMove(char board[ROW][COL], int row, int col); //电脑下棋 voidComputerMove(char board[ROW][COL], int row, int col); //判断输赢 charIsWin(char board[ROW][COL], int row, int col);
voidmanu() { printf("***********************\n"); printf("****** 1. Play ******\n"); printf("****** 0. Exit ******\n"); printf("***********************\n"); } voidInPut(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] =' '; } } } //打印棋盘 voidDisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { //1.打印数据 int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); //数据 if (j < col - 1) printf("|"); //分隔线 } printf("\n"); //2.打印分割行 if (i < col - 1) { int i = 0; for (i = 0; i < col; i++) { printf("---"); if (i < col - 1) printf("|"); } printf("\n"); } } } //玩家下棋 voidPlayerMove(char board[ROW][COL], int row, int col) {
while(1) { printf("请输入坐标:>\n"); printf("输入行号列号,中间用空格隔开:>"); int x = 0; int y = 0; 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"); } } //电脑下棋 voidComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑下棋:>\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
intIsFull(char board[ROW][COL],int row,int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] ==' ') { return0; } } } return1; } //判断输赢 charIsWin(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][0] != ' ') return board[i][0]; } //列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') return board[0][i]; } //对角 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') return board[0][0]; if (board[0][2] == board[1][2] && board[1][2] == board[2][0] && board[0][2] != ' ') return board[0][2]; if (IsFull(board, row, col) == 1) { return'Q'; } return'C'; // 玩家赢 '*' //玩家输 '#' //平局 'Q' //继续 'C' }
写在最后
以上即为这个游戏所有详细操作及解释,当然还有可优化的地方如下;
1. 在game.c中判断时代码不具有很强的复用性,可在后期进行改进优化
//判断输赢
char IsWin(char board\[ROW]\[COL], int row, int col)
2. 在界面上可以采用图形库,,让游戏界面更美观
感谢阅读!创作不易,如果觉得其中一点有帮助到您,还请关注我,一起进步。 当然来一个点赞也是可以的哦! 🚀Live long and Prosper!🖖🏼