ChessData.h
bool checkWin(ChessData* game, int row, int col); //row,col表示当前落子
ChessData.cpp
bool checkWin(ChessData* game, int row, int col)
{
// 横竖斜四种大情况,每种情况都根据当前落子往后遍历5个棋子,有一种符合就算赢
// 水平方向
for (int i = 0; i < 5; i )
{
// 往左5个,往右匹配4个子,20种情况
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;
}
// 竖直方向(上下延伸4个)
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 &&
// 第[row i]行,第[col-i]的棋子,与右上方连续4个棋子都相同
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 )
{
// 第[row i]行,第[col-i]的棋子,与右下方连续4个棋子都相同
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;
}
调用AI接口
main.cpp
#include <stdio.h>
bool checkOver() {
if (checkWin(&game, clickPosRow, clickPosCol)) {
Sleep(1500);
if (game.playerFlag == false) { //黑棋赢(玩家赢),此时标记已经反转,轮到白棋落子
mciSendString("play res/不错.mp3", 0, 0, 0);
loadimage(0, "res/胜利.jpg");
} else {
mciSendString("play res/失败.mp3", 0, 0, 0);
loadimage(0, "res/失败.jpg");
}
getch();
return true;
}
return false;
}
显示分数
在胜利窗口,或者失败窗口中,显示分数。
main.cpp
#define INIT_SCORE 1000
int score; // 当前分数
void initScore() {
// 显示分数的字体设置
settextcolor(WHITE);
settextstyle(50, 0, "微软雅黑");
FILE *fp = fopen("score.data", "rb");
if (fp == NULL) {
score = INIT_SCORE;
} else {
fread(&score, sizeof(score), 1, fp);
}
if (fp)fclose(fp);
}
void init() {
......
initScore();
}
更新分数
ChessData.cpp
bool checkOver() {
if (checkWin(&game, clickPosRow, clickPosCol)) {
Sleep(1500);
if (game.playerFlag == 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;
}
项目迭代联网对战功能
通过项目的实战积累,在实战中成长
服务器联网通信开发。
游戏大厅配对功能服务器端业务开发。
AI迭代
使用搜索树,提高算度。