Игра в шахматы: ошибка ошибки сегментации. Вы можете найти это?

Можете ли вы найти мою ошибку? Программа компилируется, но при ее запуске происходит сбой и появляется ошибка сегментации: 11 после пятой строки. Программа в 3 файлах. Я играю в шахматы и сейчас все, что от меня требуется, — это напечатать доску. Это насколько это возможно:

    cts/Software/Chess\ Game/a.out ; exit;
setup complete
rkb*qbkr
pppppppp
________
________
________
Segmentation fault: 11
logout

[Process completed]

Файл 1: board.h

    /*******************************
*
*   Matthew Buchanan
*
*   Bocan Projects
*
*   13:11 21 November 2014
*
*   board.h
*
*******************************/

#include <iostream>

using namespace std;

#include "piece.h"
const int board_width = 8;
const int board_height = 8;

class board
{
private:
int square[board_height][board_width];
piece *pieces[board_height][board_width];
int make = 0;
void print_square();
public:
void setup();
void print();
void update();
};

void board::print_square()
{
cout << "_";
}

void board::setup()
{
if(make == 1)                       //prevents setup function from being ran more than once
return;
else
{
for(int i = 0; i < board_height; i++)
{
for(int j = 0; j < board_width; j++)
{
pieces[board_height][board_width] = NULL;
}
}
for(int i = 0; i < board_width; i++)
{
pieces[6][i] = new piece(1, 1);
pieces[1][i] = new piece(1, 2);
}
pieces[0][0] = new piece(2, 2);
pieces[0][1] = new piece(3, 2);
pieces[0][2] = new piece(4, 2);
pieces[0][3] = new piece(5, 2);
pieces[0][4] = new piece(6, 2);
pieces[0][5] = new piece(4, 2);
pieces[0][6] = new piece(3, 2);
pieces[0][7] = new piece(2, 2);
pieces[7][0] = new piece(2, 1);
pieces[7][1] = new piece(3, 1);
pieces[7][2] = new piece(4, 1);
pieces[7][3] = new piece(6, 1);
pieces[7][4] = new piece(5, 1);
pieces[7][5] = new piece(4, 1);
pieces[7][6] = new piece(3, 1);
pieces[7][7] = new piece(2, 1);

}
make = 1;
}

void board::print()
{
for(int i = 0; i < board_height; i++)
{
for(int j = 0; j < board_width; j++)
{
if(pieces[i][j] == NULL)
{
print_square();
}
else
{
pieces[i][j] -> print();
}
}
cout << endl;
}
}

void board::update()
{
return;
}

Файл 2: piece.h

    /*******************************
*
*   Matthew Buchanan
*
*   Bocan Projects
*
*   13:11 21 November 2014
*
*   piece.h
*
*   Types:
*   1 - Pawn
*   2 - Rook
*   3 - Knight
*   4 - Bishop
*   5 - King
*   6 - Queen
*
*******************************/

class piece
{
private:
int type;
int team;
public:
piece();
piece(int, int);
void set_type(int);
void set_team(int);
int get_type();
int get_team();
void print();
//void move(); the location and the move functions will be kept by the board class in board.h
};

piece::piece()
{
type = 0;               //0 is the null type
team = 0;               //0 is the null type
}

piece::piece(int t, int c)
{
type = t;
team = c;
}

void piece::set_type(int t)
{
type = t;
}

void piece::set_team(int c)
{
team = c;
}

int piece::get_type()
{
return type;
}

int piece::get_team()
{
return team;
}

void piece::print()
{
if(type == 1 && team == 1)
cout << "P";
if(type == 1 && team == 2)
cout << "p";
if(type == 2 && team == 1)
cout << "R";
if(type == 2 && team == 2)
cout << "r";
if(type == 3 && team == 1)
cout << "K";
if(type == 3 && team == 2)
cout << "k";
if(type == 4 && team == 1)
cout << "B";
if(type == 4 && team == 2)
cout << "b";
if(type == 5 && team == 1)
cout << "^";
if(type == 5 && team == 2)
cout << "*";
if(type == 6 && team == 1)
cout << "Q";
if(type == 6 && team == 2)
cout << "q";
}

И Файл 3, который содержит main: chess.cpp

    /*********************************
*
*   Matthew Buchanan
*
*   Bocan Projects
*
*   16:24 18 November 2014
*
*   Chess Game
*
*********************************/

#include <iostream>

using namespace std;

#include "board.h"
int main()
{
board chess_game;
chess_game.setup();
cout << "setup complete" << endl;
chess_game.print();
cin.ignore();
return 0;

}

-4

Решение

этот:

pieces[board_height][board_width] = NULL;

должно быть так:

pieces[i][j] = NULL;

Также в следующий раз скомпилируйте его с -Wall. он должен предупредить вас, что я и j не используются.

0

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector