Commit 40b1e610 authored by Brandon Toledo's avatar Brandon Toledo
Browse files

The up movemet is working

parent 830acdd0
...@@ -14,7 +14,7 @@ class Display { ...@@ -14,7 +14,7 @@ class Display {
cout <<"High Score: "<< setw(6) << highScore << setfill(' '); // the player cout <<"High Score: "<< setw(6) << highScore << setfill(' '); // the player
cout << setw((size*5) - 1) << "Score: " << setw(6) << score << endl << endl; cout << setw((size*5) - 1) << "Score: " << setw(6) << score << endl << endl;
} }
//this functions is to print part of the box for 2014 //this functions are for printing the board
void top() {cout << '+' << '-' << '-' << '-' << '-' << '-' << '-';} // "+------" void top() {cout << '+' << '-' << '-' << '-' << '-' << '-' << '-';} // "+------"
void middle() {cout << '|' << ' ' << ' ' << ' ' << ' ' << ' ' << ' ';}// "| " void middle() {cout << '|' << ' ' << ' ' << ' ' << ' ' << ' ' << ' ';}// "| "
void number(int value); // "| n " n is any number void number(int value); // "| n " n is any number
......
...@@ -15,12 +15,12 @@ Game2048::Game2048(int gameSize,int& highScore) { // this initialize a game ...@@ -15,12 +15,12 @@ Game2048::Game2048(int gameSize,int& highScore) { // this initialize a game
} }
void Game2048::playGame(){ void Game2048::playGame(){
*game = { {4,0,0,0}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}}; *game = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};
gameDisplay.showGame(*game, score, highScore); //shift.right(*game, score, highScore);
shift.right(*game, score); //shift.left(*game, score, highScore);
gameDisplay.showGame(*game, score, highScore); shift.down(*game, score, highScore);
shift.left(*game, score); shift.right(*game, score, highScore);
gameDisplay.showGame(*game, score, highScore);
delete game; delete game;
} }
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "Display.h"
#include "Movement.h" #include "Movement.h"
using namespace std; using namespace std;
...@@ -16,6 +15,5 @@ class Game2048 { ...@@ -16,6 +15,5 @@ class Game2048 {
int gameSize; int gameSize;
int score; int score;
Movement shift; Movement shift;
Display gameDisplay;
vector<vector<int>>* game = nullptr; vector<vector<int>>* game = nullptr;
}; };
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "Movement.h" #include "Movement.h"
#include "Display.h"
using namespace std; using namespace std;
void Movement::up(vector<vector<int>>& game, int& score){ void Movement::up(vector<vector<int>>& game, int& score, const int& highScore){
int i; for (vector<int> line: game){
for (int value: line){
cout << value << " ";
}
cout << endl;
}
cout << endl;
for (int i = game.size() - 1; i >= 0; i--){
for (int u = 0; u < game.size(); u++){
cout << game.at(u).at(i) << " ";
}
cout << endl;
}
} }
void Movement::down(vector<vector<int>>& game, int& score){ void Movement::down(vector<vector<int>>& game, int& score, const int& highScore){
int i; for (int i = game.size() - 1; i >= 0; i--){
for (int u = 0; u < game.size() - 1; u++){
if ((game.at(u).at(i) == game.at(u + 1).at(i)) && (game.at(u).at(i) != 0)) { // check if they are the same to add them
score += add(game.at(u).at(i), game.at(u + 1).at(i)); // this is add to the score
}
else if ((game.at(u).at(i) == 0) && (game.at(u + 1).at(i) != 0)){ //check to move the value if slot is empty
transfer(game.at(u).at(i), game.at(u + 1).at(i));
if (u > 0){ // this would stop it when it is at the end
u -= 2;
}
}
}
}
transition(game);
gameDisplay.showGame(game, score, highScore);
} }
void Movement::right(vector<vector<int>>& game, int& score){
for (vector<int>& row: game){
for (int i = row.size() - 1; i > 0; i--){
if ((row.at(i) == row.at(i - 1)) && (row.at(i) != 0)) { void Movement::right(vector<vector<int>>& game, int& score, const int& highScore){
score += add(row.at(i), row.at(i - 1)); for (int i = 0; i < game.size(); i++){
for (int u = game.size() - 1; u > 0; u--){
if ((game.at(i).at(u) == game.at(i).at(u - 1)) && (game.at(i).at(u) != 0)) {
score += add(game.at(i).at(u), game.at(i).at(u - 1));
} }
else if ((row.at(i) == 0) && (row.at(i - 1) != 0)){ else if ((game.at(i).at(u) == 0) && (game.at(i).at(u - 1) != 0)){
transfer(row.at(i), row.at(i - 1)); transfer(game.at(i).at(u), game.at(i).at(u - 1));
if (i < 3) { if (u < 3) {
i += 2; u += 2;
} }
} }
} }
} }
transition(game); transition(game);
gameDisplay.showGame(game, score, highScore);
} }
void Movement::left(vector<vector<int>>& game, int& score){ void Movement::left(vector<vector<int>>& game, int& score, const int& highScore){
for (vector<int>& row: game){ //seperates every row of the vector for (int i = 0; i < game.size(); i++){ //seperates every row of the vector
for (int i = 0; i < row.size() - 1; i++){ // individual value for (int u = 0; u < game.size() - 1; u++){ // individual value
if ((row.at(i) == row.at(i + 1)) && (row.at(i) != 0)) { // check if they are the same then add them if ((game.at(i).at(u) == game.at(i).at(u + 1)) && (game.at(i).at(u) != 0)) { // check if they are the same to add them
score += add(row.at(i), row.at(i + 1)); // this add to the score score += add(game.at(i).at(u), game.at(i).at(u + 1)); // this is add to the score
} }
else if ((row.at(i) == 0) && (row.at(i + 1) != 0)){ //check to move them to the left if box is empty else if ((game.at(i).at(u) == 0) && (game.at(i).at(u + 1) != 0)){ //check to move the value if slot is empty
transfer(row.at(i), row.at(i + 1)); transfer(game.at(i).at(u), game.at(i).at(u + 1));
if (i > 0){ // it would stop when it is at the end if (u > 0){ // this would stop it when it is at the end
i -= 2; u -= 2;
} }
} }
} }
} }
transition(game); transition(game);
gameDisplay.showGame(game, score, highScore);
} }
void Movement::transition(vector<vector<int>>& game){ //this is to prevent values to add them twice void Movement::transition(vector<vector<int>>& game){ //this is to prevent values to add them twice
for (vector<int>& line: game){ for (vector<int>& line: game){
for (int& value: line){ for (int& value: line){
if ((value != 0) && !(value%2 == 0)){ // it add one to all the odd numbers if ((value != 0) && !(value % 2 == 0)){ // it add one to all the odd numbers
value += 1; value += 1;
} }
} }
......
#ifndef MOVEMENT_H #ifndef MOVEMENT_H
#define MOVEMENT_H #define MOVEMENT_H
#include "Display.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
...@@ -7,11 +8,12 @@ using namespace std; ...@@ -7,11 +8,12 @@ using namespace std;
class Movement { class Movement {
public: public:
void up(vector<vector<int>>& game, int& score); void up(vector<vector<int>>& game, int& score, const int& highScore);
void down(vector<vector<int>>& game, int& score); void down(vector<vector<int>>& game, int& score, const int& highScore);
void right(vector<vector<int>>& game, int& score); void right(vector<vector<int>>& game, int& score, const int& highScore);
void left(vector<vector<int>>& game, int& score); void left(vector<vector<int>>& game, int& score, const int& highScore);
private: private:
Display gameDisplay;
void transition (vector<vector<int>>& game); //no values are added twice void transition (vector<vector<int>>& game); //no values are added twice
void transfer(int& num1, int& num2){ // to move the number if the next slot is empty void transfer(int& num1, int& num2){ // to move the number if the next slot is empty
num1 = num2; num1 = num2;
...@@ -24,3 +26,4 @@ class Movement { ...@@ -24,3 +26,4 @@ class Movement {
} }
}; };
#endif
...@@ -5,11 +5,28 @@ using namespace std; ...@@ -5,11 +5,28 @@ using namespace std;
int main() { int main() {
int highScore = 2048;
int highScore = 2048;
Game2048 game(4,highScore); Game2048 game(4,highScore);
game.playGame(); game.playGame();
/*
int y = 0;
vector<vector<int>> game(4, vector<int>(4,0));
for (int i = 0; i < game.size(); i++){
for(int u = 0;u < game.size()-1; u++) {
y++;
game.at(u).at(i) = y;
}
}
for (vector<int> line: game){
for (int value: line){
cout << value << " ";
}
cout << endl;
}
*/
return 0; return 0;
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment