Simple console chess game

Hello, I am trying to create a simple console chess game by using a class JustABoard with an 8x8 array of the board and class Board . (However with valid moves I use a [64] bit board and then convert from a position on the [64] item board to the [8][8] board). I also have a base class Piece with derived classes of each piece which each has its member function legalMove of each respective piece.

It is a work in progress but currently when I run it, it crashes after it reaches line 92 in the Definitions.cpp k.currentPosition();. However if I comment out all the code related to the Knight piece, it compiles and runs, displaying the board as it should and it moves the pieces to the user desired square on the board. The problem is that any piece can move anywhere at the moment because the play() function is unaware of the legal moves of each piece. For the sake of testing I have only tried to implement the legal moves for the white pawn by trying to call currentPosition() and legalMoveP and have failed to do so.

To further elaborate I do have the code for currentPosition() and legal moves of each piece written down. It is simply a matter of implementing it to the game which I am unable of doing.


Do ignore some of the comments as some are just instructions to myself on what I have left to do/add to the code.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stdafx.h"
#include <iostream>
#include "Piece.h"
#include <string>
#include "board.h"
//Implement currentPlayer into main game loop so player turn switches after every move-------------------------------------


using namespace std;

//void startGame();
void printBoard();
//int currentPlayer = 2;



//Use monospaced font to view board as uppercase letters for white and lowercase for black
void printBoard()
{

	cout << "\t\t\t\t\t\t\t Chess Game\n" << endl;

	cout << "This is a representation of the board.\n" << endl;
	cout << "    0 1 2 3 4 5 6 7" << endl;
	cout << "   ________________" << endl;
	cout << "0 | r n b q k b n r " << endl;
	cout << "1 | p p p p p p p p " << endl;
	cout << "2 | - - - - - - - - " << endl;
	cout << "3 | - - - - - - - - " << endl;
	cout << "4 | - - - - - - - - " << endl;
	cout << "5 | - - - - - - - - " << endl;
	cout << "6 | P P P P P P P P " << endl;
	cout << "7 | R N B Q K B N R \n" << endl;
}



int main()
{
	Board theGame;

	printBoard();
	theGame.play();


    return 0;
}


board.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef BOARD_H
#define BOARD_H 
#include <string>
#include "Piece.h"

using namespace std;

class JustABoard
{
public: 
	string static board[8][8];
};

class Board {
private:
	JustABoard currentBoard; 
public:
	void static getUserMove(string); //Coordinates of source, coordinates of destination
	void displayBoard(); // the current state of the game
	void play();
};




#endif 


Piece.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//kingCheck(), and legelMove() calculations based on AlphaBetaChess.java by Logic Crazy Chess
//https://onedrive.live.com/?authkey=%21AIKDmEOnXEbMfHk&cid=D4629BC8D856F7D5&id=D4629BC8D856F7D5%21179&parId=D4629BC8D856F7D5%21176&o=OneUp

#ifndef PIECE_H
#define PIECE_H
#include <string>

//abstract/base class
class Piece {
public:
	bool static kingSafe(); //Check if king is safe
	static std::string currentPosition(); //Current Horizontal and Vertical position of piece
};

class Pawn : public Piece // derive class Pawn from class Piece 
{
public:
	bool isWhite(); //Determine which direction pawn can move in based on if it is White or Black
	static std::string legalMoveP(int); //Retrieve legal moves for pawn

};

class Rook : public Piece
{
public:
	static std::string legalMoveR(int);
};

class Bishop : public Piece
{
public:
	static std::string legalMoveB(int);
};

class Knight : public Piece
{
public:
	static std::string legalMoveN(int);
};

class Queen : public Piece
{
public:
	static std::string legalMoveQ(int);
};

class King : public Piece
{
public:
	static std::string legalMoveK(int);
};

#endif


Definitions.cpp (link to codepad in order to avoid the character maximum on a post)

http://codepad.org/VwQk1lNf


Thank you for your help.
Last edited on
Topic archived. No new replies allowed.