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) {
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;
showScore(score, highScore, game.size());
for (const vector<int>& colums: game ) {
showScore(score, highScore, board.size());
for (const vector<int>& colums: board ) {
for(int value: colums){
top();
}
......@@ -31,7 +31,7 @@ void Display::showGame(const vector<vector<int>>& game, const int& score, const
}
cout << '|' << endl;
}
for (const vector<int>& colums: game){
for (const vector<int>& colums: board){
top();
}
cout << '+' << endl << endl << endl;
......
......@@ -8,7 +8,7 @@ using namespace std;
class Display {
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:
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
......
......@@ -5,28 +5,47 @@
#include "Game2048.h"
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->highScore = highScore;
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(){
*game = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};
shift.right(*game, score);
board.showGame(*game, score, highScore);
shift.left(*game, score);
board.showGame(*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);
*board = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};
movement.right(*board, score);
display.showGame(*board, score, highScore);
movement.left(*board, score);
display.showGame(*board, score, highScore);
movement.up(*board, score);
display.showGame(*board, score, highScore);
movement.right(*board, score);
display.showGame(*board, score, highScore);
movement.down(*board, score);
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 {
public:
Game2048(int gameSize, int& highScore);
void playGame();
void Draw() {display.showGame(*board, score, highScore);}
void Shift(char input);
private:
int highScore;
int gameSize;
int score;
Movement shift;
Display board;
vector<vector<int>>* game = nullptr;
Movement movement;
Display display;
vector<vector<int>>* board = nullptr;
};
......@@ -4,76 +4,76 @@
using namespace std;
void Movement::up(vector<vector<int>>& game, int& score){
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
void Movement::up(vector<vector<int>>& board, int& score){
for (int i = board.size() - 1; i >= 0; i--){
for (int u = 0; u < board.size() - 1; u++){
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(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
transfer(game.at(u).at(i), game.at(u + 1).at(i));
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(board.at(u).at(i), board.at(u + 1).at(i));
if (u > 0){ // this would stop it when it is at the end
u -= 2;
}
}
}
}
transition(game);
transition(board);
}
void Movement::down(vector<vector<int>>& game, int& score){
for (int i = game.size() - 1; i >= 0; i--){
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)) {
score += add(game.at(u).at(i), game.at(u - 1).at(i));
void Movement::down(vector<vector<int>>& board, int& score){
for (int i = board.size() - 1; i >= 0; i--){
for (int u = board.size() - 1; u > 0; u--){
if ((board.at(u).at(i) == board.at(u - 1).at(i)) && (board.at(u).at(i) != 0)) {
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)){
transfer(game.at(u).at(i), game.at(u - 1).at(i));
else if ((board.at(u).at(i) == 0) && (board.at(u - 1).at(i) != 0)){
transfer(board.at(u).at(i), board.at(u - 1).at(i));
if (u < 3){
u += 2;
}
}
}
}
transition(game);
transition(board);
}
void Movement::right(vector<vector<int>>& game, int& score){
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));
void Movement::right(vector<vector<int>>& board, int& score){
for (int i = 0; i < board.size(); i++){
for (int u = board.size() - 1; u > 0; u--){
if ((board.at(i).at(u) == board.at(i).at(u - 1)) && (board.at(i).at(u) != 0)) {
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)){
transfer(game.at(i).at(u), game.at(i).at(u - 1));
else if ((board.at(i).at(u) == 0) && (board.at(i).at(u - 1) != 0)){
transfer(board.at(i).at(u), board.at(i).at(u - 1));
if (u < 3) {
u += 2;
}
}
}
}
transition(game);
transition(board);
}
void Movement::left(vector<vector<int>>& game, int& score){
for (int i = 0; i < game.size(); i++){
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)) {
score += add(game.at(i).at(u), game.at(i).at(u + 1));
void Movement::left(vector<vector<int>>& board, int& score){
for (int i = 0; i < board.size(); i++){
for (int u = 0; u < board.size() - 1; u++){
if ((board.at(i).at(u) == board.at(i).at(u + 1)) && (board.at(i).at(u) != 0)) {
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)){
transfer(game.at(i).at(u), game.at(i).at(u + 1));
else if ((board.at(i).at(u) == 0) && (board.at(i).at(u + 1) != 0)){
transfer(board.at(i).at(u), board.at(i).at(u + 1));
if (u > 0){
u -= 2;
}
}
}
}
transition(game);
transition(board);
}
void Movement::transition(vector<vector<int>>& game){ //this is to prevent values to add them twice
for (vector<int>& line: game){
void Movement::transition(vector<vector<int>>& board){ //this is to prevent values to add them twice
for (vector<int>& line: board){
for (int& value: line){
if ((value != 0) && !(value % 2 == 0)){ // it add one to all the odd numbers
value += 1;
......@@ -83,4 +83,7 @@ void Movement::transition(vector<vector<int>>& game){ //this is to prevent val
}
//void Movement::randomNumber()
\ No newline at end of file
/*void Movement::randomNumber(vector<vector<int>>& board){
}
*/
\ No newline at end of file
......@@ -7,13 +7,13 @@ using namespace std;
class Movement {
public:
void up(vector<vector<int>>& game, int& score);
void down(vector<vector<int>>& game, int& score);
void right(vector<vector<int>>& game, int& score);
void left(vector<vector<int>>& game, int& score);
void randomNumber(vector<vector<int>>& game);
void up(vector<vector<int>>& board, int& score);
void down(vector<vector<int>>& board, int& score);
void right(vector<vector<int>>& board, int& score);
void left(vector<vector<int>>& board, int& score);
void randomNumber(vector<vector<int>>& board);
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
num1 = num2;
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