三子棋主要功能分析:
- 简易游戏界面
- 下棋及对应棋盘的打印
- 胜负判断
简易三子棋实现的基本子函数:
-
界面函数
-
棋盘初始化
-
棋盘打印
-
玩家、电脑下棋函数
-
判断函数
界面函数:
void manu() { printf("*************************\n"); printf("***** welcome! *******\n"); printf("***** 1:play *******\n"); printf("***** 2:quit *******\n"); printf("*************************\n"); //界面函数只要是用于main函数中循环调用 }
棋盘初始化
void InitChess(char chess[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { chess[i][j] = ' ';//初始化为‘ ’方便后续打印 } } }
棋盘打印函数
上图为打印界面样式,采用每一个小格打印两行,第一行为“ %c ”加‘|’第二行为---加‘|’,最后一列不打印‘|’,在最后一行中不打印---,由此写成一下循环
void PrintBoard(char chess[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", chess[i][j]); if (j < row - 1)//最后一列不打印‘|’ printf("|"); } printf("\n"); if (i < row - 1)//最后一行不打印--- { for (int j = 0; j < col; j++) { printf("---"); if (j < row - 1) printf("|"); } printf("\n"); } } }
下棋函数
void Player(char chess[ROW][COL], int row, int col) //玩家下棋 { int row1 = 0; int col1 = 0; while (1) { printf("请输入位置>>"); scanf("%d %d", &row1, &col1); if (row1 > ROW || row1 <= 0 || col1 > COL || col1 <= 0) { printf("pos error\n"); continue; } if (chess[row1-1][col1-1] != ' ') { printf("pose error\n"); continue; } else { chess[row1-1][col1-1] = '#'; break; } } }
void computer(char chess[ROW][COL], int row, int col) //电脑下棋 { while (1) { int row1 = rand() % 3;//srand函数在进入函数前调用 int col1 = rand() % 3; if (row1 > ROW || row1 <= 0 || col1 > COL || col1 <= 0) { continue; } if (chess[row1 - 1][col1 - 1] != ' ') { continue; } else { chess[row1 - 1][col1 - 1] = '*'; break; } } }
判断函数
int judge(char chess[ROW][COL], int row, int col) //简易判断,只考虑三子棋,没有写的更加深入 { for (int i = 0; i < ROW; i++) { if (chess[i][1] == chess[i][2] && chess[i][2] == chess[i][0] && chess[i][1] == '#') { return 1; } } for (int i = 0; i < COL; i++) { if (chess[1][i] == chess[2][i] && chess[2][i] == chess[0][i]&&chess[1][i]=='#') { return 1; } } if (chess[1][1] == chess[2][2] && chess[2][2] == chess[0][0]&&chess[0][0]=='#') return 1; if (chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[0][0] == '#') return 1; for (int i = 0; i < ROW; i++) { if (chess[i][1] == chess[i][2] && chess[i][2] == chess[i][0] && chess[i][1] == '*') { return -1; } } for (int i = 0; i < COL; i++) { if (chess[1][i] == chess[2][i] && chess[2][i] == chess[0][i] && chess[1][i] == '*') { return -1; } } if (chess[1][1] == chess[2][2] && chess[2][2] == chess[0][0] && chess[0][0] == '*') return -1; if (chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[0][0] == '*') return -1; return 0; }
主函数
int main() { int flag = 0; while (1) { manu(); scanf("%d", &flag); switch (flag) { case 1: game(); break; case 2:return 0; default: printf("input error"); continue; break; } } } void game() { srand((unsigned int)time(NULL)); char chess[ROW][COL]; int flag = 0; InitChess(chess, ROW, COL); PrintBoard(chess, ROW, COL); while (1) { Player(chess, ROW, COL); PrintBoard(chess, ROW, COL); flag = judge(chess, ROW, COL); if (flag == 1) { printf("You win\n"); return; } else if(flag== -1){ printf("You lose\n"); return; } printf("电脑输入\n"); computer(chess, ROW, COL); PrintBoard(chess, ROW, COL); flag = judge(chess, ROW, COL); if (flag == 1) { printf("You win\n"); return; } else if (flag == -1) { printf("You lose\n"); return; } }
结果展示