• Advertisement
Sign in to follow this  

checkers game

This topic is 3571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a checkers game in c++ i want to start with text before i add graphics. I am doing it the same way i made my tetris game where the game board is a 2d int array and then the draw function would go through the game board and draw a different image depending on what number is there. So i am breaking it into classes. Basically i have a Piece class that just has the x and y position of the piece so the row and col it will be at on the game board, a bool isKing to know if its a king or not and a height and a width to be used for drawing it later. Anyway i think this could actually be a struct not a class since it has no methods except an empty constructor. what do you think? Then i have a board class that has the 2d int array and a display method and a set method that sets the checkerboard pattern, 1 for gray square 2 for black square. It also has a display method to display the board to the screen. Now i am working on the player class that would have a player name, and what color they are red or black pieces and an array of pieces. Then they can place the pieces on the board, move them around and jump other pieces. I can't figure out how to place a piece the program just crashes i think it might be a memory thing thats i don't think the Piece has to be a class but could just be a struct or something. I havent done much with classes in c++ but i need to use classes for this because there are more then 1 piece to keep track of at once. Any thoughts or suggestions? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
In C++, there is only one difference between structs and classes - in structs, members default to public visibility and in classes they default to private visibility (and the same is true for inheritance). So, the choice between using a struct or a class is just personal preference.

About the program crashing, we can't know what causes it until we see some code.

Share this post


Link to post
Share on other sites
So i figured out how to make a player with a name and an array of pieces. Now i am working on the player setting the pieces on the board. If i have 1 player it works fine. But if i add another player it overwrites the first player's pieces. I have a int value variable that is set to 3 if the player is red pieces and 4 if the player is black pieces. If i create the first player and set its color and then create the 2nd player and set its color the value of the pieces of the first player take on the same value of the pieces of the 2nd player. I will post the Player class that shows how i am setting a piece. I think its a pointer thing where its overwriting memory instead of making each Player a copy of stuff and i have seen those problems before but don't really know how to fix them that well. I will post the player class and the main method and the piece struct. I keep the pieces struct in a header file and just add the header file when i need Pieces. Is that the problem because its the same header so it uses the same values? What if i make the strcut extern doesn't that mean it can be declared in more then 1 place or something?

Player class

#include <iostream>
#include "board.h"
#include "Player.h"

using namespace std;

Piece Player::pieces[12];
string Player::name;
bool Player::isRed; // true if the player is red false otherwise
int Player::colorChoice; // 1 for black, 2 for red

Player::Player()
{
setPlayerNameAndColor();
if (colorChoice == 1)
isRed = false;
else if (colorChoice == 2)
isRed = true;
setPiecesColor();
} //end Player constructor

// show the color options
void Player::showColors()
{
cout << "Please enter a number for the color\n";
cout << "1. Black\n"
<< "2. Red\n"
<< "Your choice: ";
cin >> colorChoice;
} //end showColors

// set the player name and color
void Player::setPlayerNameAndColor()
{
cout << "Please enter a player name: ";
cin >> name;
cout << endl;
showColors();
} // end setPlayerNameAndColor

// set the color of the pieces
void Player::setPiecesColor()
{
if (isRed)
{
for (int i = 0; i < 12; i++)
pieces.value = 3;
} // end isRed
else if (!isRed)
{
for (int i = 0; i < 12; i++)
pieces.value = 4;
} // end else if
} // end setPiecesColor

// display the player name and pieces
void Player::displayPlayer()
{
cout << name << endl;
for (int i = 0; i < 12; i++)
cout << pieces.value << " ";
cout << endl;
} // end displayPlayer

// place the pieces on the board
void Player::setOnePiece(int xPos, int yPos, Board *b)
{
b->board[yPos][xPos] = pieces[0].value;
} // end setOneRedPiece

// return true if there is a collision
bool checkCollision(int xPos, int yPos, Board b)
{
// this means you are not on a gray square
if (b.board[yPos][xPos] != 1)
return true;
return false;
} // end checkCollision

