动态数组使用相同的值实现整体

时间:2013-12-04 19:03:25

标签: c++ arrays function object methods

该程序试图检查国际象棋移动是否有效(对不起,评论是用波兰语写的,我希望代码很清楚,即使不了解它们)。对于典当,一切都按预期工作,但对于塔而言,存在问题。我调试了程序,当我在行中停止它时

if(fRow < tRow)

数组的每个字段都与sTab [0] [0]具有相同的值。很少有更高的值是正常的。
我尝试用这个做了大约2个小时没有任何帮助,也许你会找出问题所在。

代码:

main.cpp中:

#include "main.h"

using namespace std;

int main() {

    chess test;
    test.show();
    cout << test.ifMove("F1->B1");
    getch();
    return 0;
}

chess.cpp:

#include "main.h"

using namespace std;

////// Konstruktor, konstruktor kopiujący oraz destruktor //////
chess::chess() : tab2D(8, 8, 'c') {
        init();
};

chess::chess(chess&c) {
};

chess::~chess() {
    for(int i=0;i<height;++i)
        delete [] sTab[i];
    delete [] sTab;
};

////// Metody //////

////// Uzupełnianie kolorów pól oraz rozstawianie figur i pionków //////
void chess::init() {
    /// kolory pól: 0 - biały, 1 - czarny ///
    int last = 0;
    for(int i = 0; i < this->height; ++i) {
        for(int j=0; j < this->width; ++j) {
            if(last == 0) {
                this->sTab[i][j] = "1";
                last = 1;
            }
            else if(last == 1) {
                this->sTab[i][j] = "0";
                last = 0;
            }
        }
        if(last == 0)
            last = 1;
        else if(last == 1)
            last = 0;
    };

    /// rozstawienie pionków ///
    for(int i = 0; i < this->width; ++i) {
        sTab[1][i] = sTab[1][i] + "wp";
        sTab[6][i] = sTab[6][i] + "bp";
    };

    /// rozstawienie koni ///
    sTab[0][2] = sTab[0][2] + "wh";
    sTab[0][5] = sTab[0][5] + "wh";
    sTab[7][2] = sTab[7][2] + "bh";
    sTab[7][5] = sTab[7][5] + "bh";

    /// rozstawienie gońców ///
    sTab[0][1] = sTab[0][1] + "wj";
    sTab[0][6] = sTab[0][6] + "wj";
    sTab[7][1] = sTab[7][1] + "bj";
    sTab[7][6] = sTab[7][6] + "bj";

    /// rozstawienie wież ///
    sTab[0][0] = sTab[0][0] + "wt";
    sTab[0][7] = sTab[0][7] + "wt";
    sTab[7][0] = sTab[7][0] + "bt";
    sTab[7][7] = sTab[7][7] + "bt";

    /// rozstawienie królowych ///
    sTab[0][4] = sTab[0][4] + "wq";
    sTab[7][3] = sTab[7][3] + "bq";

    /// rozstawienie królów ///
    sTab[0][3] = sTab[0][3] + "wk";
    sTab[7][4] = sTab[7][4] + "bk";

    /// dopełnienie pustych pól zerami i jedynkami ///
    for(int i = 0; i < this->height; ++i) {
        for(int j=0; j < this->width; ++j) {
            if(sTab[i][j] == "0")
                sTab[i][j] = "000";
            else if(sTab[i][j] == "1")
                sTab[i][j] = "111";
        }
    };

    sTab[5][0] = "0bt";
};

////// Wyświetlenie szachownicy //////
void chess::show() {
    for(int i = 0; i < (this->height + 1); ++i) { /// przechodzimy po 1 polu więcej, żeby dodać kolumnę i wiersz z oznaczeniem kolumn i wierszy
        for(int j=0; j < (this->width + 1); ++j) {
            if(i == 0 && j == 0)
                cout << "   ";
            else if (i != 0 && j == 0) { /// numeracja wierszy
                switch (i) {
                case 1:
                    cout << "A  ";
                    break;
                case 2:
                    cout << "B  ";
                    break;
                case 3:
                    cout << "C  ";
                    break;
                case 4:
                    cout << "D  ";
                    break;
                case 5:
                    cout << "E  ";
                    break;
                case 6:
                    cout << "F  ";
                    break;
                case 7:
                    cout << "G  ";
                    break;
                case 8:
                    cout << "H  ";
                    break;
                default:
                    break;
                }
            }
            else if (i == 0 && j != 0) { /// numeracja kolumn
                cout << j << "   "; 
            }
            else {
                cout << this->sTab[i-1][j-1] << " ";
            }
        }
        cout << endl << endl;
    };
};

