The title pretty much says it all. I'm required to write a program for a Software course I'm taking that puts two basic algorithms against each other in a game of checkers. For this, I have a Square class that stores whether the square is occupied (bool), which player by (enum Pl, either NA, P1, or P2), and the label of that square (Index, int). I then created a Board class, which is effectively a 2D array of Squares that will have dimensions specified by the user. However, in the implementation file of my Board class, I'm having an error using the board[][] array in one of my functions. I get the error "board was not declared in this scope", which shouldn't be too tricky to figure out, except that the exact same syntax (at least from what I can see, and I've been working on fixing this for a while now) that's causing the error works perfectly in the constructor for the Board class in the same file. Below is the code for my Board.h file, and the relevant parts of my Board.cpp file:
Board.h:
#ifndef BOARD_H
#define BOARD_H
#include "Square.h"
#include <string>
class Board
{
public:
Board(int);
int getBsize();
int getWinner();
std::string getCmove();
Square getSquare(int, int);
void setWinner(int);
void setCmove(std::string);
bool canMoveL(int, int);
bool canMoveR(int, int);
bool canTakeL(int, int);
bool canTakeR(int, int);
void P1Move(int, int);
void P2Move(int, int);
void printCmove(std::string);
void printWin(std::string);
private:
Square board[12][12];
int bSize;
int winner;
std::string cMove;
};
#endif // BOARD_H
Board.cpp constructor as well as top of the file (in case the error is with the libraries and files I've included in Board.cpp):
#include "Square.h"
#include "Board.h"
#include <string>
Board::Board(int bSize)
{
Board::bSize = bSize;
Board::winner = 0;
Board::cMove = "";
Board::board[12][12];
int x = bSize-1;
int index = 1;
for(int j = 0; j < bSize; j++)
{
for(int i = 0; i < bSize; i++)
{
if(((i%2!=0)&&(j%2==0))||((i%2==0)&&(j%2!=0)))
{
if((j==x/2)||(j==x/2+1))
{
board[i][j] = Square(false, NA, index);
index++;
}
else if(j < x/2)
{
board[i][j] = Square(true, P1, index);
index++;
}
else if(j > x/2+1)
{
board[i][j] = Square(true, P2, index);
index++;
}
}
else
{
board[i][j] = Square(false, NA, 0);
}
}
}
}
Function in Board.cpp causing the error:
bool canMoveL(int i, int j)
{
bool temp = false;
Square sq = board[i][j];
if((i-1 < 0)||((sq.getPl() == P1)&&(j-1 >= bSize))||((sq.getPl() == P2)&&(j-1 < 0)))
{
return temp;
}
if(sq.getOcc() == 0)
{
return temp;
}
else
{
if(sq.getPl() == P1)
{
temp = true;
}
else if(sq.getPl() == P2)
{
temp = true;
}
}
return temp;
}
Note: Since I haven't been able to test the canMovL function, I realise there may be errors in that as well, but I'm specifically just looking for help fixing the error I get with the board array in canMovL(). Thanks.