国际象棋棋盘(协会) [英] Chess Board (ASSOCIATION)

查看:73
本文介绍了国际象棋棋盘(协会)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的你们是对的这是一项任务..但我真的很紧张&沮丧,

伙计我真的需要你的建议才能更好地理解..

现在我要画一局棋盘&&关联棋子.. !!

我希望有一个人能帮我理解更好...... !!



其实我不要理解我的讲师.. !!!



我发布两个代码.. !!

一个我是现在它只是国际象棋棋盘&& OtherOne来自我的讲师'关于协会.. !!!



CHESS BOARD

Yes You Guys Are Right This is an assignment.. But i'm Really Much Tensed & Frustrated,,
Guys I really need your suggestions to understand better ..
For now I've to draw a chess board && associate pieces of chess.. !!
I Hope There Should Be SomeOne Who Help Me To Understand Better .. !!

Actually I Don't Understand My Lecturer .. !!!

I'm Posting Two Codes .. !!
One That I've now it's just Chess Board && The OtherOne is From my lecturer its' about association .. !!!

CHESS BOARD

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

class ChessBoard
{
    private:
    string MyBoard[26];
    public:
    void setBoard(string []);
    void DisplayBoard();
   

};

void ChessBoard::setBoard(string arr[])
{
    for(int i=0;i<26;i++)
    MyBoard[i]=arr[i];
}
void ChessBoard::DisplayBoard()
{
    for(int i=0;i<26;i++)
    cout<<MyBoard[i]<<endl;;

}

int main()
   {             /////   2   6   10  14  18  22  26  30
   string arr[26]={"--1---2---3---4---5---6---7---8---",
                      "-    ####    ####    ####    ####-",
                      "1    ####    ####    ####    ####1",//2
                      "-    ####    ####    ####    ####-",
                      "-####    ####    ####    ####    -",
                      "2####    ####    ####    ####    2",//5
                      "-####    ####    ####    ####    -",
                      "-    ####    ####    ####    ####-",
                      "3    ####    ####    ####    ####3",//8
                      "-    ####    ####    ####    ####-",
                      "-####    ####    ####    ####    -",
                      "4####    ####    ####    ####    4",//11
                      "-####    ####    ####    ####    -",
                      "-    ####    ####    ####    ####-",
                      "5    ####    ####    ####    ####5",//14
                      "-    ####    ####    ####    ####-",
                      "-####    ####    ####    ####    -",
                      "6####    ####    ####    ####    6",//17
                      "-####    ####    ####    ####    -",
                      "-    ####    ####    ####    ####-",
                      "7    ####    ####    ####    ####7",//20
                      "-    ####    ####    ####    ####-",
                      "-####    ####    ####    ####    -",
                      "8####    ####    ####    ####    8",//23
                      "-####    ####    ####    ####    -",
                      "--1---2---3---4---5---6---7---8---"
                      };
   ChessBoard b;
   b.setBoard(arr);
   b.DisplayBoard();
   system("pause");
   return 0;
}





协会



ASSOCIATION

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

class Piece;

class Position {
	Piece *piece;
	string name;
public:
	Position (string s) {name = s; piece = NULL;}
	void put(Piece *p) {piece = p;}
	void remove() {piece = NULL;}
	string getName() {return name;}
	Piece *getPiece() {return piece;}
};

class Piece {
	Position *pos;
	string name;
public:
	Piece (string s) {name = s; pos = NULL;}
	void putAt(Position *p) {pos = p; pos -> put(this);}
	void removeFrom() {pos->remove(); pos = NULL;}
	void print() { cout << name << "is at " << pos->getName() << endl;}
	Position *getPosition() {return pos;}
	string getName() {return name;}
	virtual void move(Position * from, Position * to) = 0;
};

class King : public Piece {
public:
	King(string s) : Piece(s) {};
	void move(Position *from, Position *to) {removeFrom(); putAt(to);}
};

class Queen : public Piece {
public:
	Queen(string s) : Piece(s) {};
	void move(Position *from, Position *to) {removeFrom(); putAt(to);}
};

void printBoard(Position *p[], int size) {
	int i = 0;
	Position *t;
	Piece *x;
	string pos, piece;

	for (i = 0; i < size; i++) {
		t = p[i];
		pos = t->getName();
		x = t->getPiece();
		if (x == NULL) piece = "Nothing";
		else piece = x->getName();
		cout << piece << " is at " << pos << endl;
	}
}

int main() {

	Position *p[10];
	p[0] = new Position("p1");
	p[1] = new Position("p2");
	p[2] = new Position("p3");
	p[3] = new Position("p4");
	p[4] = new Position("p5");
	p[5] = new Position("p6");
	p[6] = new Position("p7");
	p[7] = new Position("p8");
	p[8] = new Position("p9");
	p[9] = new Position("p10");

	printBoard(p, 10);
	cout << endl;

	Piece *pp1, *pp2;
	pp1 = new King("Black King");
	pp2 = new Queen("White Queen");

	pp1->putAt(p[1]);
	pp2->putAt(p[3]);
	
	printBoard(p, 10);
	cout << endl;

	pp1->move(p[1], p[2]);
	pp2->move(p[3], p[4]);
	
	printBoard(p, 10);
	cout << endl;

	return 0;
}





