简体   繁体   中英

C++ classes and pointers

I need help in my project. The project is about chess game and I have to write code and have an interface where user can play the game but that's something our teacher gave us and we only need to code. So my problem is, in my board.cpp code and (i think) in all my figures.

Let me explain a little bit about the project itself: I have interface that sends me a string that it contain the square that have a figure(or not) and the square where we want to move the figure.

for example: if I want to move a white pawn from A2 to A3 the string that I will receive is 'A2A3' , then I have to return char* that contains a number (0 - 7). So I didn't have any problems before I ran the program that mean everything was good but then while the program was running I recieved the next message: check error here please

After that it points me to the function where the check function is (in board.cpp) and several times I've noticed this : check this link please It looks like my _wFigure(and the _bFigure) doesn't getting the values in the intalization... so to help you understand more of my code i paste here my board.cpp code:

#include "Board.h"
#include "Square.h"
#include "Figure.h"
#include "King.h"
#include "Knight.h"
#include "Bishop.h"
#include "Pawn.h"
#include "Queen.h"
#include "Rook.h"
#include <string>
char conventor_col(int num);
char conventor_row(int num);
Board::Board()
{
   Figure* _bFigure = new Figure[16];
   Figure* _wFigure = new Figure[16];

int i = 0;
int place = 8;
int row = 6;
int j;
_board = "rnbkqbnrpppppppp################################PPPPPPPPRBNKQNBR";
_boardSq = new Square[64];
_boardSq[0] = Square(8, 1, false, 'r', 0);
_boardSq[1] = Square(8, 2, true, 'n', 1);
_boardSq[2] = Square(8, 3, false, 'b', 2);
_boardSq[3] = Square(8, 4, true, 'k', 3);
_boardSq[4] = Square(8, 5, false, 'q', 4);
_boardSq[5] = Square(8, 6, true, 'b', 5);
_boardSq[6] = Square(8, 7, false, 'n', 6);
_boardSq[7] = Square(8, 8, true, 'r', 7);
for (i = 1; i <= 8; i++)
{
    if (i % 2 == 1)
    {
        _boardSq[place] = Square(7, i, true, 'p', place);
        place++;
    }
    else
    {
        _boardSq[place] =Square(7, i, false, 'p', place);
        place++;
    }
}
for (i = 0; i < 3; i++)
{
    for (j = 1; j < 8; j++)
    {
        if (i % 2 == 1 && row % 2 == 0)
        {
            _boardSq[place] = Square(row, j, false, '#', place);
            place++;
        }
        else if (i % 2 == 0 && row % 2 == 0)
        {
            _boardSq[place] = Square(row, j, true, '#', place);
            place++;
        }
        else if (i % 2 == 0 && row % 2 == 1)
        {
            _boardSq[place] =Square(row, j, true, '#', place);
            place++;
        }
        else if (i % 2 == 1 && row % 2 == 1)
        {
            _boardSq[place] =Square(row, j, false, '#', place);
            place++;

        }
    }
    row--;

}
for (i = 1; i <= 8; i++)
{
    if (i % 2 == 1)
    {
        _boardSq[place] = Square(2, i, false, 'P', place);
        place++;
    }
    else
    {
        _boardSq[place] = Square(2, i, true, 'P', place);
        place++;
    }
}
_boardSq[56] = Square(1, 1, true, 'R', 56);
_boardSq[57] = Square(1, 2, false, 'N', 57);
_boardSq[58] = Square(1, 3, true, 'B', 58);
_boardSq[59] = Square(1, 4, false, 'q', 59);
_boardSq[60] = Square(1, 5, true, 'K', 60);
_boardSq[61] = Square(1, 6, false, 'B', 61);
_boardSq[62] = Square(1, 7, true, 'N', 62);
_boardSq[63] = Square(1, 8, false, 'R', 63);

string place_3 = "a7";
string place_4 = "a2";
for (i = 8; i < 16; i++)
{
    place_3[0] = place_3[0] + i - 8;
    _bFigure[i]= Pawn(0, 'p', place_3, _boardSq[i]);

}
_bFigure[0] = Rook(0, 'r',"a8", _boardSq[0]);
_bFigure[7] = Rook(0, 'r',"h8", _boardSq[7]);
_bFigure[1] = Knight(0, 'n', "b8", _boardSq[1]);
_bFigure[6] = Knight(0, 'n', "g8", _boardSq[6]);
_bFigure[2] = Bishop(0, 'b', "c8", _boardSq[2]);
_bFigure[5] = Bishop(0, 'b', "f8", _boardSq[5]);
_bFigure[3] = Queen(0, 'q', "d8", _boardSq[3]);
_bFigure[4] = King(0, 'k', "e8", _boardSq[4]);

for (i = 48; i < 56; i++)
{
    place_4 = place_4[0] + i - 48;
    _wFigure[i-40] = Pawn(1, 'P', place_4, _boardSq[i]);
}
_wFigure[0] = Rook(1, 'R', "a1", _boardSq[0]);
_wFigure[7] = Rook(1, 'R', "h1", _boardSq[7]);
_wFigure[1] = Knight(1, 'N', "b1", _boardSq[1]);
_wFigure[6] = Knight(1, 'N', "g1", _boardSq[6]);
_wFigure[2] = Bishop(1, 'B', "c1", _boardSq[2]);
_wFigure[5] = Bishop(1, 'B', "f1", _boardSq[5]);
_wFigure[4] = Queen(1, 'Q', "e1", _boardSq[4]);
_wFigure[3] = King(1, 'K', "d1", _boardSq[3]);


}
//true - white
//false - black
Board::~Board()
{
delete[] _bFigure;
delete[] _wFigure;

}
string Board::board_now()
{
return _board;
}
Square* Board::board_now_sq()
{
return _boardSq;
}
void Board::change_board(string change)
{
int i = 0;
int place1 = 0;
int place2 = 0;
char temp;
for (i = 0; i < 64; i++)
{
    if (conventor_row( _boardSq[i].get_row()) == change[1] && conventor_col( _boardSq[i].get_column()) == change[0])
    {
        place1 = _boardSq[i].get_place();
    }
    if (conventor_row( _boardSq[i].get_row()) == change[3] && conventor_col( _boardSq[i].get_column()) == change[2])
    {
        place2 = _boardSq[i].get_place();
    }
}
temp = _board[place1];
_board[place1] = _board[place2];
_board[place2] = temp;

}
void Board::change_board_sq(string change)
{
int i = 0;
int place1;
int place2;
for (i = 0; i < 64; i++)
{
    if (_boardSq[i].get_row() == change[1] && _boardSq[i].get_column() == change[0])
    {
        place1 = i;
    }
    if (_boardSq[i].get_row() == change[3] && _boardSq[i].get_column() == change[2])
    {
        place2 = i;
    }
}
Square temp(_boardSq[place1]);
_boardSq[place1].set_taken(_boardSq[place2].get_taken());
_boardSq[place2].set_taken(temp.get_taken());
}

