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
  • master
1 result

Target

Select target project
  • h45-taylor/assignment-2048
1 result
Select Git revision
  • master
1 result
Show changes
Commits on Source (2)
File added
...@@ -105,9 +105,8 @@ void Board::userInput() { ...@@ -105,9 +105,8 @@ void Board::userInput() {
} }
void Board::moveLeft() { void Board::moveLeft() {
for (int i = 0; i < SIZE; ++i) { for (int i = 0; i < SIZE; ++i) { // loops through all the rows
// Slide tiles to the left for (int j = 0; j < SIZE - 1; ++j) { // loops through the columns except the column on the right
for (int j = 0; j < SIZE - 1; ++j) {
if (board[i][j] == 0) { // If the current tile is empty move the numbers left if (board[i][j] == 0) { // If the current tile is empty move the numbers left
for (int k = j + 1; k < SIZE; ++k) { for (int k = j + 1; k < SIZE; ++k) {
if (board[i][k] != 0) { //checks if the position to the left is empty if (board[i][k] != 0) { //checks if the position to the left is empty
...@@ -118,8 +117,7 @@ void Board::moveLeft() { ...@@ -118,8 +117,7 @@ void Board::moveLeft() {
} }
} }
} else { } else {
// If the current tile is not empty, try to merge with the next tile
for (int k = j + 1; k < SIZE; ++k) { 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] != 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 if (board[i][j] == board[i][k]) { // If Number in Grid position i j match i k
...@@ -134,28 +132,26 @@ void Board::moveLeft() { ...@@ -134,28 +132,26 @@ void Board::moveLeft() {
} }
} }
} }
void Board::moveRight() { void Board::moveRight() {
for (int i = 0; i < SIZE; ++i) { for (int i = 0; i < SIZE; ++i) {
// Slide tiles to the left for (int j = SIZE - 1; j > 0; --j) {
for (int j = 0; j < SIZE - 1; ++j) { if (board[i][j] == 0) {
if (board[i][j] == 0) { // If the current tile is empty move the numbers left for (int k = j - 1; k >= 0; --k) {
for (int k = j + 1; k < SIZE; ++k) {
if (board[i][k] != 0) { if (board[i][k] != 0) {
board[i][j] = board[i][k]; board[i][j] = board[i][k];
board[i][k] = 0; board[i][k] = 0;
moved = true; // set movement bool to true to not allow 2 moves at once moved = true;
break; break;
} }
} }
} else { } else {
// If the current tile is not empty, try to merge with the next tile for (int k = j - 1; k >= 0; --k) {
for (int k = j + 1; k < SIZE; ++k) { if (board[i][k] != 0) {
if (board[i][k] != 0) { //check if there is a number to the left if (board[i][j] == board[i][k]) {
if (board[i][k] == board[i][j]) { // If Number in Grid position i j match i k board[i][j] *= 2;
board[i][k] *= 2; // then double the value of the left tile board[i][k] = 0;
board[i][j] = 0; // set the tile to the right of the merged one to 0 moved = true;
moved = true; // set movement bool to true
} }
break; break;
} }
...@@ -166,14 +162,61 @@ void Board::moveRight() { ...@@ -166,14 +162,61 @@ void Board::moveRight() {
} }
void Board::moveUp() { void Board::moveUp() {
// create for (int j = 0; j < SIZE; ++j) {
for (int i = 0; i < SIZE - 1; ++i) {
if (board[i][j] == 0) {
for (int k = i + 1; k < SIZE; ++k) {
if (board[k][j] != 0) {
board[i][j] = board[k][j];
board[k][j] = 0;
moved = true;
break;
}
}
} else {
for (int k = i + 1; k < SIZE; ++k) {
if (board[k][j] != 0) {
if (board[i][j] == board[k][j]) {
board[i][j] *= 2;
board[k][j] = 0;
moved = true;
}
break;
}
}
}
}
}
} }
void Board::moveDown() { void Board::moveDown() {
// create for (int j = 0; j < SIZE; ++j) {
for (int i = SIZE - 1; i > 0; --i) {
if (board[i][j] == 0) {
for (int k = i - 1; k >= 0; --k) {
if (board[k][j] != 0) {
board[i][j] = board[k][j];
board[k][j] = 0;
moved = true;
break;
}
}
} else {
for (int k = i - 1; k >= 0; --k) {
if (board[k][j] != 0) {
if (board[i][j] == board[k][j]) {
board[i][j] *= 2;
board[k][j] = 0;
moved = true;
}
break;
}
}
}
}
}
} }
bool Board::isGameOver() { bool Board::isGameOver() {
// create
return true; return true;
} }
# 2048 Game # 2048 Game
- [The Rules](#the-rules) - [The Rules](#the-rules)
- [The Aim](#the-aim)
- [The Functions](#the-functions-in-depth) - [The Functions](#the-functions-in-depth)
- [Images](#images) - [Images](#images)
- [How To Play](#how-to-play)
- [Show randomness](#randomness-proof)
...@@ -60,7 +61,10 @@ I then use a switch statement to match what key was pressed and to run a functio ...@@ -60,7 +61,10 @@ I then use a switch statement to match what key was pressed and to run a functio
Compile using "g++ main.cpp 2048.cpp -o 2048" Compile using "g++ main.cpp 2048.cpp -o 2048"
Then run using ./2048 Then run using ./2048
#### How To Play
Use Keys W,A,S,D to select your move and press enter to submit your move.
then follow [The Rules](#the-rules)
# Images # Images
......
#pragma once #pragma once
#include <vector> //#include <vector>
class Board { class Board {
private: private:
......