You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.1 KiB

#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()
{
//创建游戏窗口
initgraph(765, 768, 1);
//显示我们的棋盘图片
loadimage(NULL, _T("res/棋盘.png"));
//加载黑棋和白棋棋子的图片
loadimage(&chessBlackImg, "res/black.png", chessSize, chessSize, true);
loadimage(&chessWhiteImg, "res/white.png", chessSize, chessSize, true);
//棋盘清零
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 {
//左上角判断
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;
}
//右上角判断
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;
}
//左下角判断
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;
}
//右下角判断
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) {
//刚才走棋的是黑方(棋手方),棋手胜
loadimage(0, "res/胜利.png",765,768);
}
else {
loadimage(0, "res/失败.png",765,768);
}
_getch();//暂停
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()
{
//最近落子点的准确位置
int row = lastPos.row;
int col = lastPos.col;
//落子点的水平方向
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;
}
}
//垂直方向
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;
}
}
//"/"方向
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;
}
}
//"\'方向
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;
}