void Player::movePiece(int pieceToMove, Board b)
{
pieces[pieceToMove].xPos++;
pieces[pieceToMove].yPos++;
if(checkCollision(pieces[pieceToMove].xPos, pieces[pieceToMove].yPos, b))
{
pieces[pieceToMove].xPos--;
pieces[pieceToMove].yPos--;
} // end if
} // end movePiece




Pieces struct

struct Piece
{
int xPos;
int yPos;
int width;
int height;
int value; // 3 if its red 4 if its black
bool isKing;
};





main function

#include <cstdlib>
#include <iostream>
#include <string.h>
#include "Board.h"
#include "Player.h"

using namespace std;

int main(int argc, char *argv[])
{
Board *b = new Board();
Player *p1 = new Player();
Player *p2 = new Player();
p1->displayPlayer();
p1->setOnePiece(0,0,b);
b->displayBoardCons();
cout << endl;
p2->setOnePiece(0,2,b);
b->displayBoardCons();

system("PAUSE");
return EXIT_SUCCESS;
} // end main





In fact if i just declare 2 players inside main all the info is overwritten. If i say:
Player *p1 = new Player(); that enters a name and color and then i say
Player *p2 = new Player(); that enters a name and color
and then i display them it has both player 1 and player 2 taking on the values of what was entered for p2 How do i make it so each player has their own seperate COPY of name and peices since each player needs 1 not both players using a different instance of the same one or something...

Share this post


Link to post
Share on other sites
Quote:
In fact if i just declare 2 players inside main all the info is overwritten. If i say:
Player *p1 = new Player(); that enters a name and color and then i say
Player *p2 = new Player(); that enters a name and color
and then i display them it has both player 1 and player 2 taking on the values of what was entered for p2 How do i make it so each player has their own seperate COPY of name and peices since each player needs 1 not both players using a different instance of the same one or something...


That's because you made your members static. That means they are shared by all instances of the Player class. Remove the static keyword from your class, and remove the external declarations in your player definition file.

BTW, you might want to use an enum for color, instead of trusting to memory that "3 is red and 4 is black", or whichever it was. try this

enum CheckerColor{red, black};//declare somewhere where all classes can have it.

//now you can do the piece struct like this
struct Piece
{
int xPos;
int yPos;
int width;
int height;
CheckerColor color;//constants are automagically handled by compiler
bool isKing;
};


Then you do the same thing with the player.

Also, move the color-input error checking to where the color is input, not the body of the constructor. Don't trust the user. What if they enter 3, or 15, or 83578151788? isRed, or your enum, will never be set. Use a local variable for colorChoice. There's no reason for it to be a member.

There is also no reason for the players and board to be made on the heap. Just declare them like variables.

EDIT: forgot to explain how to assign enums

enum CheckerColor{red, black};

CheckerColor color = red;

Piece pieces[12];
for(int i=0; i<12 ; i++){
pieces.color = player_color;//because player_color is also a CheckerColor
}

Share this post


Link to post
Share on other sites
Quote:

enum CheckerColor{red, black};//declare somewhere where all classes can have it.


Where would i declare this so it would happen?

Quote:

//now you can do the piece struct like this

struct Piece

{

int xPos;

int yPos;

int width;

int height;

CheckerColor color;//constants are automagically handled by compiler

bool isKing;

};



Then you do the same thing with the player.

What do you mean dothe same thing with the player?

Finally what does this mean
Quote:

There is also no reason for the players and board to be made on the heap. Just declare them like variables.


Share this post


Link to post
Share on other sites
You might put the enum in it's own header. That would probably be the best option, except that it seems a shame to make a whole file just for one type. You could get away with putting it wherever you declare the struct Piece.

You're right, I wasn't perfectly lucid about how to apply the color enum to Player. Replace bool isRed with a CheckerColor. Or use a bool in the piece struct. The important part is to use the same method for every place where color matters, and for it to be very clear how it works. Either use a bool in both places, or an enum.

Instead on Player* p1 = new Player(), just do Player p1. Then p1.display().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement