C/C++:实现象棋游戏

article/2025/9/20 15:48:43
  • 大体思路
    采用面相过程的设计方式实现,类似于我们平时做的课程设计,实现这样的小游戏无非就是多了图形处理库。这里使用的是acllib图形库。

    设计这种小游戏,首先要从宏观上去认识:象棋,要有棋盘,要有棋子,棋子要移动。
    对于棋盘,十行九列画出即可。
    对于棋子,分黑红两方,按照指定位置画出。
    如何移动棋子,我们有mouseEvent函数。

    初始化棋盘棋子:initmap,initpaint
    利用鼠标实现棋子移动分两步(mouseEvent):
    第一次点击,记录点击棋子的信息
    第二次点击,将第一次点击记录的棋子信息移动到第二次点击的位置。
    最后在添加亿点细节。

  • 首先画棋盘——chessboard()

  • 初始化棋子信息——initmp()

  • 将棋子画到棋盘上——initpaint()

  • 注册鼠标,通过鼠标点击不同位置实现棋子的移动(按照象棋行棋规范)——mouseEvent(int x, int y, int button, int e)x、y为点击位置,button(鼠标左右键与滑轮)、e(点击或抬起)为事件。

  • 行棋规范——judge_piece(int x, int y)代表能否从上一步走到x,y处。

此外,实现象棋必然缺不了人机对战。对于人机算法我们可以采用最简单的遍历。每次遍历获取所有人机方棋子能走的地方,然后根据评估函数找出这些步中对人机方来说收益最大的一步。思路很简单,但是想写出走一步看很多步的算法也很难。首先评估函数要尽量具有代表性,这直接决定了人机的棋力;其次需要迭代遍历,这样次数多了时间复杂度就会很高。

这里只写了走一步看一步的人机代码供参考,更深入的可以自行去了解:

1、void Get_All_path()——获取人机所有可能走的方式(将step存入向量v中)

2、int calculate_value()——评估函数

3、void Get_max_way()——得到对人机最有利的那一步(传到prex,prey与nowx,nowy中,即从pre走到now)。
在这里插入图片描述

struct step
{int x1, y1, x2, y2;
};//从x1,y1走到x2,y2
void Get_All_path()
{v.clear();//全局变量step temp;for (int a = 0; a < 16; a++){if (black_chess[a].is == 0)continue;prex = black_chess[a].row;prey = black_chess[a].col;temp.x1 = prex;temp.y1 = prey;for(int i=0;i<10;i++)for (int j = 0; j < 9; j++){temp.x2 = i;temp.y2 = j;if (judge_piece(i, j) == 1&&mp[i][j].color!='B'){v.push_back(temp);}}}
}
int calculate_value()
{int sum1 = 0, sum2 = 0;for(int a=0;a<10;a++)for (int b = 0; b < 9; b++){if (mp[a][b].color == 'B' && mp[a][b].is == 1){switch (mp[a][b].index){case 0:sum1 += 100;break;case 1:sum1 += 50;break;case 2:sum1 += 10;case 3:sum1 += 10;break;case 4:sum1 += 1500;break;case 5:sum1 += 50;break;case 6:sum1 += 20;break;default:break;}}else if (mp[a][b].color == 'R' && mp[a][b].is == 1){switch (mp[a][b].index){case 7:sum2 += 100;break;case 8:sum2 += 50;break;case 9:sum2 += 10;case 10:sum2 += 10;break;case 11:sum2 += 1500;break;case 12:sum2 += 50;break;case 13:sum2 += 20;break;default:break;}}}return sum1 - sum2;
}
void Get_max_way()
{//step ans;int minn = -150000;int calculate;Get_All_path();for (int a = 0; a<v.size(); a++){int f = 1;int index= mp[v[a].x2][v[a].y2].index;int pos=0;int generalx=mp[v[a].x1][v[a].y1].row, generaly= mp[v[a].x1][v[a].y1].col;if (mp[v[a].x2][v[a].y2].color == 'R')//黑方吃子{if (mp[v[a].x2][v[a].y2].pos == 1)//被吃掉的为南兵{pos = 1;mp[v[a].x2][v[a].y2].pos = 0;}if (mp[v[a].x1][v[a].y1].index == 4)//黑将移动{black_general_x = v[a].x2;black_general_y = v[a].y2;}mp[v[a].x1][v[a].y1].is = 0;mp[v[a].x1][v[a].y1].color = NULL;mp[v[a].x2][v[a].y2].is = 1;mp[v[a].x2][v[a].y2].color = 'B';mp[v[a].x2][v[a].y2].index = mp[v[a].x1][v[a].y1].index;}else{f = 0;if (mp[v[a].x1][v[a].y1].index == 4){black_general_x = v[a].x2;black_general_y = v[a].y2;}mp[v[a].x1][v[a].y1].is = 0;mp[v[a].x1][v[a].y1].color = NULL;mp[v[a].x2][v[a].y2].is = 1;mp[v[a].x2][v[a].y2].color = 'B';mp[v[a].x2][v[a].y2].index = mp[v[a].x1][v[a].y1].index;}calculate = calculate_value();if (minn < calculate){prex = v[a].x1;prey = v[a].y1;minn = calculate;nowx = v[a].x2;nowy = v[a].y2;}//printf("%d\n", minn);if (f == 1){mp[v[a].x2][v[a].y2].pos = pos;if (mp[v[a].x2][v[a].y2].index == 4){black_general_x = generalx;black_general_y = generaly;}mp[v[a].x1][v[a].y1].is = 1;mp[v[a].x1][v[a].y1].color = 'B';mp[v[a].x1][v[a].y1].index = mp[v[a].x2][v[a].y2].index;mp[v[a].x2][v[a].y2].color = 'R';mp[v[a].x2][v[a].y2].index = index;mp[v[a].x2][v[a].y2].is = 1;}else{if (mp[v[a].x2][v[a].y2].index == 4){black_general_x = generalx;black_general_y = generaly;}mp[v[a].x1][v[a].y1].is = 1;mp[v[a].x1][v[a].y1].color = 'B';mp[v[a].x1][v[a].y1].index = mp[v[a].x2][v[a].y2].index;mp[v[a].x2][v[a].y2].color = NULL;mp[v[a].x2][v[a].y2].index = index;mp[v[a].x2][v[a].y2].is = 0;}}//printf("%d\n", v.size());//printf("%d\n", minn);//return ans;
}

