二维数组值交换c ++

时间:2017-06-30 00:51:08

标签: c++ arrays swap

using namespace std;

string piece;
string prompt;
int a,b;
int board[8][8];

const int  rook = 500;
const int knight = 300;
const int bishop = 305;
const int queen = 900;
const int king = 2000;
const int piyon = 100;

int startup[8][8] = {rook,knight,bishop,queen,king,bishop,knight,rook, piyon,piyon,piyon,piyon,piyon,piyon,piyon,piyon,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-piyon,-piyon,-piyon,-piyon,-piyon,-piyon,-piyon,-piyon,-rook,-knight,-bishop,-queen,-king,-bishop,-knight,-rook
};

void initial();
void updateboard();
void getinfo();
void move_piece();

int main(int argc, char** argv) {

    initial();

    while(true) {
        getinfo();
        updateboard();
    }
    return 0;
}

void initial() {
    int a,b;

    for(a=0;a<8;a++)
    {
        for(b=0;b<8;b++){
            board[a][b] = startup[a][b];        
        }
    }
}

void updateboard() {

    system("cls");

    for(a=0;a<8;a++) {

        cout<<endl<<endl;

        for(b=0;b<8;b++) {

            if (board[a][b] == piyon) {
                piece = "P";
                goto label;                     
            }
            else if (board[a][b] == king ) {
                piece= "S";
                goto label;
            }
            else if(board[a][b] == bishop ) {
                piece = "F";
                goto label;
            }
            else if (board[a][b] ==knight) {
                piece = "A";
                goto label;
            }
            else if (board[a][b] ==rook) {
                piece = "R";
                goto label;
            }
            else if (board[a][b] ==queen) {
                piece = "V";
                goto label; 
            }
            else if(board[a][b] == 0)
            {
                piece = "0";
                goto label;
            }
            else if(board[a][b] == -king)
             {
                piece = "s";
                goto label;
            }
            else if(board[a][b] == -queen)
             {
                piece = "v";
                goto label;
            }
            else if(board[a][b] == -rook)
             {
                piece = "r";
                goto label;
            }
            else if(board[a][b] == -knight)
             {
                piece = "a";
                goto label;
            }
            else if(board[a][b] == -piyon)
             {
                piece = "p";
                goto label;
            }
            else if(board[a][b] == -bishop)
             {
                piece = "f";
                goto label;
            }
            else {
                piece = "0";
                goto label;
            }

        label:
            cout<<"  "<<piece<<"  ";
        }   
    }
}

   void move_piece () {

    int destinationrow;
    int destinationcol;
    int row;
    int col;

    cout<<"its your turn..."<<endl;
    scanf("%d %d",&row,&col);
    scanf("%d %d",&destinationrow,&destinationcol);

    int temp;
}

void getinfo (void) {

    cin>>prompt;

    if(soru.substr(0,4) == "exit") {
        return;
    }
    if (prompt.substr(0,3)=="new") {
        initial();
    }
    if(soru.substr(0,5)=="print") {
        updateboard();
    }
    if(soru.substr(0,5) == "move") {
        move_piece();
    }
}

这是我的程序代码,我真正想做的是交换国际象棋方块值,以便我能够移动另一个方块。 虽然我做了交换功能所需的一切,但它无论如何都没有用。 piyon是整数类型和棋子,我想进行处理,移动另一个方块。任何帮助,将不胜感激。问候..

2 个答案:

答案 0 :(得分:0)

您编写的函数交换字段以查找可能的输入值。你在板上有一些东西吗?

我设置了一个8x8的电路板:

0 0 0 0 0 0 0 10 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
5 0 0 0 0 0 0 0 

已初始化destinationrow = 0destinationcol = 0row = 7col = 7,并使用了您的move_piece()函数。 输出是:

0 0 0 0 0 0 0 5 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
10 0 0 0 0 0 0 0 

顺便说一句,为什么要指定temp = piyon?它永远不会被使用。

答案 1 :(得分:0)

你的方法是......技术性的。我建议您对布局进行强烈更改。首先,C ++并不打算用作过程语言。也就是说,使用类。并使用象征意义 - &#34; 305&#34;是一个任意数字,而不是主教。如果您不希望数字成为实际的类(具有功能),我将从以下开始:

enum Figure { pawn, rook, ... };

然后继续使用代表字段的存储:

enum Color { black, white };

struct Field{
    bool is_empty;
    Color player;
    Figure figure;
    Field(){ is_empty = true; }
    Field(const Color _player, const Figure _figure) : player(_player), figure(_figure) { is_empty = false; }
}

然后围绕它构建一个类:

struct Coordinate{ size_t i, j; }

class Game {

//going with private first for you to get the structure
//in your code, you should start with public
private: 

    std::array< std::array< Field, 8>, 8> board; //use std::array instead of raw arrays

    void initialize_board();

public:

    Game();

    bool move_possible(const Coordinate from, const Coordinate to) const;

    void do_move(const Coordinate from, const Coordinate to);

    void print_board() const;
}

这将是我开始的地方。您可能会添加更多私有方法,也许还有一些其他功能,如记录器,以国际象棋典型方式记录所有移动,您可能会更改存在的方法。也许你需要一些方法来获得一个数字在UI上显示的数字。我要做的一件事就是创建像move_possible_for_rook这样的方法,以便让move_possible变小。

我想告诉你的重要部分是象征意义。我使用的东西是所有东西的直接表示,而不是必须被解释的东西。我没有使用305作为主教,我使用了一个类图。我没有使用单数i和j来处理字段而是使用结构坐标。我将事物分成了一些方法,这些方法可以让您对现在的代码部分进行概述。这允许&#34;什么&#34;在一个小的头文件中,给你一个关于什么可以调用的概述,同时&#34;如何&#34;在源文件中,在您使用它时与您的想法无关。得到这个想法?

关于你的问题,我的风格:

void Game::swap(const Coordinates coord_1, const Coordinates coord_2){
      std::swap(board[coord_1.i, coord_1.j], board[coord_2.i, coord_2.j]);
}

这很简单。获取一些关于标准库包含的内容的教程,以及何时可以使用它而不是单独执行,请执行此操作。

虽然在您的上下文中,交换有点不寻常。除了铸造之外,别看它有用。

(顺便说一下,有些人甚至可能会争辩说,一个人物应该是一个对某个人物的移动负责的一个阶级。但是在你的情况下不会那么远。如果你打算计划能够增加额外的数字,就像在国际象棋中一样,但在常规国际象棋中有点过分。)

编辑:如果你想要数字的值为305以便管理国际象棋数字的价值,那么#34; bishop的价值为3&#34;系统 - 糟糕的方法。不要将表示与功能混合。在我提出的风格中,您只需添加一个函数unsigned int value(const Figure figure);