////// Metoda sprawdzająca czy ruch jest możliwy do wykonania //////
////// return 0 - ruch niemożliwy, 1 - ruch możliwy na puste pole, 2 - ruch możliwy + bicie, 3 - pole z którego chcemy ruszyć jest puste //////
int chess::ifMove(string move) {
    /// f - from (pole z którego ruszamy), t - to (pole docelowe), Col - kolumna, Row - wiersz ///
    const char *fChar = &move[1];
    int fCol = atoi(fChar) - 1;
    const char *tChar = &move[5];
    int tCol = atoi(tChar) - 1;
    int fRow, tRow;
    switch (move[0]) {
    case 'A':
        fRow = 0;
        break;
    case 'B':
        fRow = 1;
        break;
    case 'C':
        fRow = 2;
        break;
    case 'D':
        fRow = 3;
        break;
    case 'E':
        fRow = 4;
        break;
    case 'F':
        fRow = 5;
        break;
    case 'G':
        fRow = 6;
        break;
    case 'H':
        fRow = 7;
        break;
    default:
        break;
    }
    switch (move[4]) {
    case 'A':
        tRow = 0;
        break;
    case 'B':
        tRow = 1;
        break;
    case 'C':
        tRow = 2;
        break;
    case 'D':
        tRow = 3;
        break;
    case 'E':
        tRow = 4;
        break;
    case 'F':
        tRow = 5;
        break;
    case 'G':
        tRow = 6;
        break;
    case 'H':
        tRow = 7;
        break;
    default:
        break;
    }

    /// Sprawdzenie jaką figurą próbujemy poruszyć i sprawdzenie warunków ///
    string fField =  this->sTab[fRow][fCol];
    char figure = fField[2];
    char color = fField[1];
    char fieldColor = fField[0];
    string tField = this->sTab[tRow][tCol];
    char hit = tField[2];
    switch(figure) {
    case 'p': // pionek
        if(fCol == tCol) {
            if(color == 'w') { /// pionek biały
                if(fRow == 1 && tRow == 3 && (this->sTab[3][tCol] == "000" || this->sTab[3][tCol] == "111")) {
                    if(hit == '0' || hit == '1')
                        return 1;
                    else
                        return 2;
                }
                if((tRow - fRow) == 1) {
                    if(hit == '0' || hit == '1')
                        return 1;
                    else
                        return 2;
                }
                else
                    return 0;
            }
            else if(color == 'b')  { /// pionek czarny
                if(fRow == 6 && tRow == 4 && (this->sTab[4][tCol] == "000" || this->sTab[4][tCol] == "111")) {
                    if(hit == '0' || hit == '1')
                        return 1;
                    else
                        return 2;
                }
                if((fRow - tRow) == 1) {
                    if(hit == '0' || hit == '1')
                        return 1;
                    else
                        return 2;
                }
                else
                    return 0;
            }
        }
        else
            return 0;
        break;
    case 't': // wieża
        if(fCol == tCol && fRow != tRow) {
            for(int i = 1; i < abs(tRow - fRow); ++i) {
                if(fRow < tRow)
                    if(this->sTab[fCol][fRow-i] != "000" && this->sTab[fCol][fRow-i] != "111")
                        return 0;
                if(fRow > tRow)
                    if(this->sTab[fCol][fRow+i] != "000" && this->sTab[fCol][fRow+i] != "111")
                        return 0;
            }
            if(hit == '0' || hit == '1')
                return 1;
            else
                return 2;
        }
        else if(fCol != tCol && fRow == tRow) {
            for(int i = 1; i < abs(tCol - fCol); ++i) {
                if(fCol < tCol)
                    if(sTab[fCol-1][fRow] != "000" && sTab[fCol-1][fRow] != "111")
                        return 0;
                if(fCol > tCol)
                    if(sTab[fCol+1][fRow] != "000" && sTab[fCol+1][fRow] != "111")
                        return 0;
            }
            if(hit == '0' || hit == '1')
                return 1;
            else
                return 2;
        }
        else
            return 0;
        break;
    default:
        return 3;
        break;
    }
    return 1;
};

编辑我为那些想要重现错误的人添加其余代码:
Tab.cpp:

#include "main.h"

using namespace std;


////// Konstruktor domyślny, konstruktor, konstruktor kopiujący oraz destruktor //////
tab2D::tab2D() {
};

tab2D::tab2D(int x, int y, char c) {

    this->width = x;
    this->height = y;
    if (c == 'm') {
        this->iTab = new int*[this->height];

        for(int i=0;i<this->height;++i)
               this->iTab[i] = new int[this->width];
    }
    else if (c == 'c') {
        this->sTab = new string*[this->height];

        for(int i=0;i<this->height;++i)
               this->sTab[i] = new string[this->width];
    }
    else {
    }
};

tab2D::tab2D(tab2D&t) {
      tab2D(t.width, t.height, 't');
};

tab2D::~tab2D() {
};

main.h:

#pragma once

#include <iostream>
#include <conio.h>
#include <string>
#include <cstdlib>

using namespace std;

enum Row;

class tab2D {

protected:
    int width;
    int height;
    string **sTab;
    int **iTab;

public:
    tab2D();
    tab2D(int x, int y, char c);
    ~tab2D();
    tab2D(tab2D&t);
};

class chess: public tab2D {

public:
    chess();
    ~chess();
    chess(chess&c);
    void init();
    int ifMove(string move);
    void show();
};

class matrix: public tab2D {

public:
    matrix(int x, int y);
    ~matrix();
    matrix(matrix&m);
};

1 个答案:

答案 0 :(得分:0)

您似乎混淆了。在某些代码中,数组sTab由[row] [column]索引,但在代码的这一部分中,您可以采用另一种方式:

if(fRow < tRow)
  if(this->sTab[fCol][fRow-i] != "000" && this->sTab[fCol][fRow-i] != "111")
    return 0;

该计划告诉你,塔楼可能不会从A6移动到A2(通过所有这些人)。