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() {
}
void Board::moveLeft() {
for (int i = 0; i < SIZE; ++i) {
// Slide tiles to the left
for (int j = 0; j < SIZE - 1; ++j) {
for (int i = 0; i < SIZE; ++i) { // loops through all the rows
for (int j = 0; j < SIZE - 1; ++j) { // loops through the columns except the column on the right
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
......@@ -118,8 +117,7 @@ void Board::moveLeft() {
}
}
} else {
// If the current tile is not empty, try to merge with the next tile
} else {
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
......@@ -134,28 +132,26 @@ void Board::moveLeft() {
}
}
}
void Board::moveRight() {
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) {
for (int j = SIZE - 1; j > 0; --j) {
if (board[i][j] == 0) {
for (int k = j - 1; k >= 0; --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
moved = true;
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
for (int k = j - 1; k >= 0; --k) {
if (board[i][k] != 0) {
if (board[i][j] == board[i][k]) {
board[i][j] *= 2;
board[i][k] = 0;
moved = true;
}
break;
}
......@@ -166,14 +162,61 @@ void Board::moveRight() {
}
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() {
// 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() {
// create
return true;
}
# 2048 Game
- [The Rules](#the-rules)
- [The Aim](#the-aim)
- [The Functions](#the-functions-in-depth)
- [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
Compile using "g++ main.cpp 2048.cpp -o 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
......
#pragma once
#include <vector>
//#include <vector>
class Board {
private:
......