王鹿其 发表于 2024-1-2 20:24:04

为什么打印不出各个棋子

#include <stdlib.h>
#include <Windows.h>
#include<easyx.h>
#include <graphics.h>
#include <conio.h>
#define ROW 10
#define COL 9

#define geziwidth 80//棋盘中一个格子的宽度
#define sxzywidth 80//棋盘上下左右距边界的距离


static int type = RED;//          设置先手后手
static int g = 1; //          记录判断先后手

struct chess
{
        int x;               //位于窗口的x,y轴坐标
        int y;
        int id;               //棋子名称
        int type;             //所属类型
        int river = 0;      //是否过河
};
enum piece
{
        SPACE = -1,                     //表示无棋子
        車, 馬, 象, 士, 将, 砲, 卒,
        俥, 马, 相, 仕, 帥, 炮, 兵,
        BEGIN, END                     //起子还是落子

};
struct START   //鼠标点击坐标
{
        int x;
        int y;
};

//定义基本数据

struct chess map;         //棋盘每一个点,每点都对应了一个棋子属性
struct START begin = { -1, -1 };    //起子坐标
struct START end = { -1, -1 };      //落子坐标
int start = BEGIN;                  //鼠标状态
enum piece blackpiece[] = { 車, 馬, 象, 士, 将, 砲, 卒 };   //黑棋
enum piece redpiece[] = { 俥, 马, 相, 仕, 帥, 炮, 兵 };       //红棋
const char* chessname[] = { "車", "馬", "象", "士", "将", "砲", "卒", "俥", "马", "相", "仕", "帥", "炮", "兵" };    //用于后面打印名称

void printfchess()       //打印棋子
{
        for (int i = 0; i < ROW; i++)
        {
                for (int j = 0; j < COL; j++)
                {
                        enum piece chessid = SPACE;
                        int chesstype;
                        if (i <= 4)//打印黑子,赋属性
                        {
                                chesstype = BLACK;
                                if (i == 0)//设置最下面一排
                                {
                                        if (j <= 4)
                                        {
                                                chessid = blackpiece;
                                        }
                                        else
                                        {
                                                chessid = blackpiece;
                                        }
                                }

                                if (i == 2)//设置炮
                                {
                                        if (j == 1 || j == 7)
                                        {
                                                chessid = blackpiece;
                                        }
                                }
                                if (i == 3)//   设置兵
                                {
                                        if ((j + 1) % 2 == 1)
                                        {
                                                chessid = blackpiece;
                                        }
                                }
                        }
                        else
                        {
                                chesstype = RED;//打印红子,附属性
                                if (9 - i == 0)
                                {
                                        if (j <= 4)
                                        {
                                                chessid = redpiece;
                                        }
                                        else
                                        {
                                                chessid = redpiece;
                                        }
                                }
                                if (9 - i == 2)
                                {
                                        if (j == 1 || j == 7)
                                        {
                                                chessid = redpiece;
                                        }
                                }
                                if (9 - i == 3)
                                {
                                        if ((j + 1) % 2 == 1)
                                        {
                                                chessid = redpiece;
                                        }
                                }
                        }
                        map.river = 0;//赋值各点位属性
                        map.id = chessid;
                        map.type = chesstype;
                        map.x = j *geziwidth + sxzywidth;
                        map.y = i * geziwidth+ sxzywidth;
                }
        }


}
void printfboard()
{
        initgraph(800, 880);
        setbkcolor(RGB(219, 121, 93));
        setlinecolor(BLACK);
        cleardevice();
        //rectangle(80, 80, 720, 800);//内正方形
        //rectangle(75, 35, 725, 765);//外正方形
        for (int i = 0; i <= 800; i += 80)//绘制横线
                line(80, i, 720, i);
        for (int i = 80; i <=720; i += 80)//绘制竖线
        {
                line(i, 80, i, 400);//黑方竖线
                line(i, 480, i, 800);//红方竖线
        }
        line(80, 400, 80, 480);
        line(720, 400, 720, 480);
        line(320, 80, 480, 240);//画四条线,即两个叉叉
        line(320, 240, 480, 80);
        line(320, 800, 480, 640);
        line(320, 640, 480, 800);

        settextcolor(BLACK);
        settextstyle(60, 0, _T("楷体"));
        outtextxy(180, 410, _T("楚"));
        outtextxy(260, 410, _T("河"));
        outtextxy(480, 410, _T("汉"));
        outtextxy(560, 410, _T("界"));

        ////下棋方显示
        //if (g % 2 == 1)
        //{
        //        settextcolor(RED);
        //        settextstyle(40, 0, "楷体");
        //        outtextxy(640, 600, "红方落子");
        //}
        //if (g % 2 == 0)
        //{
        //        settextcolor(BLACK);
        //        settextstyle(40, 0, "楷体");
        //        outtextxy(640, 110, "黑方落子");
        //}
        for (int i = 0; i < ROW; i++)
        {
                for (int j = 0; j < COL; j++)
                {
                        if (map.id != SPACE)
                        {
                                fillcircle(map.x, map.y, 50);
                                fillcircle(map.x, map.y, 35);
                                settextstyle(30, 10, "黑体");
                                setbkmode(TRANSPARENT);
                                settextcolor(map.type);
                                outtextxy(map.x , map.y , chessname.id]);
                        }
                }
        }
        printfchess();
        system("pause");
    closegraph();
}




int main()
{
        printfboard();
        printfchess();
       
}

FishC 发表于 2024-1-2 20:24:19

你好,代码中有一些问题导致无法打印出棋子。首先,在定义chess结构体时,不能在结构体内部为变量赋初始值,需要在初始化时进行赋值。其次,在定义枚举类型时,不能直接使用中文字符作为枚举变量的值,建议使用英文字符或数字代替。

以下是修正后的代码:

#include <iostream>
#include <string>
using namespace std;

const int ROW = 10;
const int COL = 9;

struct Chess {
    int x;
    int y;
    string id;
    string type;
    bool river;
};

enum Piece {
    SPACE = -1,
    R, XIANG, SHI, JIANG, MA, PAO, ZU
};

Chess map;

void printChess() {
    for (int i = 0; i < ROW; i++) {
      for (int j = 0; j < COL; j++) {
            if (map.id == "") {
                cout << "";
            } else {
                cout << map.id;
            }
            cout << " ";
      }
      cout << endl;
    }
}

int main() {
    // 初始化棋盘
    for (int i = 0; i < ROW; i++) {
      for (int j = 0; j < COL; j++) {
            map.x = i;
            map.y = j;
            map.id = "";
            map.type = "";
            map.river = false;
      }
    }

    // 初始化棋子
    map.id = "車";
    map.id = "馬";
    map.id = "相";
    map.id = "仕";
    map.id = "將";
    map.id = "仕";
    map.id = "相";
    map.id = "馬";
    map.id = "車";
    map.id = "炮";
    map.id = "炮";
    map.id = "兵";
    map.id = "兵";
    map.id = "兵";
    map.id = "兵";
    map.id = "兵";
    map.id = "車";
    map.id = "馬";
    map.id = "象";
    map.id = "士";
    map.id = "將";
    map.id = "士";
    map.id = "象";
    map.id = "馬";
    map.id = "車";
    map.id = "炮";
    map.id = "炮";
    map.id = "卒";
    map.id = "卒";
    map.id = "卒";
    map.id = "卒";
    map.id = "卒";

    printChess();

    return 0;
}

运行该代码即可打印出棋盘上的各个棋子。注意,我将棋子名称改为了中文,并使用了二维数组来表示棋盘。

希望能帮到你,如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: 为什么打印不出各个棋子