C++五子棋(六)——游戏结束

规则原理

如图

1 2 3 4 5

判断游戏结束

  • chessData.h
1
2
//row,col	表示当前落子
bool checkWin(ChessData* game, int row, int col);
  • 横、竖、斜(斜有两种)共四种情况,每种情况根据当前落子往后遍历5个子,有一种符合就胜利
  • chessData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
bool checkWin(ChessData* game, int row, int col){
//横
for(int i = 0; i < 5; i++){
if(col - i >= 0 &&
col - i + 4 < BOARD_GRAD_SIZE &&
game->chessMap[row][col-i] == game->chessMap[row][col-i+1] &&
game->chessMap[row][col-i] == game->chessMap[row][col-i+2] &&
game->chessMap[row][col-i] == game->chessMap[row][col-i+3] &&
game->chessMap[row][col-i] == game->chessMap[row][col-i+4]){return true;}
}

//竖
for(int i = 0; i < 5; i++){
if(row - i >= 0 &&
row - i + 4 < BOARD_GRAD_SIZE &&
game->chessMap[row-i][col] == game->chessMap[row-i+1][col] &&
game->chessMap[row-i][col] == game->chessMap[row-i+2][col] &&
game->chessMap[row-i][col] == game->chessMap[row-i+3][col] &&
game->chessMap[row-i][col] == game->chessMap[row-i+4][col]){return true;}
}

// “/”方向
for(int i = 0; i < 5; i++){
if(row + i < BOARD_GRAD_SIZE &&
row + i - 4 >= 0 &&
col - i >= 0 &&
col - i + 4 < BOARD_GRAD_SIZE &&
game->chessMap[row+i][col-i] == game->chessMap[row+i-1][col-i+1] &&
game->chessMap[row+i][col-i] == game->chessMap[row+i-2][col-i+2] &&
game->chessMap[row+i][col-i] == game->chessMap[row+i-3][col-i+3] &&
game->chessMap[row+i][col-i] == game->chessMap[row+i-4][col-i+4]){return true;}
}

// “\”方向
for(int i = 0; i < 5; i++){
if(row - i >= 0 &&
row - i - 4 < BOARD_GRAD_SIZE &&
col - i >= 0 &&
col - i + 4 < BOARD_GRAD_SIZE &&
game->chessMap[row-i][col-i] == game->chessMap[row-i+1][col-i+1] &&
game->chessMap[row-i][col-i] == game->chessMap[row-i+2][col-i+2] &&
game->chessMap[row-i][col-i] == game->chessMap[row-i+3][col-i+3] &&
game->chessMap[row-i][col-i] == game->chessMap[row-i+4][col-i+4]){return true;}
}

return false;

}

调用接口

  • main.cpp
1
#include <stdio.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
bool checkOver(){
if(checkWin(&game, clickPosRow, clickPosCol)){
Sleep(1500);
if(game.playFlag = false){
//黑棋胜利,此时标记已经转为白棋落子
mciSendString("play res/不错.mp3", 0, 0, 0);
loadimage(0, "res/胜利.jpg");
score += 100; //更新分数
}else{
mciSendString("play res/失败.mp3", 0, 0, 0);
loadimage(0, "res/失败.jpg");
score -= 100; //同理
}


//用于显示分数
char scoreText[64];
sprintf(scoreText, "当前分数:%d", score);
outtextxy(310, 800, scoreText);

//记录分数
FILE* fp = fopen("score.data", "wb");
fwrite(&score, sizeof(score), 1, fp);
fclose(fp);


getch();
return true;

}

return false;

}

显示分数

  • main.cpp
1
2
#define INIT_SCORE 1000
int score; //全局变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void initScore(){
//分数字体设置
settextcolor(WHITE); //color
settextstyle(50, 0, "微软雅黑"); //style

FILE *fp = fopen("score.data", "rb");
if(fp == NULL){
score = INIT_SCORE;
}else{
fread(&score, sizeof(score), 1, fp);
}
if (fp)fclose(fp);

}
  • 然后在main.cpp文件的 init() 函数定义中 继续 添加代码
1
initScore();	//这一行添加到init()函数定义中

结束

到这里五子棋的全部功能已经实现了,但是你会发现程序无法运行。这是理所当然的,因为我们的main函数还没有写,在下一篇文章(也就是本项目的最后一章)我们将完善main函数并做该项目的总结。

作者

CairBin

发布于

2021-07-15

更新于

2021-07-15

许可协议

评论