So in your Board
class you have:
// returns the ID of the passed cell
int Board::AtPosition(int x, int y)
{
return board[x*y];
}
// put a mark in the cell and return true
void Board::SetPosition(int x, int y, int player_ID)
{
board[x*y +x] = player_ID;
//value = pd[row * 4 + column];
}
So you're using x
and y
as you parameter names which are shadowing the x and y class members. You need to think about what you are trying to do. For a 3 x 3 array you have:
0 1 2
3 4 5
6 7 8
In your code it seems you intend to use x as the column number and y as the row number (but not consistently, so I can't be sure). So for x=1 and y=2 your index is 5. So how do you calculate that? You have:
board[x*y +x] = player_ID;
You need (something like):
board[this->x*y +x] = player_ID;
And also in AtPosition
you need:
return board[this->x*y +x];
But I would recommend re-naming your vars to avoid shadowing. Maybe the member vars would be better called xsize
/ysize
or xlen
/ylen
.
Also in operator<<
you have:
if(i % rhs.y == 0)
{
os << std::endl;
}
os << rhs.board[i];
Which I think needs to be:
if(i % rhs.x == 0)
{
os << std::endl;
}
os << rhs.board[i];
(Because I'm guessing rhs.x
is the width, but if it's not, you have other code that's wrong.)