char conventor_row(int num)
{

switch (num)
{
case 1:
    return '1';
    break;
case 2:
    return '2';
    break;
case 3:
    return '3';
    break;
case 4:
    return '4';
    break;
case 5:
    return '5';
    break;
case 6:
    return '6';
    break;
case 7:
    return '7';
    break;
case 8:
    return '8';
    break;
}

}

char conventor_col(int num)
{

    switch (num)
    {
    case 1:
        return 'a';
        break;
    case 2:
        return 'b';
        break;
    case 3:
        return 'c';
        break;
    case 4:
        return 'd';
        break;
    case 5:
        return 'e';
        break;
    case 6:
        return 'f';
        break;
    case 7:
        return 'g';
        break;
    case 8:
        return 'h';
        break;
    }
}

void Board::operator=(const Board& temp)
{
    this->_board = temp._board;
    this->_boardSq = temp._boardSq;
    this->_wFigure = temp._wFigure;
    this->_bFigure = temp._bFigure;

}

int Board::checker(string change,int Pcolor)
{
    int i;
    int checkF;
    for (i = 0; i < 64; i++)
    {
        if (change[0] == conventor_col(_boardSq[i].get_column()) && change[1] == conventor_row(_boardSq[i].get_row())) //error 2
        {
            if (_boardSq[i].get_taken() == '#')
            {
                return '2';
            }
        }
        if (change[2] == conventor_col(_boardSq[i].get_column()) && change[3] == conventor_row(_boardSq[i].get_row())) // error 3
        {
            if (Pcolor % 2 == 0) // white player
            {
                for (int j = 0; j < 16; j++)
                {
                    if (_wFigure[j].get_place_sq().get_place() == _boardSq[i].get_place())
                    {
                        return '3';
                    }
                }
            }
            else // black player
            {
                for (int j = 0; j < 16; j++)
                {
                    if (_bFigure[j].get_place_sq().get_place() == _boardSq[i].get_place())
                    {
                        return '3';
                    }
                }
            }
        }
    }
    if (Pcolor % 2 == 0) // error 5-7 +1
    {
        for (i = 0; i < 16; i++)
        {
            if ((_wFigure[i].get_place_str()[0] == change[0]) && (_wFigure[i].get_place_str()[1] == change[1]))
            {
                checkF= _wFigure[i].ligal_move(change, *this);
            }
        }
    }
    else
    {
        for (i = 0; i < 16; i++)
        {
            if ((_bFigure[i].get_place_str()[0] == change[0]) && (_bFigure[i].get_place_str()[1] == change[1]))
            {
                checkF = _bFigure[i].ligal_move(change, *this);
            }
        }
    }
    if (checkF != 0)
    {
        return checkF;
    }

    // error 4
    Board temp; 
    temp = *this;
    temp.change_board(change);
    temp.change_board_sq(change);
    if (Pcolor % 2 == 1) 
    {
        for (i = 0; i < 16; i++)
        {
            if ((_wFigure[i].is_check(change,temp)==true && _wFigure[i].get_name()!='K'))
            {
                return '4';
            }
        }
    }
    else
    {
        for (i = 0; i < 16; i++)
        {
            if ((_bFigure[i].is_check(change, temp) == true && _bFigure[i].get_name() != 'k'))
            {
                return '4';
            }
        }
    }

    return '0';
}

