Commit 25d70808 authored by Brandon Toledo's avatar Brandon Toledo
Browse files

seperate display and movement

parent e552481e
...@@ -16,11 +16,16 @@ Game2048::Game2048(int gameSize,int& highScore) { // this initialize a game ...@@ -16,11 +16,16 @@ Game2048::Game2048(int gameSize,int& highScore) { // this initialize a game
void Game2048::playGame(){ void Game2048::playGame(){
*game = { {4,0,0,2}, {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}};
shift.right(*game, score, highScore); shift.right(*game, score);
shift.left(*game, score, highScore); board.showGame(*game, score, highScore);
shift.up(*game, score, highScore); shift.left(*game, score);
shift.right(*game, score, highScore); board.showGame(*game, score, highScore);
shift.down(*game, score, highScore); shift.up(*game, score);
board.showGame(*game, score, highScore);
shift.right(*game, score);
board.showGame(*game, score, highScore);
shift.down(*game, score);
board.showGame(*game, score, highScore);
delete game; delete game;
} }
......
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "Movement.h" #include "Movement.h"
#include "Display.h"
using namespace std; using namespace std;
...@@ -15,5 +16,6 @@ class Game2048 { ...@@ -15,5 +16,6 @@ class Game2048 {
int gameSize; int gameSize;
int score; int score;
Movement shift; Movement shift;
Display board;
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, const int& highScore){ void Movement::up(vector<vector<int>>& game, int& score){
for (int i = game.size() - 1; i >= 0; i--){ for (int i = game.size() - 1; i >= 0; i--){
for (int u = 0; u < game.size() - 1; u++){ 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 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
...@@ -20,10 +19,9 @@ void Movement::up(vector<vector<int>>& game, int& score, const int& highScore){ ...@@ -20,10 +19,9 @@ void Movement::up(vector<vector<int>>& game, int& score, const int& highScore){
} }
} }
transition(game); transition(game);
gameDisplay.showGame(game, score, highScore);
} }
void Movement::down(vector<vector<int>>& game, int& score, const int& highScore){ void Movement::down(vector<vector<int>>& game, int& score){
for (int i = game.size() - 1; i >= 0; i--){ for (int i = game.size() - 1; i >= 0; i--){
for (int u = game.size() - 1; u > 0; u--){ for (int u = game.size() - 1; u > 0; u--){
if ((game.at(u).at(i) == game.at(u - 1).at(i)) && (game.at(u).at(i) != 0)) { if ((game.at(u).at(i) == game.at(u - 1).at(i)) && (game.at(u).at(i) != 0)) {
...@@ -38,10 +36,9 @@ void Movement::down(vector<vector<int>>& game, int& score, const int& highScore) ...@@ -38,10 +36,9 @@ void Movement::down(vector<vector<int>>& game, int& score, const int& highScore)
} }
} }
transition(game); transition(game);
gameDisplay.showGame(game, score, highScore);
} }
void Movement::right(vector<vector<int>>& game, int& score, const int& highScore){ void Movement::right(vector<vector<int>>& game, int& score){
for (int i = 0; i < game.size(); i++){ for (int i = 0; i < game.size(); i++){
for (int u = game.size() - 1; u > 0; u--){ 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)) { if ((game.at(i).at(u) == game.at(i).at(u - 1)) && (game.at(i).at(u) != 0)) {
...@@ -56,10 +53,9 @@ void Movement::right(vector<vector<int>>& game, int& score, const int& highScore ...@@ -56,10 +53,9 @@ void Movement::right(vector<vector<int>>& game, int& score, const int& highScore
} }
} }
transition(game); transition(game);
gameDisplay.showGame(game, score, highScore);
} }
void Movement::left(vector<vector<int>>& game, int& score, const int& highScore){ void Movement::left(vector<vector<int>>& game, int& score){
for (int i = 0; i < game.size(); i++){ for (int i = 0; i < game.size(); i++){
for (int u = 0; u < game.size() - 1; u++){ for (int u = 0; u < game.size() - 1; u++){
if ((game.at(i).at(u) == game.at(i).at(u + 1)) && (game.at(i).at(u) != 0)) { if ((game.at(i).at(u) == game.at(i).at(u + 1)) && (game.at(i).at(u) != 0)) {
...@@ -74,7 +70,6 @@ void Movement::left(vector<vector<int>>& game, int& score, const int& highScore) ...@@ -74,7 +70,6 @@ void Movement::left(vector<vector<int>>& game, int& score, const int& highScore)
} }
} }
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
...@@ -85,4 +80,7 @@ void Movement::transition(vector<vector<int>>& game){ //this is to prevent val ...@@ -85,4 +80,7 @@ void Movement::transition(vector<vector<int>>& game){ //this is to prevent val
} }
} }
} }
} }
\ No newline at end of file
//void Movement::randomNumber()
\ No newline at end of file
#ifndef MOVEMENT_H #ifndef MOVEMENT_H
#define MOVEMENT_H #define MOVEMENT_H
#include "Display.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
...@@ -8,12 +7,12 @@ using namespace std; ...@@ -8,12 +7,12 @@ using namespace std;
class Movement { class Movement {
public: public:
void up(vector<vector<int>>& game, int& score, const int& highScore); void up(vector<vector<int>>& game, int& score);
void down(vector<vector<int>>& game, int& score, const int& highScore); void down(vector<vector<int>>& game, int& score);
void right(vector<vector<int>>& game, int& score, const int& highScore); void right(vector<vector<int>>& game, int& score);
void left(vector<vector<int>>& game, int& score, const int& highScore); void left(vector<vector<int>>& game, int& score);
void randomNumber(vector<vector<int>>& game);
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;
......
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