Commit 5f63363a authored by Brandon Toledo's avatar Brandon Toledo
Browse files

2048 mostly finish

parent 5d9fc366
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <cstdlib>
#include "Display.h" #include "Display.h"
#include "Movement.h" #include "Movement.h"
#include "Game2048.h" #include "Game2048.h"
...@@ -13,23 +14,20 @@ Game2048::Game2048(int gameSize,int& highScore) { // this initialize a board ...@@ -13,23 +14,20 @@ Game2048::Game2048(int gameSize,int& highScore) { // this initialize a board
score = 0; score = 0;
board = 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(){
*board = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}}; char choise;
movement.right(*board, score); while(true){
display.showGame(*board, score, highScore); randomNumber();
movement.left(*board, score); Draw();
display.showGame(*board, score, highScore); cin << choise;
movement.up(*board, score); Shift(choise);
display.showGame(*board, score, highScore);
movement.right(*board, score); }
display.showGame(*board, score, highScore);
movement.down(*board, score);
display.showGame(*board, score, highScore);
delete board; delete board;
} }
*/
void Game2048::Shift(char input){ void Game2048::Shift(char input){
switch(input){ switch(input){
...@@ -46,6 +44,37 @@ void Game2048::Shift(char input){ ...@@ -46,6 +44,37 @@ void Game2048::Shift(char input){
movement.right(*board, score); movement.right(*board, score);
break; break;
} }
}
bool Game2048::addNumber(){
int num = rand() % 5;
int numX;
int numY;
bool pass = true;
bool success = false;
if (num != 4) {
num = 2;
}
for (vector<int> line: *board){
for(int value: line){
if (value == 0) {
success = true;
}
}
}
if (!success){
return success;
}
while(pass){
numX = rand() % board->size();
numY = rand() % board->size();
if (board->at(numX).at(numY) == 0){
board->at(numX).at(numY) = num;
pass = false;
}
}
return success;
} }
\ No newline at end of file
...@@ -7,11 +7,12 @@ using namespace std; ...@@ -7,11 +7,12 @@ using namespace std;
class Game2048 { class Game2048 {
public: public:
Game2048(int gameSize, int& highScore); Game2048(int gameSize, int& highScore); // remove the pass by reference in high score
void playGame(); void playGame();
void Draw() {display.showGame(*board, score, highScore);} void Draw() {display.showGame(*board, score, highScore);}
void Shift(char input); void Shift(char input);
void testInit(){*board = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};} void testInit(){*board = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};}
bool addNumber();
......
...@@ -80,10 +80,4 @@ void Movement::transition(vector<vector<int>>& board){ //this is to prevent va ...@@ -80,10 +80,4 @@ void Movement::transition(vector<vector<int>>& board){ //this is to prevent va
} }
} }
} }
} }
\ No newline at end of file
/*void Movement::randomNumber(vector<vector<int>>& board){
}
*/
\ No newline at end of file
...@@ -11,7 +11,6 @@ class Movement { ...@@ -11,7 +11,6 @@ class Movement {
void down(vector<vector<int>>& board, int& score); void down(vector<vector<int>>& board, int& score);
void right(vector<vector<int>>& board, int& score); void right(vector<vector<int>>& board, int& score);
void left(vector<vector<int>>& board, int& score); void left(vector<vector<int>>& board, int& score);
void randomNumber(vector<vector<int>>& board);
private: private:
void transition (vector<vector<int>>& board); //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
......
...@@ -3,13 +3,14 @@ ...@@ -3,13 +3,14 @@
#include "Game2048.h" #include "Game2048.h"
using namespace std; using namespace std;
/*
// int main() { int main() {
// int highScore = 2048; int highScore = 2048;
// Game2048 game(4,highScore); Game2048 game(4,highScore);
// game.playGame(); game.playGame();
// return 0; return 0;
// } }
*/
\ No newline at end of file
...@@ -76,6 +76,9 @@ void C2048GameState::Update(CGameEngine* game) ...@@ -76,6 +76,9 @@ void C2048GameState::Update(CGameEngine* game)
cout << " 2048GameState Update" << endl; cout << " 2048GameState Update" << endl;
game2048->Shift(input); game2048->Shift(input);
if (!game2048->addNumber()) {
game->Quit();
}
} }
void C2048GameState::Draw(CGameEngine* game) void C2048GameState::Draw(CGameEngine* game)
......
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