Commit 45bf4c05 authored by Brandon Toledo's avatar Brandon Toledo

The numbers can be move to the right and left, scores are show

parent 86caa1fc
......@@ -6,11 +6,13 @@
using namespace std;
void Display::number (int value) {
if (value == 0) { middle();} //zero means that it is empty
if (value == 0) { middle();} //zero means that it is empty slot
else {cout << '|' << ' ' << setw(4) << value << ' ';}
}
void Display::showGame(const vector<vector<int>>& game) {
void Display::showGame(const vector<vector<int>>& game, 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 ) {
for(int value: colums){
top();
......
......@@ -2,16 +2,22 @@
#define DISPLAY_H
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
class Display {
public:
void showGame(const vector<vector<int>>& game);
private: //this functions is to print part of the box for 2014
void top() {cout << '+' << '-' << '-' << '-' << '-' << '-' << '-';}
void middle() {cout << '|' << ' ' << ' ' << ' ' << ' ' << ' ' << ' ';}
void number(int value);
void showGame(const vector<vector<int>>& game, 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
cout << setw((size*5) - 1) << "Score: " << setw(6) << score << endl << endl;
}
//this functions is to print part of the box for 2014
void top() {cout << '+' << '-' << '-' << '-' << '-' << '-' << '-';} // "+------"
void middle() {cout << '|' << ' ' << ' ' << ' ' << ' ' << ' ' << ' ';}// "| "
void number(int value); // "| n " n is any number
};
#endif
\ No newline at end of file
......@@ -3,21 +3,24 @@
#include "Display.h"
#include "Movement.h"
#include "Game2048.h"
using namespace std;
Game2048::Game2048(int gameSize) {
void print(const vector<vector<int>>& game);
Game2048::Game2048(int gameSize,int& highScore) { // this initialize a game
this->gameSize = gameSize;
this->highScore = highScore;
score = 0;
game = new vector<vector<int>>(gameSize, vector<int>(gameSize,0));
}
void Game2048::playGame(){
*game = { {2,0,0,2}, {2,0,2,2}, {4,2,0,2}, {2,2,2,2}};
gameDisplay.showGame(*game);
shift.left(*game);
gameDisplay.showGame(*game);
*game = { {4,0,0,0}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};
gameDisplay.showGame(*game, score, highScore);
shift.right(*game, score);
gameDisplay.showGame(*game, score, highScore);
shift.left(*game, score);
gameDisplay.showGame(*game, score, highScore);
delete game;
}
......@@ -7,11 +7,12 @@ using namespace std;
class Game2048 {
public:
Game2048(int gameSize);
Game2048(int gameSize, int& highScore);
void playGame();
private:
int highScore;
int gameSize;
int score;
Movement shift;
......
......@@ -3,27 +3,51 @@
#include "Movement.h"
using namespace std;
void Movement::up(vector<vector<int>>& game){
int i;
}
void Movement::down(vector<vector<int>>& game){
void Movement::up(vector<vector<int>>& game, int& score){
int i;
}
void Movement::right(vector<vector<int>>& game){
void Movement::down(vector<vector<int>>& game, int& score){
int i;
}
void Movement::left(vector<vector<int>>& game){
void Movement::right(vector<vector<int>>& game, int& score){
for (vector<int>& row: game){
for (int i = 0; i < row.size() - 1; i++){
if ((row.at(i) == row.at(i + 1)) && (row.at(i) != 0)) {
add(row.at(i), row.at(i + 1));
i--;
}
else if ((row.at(i) == 0) && (row.at(i + 1) != 0)){
transfer(row.at(i), row.at(i+1));
i -= 2;
}
}
for (int i = row.size() - 1; i > 0; i--){
if ((row.at(i) == row.at(i - 1)) && (row.at(i) != 0)) {
score += add(row.at(i), row.at(i - 1));
}
else if ((row.at(i) == 0) && (row.at(i - 1) != 0)){
transfer(row.at(i), row.at(i - 1));
if (i < 3) {
i += 2;
}
}
}
}
transition(game);
}
void Movement::left(vector<vector<int>>& game, int& score){
for (vector<int>& row: game){ //seperates every row of the vector
for (int i = 0; i < row.size() - 1; i++){ // individual value
if ((row.at(i) == row.at(i + 1)) && (row.at(i) != 0)) { // check if they are the same then add them
score += add(row.at(i), row.at(i + 1)); // this 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
transfer(row.at(i), row.at(i + 1));
if (i > 0){ // it would stop when it is at the end
i -= 2;
}
}
}
}
transition(game);
}
void Movement::transition(vector<vector<int>>& game){ //this is to prevent values to add them twice
for (vector<int>& line: game){
for (int& value: line){
if ((value != 0) && !(value%2 == 0)){ // it add one to all the odd numbers
value += 1;
}
}
}
}
\ No newline at end of file
......@@ -7,20 +7,20 @@ using namespace std;
class Movement {
public:
void up(vector<vector<int>>& game);
void down(vector<vector<int>>& game);
void right(vector<vector<int>>& game);
void left(vector<vector<int>>& game);
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);
private:
void transfer(int& num1, int& num2){ // to move the number if
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
num1 = num2;
num2 = 0;
}
int add(int& num1, int& num2){ //this would return adition and would be use for the score
num1 += num2;
num1 += num2 - 1; // this is to prevent them from adding twice
num2 = 0;
return num1;
return (num1 + 1);
}
};
#endif
\ No newline at end of file
......@@ -2,10 +2,12 @@
#include <vector> // for 2D vector
#include "Game2048.h"
using namespace std;
int main() {
int highScore = 2048;
Game2048 game(4);
Game2048 game(4,highScore);
game.playGame();
......
Markdown is supported
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