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

The up movemet is working

parent 830acdd0
......@@ -14,7 +14,7 @@ class Display {
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
//this functions are for printing the board
void top() {cout << '+' << '-' << '-' << '-' << '-' << '-' << '-';} // "+------"
void middle() {cout << '|' << ' ' << ' ' << ' ' << ' ' << ' ' << ' ';}// "| "
void number(int value); // "| n " n is any number
......
......@@ -15,12 +15,12 @@ Game2048::Game2048(int gameSize,int& highScore) { // this initialize a game
}
void Game2048::playGame(){
*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);
*game = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};
//shift.right(*game, score, highScore);
//shift.left(*game, score, highScore);
shift.down(*game, score, highScore);
shift.right(*game, score, highScore);
delete game;
}
#include <iostream>
#include <vector>
#include "Display.h"
#include "Movement.h"
using namespace std;
......@@ -16,6 +15,5 @@ class Game2048 {
int gameSize;
int score;
Movement shift;
Display gameDisplay;
vector<vector<int>>* game = nullptr;
};
#include <iostream>
#include <vector>
#include "Movement.h"
#include "Display.h"
using namespace std;
void Movement::up(vector<vector<int>>& game, int& score){
int i;
void Movement::up(vector<vector<int>>& game, int& score, const int& highScore){
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){
int i;
void Movement::down(vector<vector<int>>& game, int& score, const int& highScore){
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)) {
score += add(row.at(i), row.at(i - 1));
void Movement::right(vector<vector<int>>& game, int& score, const int& highScore){
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)){
transfer(row.at(i), row.at(i - 1));
if (i < 3) {
i += 2;
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));
if (u < 3) {
u += 2;
}
}
}
}
transition(game);
gameDisplay.showGame(game, score, highScore);
}
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
void Movement::left(vector<vector<int>>& game, int& score, const int& highScore){
for (int i = 0; i < game.size(); i++){ //seperates every row of the vector
for (int u = 0; u < game.size() - 1; u++){ // individual value
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(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
transfer(row.at(i), row.at(i + 1));
if (i > 0){ // it would stop when it is at the end
i -= 2;
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(game.at(i).at(u), game.at(i).at(u + 1));
if (u > 0){ // this would stop it when it is at the end
u -= 2;
}
}
}
}
transition(game);
gameDisplay.showGame(game, score, highScore);
}
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
if ((value != 0) && !(value % 2 == 0)){ // it add one to all the odd numbers
value += 1;
}
}
......
#ifndef MOVEMENT_H
#define MOVEMENT_H
#include "Display.h"
#include <iostream>
#include <vector>
......@@ -7,11 +8,12 @@ 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 up(vector<vector<int>>& game, int& score, const int& highScore);
void down(vector<vector<int>>& game, int& score, const int& highScore);
void right(vector<vector<int>>& game, int& score, const int& highScore);
void left(vector<vector<int>>& game, int& score, const int& highScore);
private:
Display gameDisplay;
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;
......@@ -24,3 +26,4 @@ class Movement {
}
};
#endif
......@@ -5,11 +5,28 @@ using namespace std;
int main() {
int highScore = 2048;
int highScore = 2048;
Game2048 game(4,highScore);
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;
}
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