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

2048 mostly finish

parent 5d9fc366
#include <iostream>
#include <vector>
#include <cstdlib>
#include "Display.h"
#include "Movement.h"
#include "Game2048.h"
......@@ -13,23 +14,20 @@ Game2048::Game2048(int gameSize,int& highScore) { // this initialize a board
score = 0;
board = new vector<vector<int>>(gameSize, vector<int>(gameSize,0));
}
/*
void Game2048::playGame(){
*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);
char choise;
while(true){
randomNumber();
Draw();
cin << choise;
Shift(choise);
}
delete board;
}
*/
void Game2048::Shift(char input){
switch(input){
......@@ -46,6 +44,37 @@ void Game2048::Shift(char input){
movement.right(*board, score);
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;
class Game2048 {
public:
Game2048(int gameSize, int& highScore);
Game2048(int gameSize, int& highScore); // remove the pass by reference in high score
void playGame();
void Draw() {display.showGame(*board, score, highScore);}
void Shift(char input);
void testInit(){*board = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};}
bool addNumber();
......
......@@ -81,9 +81,3 @@ 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 {
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>>& board); //no values are added twice
void transfer(int& num1, int& num2){ // to move the number if the next slot is empty
......
......@@ -3,13 +3,14 @@
#include "Game2048.h"
using namespace std;
/*
int main() {
// int main() {
int highScore = 2048;
Game2048 game(4,highScore);
game.playGame();
// int highScore = 2048;
// Game2048 game(4,highScore);
// game.playGame();
// return 0;
// }
return 0;
}
*/
\ No newline at end of file
......@@ -76,6 +76,9 @@ void C2048GameState::Update(CGameEngine* game)
cout << " 2048GameState Update" << endl;
game2048->Shift(input);
if (!game2048->addNumber()) {
game->Quit();
}
}
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