Update Chess.cpp

main
peauxh9ov 5 months ago
parent 11e26792a0
commit 10d366fe7c

@ -1,37 +1,37 @@
#include "Chess.h"
#include <conio.h>
//修正png透明背景无法显示网上当的
void putimagePNG(int x, int y, IMAGE* picture) //x为载入图片的X坐标y为Y坐标
//<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Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼƬ<EFBFBD><EFBFBD>X<EFBFBD><EFBFBD><EFBFBD>꣬yΪY<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
// 变量初始化
DWORD* dst = GetImageBuffer(); // GetImageBuffer()函数用于获取绘图设备的显存指针EASYX自带
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
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* src = GetImageBuffer(picture); //获取picture的显存指针
int picture_width = picture->getwidth(); //获取picture的宽度EASYX自带
int picture_height = picture->getheight(); //获取picture的高度EASYX自带
int graphWidth = getwidth(); //获取绘图区的宽度EASYX自带
int graphHeight = getheight(); //获取绘图区的高度EASYX自带
int dstX = 0; //在显存里像素的角标
DWORD* src = GetImageBuffer(picture); //<EFBFBD><EFBFBD>ȡpicture<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ָ<EFBFBD><EFBFBD>
int picture_width = picture->getwidth(); //<EFBFBD><EFBFBD>ȡpicture<EFBFBD>Ŀ<EFBFBD><EFBFBD>ȣ<EFBFBD>EASYX<EFBFBD>Դ<EFBFBD>
int picture_height = picture->getheight(); //<EFBFBD><EFBFBD>ȡpicture<EFBFBD>ĸ߶ȣ<EFBFBD>EASYX<EFBFBD>Դ<EFBFBD>
int graphWidth = getwidth(); //<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD>ȣ<EFBFBD>EASYX<EFBFBD>Դ<EFBFBD>
int graphHeight = getheight(); //<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD>ĸ߶ȣ<EFBFBD>EASYX<EFBFBD>Դ<EFBFBD>
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 ix = 0; ix < picture_width; ix++)
{
int srcX = ix + iy * picture_width; //在显存里像素的角标
int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA是透明度
int sr = ((src[srcX] & 0xff0000) >> 16); //获取RGB里的R
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<EFBFBD><EFBFBD>͸<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int sr = ((src[srcX] & 0xff0000) >> 16); //<EFBFBD><EFBFBD>ȡRGB<EFBFBD><EFBFBD><EFBFBD>R
int sg = ((src[srcX] & 0xff00) >> 8); //G
int sb = src[srcX] & 0xff; //B
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 dg = ((dst[dstX] & 0xff00) >> 8);
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
| ((sg * sa / 255 + dg * (255 - sa) / 255) << 8) //αp=sa/255 , FP=sg , BP=dg
| (sb * sa / 255 + db * (255 - sa) / 255); //αp=sa/255 , FP=sb , BP=db
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) //<EFBFBD><EFBFBD>p=sa/255 , FP=sg , BP=dg
| (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()
{
//显示棋盘
loadimage(0, "graphics/map.png");
//显示棋子
loadimage(&chessBlackImg, "graphics/black.png", chessSize-2, chessSize-2, true);
loadimage(&chessWhiteImg, "graphics/white.png", chessSize-2, chessSize-2, true);
//<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
loadimage(0, "map.png");//更改路径
//<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
loadimage(&chessBlackImg, "black.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 j = 0; j < gradeSize; ++j)
chessMap[i][j] = 0;
@ -86,7 +86,7 @@ bool Chess::clickBoard(int x, int y, ChessPos* pos)
int len;
bool ret = false;
do {
//左上角
//<EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD>
len = sqrt((x - leftTopPosX) * (x - leftTopPosX) + (y - leftTopPosY) * (y - leftTopPosY));
if (len < offset)
{
@ -97,7 +97,7 @@ bool Chess::clickBoard(int x, int y, ChessPos* pos)
break;
}
//右上角
//<EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD>
int x2 = leftTopPosX + chessSize;
int y2 = leftTopPosY;
len = sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
@ -110,7 +110,7 @@ bool Chess::clickBoard(int x, int y, ChessPos* pos)
break;
}
//左下角
//<EFBFBD><EFBFBD><EFBFBD>½<EFBFBD>
int x3 = leftTopPosX;
int y3 = leftTopPosY + chessSize;
len = sqrt((x - x3) * (x - x3) + (y - y3) * (y - y3));
@ -123,7 +123,7 @@ bool Chess::clickBoard(int x, int y, ChessPos* pos)
break;
}
//右下角
//<EFBFBD><EFBFBD><EFBFBD>½<EFBFBD>
int x4 = leftTopPosX + chessSize;
int y4 = leftTopPosY + chessSize;
len = sqrt((x - x4) * (x - x4) + (y - y4) * (y - y4));
@ -160,12 +160,12 @@ int Chess::getGradeSize()
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 col = lastPos.col;
//落子水平方向
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮƽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < 5; i++)
{
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++)
{
if (row - i >= 0 &&
@ -197,14 +197,14 @@ bool Chess::checkWin()
}
}
// “/"方向
// <EFBFBD><EFBFBD>/"<22><><EFBFBD><EFBFBD>
for (int i = 0; i < 5; i++)
{
if (row + i < gradeSize &&
row + i - 4 >= 0 &&
col - i >= 0 &&
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 - 2][col - i + 2] &&
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++)
{
// 第[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 &&
row - i + 4 < gradeSize &&
col - i >= 0 &&
@ -243,15 +243,15 @@ bool Chess::checkOver()
if (checkWin())
{
Sleep(3000);
//赢棋
//Ӯ<EFBFBD><EFBFBD>
if (!playerFlag)
{
loadimage(0, "graphics/win.jpg",400,400,true);
loadimage(0, "win.jpg",400,400,true);//更改路径
}
//失败
//ʧ<EFBFBD><EFBFBD>
else
{
loadimage(0, "graphics/fail.png", 400, 400, true);
loadimage(0, "fail.png", 400, 400, true);//更改路径
}
Sleep(5000);
return true;
@ -274,5 +274,5 @@ void Chess::updateGameMap(ChessPos* pos)
{
lastPos = *pos;
chessMap[pos->row][pos->col] = playerFlag ? CHESS_BLACK : CHESS_WHITE;
playerFlag = !playerFlag;//换方
playerFlag = !playerFlag;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
Loading…
Cancel
Save