【发布时间】:2017-11-30 05:03:46
【问题描述】:
我目前正在开发一款小型 C++ 控制台游戏。我遇到的第一个问题是棋盘显示不好(字符串 8x8 数组)。但首先,让我给你做个简短的介绍。
我的想法是将每个国际象棋人物视为一个对象 - 因此白色和黑色都有自己的实例 - 对于典当它是 16 个实例(白色和黑色相等 8 个),对于车它是 4 个,等等。 .
然后我创建了另一个类,称为ChessBoard,它存储诸如被杀数字、特定玩家的结果等信息,并用于监督其他类。最重要的是,它存储了上面提到的字符串数组,其中包含每个图形的图形表示(std::string pic 行)。现在,回到问题:当我尝试打印出这个数组以检查它的完整性和顺序时,它显示它的所有单元格都是垂直显示的。我想以这种方式显示它(P字段代表具体的数字,一个空字段位于下方):
***** ***** ***** ***** ***** ***** ***** *****
* P * * P * * P * * P * * P * * P * * P * * P *
***** ***** ***** ***** ***** ***** ***** *****
----- ----- ----- ----- ----- ----- ----- -----
| | | | | | | | | | | | | | | |
----- ----- ----- ----- ----- ----- ----- -----
我知道这个问题在这个网站上被提出了很多次,但是我尝试了其他用户给出的所有可能的解决方案,但它不起作用 - 唯一的区别是一些单一标志的位置,但整体阵列的显示是还是很糟糕。
项目的依赖:
BoardLogic.h -> BoardComponents.h
main.cpp -> BoardLogic.cpp -> BoardComponents.cpp
我会给你一个项目文件的小视图。 这是我工作的一个开始,所以我只投射了 Pawn 的实例并尝试在板上正确显示它们。
BoardComponents.h
#ifndef BOARDCOMPONENTS_H_
#define BOARDCOMPONENTS_H_
#include "BoardLogic.h"
#include <string>
class Pawn {
public:
bool isAlive;
std::string pic = "*****\n* P *\n*****";
struct currentPosition {
int x;
int y;
};
Pawn::currentPosition pPos;
private:
};
class Rook {
public:
bool isAlive;
std::string pic = "*****\n* R *\n*****";
struct currentPosition {
int x;
int y;
};
Rook::currentPosition rPos;
private:
};
class Knight {
public:
bool isAlive;
std::string pic = "*****\n* k *\n*****";
struct currentPosition {
int x;
int y;
};
Knight::currentPosition kPos;
private:
};
class Bishop {
public:
bool isAlive;
std::string pic = "*****\n* B *\n*****";
struct currentPosition {
int x;
int y;
};
Bishop::currentPosition bPos;
private:
};
class Queen {
public:
bool isAlive;
std::string pic = "*****\n* Q *\n*****";
struct currentPosition {
int x;
int y;
};
Queen::currentPosition qPos;
private:
};
class King {
public:
bool isAlive;
std::string pic = "*****\n* K *\n*****";
struct currentPosition {
int x;
int y;
};
King::currentPosition kingPos;
private:
};
class ChessBoard {
public:
std::string chessBoard[8][8];
int wChessKilled;
int bChessKilled;
int wPlayerResult;
int bPlayerResult;
bool winner;
private:
};
#endif
BoardLogic.cpp - 此文件创建游戏的新实例及其组件类
#include "BoardLogic.h"
#include "BoardComponents.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void ChessGame::NewGameInstance() {
ChessBoard * cBoard = new ChessBoard;
Pawn * wPawn = new Pawn[8];
Pawn * bPawn = new Pawn[8];
Rook * wRook = new Rook[2];
Rook * bRook = new Rook[2];
Knight * wKnight = new Knight[2];
Knight * bKnight = new Knight[2];
Bishop *wBishop = new Bishop[2];
Bishop *bBishop = new Bishop[2];
Queen *wQueen = new Queen;
Queen *bQueen = new Queen;
King * wKing = new King;
King *bKing = new King;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cBoard->chessBoard[i][j] = "-----\n| |\n-----";
}
}
for (int i = 0; i < 8; i++) {
wPawn[i].pPos.x = i;
wPawn[i].pPos.y = 1;
bPawn[i].pPos.x = i;
bPawn[i].pPos.y = 6;
wPawn[i].isAlive = true;
bPawn[i].isAlive = true;
cBoard->chessBoard[wPawn[i].pPos.x][wPawn[i].pPos.y] = wPawn[i].pic;
cBoard->chessBoard[bPawn[i].pPos.x][bPawn[i].pPos.y] = bPawn[i].pic;
}
for (int i = 0; i < 8; i++) {
for (int j = 7; j >= 0; j--) {
cout << cBoard->chessBoard[i][j];
}
}
【问题讨论】:
-
有点跑题了,但是您是否考虑过将您的国际象棋游戏分成两个不同的部分? IE。引擎和用户界面?有标准的国际象棋协议允许您的引擎与其他用户界面一起工作,而其他引擎也可以与您的 ui 一起工作。您甚至可以让您的引擎与另一个引擎对战。 en.wikipedia.org/wiki/Chess_engine
-
当然,但是由于我想学习一些更高级的编程技术,我真的很想从头开始创建它。是的,我想拆分程序,这就是我使用这个班级系统的原因。最终,文件“BoardComponents”将存储棋盘的所有元素(棋子等)及其统计数据,而“BoardLogic”将处理游戏中的事件、规则等。这种解决方案并不特别,它是只是一种自我保护的本能:P
标签: c++ arrays multidimensional-array chess