From f6eab5ce6a7eb0b665b8c76ffc102359a9e86388 Mon Sep 17 00:00:00 2001 From: h45-taylor <harvey.taylor3@live.uwe.ac.uk> Date: Mon, 11 Dec 2023 19:47:08 +0000 Subject: [PATCH] implemented all the functions apart from movement --- 2048.cpp | 132 ++++++++++++++++++++++++++++++++++++++++++++++-------- README.md | 7 ++- game.hpp | 20 +++++++-- main.cpp | 3 +- 4 files changed, 137 insertions(+), 25 deletions(-) diff --git a/2048.cpp b/2048.cpp index dfbdf74..dafdbe2 100644 --- a/2048.cpp +++ b/2048.cpp @@ -1,30 +1,126 @@ #include "game.hpp" - +#include <ctime> // so i can use time() +#include <cstdlib> // so i can use srand() #include <iostream> -#include <cstdlib> -#include <iomanip> +#include <iomanip> Board::Board() { - //create a 2D array using std::vector using int for boolen values in - board = std::vector<std::vector<int>>(4, std::vector<int>(4, 0)); - STARTER_TILE(); - STARTER_TILE(); + initialise(); +} + +void Board::initialise() { + srand(time(0)); // uses the time in the system clock to create a seed for random number + gameOver = false; + moved = false; + for (int i = 0; i < SIZE; ++i) { // loops through the colums and rows of the grid to create it + for (int j = 0; j < SIZE; ++j) { + board[i][j] = 0; + } + } + generateRandomTile(); + generateRandomTile(); +} + +void Board::run() { + while (!gameOver) { + printBoard(); + userInput(); + moved = false; + } + std::cout << "Game Over! Thanks for playing." << std::endl; } -void CREATE_BOARD(){ - std::cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << std::endl; //just adds boarder +void Board::generateRandomTile() { + int emptyCells = 0; + for (int i = 0; i < SIZE; ++i) { + for (int j = 0; j < SIZE; ++j) { + if (board[i][j] == 0) { // loops through the grid to find the empty cells + emptyCells++; // if the cell is empty increment emptycells variable by 1 + } + } + } + + if (emptyCells == 0) { // check that there is an empty square to generate random box. + gameOver = isGameOver(); // if no boxes can be created then end the game to keep inline with the games rules. + return; + } - for (size_t X = 0; X < board.size(); X++) // This loops through the row or the X axis using board.size to loop the correct amount. - { - for (size_t Y = 0; Y < board.size(); Y++) // This loops throughthe colums or the Y axis - { - int tile = board[X][Y]; - } + int randomIndex = std::rand() % emptyCells + 1; // this will choose a random place on the board to place a tile + emptyCells = 0; // reset the variable + for (int i = 0; i < SIZE; ++i) { + for (int j = 0; j < SIZE; ++j) { + if (board[i][j] == 0) { + emptyCells++; + if (emptyCells == randomIndex) { // when the empty cells variable reaches the same number as random index it will place a tile + board[i][j] = (std::rand() % 2 + 1) * 2; // this randomly selects a number either 2 or 4 to set the tile to. + return; + } + } + } + } +} + +void Board::printBoard() { + system("clear"); // removes the terminal history + for (int i = 0; i < SIZE; ++i) { + for (int j = 0; j < SIZE; ++j) { // loops through the grid + std::cout << std::setw(4) << board[i][j]; // prints the board with 4 character width between + } std::cout << std::endl; } - std::cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << std::endl; } -void STARTER_TILE(){ +void Board::userInput() { + char input; + std::cout << "Enter move (W/A/S/D): "; + std::cin >> input; + + switch (input) { + case 'W': + case 'w': + moveUp(); + break; + case 'A': + case 'a': + moveLeft(); + break; + case 'S': + case 's': + moveDown(); + break; + case 'D': + case 'd': + moveRight(); + break; + default: + std::cout << "Invalid input. Please use W/A/S/D." << std::endl; + } + + if (!moved) { + std::cout << "Invalid move. Try again." << std::endl; + userInput(); + } + + generateRandomTile(); +} + +void Board::moveLeft() { + // create +} + +void Board::moveRight() { + // create +} + +void Board::moveUp() { + // create +} -} \ No newline at end of file +void Board::moveDown() { + // create +} + +bool Board::isGameOver() { + // create + return true; +} diff --git a/README.md b/README.md index f22cddc..97d7a4d 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ * If 2 of the same numbers touch they will combine and add up to the value of both added together (e.g. if a 2 block touches another 2 block they will combine and become one 4 block). -* You can use the 2 common layouts of cursor keys (arrow keys or wasd). +* You can use w/a/s/d or W/A/S/D to control the game. * If there is no empty cells left with no valid moves then the game will end.. @@ -50,10 +50,13 @@ The final if statement will get a random number and divide it by 2 and the resul ### Print Board Function -First we use linux system commands to clear the previous grid +First we use linux system commands to clear the previous grid and then loop through the board using the setw code to make it format a bit nicer. +### User Input Function +I initilise a variable to hold the user input and then output to ask for movement key and this key is stored in the input variable. +I then use a switch statement to match what key was pressed and to run a function to move the board in the respected way that was inputted. # Images diff --git a/game.hpp b/game.hpp index 5b896a8..3cdc8dd 100644 --- a/game.hpp +++ b/game.hpp @@ -4,10 +4,24 @@ class Board { private: -std::vector<std::vector<int>> board; +static const int SIZE = 4; +int board[SIZE][SIZE]; +bool gameOver; +bool moved; + +void initialise(); +void generateRandomTile(); +void printBoard(); +void userInput(); +void moveLeft(); +void moveRight(); +void moveUp(); +void moveDown(); +bool isGameOver(); public: Board(); -void CREATE_BOARD(); +void run(); -}; \ No newline at end of file +}; +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index e84f1bc..9be5f72 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,5 @@ int main(){ Board game; - - return false; + return 0; } \ No newline at end of file -- GitLab