现在我要将棋子与这个棋盘联系起来.. ??

现在我只是不知道我从哪里开始...... !!!




<任何解释建议都应该得到赞赏。 ! >

推荐答案

首先,你需要问你的教授。先问你的同学。如果他们得到它,问题就在于你。如果没有,你需要一起要求你的老师按照他的学生可以跟随他的步伐前进。如果需要,请与学校联系。



你老师的设计真的不太好。我可以看到几种方法来改进它。但是,你必须按照他要求的方式工作。我没有看到任何简单的方法从你的代码库中绘制带有碎片的板。电路板图纸似乎已从电路板本身中删除。当你想要两个坐标时,你的位置类允许任何字符串成为一个位置。字符串也很难解析。但是,您需要创建零件并在板上为每个零件指定一个位置。然后,您需要处理这些碎片,以了解它可以移动的位置和位置。如果你现在感到困惑,当你开始添加移动棋子的规则时,你将完全迷失。随机陌生人的帮助不会让你经过一个你不懂的老师,所以要处理根本问题。如果是你,请跟老师一起学习更多,所以他也知道你需要帮助。如果是老师,请抱怨他是否不会尝试改进。
First of all, you need to ask your professor. Ask your class mates first. If they get it, the problem is you. If not, you need to ask together for your teacher to move at a pace where his students can follow him. Talk to the school if need be.

Your teacher's design is really not very good. I can see several ways to improve it. However, you have to work with what he asked for. I don't see any easy way to draw a board with pieces on it from the code base you have. The board drawing seems removed from the board itself. Your position class lets any string be a position when really you want two co-ordinates. A string will also be harder to parse. However, you need to create pieces and assign a position on the board to each piece. Then you need to work with the pieces to know what is where and where it can be moved. If you're confused now, when you start to add rules for moving pieces, you will be totally lost. No amount of help from random strangers will get you past a teacher you don't understand, so deal with the root issue. If it's you, buckle down with the teacher and study more, so he also knows you need the help. If it's the teacher, complain if he won't try to improve.


没有任何sensibkle方式来执行关联,因为您目前没有代表棋盘的数据结构。国际象棋棋盘由八行组成,每行八个。您和您的讲师的数据结构都没有反映出来。



如果是后者,您的讲师可能故意将该职位表示为字符串:在国际象棋中通常的表示法一个位置将列编码为'a'和'h'之间的字符,将行编码为1到8之间的数字。因此,e2表示第二行第5列中的字段。在任何情况下,您都需要一个功能,将位置的名称转换为对棋盘上某个位置的引用。



如果是您的代码,你的字符串数组仅用于打印空白板,但它完全不适合表示国际象棋棋盘上的位置或与之相关的棋子。你应该考虑棋盘的结构,而不是考虑打印整个棋盘的方法:它是一个二维的8x8字段阵列。你的第一个问题应该是该阵列的定义,而不是如何打印它。



一旦你有了这个,那么这种关联就很简单:

1.对于你棋盘阵列中的每个位置...

1.1 ...初始化它不保留任何一块

2.对于每一块,......

2.1 ...获得位置

2.2 ...将位置的名称翻译成棋盘上的真实位置

2.3 ..在该位置存储指向该部分的指针



一旦你有了这个,就可以考虑如何最好地打印它。您可以迭代所有行,并且对于每一行重复遍历所有字段以获得打印该字段作为整体所需的第一,第二,第三......行文本。如果该字段没有任何部分,则每行只是一串空白或哈希。如果没有,该字段的每一行都可以保存与其关联的部分名称的一部分。
There is no sensibkle way to perform an association because you currently have no data structure representing the chess board. A chess board consists of eight rows with eight fields each. Neither your nor your lecturer's data structures reflect that.

In case of the latter, your lecturer may intentionally have represented the position as a string: in chess the usual notation for a position encodes the column as a character between 'a' and 'h' and the row as a number between 1 and 8. So "e2" represents the field in the 5th column of the second row. In any case, you need a function that translates the "name" of a position into a reference to a location on your chess board.

In case of your code, your string array only serves to print an empty board, but it is totally unsuitable for representing locations on a chess board or pieces associated to those. Rather than thinking of a way to print an entire board, you should have considered the structure of a chess board: it is a two-dimensional 8x8 array of fields. Your first concern should be the definition of that array, not how to print it.

Once you have that, the association is rather simple:
1. for every location in your chess board array ...
1.1 ... initialize it to hold no piece
2. for every piece,...
2.1 ... get the position
2.2 ... translate the position's name into a real location on your chess board
2.3 ... store a pointer to that piece within that location

Once you have that you can consider how best to print this. You can iterate over all rows, and for each row iterate repeatedly over all fields to get the first, second, third,... line of text needed to print that field as a whole. If the field holds no piece, each row would simply be a string of blanks or hashes. If not, each row of that field could hold a part of the name of the piece that it is associated to.


这篇关于国际象棋棋盘(协会)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