11 Replies - 3715 Views - Last Post: 10 March 2009 - 01:20 PM Rate Topic: -----

#1 Restrikted  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 09-March 09

Help with coding C++ chess

Posted 09 March 2009 - 03:45 PM

In my chess program I have a base class called Piece with 6 subclasses of each piece type (King, Queen, Rook, Knight, Bishop, Pawn), as well as a class called Game, which handles all of the gameplay. In my Game class header, I declare a two dimensional array Pieces (Piece* Board[8][8]). The problem I'm running into is that I can't figure out how to call that array while in a subclass of Piece. For instance, in my Queen class, I'm trying to access and alter the Board, but I get 'Board is an undeclared identifier error'. I know I need to reference Game's Board in the header of the Piece class, but I'm not exactly sure how to do that.
Header of my Game class:
class Game
{
public:

	Game();

	void StartGame();
	void DrawBoard();
	bool IsCheckMate();
	
	Piece* Board[8][8];

};



Is This A Good Question/Topic? 1

Replies To: Help with coding C++ chess

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Help with coding C++ chess

Posted 09 March 2009 - 03:51 PM

Do you have an instance of "Game" that your pieces have access to? You'll use that object's methods and you'll need to include the header file in each piece's corresponding cpp file.

//Queen.cpp for example
#include "Queen.h" //Queen.h will probably include Game.h

void Queen::MovePiece(Piece* theBoard)
{
   theBoard->update(); //or whatever
}

//OR


void Queen::MovePiece(Game* theGame)
{
   theGame->theBoard->update(); //or whatever
}





Hope that helps. If you could be more specific I could could get a better idea of what you;re trying to achieve.

This post has been edited by KYA: 09 March 2009 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Help with coding C++ chess

Posted 09 March 2009 - 03:54 PM

Board is not a member of Peice, rather it is the name of an instance of peace, which then exists within the realm of the Game object. So, make sure that you have a game object in the same scope as your class, and make sure you are accessing Board through this game object.
Was This Post Helpful? 0
  • +
  • -

#4 Restrikted  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 09-March 09

Re: Help with coding C++ chess

Posted 09 March 2009 - 04:59 PM

It's neither, I have board as an array that holds pointers of instances of Pieces.
Was This Post Helpful? 0
  • +
  • -

#5 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Help with coding C++ chess

Posted 09 March 2009 - 05:01 PM

Can I ask why a chess piece would want to alter the board?
Was This Post Helpful? 0
  • +
  • -

#6 Restrikted  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 09-March 09

Re: Help with coding C++ chess

Posted 09 March 2009 - 05:25 PM

The individual pieces need to move around the board (thus altering the board), like this
void Queen::Move(int desx, int desy)
{
	Board[desy][desx] = *this;
	Board[LocY][LocX] = NULL;
}

Where the first line of the function moves the instance of the piece to a new location, and the next line sets the old location to Null.

Basically, I just don't know the syntax I would need to be able to give the pieces access to the board so the above would become a valid statement.
Was This Post Helpful? 0
  • +
  • -

#7 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Help with coding C++ chess

Posted 09 March 2009 - 05:32 PM

EDIT: Never mind. I asked a dumb question. I think you should go with KYA's solution.

This post has been edited by bsaunders: 09 March 2009 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#8 Restrikted  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 09-March 09

Re: Help with coding C++ chess

Posted 09 March 2009 - 07:55 PM

KYA, how would I give my pieces access to an instance of Game that would be declared in main? I can't just call it like this (with myGame as an instance of Game, declared in main), myGame.Board[y][x] = *this;
So what would be the syntax to make that work?
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Help with coding C++ chess

Posted 09 March 2009 - 09:54 PM

int main()
{
	Game* theGame = new Game(); //initialize, etc.. 
	theGame->startGame();
	//what controls your Piece class?
}



