Update Chess.cpp

main
peauxh9ov 5 months ago
parent 11e26792a0
commit 10d366fe7c

@ -1,37 +1,37 @@
#include "Chess.h" #include "Chess.h"
#include <conio.h> #include <conio.h>
//修正png透明背景无法显示网上当的 //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>png͸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD>ģ<EFBFBD>
void putimagePNG(int x, int y, IMAGE* picture) //x为载入图片的X坐标y为Y坐标 void putimagePNG(int x, int y, IMAGE* picture) //xΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼƬ<EFBFBD><EFBFBD>X<EFBFBD><EFBFBD><EFBFBD>꣬yΪY<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{ {
// 变量初始化 // <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
DWORD* dst = GetImageBuffer(); // GetImageBuffer()函数用于获取绘图设备的显存指针EASYX自带 DWORD* dst = GetImageBuffer(); // GetImageBuffer()<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>ȡ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ָ<EFBFBD>룬EASYX<EFBFBD>Դ<EFBFBD>
DWORD* draw = GetImageBuffer(); DWORD* draw = GetImageBuffer();
DWORD* src = GetImageBuffer(picture); //获取picture的显存指针 DWORD* src = GetImageBuffer(picture); //<EFBFBD><EFBFBD>ȡpicture<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ָ<EFBFBD><EFBFBD>
int picture_width = picture->getwidth(); //获取picture的宽度EASYX自带 int picture_width = picture->getwidth(); //<EFBFBD><EFBFBD>ȡpicture<EFBFBD>Ŀ<EFBFBD><EFBFBD>ȣ<EFBFBD>EASYX<EFBFBD>Դ<EFBFBD>
int picture_height = picture->getheight(); //获取picture的高度EASYX自带 int picture_height = picture->getheight(); //<EFBFBD><EFBFBD>ȡpicture<EFBFBD>ĸ߶ȣ<EFBFBD>EASYX<EFBFBD>Դ<EFBFBD>
int graphWidth = getwidth(); //获取绘图区的宽度EASYX自带 int graphWidth = getwidth(); //<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD>ȣ<EFBFBD>EASYX<EFBFBD>Դ<EFBFBD>
int graphHeight = getheight(); //获取绘图区的高度EASYX自带 int graphHeight = getheight(); //<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD>ĸ߶ȣ<EFBFBD>EASYX<EFBFBD>Դ<EFBFBD>
int dstX = 0; //在显存里像素的角标 int dstX = 0; //<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>صĽDZ<EFBFBD>
// 实现透明贴图 公式: Cp=αp*FP+(1-αp)*BP 贝叶斯定理来进行点颜色的概率计算 // ʵ<EFBFBD><EFBFBD>͸<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ <20><>ʽ<EFBFBD><CABD> Cp=<3D><>p*FP+(1-<2D><>p)*BP <20><> <20><>Ҷ˹<D2B6><CBB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>ɫ<EFBFBD>ĸ<EFBFBD><C4B8>ʼ<EFBFBD><CABC><EFBFBD>
for (int iy = 0; iy < picture_height; iy++) for (int iy = 0; iy < picture_height; iy++)
{ {
for (int ix = 0; ix < picture_width; ix++) for (int ix = 0; ix < picture_width; ix++)
{ {
int srcX = ix + iy * picture_width; //在显存里像素的角标 int srcX = ix + iy * picture_width; //<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>صĽDZ<EFBFBD>
int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA是透明度 int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA<EFBFBD><EFBFBD>͸<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int sr = ((src[srcX] & 0xff0000) >> 16); //获取RGB里的R int sr = ((src[srcX] & 0xff0000) >> 16); //<EFBFBD><EFBFBD>ȡRGB<EFBFBD><EFBFBD><EFBFBD>R
int sg = ((src[srcX] & 0xff00) >> 8); //G int sg = ((src[srcX] & 0xff00) >> 8); //G
int sb = src[srcX] & 0xff; //B int sb = src[srcX] & 0xff; //B
if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight) if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight)
{ {
dstX = (ix + x) + (iy + y) * graphWidth; //在显存里像素的角标 dstX = (ix + x) + (iy + y) * graphWidth; //<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>صĽDZ<EFBFBD>
int dr = ((dst[dstX] & 0xff0000) >> 16); int dr = ((dst[dstX] & 0xff0000) >> 16);
int dg = ((dst[dstX] & 0xff00) >> 8); int dg = ((dst[dstX] & 0xff00) >> 8);
int db = dst[dstX] & 0xff; int db = dst[dstX] & 0xff;
draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16) //公式: Cp=αp*FP+(1-αp)*BP αp=sa/255 , FP=sr , BP=dr draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16) //<EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD> Cp=<3D><>p*FP+(1-<2D><>p)*BP <20><> <20><>p=sa/255 , FP=sr , BP=dr
| ((sg * sa / 255 + dg * (255 - sa) / 255) << 8) //αp=sa/255 , FP=sg , BP=dg | ((sg * sa / 255 + dg * (255 - sa) / 255) << 8) //<EFBFBD><EFBFBD>p=sa/255 , FP=sg , BP=dg
| (sb * sa / 255 + db * (255 - sa) / 255); //αp=sa/255 , FP=sb , BP=db | (sb * sa / 255 + db * (255 - sa) / 255); //<EFBFBD><EFBFBD>p=sa/255 , FP=sb , BP=db
} }
} }
} }
@ -63,13 +63,13 @@ Chess::Chess(int gradeSize, int marginX, int marginY, double chessSize)
void Chess::init() void Chess::init()
{ {
//显示棋盘 //<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
loadimage(0, "graphics/map.png"); loadimage(0, "map.png");//更改路径
//显示棋子 //<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
loadimage(&chessBlackImg, "graphics/black.png", chessSize-2, chessSize-2, true); loadimage(&chessBlackImg, "black.png", chessSize-2, chessSize-2, true);//更改路径
loadimage(&chessWhiteImg, "graphics/white.png", chessSize-2, chessSize-2, true); loadimage(&chessWhiteImg, "white.png", chessSize-2, chessSize-2, true);//更改路径
//清零棋盘 //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < gradeSize; ++i) for (int i = 0; i < gradeSize; ++i)
for (int j = 0; j < gradeSize; ++j) for (int j = 0; j < gradeSize; ++j)
chessMap[i][j] = 0; chessMap[i][j] = 0;
@ -86,7 +86,7 @@ bool Chess::clickBoard(int x, int y, ChessPos* pos)
int len; int len;
bool ret = false; bool ret = false;
do { do {
//左上角 //<EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD>
len = sqrt((x - leftTopPosX) * (x - leftTopPosX) + (y - leftTopPosY) * (y - leftTopPosY)); len = sqrt((x - leftTopPosX) * (x - leftTopPosX) + (y - leftTopPosY) * (y - leftTopPosY));
if (len < offset) if (len < offset)
{ {
@ -97,7 +97,7 @@ bool Chess::clickBoard(int x, int y, ChessPos* pos)
break; break;
} }
//右上角 //<EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD>
int x2 = leftTopPosX + chessSize; int x2 = leftTopPosX + chessSize;
int y2 = leftTopPosY; int y2 = leftTopPosY;
len = sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2)); len = sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
@ -110,7 +110,7 @@ bool Chess::clickBoard(int x, int y, ChessPos* pos)
break; break;
} }
//左下角 //<EFBFBD><EFBFBD><EFBFBD>½<EFBFBD>
int x3 = leftTopPosX; int x3 = leftTopPosX;
int y3 = leftTopPosY + chessSize; int y3 = leftTopPosY + chessSize;
len = sqrt((x - x3) * (x - x3) + (y - y3) * (y - y3)); len = sqrt((x - x3) * (x - x3) + (y - y3) * (y - y3));
@ -123,7 +123,7 @@ bool Chess::clickBoard(int x, int y, ChessPos* pos)
break; break;
} }
//右下角 //<EFBFBD><EFBFBD><EFBFBD>½<EFBFBD>
int x4 = leftTopPosX + chessSize; int x4 = leftTopPosX + chessSize;
int y4 = leftTopPosY + chessSize; int y4 = leftTopPosY + chessSize;
len = sqrt((x - x4) * (x - x4) + (y - y4) * (y - y4)); len = sqrt((x - x4) * (x - x4) + (y - y4) * (y - y4));
@ -160,12 +160,12 @@ int Chess::getGradeSize()
bool Chess::checkWin() bool Chess::checkWin()
{ {
// 横竖斜四种大情况每种情况都根据当前落子往后遍历5个棋子有一种符合就算赢 // <EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ַ<EFBFBD><EFBFBD>Ͼ<EFBFBD><EFBFBD><EFBFBD>Ӯ
// 水平方向 // ˮƽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int row = lastPos.row; int row = lastPos.row;
int col = lastPos.col; int col = lastPos.col;
//落子水平方向 //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮƽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
if (col - i >= 0 && if (col - i >= 0 &&
@ -181,7 +181,7 @@ bool Chess::checkWin()
} }
} }
// 竖直方向(上下延伸4个) // <EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34>)
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
if (row - i >= 0 && if (row - i >= 0 &&
@ -197,14 +197,14 @@ bool Chess::checkWin()
} }
} }
// “/"方向 // <EFBFBD><EFBFBD>/"<22><><EFBFBD><EFBFBD>
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
if (row + i < gradeSize && if (row + i < gradeSize &&
row + i - 4 >= 0 && row + i - 4 >= 0 &&
col - i >= 0 && col - i >= 0 &&
col - i + 4 < gradeSize && col - i + 4 < gradeSize &&
// 第[row+i]行,第[col-i]的棋子与右上方连续4个棋子都相同 // <EFBFBD><EFBFBD>[row+i]<5D>У<EFBFBD><D0A3><EFBFBD>[col-i]<5D><><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>ͬ
chessMap[row + i][col - i] == chessMap[row + i - 1][col - i + 1] && chessMap[row + i][col - i] == chessMap[row + i - 1][col - i + 1] &&
chessMap[row + i][col - i] == chessMap[row + i - 2][col - i + 2] && chessMap[row + i][col - i] == chessMap[row + i - 2][col - i + 2] &&
chessMap[row + i][col - i] == chessMap[row + i - 3][col - i + 3] && chessMap[row + i][col - i] == chessMap[row + i - 3][col - i + 3] &&
@ -216,10 +216,10 @@ bool Chess::checkWin()
} }
} }
// “\“ 方向 // <EFBFBD><EFBFBD>\<5C><> <20><><EFBFBD><EFBFBD>
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
// 第[row+i]行,第[col-i]的棋子与右下方连续4个棋子都相同 // <EFBFBD><EFBFBD>[row+i]<5D>У<EFBFBD><D0A3><EFBFBD>[col-i]<5D><><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>ͬ
if (row - i >= 0 && if (row - i >= 0 &&
row - i + 4 < gradeSize && row - i + 4 < gradeSize &&
col - i >= 0 && col - i >= 0 &&
@ -243,15 +243,15 @@ bool Chess::checkOver()
if (checkWin()) if (checkWin())
{ {
Sleep(3000); Sleep(3000);
//赢棋 //Ӯ<EFBFBD><EFBFBD>
if (!playerFlag) if (!playerFlag)
{ {
loadimage(0, "graphics/win.jpg",400,400,true); loadimage(0, "win.jpg",400,400,true);//更改路径
} }
//失败 //ʧ<EFBFBD><EFBFBD>
else else
{ {
loadimage(0, "graphics/fail.png", 400, 400, true); loadimage(0, "fail.png", 400, 400, true);//更改路径
} }
Sleep(5000); Sleep(5000);
return true; return true;
@ -274,5 +274,5 @@ void Chess::updateGameMap(ChessPos* pos)
{ {
lastPos = *pos; lastPos = *pos;
chessMap[pos->row][pos->col] = playerFlag ? CHESS_BLACK : CHESS_WHITE; chessMap[pos->row][pos->col] = playerFlag ? CHESS_BLACK : CHESS_WHITE;
playerFlag = !playerFlag;//换方 playerFlag = !playerFlag;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
} }
Loading…
Cancel
Save