|
|
|
|
#include "Chess.h"
|
|
|
|
|
#include<cmath>
|
|
|
|
|
#include <graphics.h>
|
|
|
|
|
#include<conio.h>
|
|
|
|
|
Chess::Chess(int gradeSize, int marginX, int marginY, float chessSize)
|
|
|
|
|
{
|
|
|
|
|
this->gradeSize = gradeSize;
|
|
|
|
|
this->margin_x = marginX;
|
|
|
|
|
this->margin_y = marginY;
|
|
|
|
|
this->chessSize = chessSize;
|
|
|
|
|
playerFlag = CHESS_BLACK;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < gradeSize; i++)
|
|
|
|
|
{
|
|
|
|
|
vector<int>row;
|
|
|
|
|
for(int j=0;j<gradeSize;j++)
|
|
|
|
|
{
|
|
|
|
|
row.push_back(0);
|
|
|
|
|
}
|
|
|
|
|
chessMap.push_back(row);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Chess::init()
|
|
|
|
|
{
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
|
|
|
|
|
initgraph(765, 768, 1);
|
|
|
|
|
//<2F><>ʾ<EFBFBD><CABE><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD>ͼƬ
|
|
|
|
|
loadimage(NULL, _T("res/<2F><><EFBFBD><EFBFBD>.png"));
|
|
|
|
|
//<2F><><EFBFBD>غ<EFBFBD><D8BA><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>ͼƬ
|
|
|
|
|
loadimage(&chessBlackImg, "res/black.png", chessSize, chessSize, true);
|
|
|
|
|
loadimage(&chessWhiteImg, "res/white.png", chessSize, chessSize, true);
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < gradeSize; i++) {
|
|
|
|
|
for (int j = 0; j < gradeSize; j++) {
|
|
|
|
|
chessMap[i][j] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
playerFlag = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Chess::clickBoard(int x, int y, ChessPos* pos)
|
|
|
|
|
{
|
|
|
|
|
int col = (x - margin_x) / chessSize;
|
|
|
|
|
int row = (y - margin_y) / chessSize;
|
|
|
|
|
int leftTopPosX = margin_x + chessSize * col;
|
|
|
|
|
int leftTopPosY = margin_y + chessSize * row;
|
|
|
|
|
int offset = chessSize * 0.1;
|
|
|
|
|
|
|
|
|
|
int len;
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
//<2F><><EFBFBD>Ͻ<EFBFBD><CFBD>ж<EFBFBD>
|
|
|
|
|
len = sqrt((x - leftTopPosX) * (x - leftTopPosX) + (y - leftTopPosY) * (x - leftTopPosY));
|
|
|
|
|
if (len < offset)
|
|
|
|
|
{
|
|
|
|
|
pos->row = row;
|
|
|
|
|
pos->col = col;
|
|
|
|
|
if (chessMap[pos->row][pos->col] == 0)
|
|
|
|
|
{
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD>Ͻ<EFBFBD><CFBD>ж<EFBFBD>
|
|
|
|
|
len = sqrt((leftTopPosX+chessSize-x) * (leftTopPosX + chessSize - x) + (y - leftTopPosY) * (x - leftTopPosY));
|
|
|
|
|
if (len < offset)
|
|
|
|
|
{
|
|
|
|
|
pos->row = row;
|
|
|
|
|
pos->col = col + 1;
|
|
|
|
|
if (chessMap[pos->row][pos->col] == 0)
|
|
|
|
|
{
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD>½<EFBFBD><C2BD>ж<EFBFBD>
|
|
|
|
|
len = sqrt((x - leftTopPosX) * (x - leftTopPosX) + (leftTopPosY+chessSize-y) * (leftTopPosY + chessSize - y));
|
|
|
|
|
if (len < offset)
|
|
|
|
|
{
|
|
|
|
|
pos->row = row + 1;
|
|
|
|
|
pos->col = col;
|
|
|
|
|
if (chessMap[pos->row][pos->col] == 0)
|
|
|
|
|
{
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD>½<EFBFBD><C2BD>ж<EFBFBD>
|
|
|
|
|
len = sqrt((leftTopPosX + chessSize - x) * (leftTopPosX + chessSize - x) + (leftTopPosY + chessSize - y) * (leftTopPosY + chessSize - y));
|
|
|
|
|
if (len < offset)
|
|
|
|
|
{
|
|
|
|
|
pos->row = row + 1;
|
|
|
|
|
pos->col = col + 1;
|
|
|
|
|
if (chessMap[pos->row][pos->col] == 0)
|
|
|
|
|
{
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} while (0);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Chess::chessDown(ChessPos* pos, chess_kind_t kind)
|
|
|
|
|
{
|
|
|
|
|
int x = margin_x + chessSize * pos->col-0.5*chessSize;
|
|
|
|
|
int y = margin_y + chessSize * pos->row-0.5*chessSize;
|
|
|
|
|
if (kind == CHESS_WHITE) {
|
|
|
|
|
putimage(x, y, &chessWhiteImg);
|
|
|
|
|
chessMap[pos->row][pos->col] = -1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
putimage(x, y, &chessBlackImg);
|
|
|
|
|
chessMap[pos->row][pos->col] = 1;
|
|
|
|
|
}
|
|
|
|
|
updateGameMap(pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Chess::getGradeSize()
|
|
|
|
|
{
|
|
|
|
|
return gradeSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Chess::getChessData(ChessPos* pos)
|
|
|
|
|
{
|
|
|
|
|
return chessMap[pos->row][pos->col];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Chess::getChessData(int row, int col)
|
|
|
|
|
{
|
|
|
|
|
return chessMap[row][col];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Chess::checkOver()
|
|
|
|
|
{
|
|
|
|
|
if (checkWin()) {
|
|
|
|
|
Sleep(1500);
|
|
|
|
|
if (playerFlag == false) {
|
|
|
|
|
//<2F>ղ<EFBFBD><D5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǻڷ<C7BA><DAB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʤ
|
|
|
|
|
loadimage(0, "res/ʤ<><CAA4>.png",765,768);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
loadimage(0, "res/ʧ<><CAA7>.png",765,768);
|
|
|
|
|
}
|
|
|
|
|
_getch();//<2F><>ͣ
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Chess::updateGameMap(ChessPos* pos)
|
|
|
|
|
{
|
|
|
|
|
lastPos = *pos;
|
|
|
|
|
chessMap[pos->row][pos->col] = playerFlag ? CHESS_BLACK : CHESS_WHITE;
|
|
|
|
|
playerFlag = !playerFlag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Chess::checkWin()
|
|
|
|
|
{
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>ȷλ<C8B7><CEBB>
|
|
|
|
|
int row = lastPos.row;
|
|
|
|
|
int col = lastPos.col;
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>ˮƽ<CBAE><C6BD><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0;i < 5; i++)
|
|
|
|
|
{
|
|
|
|
|
if (col - i >= 0 && col - i + 4 < gradeSize &&
|
|
|
|
|
chessMap[row][col - i] == chessMap[row][col - i + 1] &&
|
|
|
|
|
chessMap[row][col - i] == chessMap[row][col - i + 2] &&
|
|
|
|
|
chessMap[row][col - i] == chessMap[row][col - i + 3] &&
|
|
|
|
|
chessMap[row][col - i] == chessMap[row][col - i + 4]) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//<2F><>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
|
{
|
|
|
|
|
if (row - i >= 0 && row - i + 4 < gradeSize &&
|
|
|
|
|
chessMap[row-i][col] == chessMap[row-i+1][col] &&
|
|
|
|
|
chessMap[row-i][col] == chessMap[row-i+2][col] &&
|
|
|
|
|
chessMap[row-i][col] == chessMap[row-i+3][col] &&
|
|
|
|
|
chessMap[row-i][col] == chessMap[row-i+4][col]) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//"/"<22><><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
|
{
|
|
|
|
|
if (row+i<gradeSize&&row+i-4&&
|
|
|
|
|
col-i>=0&&col-i+4<gradeSize&&
|
|
|
|
|
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] &&
|
|
|
|
|
chessMap[row + i][col - i] == chessMap[row + i - 4][col - i + 4]) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//"\'<27><><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
|
{
|
|
|
|
|
if (row - i >= 0 && row - i + 4 < gradeSize &&
|
|
|
|
|
col - i >= 0 && col - i + 4 < gradeSize &&
|
|
|
|
|
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] &&
|
|
|
|
|
chessMap[row - i][col - i] == chessMap[row - i + 4][col - i + 4]) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|