Hi,
I've got one question. Imagine that you are making 2048 and you want to chceck whether game is over (what means that there are no empty places on the board and there are not the same numbers next to each other).
The only option which come to my mind is write function like this (board is a vector< vector<int> > board{ { 0,0,0,0 },{ 0,0,0,0 },{ 0,0,0,0 },{ 0,0,0,0 } }):
1 2 3 4 5 6 7 8 9 10
|
bool game_end() {
bool end = true;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (board[i][j] == 0 || board[i][j] == board[i + 1][j] || board[i][j] == board[i - 1][j] || board[i][j] == board[i][j + 1] || board[i][j] == board[i][j - 1])
return false;
}
}
return end;
}
|
The problem is that when I'm on the position, let's say, board[0][0], there is no position such as board[-1][0] and it will cause the error. Creating border like this would do the job
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
but this would require a lot of changes in the code.
So, is there any better you can think of?
Thanks for your answer.