Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • h45-taylor/assignment-2048
1 result
Select Git revision
Show changes
Commits on Source (2)
...@@ -105,11 +105,64 @@ void Board::userInput() { ...@@ -105,11 +105,64 @@ void Board::userInput() {
} }
void Board::moveLeft() { void Board::moveLeft() {
// create for (int i = 0; i < SIZE; ++i) {
// Slide tiles to the left
for (int j = 0; j < SIZE - 1; ++j) {
if (board[i][j] == 0) { // If the current tile is empty move the numbers left
for (int k = j + 1; k < SIZE; ++k) {
if (board[i][k] != 0) { //checks if the position to the left is empty
board[i][j] = board[i][k]; // moves the current tile as far left as it can without merging
board[i][k] = 0; // sets the original tile to 0
moved = true; // set movement bool to true to not allow 2 moves at once
break;
}
}
} else {
// If the current tile is not empty, try to merge with the next tile
for (int k = j + 1; k < SIZE; ++k) {
if (board[i][k] != 0) { //check if there is a number to the left
if (board[i][j] == board[i][k]) { // If Number in Grid position i j match i k
board[i][j] *= 2; // then double the value of the left tile
board[i][k] = 0; // set the tile to the right of the merged one to 0
moved = true; // set movement bool to true
}
break;
}
}
}
}
}
} }
void Board::moveRight() { void Board::moveRight() {
// create for (int i = 0; i < SIZE; ++i) {
// Slide tiles to the left
for (int j = 0; j < SIZE - 1; ++j) {
if (board[i][j] == 0) { // If the current tile is empty move the numbers left
for (int k = j + 1; k < SIZE; ++k) {
if (board[i][k] != 0) {
board[i][j] = board[i][k];
board[i][k] = 0;
moved = true; // set movement bool to true to not allow 2 moves at once
break;
}
}
} else {
// If the current tile is not empty, try to merge with the next tile
for (int k = j + 1; k < SIZE; ++k) {
if (board[i][k] != 0) { //check if there is a number to the left
if (board[i][k] == board[i][j]) { // If Number in Grid position i j match i k
board[i][k] *= 2; // then double the value of the left tile
board[i][j] = 0; // set the tile to the right of the merged one to 0
moved = true; // set movement bool to true
}
break;
}
}
}
}
}
} }
void Board::moveUp() { void Board::moveUp() {
......
...@@ -23,5 +23,4 @@ public: ...@@ -23,5 +23,4 @@ public:
Board(); Board();
void run(); void run();
}; };
#endif \ No newline at end of file
\ No newline at end of file
...@@ -2,5 +2,7 @@ ...@@ -2,5 +2,7 @@
int main(){ int main(){
Board game; Board game;
game.run();
return 0; return 0;
} }
\ No newline at end of file