I'd need to see how you have your pieces set up, etc... Logically, I'd have a Board class that contains the Pieces. You have a board constructed of 8 x 8 pieces. Which is odd because a chess game doesn't have 64 pieces (given there is at least the equivalent 2 or 3 rows of blank squares at all times (I can't remember the exact number w/o looking at a chess board)
Was This Post Helpful? 0
  • +
  • -

#10 Restrikted  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 09-March 09

Re: Help with coding C++ chess

Posted 09 March 2009 - 10:15 PM

I'm using the Game class to control the Piece class (if by control you mean feed it arguments and such), but the class is standalone with 8 subclasses for the actual pieces. The reason I have a Board of Pieces is because every square has the opportunity to contain a Piece. Originally I have the pieces set where they should be, with the rest of the squares being set to NULL, and as a piece moves, the square it was previously at is set to NULL. I don't see a need to make a class for board because it is simple a container for the pieces, it would do nothing else.

Here's some of the code to help you realize what's happening:
// Queen.h

#pragma once
#include "Piece.h"
#include "Game.h"

class Queen: public Piece
{
public:

	Queen(int locx, int locy, int side);// Constructor

	void Draw(); // Draws the Queen
	void Move(int desx, int desy); // Moves the Queen
	myGame.Board;
};

// Queen.cpp

#include "Queen.h"
#include "Game.h"

Queen::Queen(int locx, int locy, int side)
{
		LocX = locx;
		LocY = locy;
		Side = side;
}

void Queen::Draw()
{
		cout << " Q ";
}

void Queen::Move(int desx, int desy)
{
	myGame.Board[desy][desx] = *this; // These two lines are where my problem is arising
	myGame.Board[LocY][LocX] = NULL; // I'm lost on how to make these calls to myGame.Board valid statements
}


// Piece.h

#pragma once

class Piece
{
public:

	Piece();

	virtual void Draw() = 0;
	virtual void Move(int desx, int desy) = 0;

	int LocX;
	int LocY;
	int Side;

};


// Game.h

#pragma once

#include "Piece.h"
#include "Rook.h"
#include "Knight.h"
#include "Bishop.h"
#include "Queen.h"
#include "King.H"
#include "Pawn.h"

using namespace std;

class Game
{
public:

	Game();

	void StartGame();
	void DrawBoard();
	bool IsCheckMate();
	
	Piece* Board[8][8];

};

// Game.cpp (some of it)

#include "Game.h"
#include "Piece.h"

#include <iostream>
using namespace std;

#define NULL 0

Game::Game()
{
	// A sample of how I'm initializing the pieces
	Rook* bRook1 = new Rook(0, 0, 2);
	Board[0][0] = bRook1;

	Knight* bKnight1 = new Knight(0, 1, 2);
	Board[0][1] = bKnight1;

	Bishop* bBishop1 = new Bishop(0, 2, 2);
	Board[0][2] = bBishop1;

	for (int i = 2; i < 6; i++)
		for (int j = 0; j < 8; j++)
			Board[i][j] = NULL;
}

void Game::StartGame()
{
	int movex, movey, finalx, finaly;
	while(!IsCheckMate())
	{
		DrawBoard();
		cout << "Player 1 to move:" << endl;
		cout << "Move from: ";
		cin >> movex >> movey;
		cout << "Move to: ";
		cin >> finalx >> finaly;
		Board[movey][movex]->Move(finalx, finaly);
	}

}


This post has been edited by Restrikted: 09 March 2009 - 10:17 PM

Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Help with coding C++ chess

Posted 10 March 2009 - 05:30 AM

Define a global pointer to your game [in main] and then in each header:

extern Game* theGame;



Then you can call the game's function on the board.
Was This Post Helpful? 0
  • +
  • -

#12 Restrikted  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 09-March 09

Re: Help with coding C++ chess

Posted 10 March 2009 - 01:20 PM

Okay I got it it to work. Thanks KYA!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1