5.游戏胜负的判断
棋局千变万化,每一步都有可能产生输赢,我们要不断地进行判断。
而判断的时机,就选择在玩家下棋和电脑下棋之后。
我们创建IsWin函数来判断输赢。
- 我们返回不同的符号来表示不同的状态
- 玩家赢时 --> 返回'*'
- 电脑赢时 --> 返回'#'
- 平局时 --> 返回'C'
- 游戏继续 --> 返回'Q'
void game() { int ret; char board[ROW][COL] = { 0 }; InitBoard(board, ROW, COL); DisplayBoard(board, ROW, COL); while (1) { PlayerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret != 'Q') { break; } ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret != 'Q') { break; } } if (ret == '*') { printf("玩家获胜\n"); } if (ret == '#') { printf("电脑获胜\n"); } if (ret == 'C') { printf("平局\n"); } }
以上的代码中包含着IsWIn函数,我们需要用它进行胜负判断,它的功能如下。
1.不仅判断行和列,还要判断两条对角线
2.判断棋盘是否被装满,是否分出胜负
int IsWin(char board[ROW][COL], int row,int col) { int i, j; 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[1][i] != ' ') { return board[0][i]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') { return board[1][1]; } if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { return board[1][1]; } if (IsFull(board, row, col) == 1) { return 'C'; } return 'Q'; }
这一块的代码比较冗杂。我们还需调用IsFull函数来判断棋盘是否已经被装满。
如果棋盘被装满且未分胜负,那就返回‘C’,代码如下。
int IsFull(char board[ROW][COL], int row, int col) { int i, j; for (i = 0;i < ROW;i++) { for (j = 0;j < COL;j++) { if (board[i][j] == ' ') { return 0;//只要有一个空格没有被填满,就返回0 } } } return 1; }
所以游戏的test.c文件应该这么写
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void game() { int ret; char board[ROW][COL] = { 0 }; InitBoard(board, ROW, COL); DisplayBoard(board, ROW, COL); while (1) { PlayerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret != 'Q') { break; } ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret != 'Q') { break; } } if (ret == '*') { printf("玩家获胜\n"); } if (ret == '#') { printf("电脑获胜\n"); } if (ret == 'C') { printf("平局\n"); } } int main() { srand((unsigned int)time(NULL)); int input; do { menu(); scanf("%d", &input); switch (input) { case 1: printf("三子棋\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择\n"); break; } } while (input); return 0; }
不要忘记包含“game.h”,那里包含着所需的头文件和函数声明。
下面是game.c文件
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void InitBoard(char board[ROW][COL],int row,int col) { int i, j; 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, j; for (i = 0;i < ROW;i++) { for (j = 0;j < COL;j++) { printf(" %c ",board[i][j]); if (j < ROW - 1) { printf("|"); } } printf("\n"); if (i < ROW - 1) { for (j = 0;j < ROW;j++) { printf("---"); if (j < col - 1) { printf("|"); } } } printf("\n"); } } void menu() { printf("**********************************\n"); printf("********* 1 play *********\n"); printf("********* 2 exit *********\n"); printf("**********************************\n"); } void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑下棋\n"); int x, y; while (1) {x = rand() % ROW; y = rand() % COL; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } void PlayerMove(char board[ROW][COL], int row, int col) { int x, y; printf("玩家下棋:\n"); do { scanf_s("%d%d", &x, &y); if (x < 1 || x > row || y < 1 || y > col) { printf("范围超出,请重新输入\n"); 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"); } } } while (1); } int IsFull(char board[ROW][COL], int row, int col) { int i, j; for (i = 0;i < ROW;i++) { for (j = 0;j < COL;j++) { if (board[i][j] == ' ') { return 0; } } } return 1; } int IsWin(char board[ROW][COL], int row,int col) { int i, j; 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[1][i] != ' ') { return board[0][i]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') { return board[1][1]; } if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { return board[1][1]; } if (IsFull(board, row, col) == 1) { return 'C'; } return 'Q'; }
最后是game.h文件
#pragma once #define COL 3 #define ROW 3 #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> void menu(); void DisplayBoard(char board[ROW][COL],int row,int col); void InitBoard(char board[ROW][COL],int row,int col); void PlayerMove(char board[ROW][COL], int row, int col); void ComputerMove(char board[ROW][COL], int row, int col); int IsFull(char board[ROW][COL], int row, int col); int IsWin(char board[ROW][COL], int row, int col);
下面让我们快乐的来上一把三子棋!
轻松赢下比赛!
本篇文章旨在分享三子棋的写法。🤠希望我的文章能够让大家有所收获!😋😋😋大佬们如果对我的文章有什么建议,或者认为那里写的不好,请在评论区写下您宝贵的意见!😀如果觉得我写的不错的话还请点个赞和关注哦~我会持续输出编程的知识的!🌞🌞🌞