diff --git a/GameLogic.cpp b/GameLogic.cpp deleted file mode 100644 index 9404d8400482bc516a3bb75e32549647a5bd4a0f..0000000000000000000000000000000000000000 --- a/GameLogic.cpp +++ /dev/null @@ -1,277 +0,0 @@ -#include "GameLogic.h" -#include <cstdlib> -#include <ctime> -#include <vector> -#include <utility> -#include <ncurses.h> -#include <stdbool.h> -#include <unistd.h> - -#define BOARD_SIZE 4 -#define EMPTY_CELL 0 - -// Global variables for logo position -const int logoStartX = 30; // Adjust this value to position the logo horizontally -const int logoStartY = 2; // Adjust this value to position the logo vertically -pid_t music_pid = -1; -bool isMusicPlaying = false; - -int board[BOARD_SIZE][BOARD_SIZE]; -int score = 0; - -void initializeGame() { - std::srand(std::time(0)); // Initialize random seed once at game start - for (int i = 0; i < BOARD_SIZE; i++) { - for (int j = 0; j < BOARD_SIZE; j++) { - board[i][j] = EMPTY_CELL; - } - } - addRandomTile(); - addRandomTile(); -} - -void addRandomTile() { - std::vector<std::pair<int, int>> emptyCells; - for (int i = 0; i < BOARD_SIZE; i++) { - for (int j = 0; j < BOARD_SIZE; j++) { - if (board[i][j] == EMPTY_CELL) { - emptyCells.push_back({i, j}); - } - } - } - if (!emptyCells.empty()) { - int randomIndex = std::rand() % emptyCells.size(); - int row = emptyCells[randomIndex].first; - int col = emptyCells[randomIndex].second; - board[row][col] = (std::rand() % 2 + 1) * 2; // Place a 2 or 4 - } -} - -void playMusic() { - if (isMusicPlaying) { - return; // Music is already playing - } - music_pid = fork(); - if (music_pid == 0) { - // Child process - execlp("mpg123", "mpg123", "-q", "Pokémon D_P_P - Eterna Forest (Piano Cover).mp3", NULL); - exit(0); // Exit child process after playing music - } else if (music_pid > 0) { - isMusicPlaying = true; - } -} - -void pauseMusic() { - if (music_pid != -1 && isMusicPlaying) { - kill(music_pid, SIGSTOP); // Pause music - isMusicPlaying = false; - } -} - -void stopMusic() { - if (music_pid != -1) { - kill(music_pid, SIGTERM); // Stop music - waitpid(music_pid, NULL, 0); // Clean up process - music_pid = -1; - isMusicPlaying = false; - } -} - -void drawHamzaLogo(int startX, int startY) { - // Define HAMZA ASCII art - const int LOGO_HEIGHT = 6; - const char* hamzaLogo[LOGO_HEIGHT] = { - " _ _ _ __ __ _____ _ _ ___ ____ _ _ _ ", - " | | | | / \\ | \\/ |__ / / \\ | | |_ _| __ ) / \\ | | | |", - " | |_| | / _ \\ | |\\/| | / / / _ \\ | | | || _ \\ / _ \\ | |_| |", - " | _ |/ ___ \\| | | |/ /_ / ___ \\ | |___ | || |_) / ___ \\| _ |", - " |_| |_/_/ \\_\\_| |_/____/_/ \\_\\ |_____|___|____/_/ \\_\\_| |_|", - " " - }; - - // Assign a special color for the logo - attron(COLOR_PAIR(12)); // Assuming 12 is a unique color pair - - // Display the logo - for (int i = 0; i < LOGO_HEIGHT; i++) { - mvprintw(startY + i, startX, "%s", hamzaLogo[i]); - } - - attroff(COLOR_PAIR(12)); -} - -void drawBoard() { - clear(); - for (int i = 0; i < BOARD_SIZE; i++) { - for (int j = 0; j < BOARD_SIZE; j++) { - // Draw grid lines - mvprintw(i * 2, j * 6, "+-----"); - mvprintw(i * 2 + 1, j * 6, "| "); - - if (board[i][j] != EMPTY_CELL) { - // Assign colors based on the value of the tile - if (board[i][j] == 2) { - attron(COLOR_PAIR(1)); - } else if (board[i][j] == 4) { - attron(COLOR_PAIR(2)); - } else if (board[i][j] == 8) { - attron(COLOR_PAIR(3)); - } else if (board[i][j] == 16) { - attron(COLOR_PAIR(4)); - } else if (board[i][j] == 32) { - attron(COLOR_PAIR(5)); - } else if (board[i][j] == 64) { - attron(COLOR_PAIR(6)); - } else if (board[i][j] == 128) { - attron(COLOR_PAIR(7)); - } else if (board[i][j] == 256) { - attron(COLOR_PAIR(8)); - } else if (board[i][j] == 512) { - attron(COLOR_PAIR(9)); - } else if (board[i][j] == 1024) { - attron(COLOR_PAIR(10)); - } else if (board[i][j] == 2048) { - attron(COLOR_PAIR(11)); - } - - mvprintw(i * 2 + 1, j * 6 + 2, "%4d", board[i][j]); - - // Turn off the color after printing - attroff(COLOR_PAIR(1)); - attroff(COLOR_PAIR(2)); - attroff(COLOR_PAIR(3)); - attroff(COLOR_PAIR(4)); - attroff(COLOR_PAIR(5)); - attroff(COLOR_PAIR(6)); - attroff(COLOR_PAIR(7)); - attroff(COLOR_PAIR(8)); - attroff(COLOR_PAIR(9)); - attroff(COLOR_PAIR(10)); - attroff(COLOR_PAIR(11)); - } - } - } - // Draw the bottom border of the grid - for (int j = 0; j < BOARD_SIZE; j++) { - mvprintw(BOARD_SIZE * 2, j * 6, "+-----"); - } - - mvprintw(BOARD_SIZE * 2 + 1, 0, "Score: %d", score); - mvprintw(BOARD_SIZE * 2 + 2, 0, "Use WASD to move, Q to quit"); - mvprintw(BOARD_SIZE * 2 + 3, 0, "T: Play/Pause | Y: Stop Music "); - drawHamzaLogo(logoStartX, logoStartY); // Draw the logo below the board - refresh(); -} - -bool makeMove(char direction) { - bool moved = false; - - if (direction == 'w') { - // Move up logic - for (int col = 0; col < BOARD_SIZE; col++) { - for (int row = 1; row < BOARD_SIZE; row++) { - if (board[row][col] != EMPTY_CELL) { - int targetRow = row; - while (targetRow > 0 && board[targetRow - 1][col] == EMPTY_CELL) { - board[targetRow - 1][col] = board[targetRow][col]; - board[targetRow][col] = EMPTY_CELL; - targetRow--; - moved = true; - } - if (targetRow > 0 && board[targetRow - 1][col] == board[targetRow][col]) { - board[targetRow - 1][col] *= 2; // Merge tiles - board[targetRow][col] = EMPTY_CELL; - score += board[targetRow - 1][col]; - moved = true; - } - } - } - } - } else if (direction == 's') { - // Move down logic - for (int col = 0; col < BOARD_SIZE; col++) { - for (int row = BOARD_SIZE - 2; row >= 0; row--) { - if (board[row][col] != EMPTY_CELL) { - int targetRow = row; - while (targetRow < BOARD_SIZE - 1 && board[targetRow + 1][col] == EMPTY_CELL) { - board[targetRow + 1][col] = board[targetRow][col]; - board[targetRow][col] = EMPTY_CELL; - targetRow++; - moved = true; - } - if (targetRow < BOARD_SIZE - 1 && board[targetRow + 1][col] == board[targetRow][col]) { - board[targetRow + 1][col] *= 2; // Merge tiles - board[targetRow][col] = EMPTY_CELL; - score += board[targetRow + 1][col]; - moved = true; - } - } - } - } - } else if (direction == 'a') { - // Move left logic - for (int row = 0; row < BOARD_SIZE; row++) { - for (int col = 1; col < BOARD_SIZE; col++) { - if (board[row][col] != EMPTY_CELL) { - int targetCol = col; - while (targetCol > 0 && board[row][targetCol - 1] == EMPTY_CELL) { - board[row][targetCol - 1] = board[row][targetCol]; - board[row][targetCol] = EMPTY_CELL; - targetCol--; - moved = true; - } - if (targetCol > 0 && board[row][targetCol - 1] == board[row][targetCol]) { - board[row][targetCol - 1] *= 2; // Merge tiles - board[row][targetCol] = EMPTY_CELL; - score += board[row][targetCol - 1]; - moved = true; - } - } - } - } - } else if (direction == 'd') { - // Move right logic - for (int row = 0; row < BOARD_SIZE; row++) { - for (int col = BOARD_SIZE - 2; col >= 0; col--) { - if (board[row][col] != EMPTY_CELL) { - int targetCol = col; - while (targetCol < BOARD_SIZE - 1 && board[row][targetCol + 1] == EMPTY_CELL) { - board[row][targetCol + 1] = board[row][targetCol]; - board[row][targetCol] = EMPTY_CELL; - targetCol++; - moved = true; - } - if (targetCol < BOARD_SIZE - 1 && board[row][targetCol + 1] == board[row][targetCol]) { - board[row][targetCol + 1] *= 2; // Merge tiles - board[row][targetCol] = EMPTY_CELL; - score += board[row][targetCol + 1]; - moved = true; - } - } - } - } - } - - if (moved) { - addRandomTile(); - } - - return moved; -} - -bool isGameOver() { - for (int i = 0; i < BOARD_SIZE; i++) { - for (int j = 0; j < BOARD_SIZE; j++) { - if (board[i][j] == EMPTY_CELL) { - return false; - } - if (i > 0 && board[i][j] == board[i - 1][j]) { - return false; - } - if (j > 0 && board[i][j] == board[i][j - 1]) { - return false; - } - } - } - return true; -} \ No newline at end of file