Commit 1f502886 authored by Brandon Toledo's avatar Brandon Toledo
Browse files

work in game2048.h

parent 25d70808
...@@ -10,10 +10,10 @@ void Display::number (int value) { ...@@ -10,10 +10,10 @@ void Display::number (int value) {
else {cout << '|' << ' ' << setw(4) << value << ' ';} else {cout << '|' << ' ' << setw(4) << value << ' ';}
} }
void Display::showGame(const vector<vector<int>>& game, const int& score, const int& highScore) { void Display::showGame(const vector<vector<int>>& board, const int& score, const int& highScore) {
cout << setw(64) << "For help press: h" << endl << endl; cout << setw(64) << "For help press: h" << endl << endl;
showScore(score, highScore, game.size()); showScore(score, highScore, board.size());
for (const vector<int>& colums: game ) { for (const vector<int>& colums: board ) {
for(int value: colums){ for(int value: colums){
top(); top();
} }
...@@ -31,7 +31,7 @@ void Display::showGame(const vector<vector<int>>& game, const int& score, const ...@@ -31,7 +31,7 @@ void Display::showGame(const vector<vector<int>>& game, const int& score, const
} }
cout << '|' << endl; cout << '|' << endl;
} }
for (const vector<int>& colums: game){ for (const vector<int>& colums: board){
top(); top();
} }
cout << '+' << endl << endl << endl; cout << '+' << endl << endl << endl;
......
...@@ -8,7 +8,7 @@ using namespace std; ...@@ -8,7 +8,7 @@ using namespace std;
class Display { class Display {
public: public:
void showGame(const vector<vector<int>>& game, const int& score, const int& highScore); // prints the board void showGame(const vector<vector<int>>& board, const int& score, const int& highScore); // prints the board
private: private:
void showScore(const int& score, const int& highScore, int size) { // this show the high scor and the void showScore(const int& score, const int& highScore, int size) { // this show the high scor and the
cout <<"High Score: "<< setw(6) << highScore << setfill(' '); // the player cout <<"High Score: "<< setw(6) << highScore << setfill(' '); // the player
......
...@@ -5,28 +5,47 @@ ...@@ -5,28 +5,47 @@
#include "Game2048.h" #include "Game2048.h"
using namespace std; using namespace std;
void print(const vector<vector<int>>& game); void print(const vector<vector<int>>& board);
Game2048::Game2048(int gameSize,int& highScore) { // this initialize a game Game2048::Game2048(int gameSize,int& highScore) { // this initialize a board
this->gameSize = gameSize; this->gameSize = gameSize;
this->highScore = highScore; this->highScore = highScore;
score = 0; score = 0;
game = new vector<vector<int>>(gameSize, vector<int>(gameSize,0)); board = new vector<vector<int>>(gameSize, vector<int>(gameSize,0));
} }
void Game2048::playGame(){ void Game2048::playGame(){
*game = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}}; *board = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};
shift.right(*game, score); movement.right(*board, score);
board.showGame(*game, score, highScore); display.showGame(*board, score, highScore);
shift.left(*game, score); movement.left(*board, score);
board.showGame(*game, score, highScore); display.showGame(*board, score, highScore);
shift.up(*game, score); movement.up(*board, score);
board.showGame(*game, score, highScore); display.showGame(*board, score, highScore);
shift.right(*game, score); movement.right(*board, score);
board.showGame(*game, score, highScore); display.showGame(*board, score, highScore);
shift.down(*game, score); movement.down(*board, score);
board.showGame(*game, score, highScore); display.showGame(*board, score, highScore);
delete game; delete board;
} }
void Game2048::Shift(char input){
switch(input){
case 'w':
movement.up(*board, score);
break;
case 'a':
movement.left(*board, score);
break;
case 's':
movement.down(*board, score);
break;
case 'd':
movement.right(*board, score);
break;
}
}
\ No newline at end of file
...@@ -9,13 +9,14 @@ class Game2048 { ...@@ -9,13 +9,14 @@ class Game2048 {
public: public:
Game2048(int gameSize, int& highScore); Game2048(int gameSize, int& highScore);
void playGame(); void playGame();
void Draw() {display.showGame(*board, score, highScore);}
void Shift(char input);
private: private:
int highScore; int highScore;
int gameSize; int gameSize;
int score; int score;
Movement shift; Movement movement;
Display board; Display display;
vector<vector<int>>* game = nullptr; vector<vector<int>>* board = nullptr;
}; };
...@@ -4,76 +4,76 @@ ...@@ -4,76 +4,76 @@
using namespace std; using namespace std;
void Movement::up(vector<vector<int>>& game, int& score){ void Movement::up(vector<vector<int>>& board, int& score){
for (int i = game.size() - 1; i >= 0; i--){ for (int i = board.size() - 1; i >= 0; i--){
for (int u = 0; u < game.size() - 1; u++){ for (int u = 0; u < board.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 ((board.at(u).at(i) == board.at(u + 1).at(i)) && (board.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 score += add(board.at(u).at(i), board.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 else if ((board.at(u).at(i) == 0) && (board.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)); transfer(board.at(u).at(i), board.at(u + 1).at(i));
if (u > 0){ // this would stop it when it is at the end if (u > 0){ // this would stop it when it is at the end
u -= 2; u -= 2;
} }
} }
} }
} }
transition(game); transition(board);
} }
void Movement::down(vector<vector<int>>& game, int& score){ void Movement::down(vector<vector<int>>& board, int& score){
for (int i = game.size() - 1; i >= 0; i--){ for (int i = board.size() - 1; i >= 0; i--){
for (int u = game.size() - 1; u > 0; u--){ for (int u = board.size() - 1; u > 0; u--){
if ((game.at(u).at(i) == game.at(u - 1).at(i)) && (game.at(u).at(i) != 0)) { if ((board.at(u).at(i) == board.at(u - 1).at(i)) && (board.at(u).at(i) != 0)) {
score += add(game.at(u).at(i), game.at(u - 1).at(i)); score += add(board.at(u).at(i), board.at(u - 1).at(i));
} }
else if ((game.at(u).at(i) == 0) && (game.at(u - 1).at(i) != 0)){ else if ((board.at(u).at(i) == 0) && (board.at(u - 1).at(i) != 0)){
transfer(game.at(u).at(i), game.at(u - 1).at(i)); transfer(board.at(u).at(i), board.at(u - 1).at(i));
if (u < 3){ if (u < 3){
u += 2; u += 2;
} }
} }
} }
} }
transition(game); transition(board);
} }
void Movement::right(vector<vector<int>>& game, int& score){ void Movement::right(vector<vector<int>>& board, int& score){
for (int i = 0; i < game.size(); i++){ for (int i = 0; i < board.size(); i++){
for (int u = game.size() - 1; u > 0; u--){ for (int u = board.size() - 1; u > 0; u--){
if ((game.at(i).at(u) == game.at(i).at(u - 1)) && (game.at(i).at(u) != 0)) { if ((board.at(i).at(u) == board.at(i).at(u - 1)) && (board.at(i).at(u) != 0)) {
score += add(game.at(i).at(u), game.at(i).at(u - 1)); score += add(board.at(i).at(u), board.at(i).at(u - 1));
} }
else if ((game.at(i).at(u) == 0) && (game.at(i).at(u - 1) != 0)){ else if ((board.at(i).at(u) == 0) && (board.at(i).at(u - 1) != 0)){
transfer(game.at(i).at(u), game.at(i).at(u - 1)); transfer(board.at(i).at(u), board.at(i).at(u - 1));
if (u < 3) { if (u < 3) {
u += 2; u += 2;
} }
} }
} }
} }
transition(game); transition(board);
} }
void Movement::left(vector<vector<int>>& game, int& score){ void Movement::left(vector<vector<int>>& board, int& score){
for (int i = 0; i < game.size(); i++){ for (int i = 0; i < board.size(); i++){
for (int u = 0; u < game.size() - 1; u++){ for (int u = 0; u < board.size() - 1; u++){
if ((game.at(i).at(u) == game.at(i).at(u + 1)) && (game.at(i).at(u) != 0)) { if ((board.at(i).at(u) == board.at(i).at(u + 1)) && (board.at(i).at(u) != 0)) {
score += add(game.at(i).at(u), game.at(i).at(u + 1)); score += add(board.at(i).at(u), board.at(i).at(u + 1));
} }
else if ((game.at(i).at(u) == 0) && (game.at(i).at(u + 1) != 0)){ else if ((board.at(i).at(u) == 0) && (board.at(i).at(u + 1) != 0)){
transfer(game.at(i).at(u), game.at(i).at(u + 1)); transfer(board.at(i).at(u), board.at(i).at(u + 1));
if (u > 0){ if (u > 0){
u -= 2; u -= 2;
} }
} }
} }
} }
transition(game); transition(board);
} }
void Movement::transition(vector<vector<int>>& game){ //this is to prevent values to add them twice void Movement::transition(vector<vector<int>>& board){ //this is to prevent values to add them twice
for (vector<int>& line: game){ for (vector<int>& line: board){
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;
...@@ -83,4 +83,7 @@ void Movement::transition(vector<vector<int>>& game){ //this is to prevent val ...@@ -83,4 +83,7 @@ void Movement::transition(vector<vector<int>>& game){ //this is to prevent val
} }
//void Movement::randomNumber() /*void Movement::randomNumber(vector<vector<int>>& board){
\ No newline at end of file
}
*/
\ No newline at end of file
...@@ -7,13 +7,13 @@ using namespace std; ...@@ -7,13 +7,13 @@ using namespace std;
class Movement { class Movement {
public: public:
void up(vector<vector<int>>& game, int& score); void up(vector<vector<int>>& board, int& score);
void down(vector<vector<int>>& game, int& score); void down(vector<vector<int>>& board, int& score);
void right(vector<vector<int>>& game, int& score); void right(vector<vector<int>>& board, int& score);
void left(vector<vector<int>>& game, int& score); void left(vector<vector<int>>& board, int& score);
void randomNumber(vector<vector<int>>& game); void randomNumber(vector<vector<int>>& board);
private: private:
void transition (vector<vector<int>>& game); //no values are added twice void transition (vector<vector<int>>& board); //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;
num2 = 0; num2 = 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