C++与easyx实现双人象棋
立即下载
资源介绍:
C++与easyx实现双人象棋
#include
#include
#include
#include
using namespace std;
int operate = 0, operatex, operatey, signx, signy;
bool judge_red_white=0;
//设置逻辑棋盘
int ChessBoard[12][11] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,1,2,3,4,5,4,3,2,1,-1,
-1,0,0,0,0,0,0,0,0,0,-1,
-1,0,6,0,0,0,0,0,6,0,-1,
-1,7,0,7,0,7,0,7,0,7,-1,
-1,0,0,0,0,0,0,0,0,0,-1,
-1,0,0,0,0,0,0,0,0,0,-1,
-1,8,0,8,0,8,0,8,0,8,-1,
-1,0,9,0,0,0,0,0,9,0,-1,
-1,0,0,0,0,0,0,0,0,0,-1,
-1,10,11,12,13,14,13,12,11,10,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
};
void DrawForesign(int x, int y, bool a, bool b)//绘制准星线条
{
if (a == 1) {
line(x - 5, y - 5, x - 5, y - 25);
line(x - 5, y - 5, x - 25, y - 5);
line(x - 5, y + 5, x - 5, y + 25);
line(x - 5, y + 5, x - 25, y + 5);
}
if (b == 1) {
line(x + 5, y - 5, x + 5, y - 25);
line(x + 5, y - 5, x + 25, y - 5);
line(x + 5, y + 5, x + 5, y + 25);
line(x + 5, y + 5, x + 25, y + 5);
}
}
static void DrawChessBoard()//绘制棋盘
{
setcolor(RGB(255, 102, 102));
for (int i = 45; i <= 855; i += 90) {//横线绘制
line(45, i, 765, i);
}
for (int i = 45; i <= 765; i += 90) {//竖线绘制
if (i == 45 || i == 765) {
line(i, 45, i, 855);
}
else {
line(i, 45, i, 405);
line(i, 495, i, 855);
}
}
//大边框绘制
line(42, 42, 42, 858);
line(42, 858, 768, 858);
line(768, 858, 768, 42);
line(768, 42, 42, 42);
//绘制九宫
line(315, 45, 495, 225);
line(495, 45, 315, 225);
line(315, 675, 495, 855);
line(495, 675, 315, 855);
//绘制兵准星
for (int i = 0; i <= 8; i += 2) {
if (i == 0) {
DrawForesign(45 + i * 90, 315, 0, 1);
DrawForesign(45 + i * 90, 585, 0, 1);
}
else if (i == 8) {
DrawForesign(45 + i * 90, 315, 1, 0);
DrawForesign(45 + i * 90, 585, 1, 0);
}
else {
DrawForesign(45 + i * 90, 315, 1, 1);
DrawForesign(45 + i * 90, 585, 1, 1);
}
}
//绘制炮准星
DrawForesign(135, 225, 1, 1);
DrawForesign(675, 225, 1, 1);
DrawForesign(135, 675, 1, 1);
DrawForesign(675, 675, 1, 1);
}
void DrawChessman(int x, int y, char *a,bool object) //绘制单个棋子
{
if (object) {
setcolor(WHITE);
setfillcolor(BLACK);
fillcircle(x, y, 34);
fillcircle(x, y, 28);
setcolor(RGB(255,102,102));
settextstyle(30, 23, "宋体");
outtextxy(x - 23, y - 14, a);
}
else {
setcolor(WHITE);
setfillcolor(BLACK);
fillcircle(x, y, 34);
fillcircle(x, y, 28);
settextstyle(30, 23, "宋体");
outtextxy(x - 23, y - 14, a);
}
}
static void DrawALLChessman() //绘制所有棋子
{
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 9; j++) {
if (ChessBoard[i][j] == 1) {
char ChessArr[] = "车";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 0);
}
if (ChessBoard[i][j] == 2) {
char ChessArr[] = "马";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 0);
}
if (ChessBoard[i][j] == 3) {
char ChessArr[] = "象";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 0);
}
if (ChessBoard[i][j] == 4) {
char ChessArr[] = "士";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 0);
}
if (ChessBoard[i][j] == 5) {
char ChessArr[] = "将";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 0);
}
if (ChessBoard[i][j] == 6) {
char ChessArr[] = "炮";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 0);
}
if (ChessBoard[i][j] == 7) {
char ChessArr[] = "卒";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 0);
}
if (ChessBoard[i][j] == 8) {
char ChessArr[] = "兵";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 1);
}
if (ChessBoard[i][j] == 9) {
char ChessArr[] = "炮";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 1);
}
if (ChessBoard[i][j] == 10) {
char ChessArr[] = "车";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 1);
}
if (ChessBoard[i][j] == 11) {
char ChessArr[] = "马";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 1);
}
if (ChessBoard[i][j] == 12) {
char ChessArr[] = "相";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 1);
}
if (ChessBoard[i][j] == 13) {
char ChessArr[] = "士";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 1);
}
if (ChessBoard[i][j] == 14) {
char ChessArr[] = "帅";
DrawChessman(j * 90 - 45, i * 90 - 45, ChessArr, 1);
}
}
}
}
bool JudgeGuideline(int x1, int y1, int x2, int y2) //判断棋子下法是否正确
{
//判断不能吃自己的子
if (ChessBoard[x2][y2] > 0) {
if (ChessBoard[x2][y2] < 8 && ChessBoard[x1][y1] < 8)return 0;
if (ChessBoard[x2][y2] > 7 && ChessBoard[x1][y1] > 7)return 0;
}
//车的走法
if (ChessBoard[x1][y1] == 1||ChessBoard[x1][y1]==10) {
if (x1 == x2 && y1 != y2) {
for (int i = min(y1, y2)+1; i < max(y1, y2); i++) {
if (ChessBoard[x1][i] > 0)return 0;
}
return 1;
}
else if (x1 != x2 && y1 == y2) {
for (int i = min(x1, x2)+1; i < max(x1, x2); i++) {
if (ChessBoard[i][y1] > 0)return 0;
}
return 1;
}else {
return 0;
}
}
//马的走法
if (ChessBoard[x1][y1]==2||ChessBoard[x1][y1]==11) {
if (x1 - x2 == 2 && (y1 - y2 == 1 || y1 - y2 == -1)) {
if (ChessBoard[x1 - 1][y1] > 0)return 0;
else return 1;
}
else if (x2 - x1 == 2 && (y1 - y2 == 1 || y1 - y2 == -1)) {
if (ChessBoard[x1 + 1][y1] > 0)return 0;
else return 1;
}
else if (y2 - y1 == 2 && (x1 - x2 == 1 || x1 - x2 == -1)) {
if (ChessBoard[x1][y1 + 1] > 0)return 0;
else return 1;
}
else if (y2 - y1 == -2 && (x1 - x2 == 1 || x1 - x2 == -1)) {
if (ChessBoard[x1][y1 - 1] > 0)return 0;
else return 1;
}
else {
return 0;
}
}
//炮的走法
if (ChessBoard[x1][y1] == 6 || ChessBoard[x1][y1] == 9) {
if (ChessBoard[x2][y2] == 0) {
if (x1 == x2 && y1 != y2) {
for (int i = min(y1, y2)+1; i < max(y1, y2); i++) {
if (ChessBoard[x1][i] > 0)return 0;
}
return 1;
}
else if (x1 != x2 && y1 == y2) {
for (int i = min(x1, x2)+1; i < max(x1, x2); i++) {
if (ChessBoard[i][y1] > 0)return 0;
}
return 1;
}
else {
return 0;
}
}
else {
if (x1 == x2 && y1 != y2) {
int temp = 0;
for (int i = min(y1, y2) + 1; i < max(y1, y2); i++) {
if (ChessBoard[x1][i] > 0)temp++;
}
if (temp == 1)return 1;
else return 0;
}
else if(x1 != x2 && y1 == y2){
int temp = 0;
for (int i = min(x1, x2) + 1; i < max(x1, x2); i++) {
if (ChessBoard[i][y1] > 0)temp++;
}
if (temp == 1)return 1;
else return 0;
}
else {
return 0;
}
}
}
//卒的走法
if (ChessBoard[x1][y1] == 7) {
if (x1 < 6) {
if (y1 == y2 && x2 - x1 == 1)return 1;
else return 0;
}
else {
if (x1 == x2 && (y1 - y2 == 1 || y2 - y1 == 1))return 1;
else if (x2 - x1 == 1 && y1 == y2)return 1;
else return 0;
}
}
//兵的走法
if (ChessBoard[x1][y1] == 8) {
if (x1 > 5) {
if (y1 == y2 && x2 - x1 == -1)return 1;
else return 0;
}
else {
if (x1 == x2 && (y1 - y2 == 1 || y2 - y1 == 1))return 1;
else if (x2 - x1 == -1 && y1 == y2)return 1;
else return 0;
}
}
//象的走法
if (ChessBoard[x1][y1] == 3) {
if (x2 < 6) {
if ((x1 - x2 == 2 || x2 - x1 == 2) && (y1 - y2 == 2 || y2 - y1 == 2)) {
if (ChessBoard[(x1 + x2) / 2][(y1 + y2) / 2] == 0)return 1;
else return 0;
}
else return 0;
}
else return 0;
}
//相的走法
if (ChessBoard[x1][y1] == 12) {
if (x2 > 5) {
if ((x1 - x2 == 2 || x2 - x1 == 2) && (y1 - y2 == 2 || y2 - y1 == 2)) {
if (ChessBoard[(x1 + x2) / 2][(y1 + y2) / 2] == 0)return 1;
else return 0;
}
else return 0;
}
else return 0;
}
//黑士走法
if (ChessBoard[x1][y1] == 4) {
if ((x2 >= 1 && x2 <= 3) && (y2 <= 6 && y2 >= 4)) {
if ((x2 - x1 == 1 || x1 - x2 == 1) && (y1 - y2 == 1 || y2 - y1 == 1)) {
return 1;
}