I'm fairly new to C++ and was trying to make a checkers program. I'm getting an error once I type the x and y coordinates for the piece I'm trying to move. Side note: It's not finished yet.
line 19: error C2864: 'CheckerBoard::player1' : only static const integral data members can be initialized within a class
line 20: error C2864: 'CheckerBoard::player2' : only static const integral data members can be initialized within a class
In getXCoordOfPiece() and getYCoordOfPiece() you store the input in local vars xInput and yInput.
In CheckerBoard::movePieces() you access the member variables with the same name which have a random value. Accessing board[yInput][xInput] will most of the time cause a crash.
I have VS 2015 Community if that helps. I still haven't figured out how to get the program to work and still need help. I've tried to fix it and figure it out but it still doesn't work.
main.cpp:
#include <iostream>
#include <string>
#include "CheckerBoard.h"
using namespace std;
int main()
{
CheckerBoard checkerBoard;
checkerBoard.playGame();
return 0;
}
CheckerBoard.h:
#pragma once
class CheckerBoard
{
public:
CheckerBoard(void);
void initBoard();
void printBoard();
int getXCoord();
int getYCoord();
int getXCoordOfPiece();
int getYCoordOfPiece();
int xCoord = getXCoord();
int yCoord = getYCoord();
int yInput = getYCoordOfPiece();
int xInput = getXCoordOfPiece();
void movePieces(char turn, int yInput, int xInput, int yCoord, int xCoord);
void placePieces();
void playGame();
private:
char turn = '1';
char player1 = 'R';
char player2 = 'B';
char board[8][8];
};
CheckerBoard.cpp:
#include "CheckerBoard.h"
#include <iostream>
using namespace std;
CheckerBoard::CheckerBoard(void)
{
}
void CheckerBoard::initBoard()
{
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
board[y][x] = ' ';
}
}
}
void CheckerBoard::printBoard()
{
cout << " | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |\n";
cout << "-----------------------------------\n";
for (int y = 0; y < 8; y++)
{
int x = 0;
cout << y + 1 << " | ";
for (int x = 0; x < 8; x++)
{
cout << board[y][x] << " | ";
}
cout << endl << "-----------------------------------" << endl;
}
}
int CheckerBoard::getXCoordOfPiece()
{
int xInput;
cout << "Enter X Coordinate of piece you'd like to move: ";
cin >> xInput;
return xInput - 1;
}
int CheckerBoard::getYCoordOfPiece()
{
int yInput;
cout << "Enter Y Coordinate of piece you'd like to move: ";
cin >> yInput;
return yInput - 1;
}
int CheckerBoard::getYCoord()
{
int yCoord;
cout << "Enter Y Coordinate of where you want to move.";
cin >> yCoord;
return yCoord - 1;
}
int CheckerBoard::getXCoord()
{
int xCoord;
cout << "Enter X Coordinate of where you want to move.";
cin >> xCoord;
return xCoord - 1;
}
void CheckerBoard::movePieces(char turn, int yInput, int xInput, int yCoord, int xCoord)
{
if (turn == '1')
{
xInput = getXCoordOfPiece();
yInput = getYCoordOfPiece();
if (board[yInput][xInput] == player1)
{
xCoord = getXCoord();
yCoord = getYCoord();
if (board[yCoord][xCoord] == player1)
{
cout << "That move is invalid!";
cout << yCoord << xCoord;
system("PAUSE");
return;
}
else if (board[yCoord][xCoord] == player2)
{
cout << "That move is invalid!";
cout << yCoord << xCoord;
system("PAUSE");
return;
}
else
{
board[yInput][xInput] = ' ';
board[yCoord][xCoord] = player1;
}
}
}
if (turn == '2')
{
xInput = getXCoordOfPiece();
yInput = getYCoordOfPiece();
if (board[yInput][xInput] == player1)
{
xCoord = getXCoord();
yCoord = getYCoord();
if (board[yCoord][xCoord] == player1)
{
cout << "That move is invalid!";
cout << yCoord << xCoord;
system("PAUSE");
return;
}
else if (board[yCoord][xCoord] == player2)
{
cout << "That move is invalid!";
cout << yCoord << xCoord;
system("PAUSE");
return;
}
else
{
board[yInput][xInput] = ' ';
board[yCoord][xCoord] = player2;
}
}
}
}
void CheckerBoard::placePieces()
{
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 8; x++)
{
if (((x + y) % 2) == 0)
{
board[y][x] = player1;
}
}
}
for (int y = 5; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
if (((x + y) % 2) == 1)
{
board[y][x] = player2;
}
}
}
}
void CheckerBoard::playGame()
{
bool isDone = false;
while (isDone == false)
{
initBoard();
placePieces();
printBoard();
movePieces(turn, yInput, xInput, yCoord, xCoord);
if (turn == '1')
{
turn = '2';
}
else if (turn == '2')
{
turn = '1';
}
system("PAUSE");
}
}
I'm getting an error once I type the x and y coordinates for the piece I'm trying to move.
Typically you'll want to be more specific than "I'm getting an error." If you're getting an error, presumably there is some sort of text or behavior you're seeing that leads you to believe this. Describing that would be helpful.
You have several members of your class that are never initialized or assigned values, but are used as if they have some meaningful value (xCoord, xInput, yCoord, etc.) Since they don't have meaningful values and you use them to index an array, you're likely to be accessing outside the bounds of that array which is a big no-no.
In:
1 2 3 4 5 6 7
int CheckerBoard::getXCoordOfPiece()
{
int xInput;
cout << "Enter X Coordinate of piece you'd like to move: ";
cin >> xInput;
return xInput;
}
xInput is a local variable which stops existing when the function returns. It is not the data member that is declared in the class definition. These functions return values. You ignore/discard the returned values.