不含人机的代码如下:

#include"acllib.h"
#include<math.h>
#include<stdio.h>
#include<queue>
#define INTERVAL 50
using namespace std;
ACL_Image chessimg;
const double pi = 3.14;
//左上角为:(102,100)
// width:55
//height:55
//棋盘大小:440*495
//棋子半径:20
const char* chessname[] = { "車","馬","象","士","将","炮","卒","车","马","相","仕","帥","砲","兵" };
struct piece
{int x, y;int index;int is;char color;int pos;//兵 卒是否过河int row, col;
};
deque<piece>q;//悔棋——三步
void paint(piece s);
struct piece mp[10][9];
void initmp();
void initpaint();
void mouseEvent(int x, int y, int button, int e);
void paint_rect(int tmpx, int tmpy);
int judge_piece(int x, int y);
void chessboard();
int mouseflag = 0;
int prex, prey;
int black_general_x = 0, black_general_y = 4, black_general_isexist = 1;
int red_general_x = 9, red_general_y = 4, red_general_isexist = 1;
piece tmp;
int judge_general(int x, int y);
int Setup()
{//initConsole();initWindow("chess", DEFAULT, DEFAULT, 650, 701);chessboard();//loadImage("qipan.bmp", &chessimg);/*beginPaint();putImageScale(&chessimg, 0, 0, 650, 701);endPaint();*/registerMouseEvent(mouseEvent);//paint(piece s);initmp();initpaint();return 0;
}
void mouseEvent(int x, int y, int button, int e)
{if (button == LEFT_BUTTON && e == BUTTON_DOWN){if (x >= INTERVAL + 8 * 55 + 33 && x <= INTERVAL + 8 * 55 + 33 + 100 && y >= INTERVAL + 55 && y <= INTERVAL + 55 + 100)// INTERVAL + 8 * 55 + 33, INTERVAL + 55){/*beginPaint();setPenColor(BLACK);setPenWidth(3);setBrushColor(EMPTY);rectangle(INTERVAL + 8 * 55 + 33, INTERVAL + 55, INTERVAL + 8 * 55 + 33 + 100, INTERVAL + 55 + 50);endPaint();*/if (!q.empty()){piece p1 = q.back();q.pop_back();piece p2 = q.back();q.pop_back();//piece p3 = p1;mp[p1.row][p1.col].color = p1.color;mp[p1.row][p1.col].index = p1.index;mp[p1.row][p1.col].is = p1.is;mp[p1.row][p1.col].pos = p1.pos;mp[p2.row][p2.col].color = p2.color;mp[p2.row][p2.col].index = p2.index;mp[p2.row][p2.col].is = p2.is;mp[p2.row][p2.col].pos = p2.pos;initpaint();}}int tmpx, tmpy, f = 0;for (int a = 0; a < 10; a++){if (f == 1)break;for (int b = 0; b < 9; b++){if (sqrt(((double)x - mp[a][b].x) * ((double)x - mp[a][b].x) + ((double)y - mp[a][b].y) * ((double)y - mp[a][b].y)) <= 20){f = 1;tmpx = a;tmpy = b;break;}}}if (f == 1)//有格子{initpaint();//paint_rect(tmpx, tmpy);/*tmp.x = mp[tmpx][tmpy].x;tmp.y = mp[tmpx][tmpy].y;tmp.is = mp[tmpx][tmpy].is;tmp.index = mp[tmpx][tmpy].index;*/if (mp[tmpx][tmpy].is == 1)//格子内有棋子{if (tmp.color == mp[tmpx][tmpy].color || tmp.color == NULL)//同色copy{paint_rect(tmpx, tmpy);prex = tmpx;prey = tmpy;tmp.is = mp[tmpx][tmpy].is;tmp.index = mp[tmpx][tmpy].index;tmp.color = mp[tmpx][tmpy].color;}else if (judge_piece(tmpx, tmpy) == 1)    //异色吃掉{q.push_back(mp[tmpx][tmpy]);q.push_back(mp[prex][prey]);while (q.size() > 6) {q.pop_front();q.pop_front();}if (mp[tmpx][tmpy].pos == 1)//若被吃掉的是南兵{mp[tmpx][tmpy].pos = 0;}else if (mp[prex][prey].pos == 1)//南兵吃别人{mp[prex][prey].pos = 0;mp[tmpx][tmpy].pos = 1;}if (mp[prex][prey].index == 4)//若是黑将移动,黑将位置重新赋值{black_general_x = tmpx;black_general_y = tmpy;}else if (mp[prex][prey].index == 11)//若是红将移动,红将位置重新赋值{red_general_x = tmpx;red_general_y = tmpy;}if (mp[tmpx][tmpy].index == 11)red_general_isexist = 0;else if (mp[tmpx][tmpy].index == 4)black_general_isexist = 0;mp[tmpx][tmpy].color = tmp.color;mp[tmpx][tmpy].index = tmp.index;mp[prex][prey].is = 0;mp[prex][prey].color = NULL;tmp.is = 0;tmp.color = NULL;initpaint();if (judge_general(tmpx, tmpy) == 1){beginPaint();setTextColor(RED);setTextSize(30);setTextBkColor(EMPTY);paintText(250, 600, "将   军!");endPaint();}}else{beginPaint();setTextColor(RED);setTextSize(30);setTextBkColor(EMPTY);paintText(250, 600, "请规范行棋");endPaint();tmp.is = 0;tmp.color = NULL;}}else//格子内没有棋子{//paint_rect(tmpx, tmpy);if (tmp.is == 1){if (judge_piece(tmpx, tmpy) == 1){q.push_back(mp[tmpx][tmpy]);q.push_back(mp[prex][prey]);if (q.size() > 6) {q.pop_front();q.pop_front();}if (mp[prex][prey].pos == 1){mp[prex][prey].pos = 0;mp[tmpx][tmpy].pos = 1;}if (mp[prex][prey].index == 4)//若是黑将移动,黑将位置重新赋值{black_general_x = tmpx;black_general_y = tmpy;}else if (mp[prex][prey].index == 11)//若是红将移动,红将位置重新赋值{red_general_x = tmpx;red_general_y = tmpy;}mp[prex][prey].is = 0;mp[prex][prey].color = NULL;mp[tmpx][tmpy].index = tmp.index;mp[tmpx][tmpy].is = tmp.is;mp[tmpx][tmpy].color = tmp.color;tmp.is = 0;tmp.color = NULL;initpaint();if (judge_general(tmpx, tmpy) == 1){beginPaint();setTextColor(RED);setTextSize(30);setTextBkColor(EMPTY);paintText(250, 600, "将   军!");endPaint();}}else{beginPaint();setTextColor(RED);setTextSize(30);setTextBkColor(EMPTY);paintText(250, 600, "请规范行棋");endPaint();tmp.is = 0;tmp.color = NULL;}//initpaint();}else paint_rect(tmpx, tmpy);//initpaint();}}if (red_general_isexist == 0){beginPaint();setTextSize(70);setTextColor(BLACK);setTextBkColor(RGB(3, 168, 158));paintText(130, 270, "黑方获胜");endPaint();}else if (black_general_isexist == 0){beginPaint();setTextSize(70);setTextColor(RED);setTextBkColor(RGB(3, 168, 158));paintText(130, 270, "红方获胜");endPaint();}}else if (button == RIGHT_BUTTON && e == BUTTON_DOWN){initpaint();tmp.is = 0;tmp.color = NULL;}//initpaint();
}
void paint(piece s)
{beginPaint();//clearDevice();//putImageScale(&chessimg, 0, 0, 650, 701);setPenColor(RGB(245, 222, 179));setPenWidth(3);setBrushColor(RGB(245, 222, 179));ellipse(s.x - 20, s.y - 20, s.x + 20, s.y + 20);if (s.color == 'B')setTextColor(BLACK);else setTextColor(RED);setTextSize((int)(20 * sin(pi / 4)) * 2);setTextBkColor(EMPTY);paintText(s.x - (int)(20 * sin(pi / 4)), s.y - (int)(20 * sin(pi / 4)), chessname[s.index]);endPaint();
}
/*void paintb(piece s)
{beginPaint();//clearDevice();setPenColor(RGB(245, 222, 179));setPenWidth(3);setBrushColor(RGB(245, 222, 179));ellipse(s.x - 20, s.y - 20, s.x + 20, s.y + 20);setTextColor(BLACK);setTextSize((int)(20 * sin(pi / 4)) * 2);setTextBkColor(EMPTY);paintText(s.x - (int)(20 * sin(pi / 4)), s.y - (int)(20 * sin(pi / 4)), chessname[s.index]);endPaint();
}*/
void initmp()
{for (int a = 0; a < 10; a++){for (int b = 0; b < 9; b++){mp[a][b].x = INTERVAL + 55 * (b);mp[a][b].y = INTERVAL + 55 * (a);mp[a][b].color = NULL;mp[a][b].index = -1;mp[a][b].is = 0;mp[a][b].pos = 0;mp[a][b].row = a;mp[a][b].col = b;}}int j = 0;for (int a = 0; a < 5; a++){mp[0][a].index = j++;mp[0][a].is = 1;mp[0][a].color = 'B';}j = 3;for (int a = 5; a < 9; a++){mp[0][a].index = j--;mp[0][a].is = 1;mp[0][a].color = 'B';}int i = 7;for (int a = 0; a < 5; a++){mp[9][a].index = i++;mp[9][a].is = 1;mp[9][a].color = 'R';}i = 10;for (int a = 5; a < 9; a++){mp[9][a].index = i--;mp[9][a].is = 1;mp[9][a].color = 'R';}mp[2][1].index = 5;mp[2][7].index = 5;mp[2][1].color = 'B';mp[2][7].color = 'B';mp[7][1].index = 12;mp[7][7].index = 12;mp[7][1].color = 'R';mp[7][7].color = 'R';mp[2][1].is = 1;mp[2][7].is = 1;mp[7][1].is = 1;mp[7][7].is = 1;for (int a = 0; a < 9; a += 2){mp[3][a].index = 6;mp[6][a].index = 13;mp[3][a].is = 1;mp[6][a].is = 1;mp[3][a].color = 'B';mp[6][a].color = 'R';mp[6][a].pos = 1;}
}
void initpaint()
{/*for (int a = 0; a < 9; a++){//mp[0][a].is = 1;paintb(mp[0][a]);}paintb(mp[2][1]);paintb(mp[2][7]);//mp[2][1].is = 1;//mp[2][7].is = 1;for (int a = 0; a < 9; a += 2){//mp[3][a].is = 1;paintb(mp[3][a]);}for (int a = 0; a < 9; a++){//mp[9][a].is = 1;paintr(mp[9][a]);}paintr(mp[7][1]);paintr(mp[7][7]);mp[7][1].is = 1;mp[7][7].is = 1;for (int a = 0; a < 9; a += 2){//mp[6][a].is = 1;paintr(mp[6][a]);}*//*beginPaint();clearDevice();putImageScale(&chessimg, 0, 0, 650, 701);endPaint();*/chessboard();for (int a = 0; a < 10; a++)for (int b = 0; b < 9; b++){if (mp[a][b].is == 1){paint(mp[a][b]);}}
}
void paint_rect(int tmpx, int tmpy)
{//initpaint();beginPaint();setPenColor(RGB(0, 0, 225));setBrushColor(EMPTY);rectangle(mp[tmpx][tmpy].x - 25, mp[tmpx][tmpy].y - 25, mp[tmpx][tmpy].x + 25, mp[tmpx][tmpy].y + 25);endPaint();
}
int judge_piece(int x, int y)//18ma07che5 12pao29xiang3 10shi6 13zu else jiang
{if (mp[prex][prey].index == 1 || mp[prex][prey].index == 8){if ((x == prex - 2 && y == prey - 1 && mp[prex - 1][prey].is == 0) || (x == prex - 2 && y == prey + 1 && mp[prex - 1][prey].is == 0)|| (x == prex - 1 && y == prey + 2 && mp[prex][prey + 1].is == 0) || (x == prex + 1 && y == prey + 2 && mp[prex][prey + 1].is == 0)|| (x == prex + 2 && y == prey + 1 && mp[prex + 1][prey].is == 0) || (x == prex + 2 && y == prey - 1 && mp[prex + 1][prey].is == 0)|| (x == prex + 1 && y == prey - 2 && mp[prex][prey - 1].is == 0) || (x == prex - 1 && y == prey - 2 && mp[prex][prey - 1].is == 0)){return 1;}else return 0;}else if (mp[prex][prey].index == 0 || mp[prex][prey].index == 7){if (prey == y){for (int i = min(prex, x) + 1; i < max(prex, x); i++){if (mp[i][prey].is == 1)return 0;}return 1;}else if (prex == x){for (int i = min(prey, y) + 1; i < max(prey, y); i++){if (mp[prex][i].is == 1)return 0;}return 1;}else return 0;}else if (mp[prex][prey].index == 5 || mp[prex][prey].index == 12){if (mp[x][y].is == 1){if (prey == y){int ant = 0;for (int i = min(prex, x) + 1; i < max(prex, x); i++){if (mp[i][prey].is == 1)ant++;}if (ant == 1)return 1;else return 0;}else if (prex == x){int ant = 0;for (int i = min(prey, y) + 1; i < max(prey, y); i++){if (mp[prex][i].is == 1)ant++;}if (ant == 1)return 1;else return 0;}else return 0;}else{if (prey == y){for (int i = min(prex, x) + 1; i < max(prex, x); i++){if (mp[i][prey].is == 1)return 0;}return 1;}else if (prex == x){for (int i = min(prey, y) + 1; i < max(prey, y); i++){if (mp[prex][i].is == 1)return 0;}return 1;}else return 0;}}else if (mp[prex][prey].index == 2 || mp[prex][prey].index == 9){if (prex <= 4){if (x <= 4){if ((x == prex - 2 && y == prey - 2 && mp[prex - 1][prey - 1].is == 0)|| (x == prex - 2 && y == prey + 2 && mp[prex - 1][prey + 1].is == 0)|| (x == prex + 2 && y == prey + 2 && mp[prex + 1][prey + 1].is == 0)|| (x == prex + 2 && y == prey - 2 && mp[prex + 1][prey - 1].is == 0)){return 1;}else return 0;}else return 0;}else if (prex >= 5){if (x > 4){if ((x == prex - 2 && y == prey - 2 && mp[prex - 1][prey - 1].is == 0)|| (x == prex - 2 && y == prey + 2 && mp[prex - 1][prey + 1].is == 0)|| (x == prex + 2 && y == prey + 2 && mp[prex + 1][prey + 1].is == 0)|| (x == prex + 2 && y == prey - 2 && mp[prex + 1][prey - 1].is == 0)){return 1;}else return 0;}else return 0;}}else if (mp[prex][prey].index == 3 || mp[prex][prey].index == 10){if (prex <= 4){if (x > 2 || y < 3 || y>5)return 0;else{if ((x == prex - 1 && y == prey - 1) || (x == prex - 1 && y == prey + 1)|| (x == prex + 1 && y == prey + 1) || (x == prex + 1 && y == prey - 1)){return 1;}else return 0;}}else{if (x < 7 || y < 3 || y>5)return 0;else{if ((x == prex - 1 && y == prey - 1) || (x == prex - 1 && y == prey + 1)|| (x == prex + 1 && y == prey + 1) || (x == prex + 1 && y == prey - 1)){return 1;}else return 0;}}}else if (mp[prex][prey].index == 6 || mp[prex][prey].index == 13){if (mp[prex][prey].pos == 0)//北卒{if (prex <= 4){if ((x == prex + 1) && (y == prey))return 1;else return 0;}else{if (((x == prex + 1) && (y == prey)) || ((x == prex) && (y = prey - 1)) || ((x == prex) && (y = prey + 1)))return 1;else return 0;}}else//南兵{if (prex >= 5){if ((x == prex - 1) && (y == prey)) { return 1; }else return 0;}else{if (((x == prex - 1) && (y == prey)) || ((x == prex) && (y = prey - 1)) || ((x == prex) && (y = prey + 1))) {return 1;}else return 0;}}}else{//int ant = 0;if (prex <= 4){int ant = 0;for (int i = prex + 1; i <= 9; i++){if (mp[i][prey].index == 4 || mp[i][prey].index == 11)break;if (mp[i][prey].is == 1)ant++;}if (ant == 0)return 1;}else{int ant = 0;for (int i = prex - 1; i >= 0; i--){if (mp[i][prey].index == 4 || mp[i][prey].index == 11)break;if (mp[i][prey].is == 1)ant++;}if (ant == 0)return 1;}if (mp[prex][prey].pos == 0){if (((x == prex) && (y == prey + 1)) || ((x == prex) && (y == prey - 1)) || ((y == prey) && (x == prex - 1)) || ((y == prey) && (x == prex + 1))){return 1;}else return 0;}else{if (((x == prex) && (y == prey + 1)) || ((x == prex) && (y == prey - 1)) || ((y == prey) && (x == prex - 1)) || ((y == prey) && (x == prex + 1))){return 1;}else return 0;}}//return 1;
}
void chessboard()
{beginPaint();clearDevice();setPenColor(RED);setPenWidth(1);for (int a = 0; a <= 9; a++){line(INTERVAL, INTERVAL + a * 55, INTERVAL + 440, INTERVAL + a * 55);}for (int a = 0; a <= 8; a++){line(INTERVAL + a * 55, INTERVAL, INTERVAL + a * 55, INTERVAL + 495);}line(INTERVAL + 3 * 55, INTERVAL, INTERVAL + 5 * 55, INTERVAL + 2 * 55);line(INTERVAL + 5 * 55, INTERVAL, INTERVAL + 3 * 55, INTERVAL + 2 * 55);line(INTERVAL + 3 * 55, INTERVAL + 7 * 55, INTERVAL + 5 * 55, INTERVAL + 9 * 55);line(INTERVAL + 5 * 55, INTERVAL + 7 * 55, INTERVAL + 3 * 55, INTERVAL + 9 * 55);setBrushColor(EMPTY);rectangle(INTERVAL - 5, INTERVAL - 5, INTERVAL + 5 + 440, INTERVAL + 5 + 495);setBrushColor(WHITE);rectangle(INTERVAL, INTERVAL + 220, INTERVAL + 440 + 1, INTERVAL + 275 + 1);setTextColor(BLACK);setTextFont("楷体");setTextSize(30);setTextBkColor(WHITE);paintText(INTERVAL + 73, INTERVAL + 235, "楚河           汉界");setTextBkColor(RGB(218, 112, 214));setTextSize(50);paintText(INTERVAL + 8 * 55 + 33, INTERVAL + 55, "悔棋");endPaint();}
int judge_general(int x, int y)//判断是否被将军
{if (mp[x][y].index == 1 || mp[x][y].index == 8){if (mp[x][y].color == 'B'){if ((red_general_x == x - 2 && red_general_y == y - 1) || (red_general_x == x - 2 && red_general_y == y + 1)|| (red_general_x == x - 1 && red_general_y == y + 2) || (red_general_x == x + 1 && red_general_y == y + 2)|| (red_general_x == x + 2 && red_general_y == y + 1) || (red_general_x == x + 2 && red_general_y == y - 1)|| (red_general_x == x + 1 && red_general_y == y - 2) || (red_general_x == x - 1 && red_general_y == y - 2)){return 1;}else return 0;}else{if ((black_general_x == x - 2 && black_general_y == y - 1) || (black_general_x == x - 2 && black_general_y == y + 1)|| (black_general_x == x - 1 && black_general_y == y + 2) || (black_general_x == x + 1 && black_general_y == y + 2)|| (black_general_x == x + 2 && black_general_y == y + 1) || (black_general_x == x + 2 && black_general_y == y - 1)|| (black_general_x == x + 1 && black_general_y == y - 2) || (black_general_x == x - 1 && black_general_y == y - 2)){return 1;}else return 0;}}else if (mp[x][y].index == 0 || mp[x][y].index == 7){if (mp[x][y].color == 'B'){if (red_general_y == y){for (int i = min(red_general_x, x) + 1; i < max(red_general_x, x); i++){if (mp[i][red_general_y].is == 1)return 0;}return 1;}else if (red_general_x == x){for (int i = min(red_general_y, y) + 1; i < max(red_general_y, y); i++){if (mp[red_general_x][i].is == 1)return 0;}return 1;}else return 0;}else{if (black_general_y == y){for (int i = min(black_general_x, x) + 1; i < max(black_general_x, x); i++){if (mp[i][black_general_y].is == 1)return 0;}return 1;}else if (black_general_x == x){for (int i = min(black_general_y, y) + 1; i < max(black_general_y, y); i++){if (mp[black_general_x][i].is == 1)return 0;}return 1;}else return 0;}}else if (mp[x][y].index == 5 || mp[x][y].index == 12){if (mp[x][y].color == 'B'){if (red_general_y == y){int ant = 0;for (int i = min(red_general_x, x) + 1; i < max(red_general_x, x); i++){if (mp[i][red_general_y].is == 1)ant++;}if (ant == 1)return 1;else return 0;}else if (red_general_x == x){int ant = 0;for (int i = min(red_general_y, y) + 1; i < max(red_general_y, y); i++){if (mp[red_general_x][i].is == 1)ant++;}if (ant == 1)return 1;else return 0;}else return 0;}else{if (black_general_y == y){int ant = 0;for (int i = min(black_general_x, x) + 1; i < max(black_general_x, x); i++){if (mp[i][black_general_y].is == 1)ant++;}if (ant == 1)return 1;else return 0;}else if (black_general_x == x){int ant = 0;for (int i = min(black_general_y, y) + 1; i < max(black_general_y, y); i++){if (mp[black_general_x][i].is == 1)ant++;}if (ant == 1)return 1;else return 0;}else return 0;}return  0;}else if (mp[x][y].index == 6 || mp[x][y].index == 13){if (mp[x][y].color == 'B'){if ((red_general_x == x + 1 && red_general_y == y) || (red_general_x == x && red_general_y == y - 1)|| (red_general_x == x && red_general_y == y + 1)){return 1;}else return 0;}else{if ((black_general_x == x - 1 && black_general_y == y) || (black_general_x == x && black_general_y == y - 1)|| (black_general_x == x && black_general_y == y + 1)){return 1;}else return 0;}}
}

打完收工!


http://chatgpt.dhexx.cn/article/PDHp4QLe.shtml

相关文章

基于Android的五子棋游戏APP设计

目 录 第一章&#xff1a;绪论 1 1.1智能手机与Android系统的发展历程 1 1.1.1 智能手机 1 1.1.2 Android系统基本情况介绍 2 1.2课题现状及应用前景 3 1.2.1 五子棋简介 3 1.2.2 课题现状及应用前景 3 第二章&#xff1a;开发环境的搭建 5 2.1 系统开发环境 5 2.2 系统开发环境…

棋类游戏-五子棋小游戏

​ 活动地址&#xff1a;CSDN21天学习挑战赛 界面效果&#xff1a; 实现&#xff1a; 第一步&#xff1a;定义好设计该游戏要用到的组件以及相关数据&#xff1b; 第二步&#xff1a;组装组件&#xff0c;给组件设置相对应的功能&#xff1b; 第三步&#xff1a;调用init()方…

基于c语言的象棋游戏

一、主要目标&#xff1a; 1.1&#xff1a;鼠标控制。 1.2&#xff1a;各棋子按照象棋规则移动 1.3&#xff1a;判断双方胜负 注&#xff1a;本设计使用vs-2017运行。需要下载graphics.h库。 二、基本流程 2.1 棋牌展示 直接输出棋盘背景图片&#xff0c;包含方格线、“楚…

【Unity连载】斗兽棋—棋类游戏开发演示(1)

序言 “黄梅时节家家雨&#xff0c;青草池塘处处蛙。有约不来过夜半&#xff0c;闲敲棋子落灯花。” “象棋终日乐悠悠&#xff0c;苦被严亲一旦丢。兵卒坠河皆不救&#xff0c;将军溺水一齐休。马行千里随波去&#xff0c;象入三川逐浪游。炮响一声天地震&#xff0c;忽然惊…

简单的象棋开发

我们需要准备的知识是c语言基础和easyx图形: easyx官网&#xff1a; https://easyx.cn/ 首先头文件少不了: #include<stdio.h>(c语言的头文件) #include<graphics.h>&#xff08;easyx的&#xff09; #include<mmsystem.h>&#xff08;音乐播放的&#x…

Java游戏开发 —— 五子棋

引言&#xff1a; 五子棋的代码实现很简单&#xff0c;难的是计算机的AI算法&#xff0c;在网上找了很多资料&#xff0c;费了好半天劲才弄明白其实现的原理&#xff0c;真的挺开阔思路的&#xff0c;很有意思&#xff01; 思路&#xff1a; 1、创建主窗口&#xff0c;加载菜单…

编程小游戏之三子棋

三子棋是一个民间的益智小游戏&#xff0c;游戏分为双方对战&#xff0c;双方依次在9宫格棋盘上摆放棋子&#xff0c;率先将自己的三个棋子连成一条线的一方则视为胜利者。下面将说明如何利用C语言在我们的计算机上简单的实现三子棋。 1.逻辑框架设计 在vs上创建两个.c文件test…

【C语言】三子棋游戏的实现(玩家VS玩家 or 玩家VS电脑)

目 录 一、三子棋游戏介绍 二、游戏功能函数分析 1 菜单显示函数 2 菜单选择函数 3 选择确认函数 4 显示当前棋盘状态函数 5 棋盘初始化函数 6 玩家下棋函数 7 电脑下棋函数 8 棋局状态判断函数 三、游戏功能函数的整合 四、三子棋游戏的实现 五、游戏…

安卓做的棋类游戏

大方大斜 前言一、直接上图二、游戏主要代码1.界面绘制代码2.游戏触摸事件代码 最后 前言 安卓制作棋类游戏&#xff0c;小时候和发小在地上画棋盘&#xff0c;有树叶或者树枝做棋子。 一、直接上图 二、游戏主要代码 1.界面绘制代码 双人界面绘制&#xff0c;主要代码代码如…

基于java的五子棋游戏设计

技术&#xff1a;Java、JSP等摘要&#xff1a;随着互联网迅速的发展&#xff0c;网络游戏已经成为人们普遍生活中不可或缺的一部分&#xff0c;它不仅能使人娱乐&#xff0c;也能够开发人的智力&#xff0c;就像本文所主要讲的五子棋游戏一样能挖掘人们聪明的才干与脑袋的机灵程…

自己设计的棋类游戏

1、构思&#xff1a; 象棋、国际象棋、井字棋等 我一开始的设计是这个样的&#xff1a;一个5x5的棋盘&#xff08;之后变成7x7&#xff09;&#xff0c;所有棋子摆成一个井字 棋子有两种&#xff0c;一种兵一种王&#xff0c;中间用中立的棋子&#xff0c;就叫box来隔开他们 …

Java围棋游戏的设计与实现

技术&#xff1a;Java等 摘要&#xff1a; 围棋作为一个棋类竞技运动&#xff0c;在民间十分流行&#xff0c;为了熟悉五子棋规则及技巧&#xff0c;以及研究简单的人工智能&#xff0c;决定用Java开发五子棋游戏。主要完成了人机对战和玩家之间联网对战2个功能。网络连接部分为…

【国际象棋】棋盘游戏-微信小程序开发流程详解

与中国象棋类似的&#xff0c;还有国际象棋&#xff0c;知道有人爱玩&#xff0c;于是凭着好奇心&#xff0c;网上研究了一下&#xff0c;跟中国象棋有相似之处&#xff0c;玩法是有些许不一样&#xff0c;不知道象棋最早出于谁之手呢&#xff0c;抽空做一做&#xff0c;最终完…

利用C语言巧妙实现棋类游戏——三子棋

小游戏&#xff1a;三子棋用C语言实现 你是否学完了C语言的函数、数组、选择结构、循环结构苦于没有实战小项目巩固自己所学的知识呢&#xff0c;今天小程序猿就给大家带来了一个游戏的小游戏——三子棋&#xff0c;利用C语言实现的&#xff0c;希望对大家能有所帮助。 我们大家…

基于C#的五子棋游戏设计

目 录 一、 毕业设计内容 3 二、 毕业设计目的 3 三、 工具/准备工作 3 四、 设计步骤和方法 3 &#xff08;一&#xff09; 总体设计 3 1&#xff0e; 总体设计思路及设计图 3 2&#xff0e; 界面设计 4 3&#xff0e; 全局变量设计 4 &#xff08;二&#xff09; 详细设计 5 …

【Unity连载】斗兽棋-棋类游戏开发演示(2)

第四章 游戏操作与指令 如同养育一个婴儿&#xff0c;父母总会一步步引领孩子成长&#xff0c;从蹒跚学步到来去如风&#xff1b;我们对游戏功能的开发&#xff0c;也无疑应当从走出第一步棋开始。现在&#xff0c;我们已经构建出了棋盘、棋子等基本的游戏逻辑对象&#xff1b…

Java游戏开发——中国象棋联机版

游戏介绍&#xff1a; 中国象棋是起源于中国的一种棋戏&#xff0c;属于二人对抗性游戏的一种&#xff0c;在中国有着悠久的历史。由于规则简单&#xff0c;趣味性强&#xff0c;成为流行极为广泛的棋类游戏。 中国象棋使用方形格状棋盘及红黑二色圆形棋子进行对弈&#xff0c…

【论文】word中三线表的快速简单制作

【论文】word中三线表的快速简单制作 首先&#xff0c;打开word点击插入→表格&#xff0c;选择需要插入表格的行列数&#xff0c;行列数没有关系&#xff0c;因为你插入后可以随意添加行列。 则插入了一下表格&#xff1a; 把你所需要的数据放进去&#xff1a; 之后&#…

如何在word中制作线宽不同的三线表

在word中难免会插入特定的三线表&#xff0c;那就现场给给大家演示一遍 1、打开word&#xff0c;点击插入&#xff0c;点击表格&#xff0c;至于插入几行几列看自己的需求哈。 2、右键点击表格&#xff0c;然后点击表格属性 3、点击边框和底纹&#xff0c;对表格去除相关边线 …

论文中的三线表绘制(word)

步骤 1、选中表格&#xff08;点击下图中的红框部分选中&#xff09; 2、右击表格选择“表格属性” 3、点击边框和底纹&#xff0c;再选择无边框&#xff08;先不要点确定&#xff09; 4、设置三线表的上下边框&#xff08;1.5磅&#xff09;&#xff0c;完成下图之后点击确定-…