Reputation: 23
I am creating a tron game which includes a 2d array to hold the game board I want to create a function that searches every index of the array to see if a collision has been made, i.e. if player one goes into a square that player two has already been it that would be a collision
I have no idea how to begin but the code I wrote is what I thought would work but it is just not searching or returning anything
board::searchForCollision(){
found = false;
for (board[0][0]; board[0][0] <100; board[0][0]++)
{ if (board[0][0] == board[0][0] +1){
found= true;
}
else
found = false;
}
return found;
}
The code I wrote is what I thought would work but it is just not searching or returning anything.
Upvotes: 2
Views: 979
Reputation: 1532
Let's say you want to create this 2D Tron game first you need a board like this :
static constexpr int boardSize = 100;
static constexpr int PlayerOneValue = 1;
static constexpr int PlayerTwoValue = 2;
class board {
int board[boardSize][boardSize];
bool tryActivateCell(const int x, const int y, const int playerValue);
board() {
memset(array, 0, sizeof(board));
}
}
And when a player move on the 2D board for every move the player do you need to call a function to activate the cell or if you cannot activate the cell because the cell is already activate by the other player return false
bool board::tryActivateCell(const int x, const int y, const int playerValue) {
// Maybe do a check to avoid an overflow if x or y is equal or greater than boardSize
const int& boardValue = board[y][x];
if (boardValue != 0 && boardValue != playerValue) {
// The other player already activate the cell
return false;
}
// Activate the cell with the value of the current player
boardValue = playerValue;
return true;
}
Finally if the previous function return false that mean the current player had a collision with a cell activate by the other player and need to be dead.
Upvotes: 1
Reputation: 15275
Unfortunately you made some logical errors.
Obviously you want to detect, if some cell in a 2 d array contains some data. Looking at you for loop:
for (board[0][0]; board[0][0] <100; board[0][0]++)
The first part of a for loop uusally initializes the "running" variable, used in the "for". But board[0][0];
does nothing. It is a noOp. No operation. You can also leave it away. It will compile to nothing. Then, in the condition part of the for loop, you are just checking if one speficic cell in your board, the cell with index 0,0 is less than 100. You are always looking at the same cell 0,0. The same is true for the last part of the "for" statement. You are always incrementing the cell 0,0.
In the following if, you are comparing if the same cell 0,0 is equal to the same cell 0,0 + 1. which is never true. It is always false. Would be like writing if (3 == 4). This can never work.
Also your handling of true and false in the if else statement does not work.
What you may want to do, is that you want to iterate of the indices of the array.
Something like
for (size_t row = 0U; row < 100 ; ++row)
for (size_t col = 0U; col < 100; ++col) {
// do something with array[row][col];
}
}
I cannot help you much more, because the question is not so clear to me.
Upvotes: 1