his header (board.h):

#pragma once
#include <string>
#include "Square.h"
#include "Figure.h"
using namespace std;
class Figure;
class Board
{
public:
Board();
~Board();
string board_now();
Square* board_now_sq();
void change_board(string change);
void change_board_sq(string change);
void operator=(const Board& temp);
int checker(string change, int Pcolor);

private:
string _board;
Square* _boardSq;
Figure* _bFigure;
Figure* _wFigure ;
};

the figure.cpp:

#include "Figure.h"
#include "Square.h"
#include <string>
using namespace std;

Figure::Figure()
{
}
int Figure::ligal_move(string move, Board B)
{
return 0;
}
Figure::Figure(int color, char name, string place_str, Square place_sq)
{
this->color = color;
this->name = name;
this->place_str=place_str;
this->set_place_sq(place_sq);
}
Figure::~Figure()
{

}
bool Figure::is_check(string move, Board B)
{
return true;
}
string Figure::get_place_str()
{
return place_str;
}
Square Figure::get_place_sq()
{
return place_sq;
}
void Figure::set_place_str(string place)
{
place_str = place;
}
void Figure::set_place_sq(Square place)
{
place_sq = place;
}
void Figure::set_name(char name_c)
{
name = name_c;
}
void Figure::set_color(int color_c)
{
color = color_c;
}
char Figure::get_name()
{
return name;
}
bool Figure::get_color()
{
return color;
}

the figure.h:

#pragma once
#include <string>
#include "Square.h"
#include "Board.h"
using namespace std;
class Board;
class Figure
{
public:
Figure();
Figure(int color, char name, string place_str, Square place_sq);
~Figure();
virtual int ligal_move(string move, Board B);
virtual string get_place_str();
virtual Square get_place_sq();
virtual void set_place_str(string place);
virtual void set_place_sq(Square place);
virtual void set_color(int color_c);
virtual void set_name(char name_c);
virtual bool get_color();
virtual char get_name();
virtual bool is_check(string move, Board B);

protected:
int color;
char name;
string place_str;
Square place_sq;
};

and here is for example one of the figures(rook.cpp):

#include "Figure.h"
#include "Square.h"
#include "Rook.h"
#include "Board.h"
#include "Horizontal.h"
#include "Vertical.h"
#include <string>
using namespace std;

Rook::Rook(int color, char name, string place_str, Square place_sq) :          Figure(color,name,place_str,place_sq)
{

}
Rook::~Rook()
{

}

int Rook::ligal_move(string move,Board B)
{
Horizontal Hmov(move, B);
Vertical Vmov(move, B);
if (move[0] == move[2] && move[1] == move[3])
{
    return 7;
}
if (move[0]<'a' || move[0]>'h' || move[2]<'a' || move[2]>'h' || move[1]<'1' || move[1]>'8' || move[3]<'1' || move[3]>'8')
{
    return 5;
}
if (Hmov.is_horizontal() == false && Vmov.is_Vertical() == false)
{
    return 6;
}
else
{
    if (Hmov.is_horizontal() == true && Hmov.is_middle1() != '#')
    {
        return 6;
    }
    else if (Vmov.is_Vertical() == true && Vmov.is_middle2() != '#')
    {
        return 6;
    }
}
if (is_check(move,B) == true)
{
    return 1;
}
return 0;
}

bool Rook::is_check(string move,Board B)
{
move[1] = move[3];
move[1] = 'a';
move[3] = 'h';
Horizontal Hmov(move, B);
move[0] = move[2];
move[1] = '8';
move[3] = '1';
Vertical Vmov(move, B);
if (color == 0)
{
    if (Vmov.is_middle2() == 'k' || Hmov.is_middle1() == 'k')
    {
        return true;
    }
    else
    {
        return false;
    }
}
else
{
    if (Vmov.is_middle2() == 'K' || Hmov.is_middle1() == 'K')
    {
        return true;
    }
    else
    {
        return false;
    }

}
}

and his header file(rook.h):

#pragma once
#include <string>
#include "Figure.h"
#include "Square.h"
#include "Board.h"
using namespace std;

class Rook : public Figure
{
public:
Rook(int color, char name, string place_str, Square place_sq);
~Rook();
int ligal_move(string move, Board B);
bool is_check(string move,Board B);

};

I really don't know why but they're all getting NULL values their initialization is in the builder Board in board.cpp ! Thanks in advance! Would really appreciate if I get good solution!

The first lines of your constructor:

Figure* _bFigure = new Figure[16];
Figure* _wFigure = new Figure[16];

are actually creating local variables, not initialising member variables. Perhaps you meant:

_bFigure = new Figure[16];
_wFigure = new Figure[16];

Alternatively you could avoid dynamic arrays entirely - just define member arrays in the header:

Figure _bFigure[16];
Figure _wFigure[16];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM