1 Star 7 Fork 18

Jacko/gobang

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
game.cpp 4.75 KB
一键复制 编辑 原始数据 按行查看 历史
Jacko 提交于 2024-07-14 12:10 . gobang
#include "func.h"
#include "image.h"
#include "ai.cpp"
void pvp() {
int flag = 0;
while (1) {
if (game(pl1, board))
break;
flag++;
if (flag >= 225) {
drawWin(0);
break;
}
if ( game(pl2, board) )
break;
flag++;
}
}
void pvc1() {
ai pl1(1);
int flag = 1;
prx = 8, pry = 8;
board[8][8] = 1;
while (1) {
if (flag >= 225) {
drawWin(0);
break;
}
if ( game(pl2, board) )
break;
flag ++;
pl1.aidraw(board);
flag++;
int winner;
if (winner = checkWin(board), winner) {
drawWin(winner);
break;
}
}
}
void pvc() {
ai pl2(2);
int flag = 0;
while (1) {
if (flag >= 225) {
drawWin(0);
break;
}
if ( game(pl1, board) )
break;
flag ++;
pl2.aidraw(board), flag++;
int winner;
if (winner = checkWin(board), winner) {
drawWin(winner);
break;
}
}
}
// 游戏模式选择
int choose() {
int x, y;
int flag;
mouse_msg msg;
for (bool idx = 1; idx; delay_fps(80)) {
cleardevice();
drawboard();
xyprintf(800, 780, "1");
putimage_withalpha(NULL, ic, 800, 50, 0, 0);
rectangle(790, 160, 975, 205);
putimage_withalpha(NULL, pl1, 800, 170, 0, 0);
drawchess(880, 235, bl);
rectangle(790, 290, 975, 335);
putimage_withalpha(NULL, pl2, 800, 300, 0, 0);
drawchess(880, 365, wh);
putimage_withalpha(NULL, gobang, 800, 400, 0, 0);
rectangle(810, 700, 975, 745);
putimage_withalpha(NULL, re, 860, 710, 0, 0);
while (mousemsg()) {
msg = getmouse();
if (msg.x > 790 && msg.x < 975 && msg.y > 160 && msg.y < 205 && msg.is_down() && msg.is_left()) {
flag = 1;
idx = 0;
break;
}
if (msg.x > 790 && msg.x < 975 && msg.y > 290 && msg.y < 335 && msg.is_down() && msg.is_left()) {
flag = 2;
idx = 0;
break;
} else if (msg.x > 810 && msg.x < 975 && msg.y > 700 && msg.y < 745 && msg.is_down() && msg.is_left()) {
flag = 0;
idx = 0;
break;
}
}
mousepos(&x, &y);
flood(x, y, 790, 975, 160, 205, pl1, 10);
flood(x, y, 790, 975, 290, 335, pl2, 10);
flood(x, y, 810, 975, 700, 745, re, 50);
}
return flag;
}
// 玩游戏
int game(PIMAGE player, char board[17][17]) {
int x, y;
bool flag;
mouse_msg msg;
for (bool idx1 = 1; idx1; delay_fps(80)) {
// cleardevice();
drawboard();
xyprintf(800, 780, "最终解释权归钟宏展所有");
chessInboard(board);
putimage_withalpha(NULL, ic, 800, 50, 0, 0);
rectangle(790, 160, 975, 205);
putimage_withalpha(NULL, pl1, 800, 170, 0, 0);
drawchess(880, 235, bl);
rectangle(790, 290, 975, 335);
putimage_withalpha(NULL, pl2, 800, 300, 0, 0);
drawchess(880, 365, wh);
putimage_withalpha(NULL, gobang, 800, 400, 0, 0);
drawchess(prx * 50, pry * 50, idex);
if (player == pl1)
flood(800, 170, 790, 975, 160, 205, pl1, 10);
else
flood(800, 300, 790, 975, 290, 335, pl2, 10);
mousepos(&x, &y);
rectangle(810, 700, 975, 745);
putimage_withalpha(NULL, re, 860, 710, 0, 0);
flood(x, y, 810, 975, 700, 745, re, 50);
// 打印虚拟棋子
if (x > 25 && x < 775 && y > 25 && y < 775) {
if (!board[(x + 25) / 50][(y + 25) / 50]) {
if (player == pl1)
drawchess((x + 25) / 50 * 50, (y + 25) / 50 * 50, vbl);
else
drawchess((x + 25) / 50 * 50, (y + 25) / 50 * 50, vwh);
}
}
// 监听鼠标动作
while (mousemsg()) {
msg = getmouse();
if (msg.x > 25 && msg.x < 775 && msg.y > 25 && msg.y < 775 && msg.is_down() && msg.is_left()) {
if (!board[(x + 25) / 50][(y + 25) / 50]) {
prx = (x + 25) / 50;
pry = (y + 25) / 50;
board[(x + 25) / 50][(y + 25) / 50] = player == pl1 ? 1 : 2;
idx1 = 0;
flag = 0;
break;
}
} else if (msg.x > 810 && msg.x < 975 && msg.y > 700 && msg.y < 745 && msg.is_down() && msg.is_left()) {
flag = 1;
idx1 = 0;
break;
}
}
}
int winner = checkWin(board);
if (winner) {
flag = winner;
drawWin(winner);
}
return flag;
}
// 判断是否有人赢了
int checkWin(char board[][17]) {
for (int i = 1; i <= 15; i++) {
for (int j = 1; j <= 15 ; j++) {
if (board[i][j]) {
int flag = 1;
if (i < 12 && j > 4) {
for (int k = 1; k <= 4; k++)
if (board[i][j] != board[i + k][j - k])
flag = 0;
} else
flag = 0;
if (flag)
return board[i][j];
flag = 1;
if (j < 12) {
for (int k = 1; k <= 4; k++)
if (board[i][j] != board[i][j + k])
flag = 0;
} else
flag = 0;
if (flag)
return board[i][j];
flag = 1;
if (i < 12) {
for (int k = 1; k <= 4; k++)
if (board[i][j] != board[i + k][j])
flag = 0;
} else
flag = 0;
if (flag)
return board[i][j];
flag = 1;
if (j < 12 && i < 12) {
for (int k = 1; k <= 4; k++)
if (board[i][j] != board[i + k][j + k])
flag = 0;
} else
flag = 0;
if (flag)
return board[i][j];
}
}
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jacko-john/gobang.git
git@gitee.com:jacko-john/gobang.git
jacko-john
gobang
gobang
master

搜索帮助