spider

mail@pastecode.io avatar
unknown
c_cpp
15 days ago
8.1 kB
2
Indexable
Never
#include <iostream>  
#include <string>  
#include "raylib.h"  

const int BOARD_WIDTH = 800;  
const int BOARD_HEIGHT = 800;  
const int CELL_SIZE = 100;  
const int CELL_CENTER_POS = CELL_SIZE / 2;  
const int QORKI_SIZE = 30;  

enum cellType {  
    emptyCell,  
    player1Qorki,  
    player2Qorki,  
    player1KingQorki,  
    player2KingQorki  
};  

struct Cell {  
    int row;  
    int col;  
    cellType cellType;  
    int cellSize = CELL_SIZE;  
};  

struct Qorki {  
    Color qorkiColor;  
    int qorkiSize = QORKI_SIZE;  
};  

struct Board {  
    int boardWidth;  
    int boardHeight;  
    Color boardColor;  
};  

struct Game {  
    Board board;  
    Cell cellInfo[8][8];  
    std::string playerOneName;  
    std::string playerTwoName;  
    bool isPlayerOneTurn;  
};  

// Function Prototypes  
void initGame(Game& game);  
void initBoard(Board& board);  
void drawBoard(Board board);  
int initCellType(int row, int col);  
void drawCellsOnBoard(Cell cell[8][8]);  
Color getCellColor(Cell cell);  
void drawQorki(Cell cell[8][8]);  
Color getQorkiColor(Cell cell);  
void updateGame(Game& game);  
Cell getCell(int x, int y, Cell cells[8][8]);  
void moveQorki(Cell selectedCell, Cell targetCell, Cell cells[8][8]);  
bool isMoveValid(Cell selectedCell, Cell targetCell, Cell cells[8][8]);  
void removeQorki(Cell cell, Cell cells[8][8]);  
void promoteQorki(Cell targetCell, Cell cells[8][8]);  

int main() {  
    Game game;  
    initGame(game);  
    initBoard(game.board);  
    InitWindow(game.board.boardWidth, game.board.boardHeight, "DAMA");  
    SetTargetFPS(60); // Set the frame rate  

    while (!WindowShouldClose()) {  
        updateGame(game);  
        BeginDrawing();  
            ClearBackground(RAYWHITE);  
            drawBoard(game.board);  
            drawCellsOnBoard(game.cellInfo);  
            drawQorki(game.cellInfo);  
        EndDrawing();  
    }  

    CloseWindow();  
    return 0;  
}  

void initGame(Game& game) {  
    game.playerOneName = "Player 1";  
    game.playerTwoName = "Player 2";  
    game.isPlayerOneTurn = true;  

    for (int row = 0; row < 8; row++) {  
        for (int col = 0; col < 8; col++) {  
            game.cellInfo[row][col].row = row;  
            game.cellInfo[row][col].col = col;  
            game.cellInfo[row][col].cellType = initCellType(row, col);  
        }  
    }  
}  

void initBoard(Board& board) {  
    board.boardHeight = BOARD_HEIGHT;  
    board.boardWidth = BOARD_WIDTH;  
    board.boardColor = RAYWHITE;  
}  

void drawBoard(Board board) {  
    DrawRectangle(0, 0, board.boardWidth, board.boardHeight, board.boardColor);  
}  

int initCellType(int row, int col) {  
    bool isEvenRow = row % 2 == 0;  
    bool isEvenCol = col % 2 == 0;  

    if ((isEvenRow && !isEvenCol) || (!isEvenRow && isEvenCol)) {  
        if (row < 3) {  
            return player1Qorki;  
        }  
        if (row > 4) {  
            return player2Qorki;  
        }  
    }  
    return emptyCell;  
}  

void drawCellsOnBoard(Cell cells[8][8]) {  
    for (int row = 0; row < 8; row++)  
        for (int col = 0; col < 8; col++) {  
            Cell currentCell = cells[row][col];  
            Color cellColor = getCellColor(currentCell);  
            DrawRectangle(col * CELL_SIZE, row * CELL_SIZE, currentCell.cellSize, currentCell.cellSize, cellColor);  
        }  
}  

Color getCellColor(Cell cell) {  
    bool isEvenRow = cell.row % 2 == 0;  
    bool isEvenCol = cell.col % 2 == 0;  
    if ((isEvenRow && isEvenCol) || (!isEvenRow && !isEvenCol)) {  
        return LIGHTGRAY; // Light square color  
    } else {  
        return DARKGRAY; // Dark square color  
    }  
}  

void drawQorki(Cell cells[8][8]) {  
    for (int row = 0; row < 8; row++) {  
        for (int col = 0; col < 8; col++) {  
            Cell currentCell = cells[row][col];  
            if (currentCell.cellType != emptyCell) {  
                Color qorkiColor = getQorkiColor(currentCell);  
                DrawCircle(col * CELL_SIZE + CELL_CENTER_POS, row * CELL_SIZE + CELL_CENTER_POS, QORKI_SIZE, qorkiColor);  
            }  
        }  
    }  
}  

Color getQorkiColor(Cell cell) {  
    switch (cell.cellType) {  
        case player1Qorki:  
            return BLUE;  
        case player2Qorki:  
            return RED;  
        case player1KingQorki:  
            return DARKBLUE;  
        case player2KingQorki:  
            return DARKRED;  
        default:  
            return RAYWHITE;  
    }  
}  

void updateGame(Game& game) {  
    static Cell selectedCell = { -1, -1, emptyCell };  
    if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {  
        Vector2 mousePos = GetMousePosition();  
        Cell clickedCell = getCell(mousePos.x, mousePos.y, game.cellInfo);  

        if (clickedCell.cellType != emptyCell) {  
            if (selectedCell.cellType == emptyCell &&   
                ((game.isPlayerOneTurn && clickedCell.cellType == player1Qorki) ||   
                 (!game.isPlayerOneTurn && clickedCell.cellType == player2Qorki))) {  
                selectedCell = clickedCell;  
            } else if (selectedCell.cellType != emptyCell) {  
                moveQorki(selectedCell, clickedCell, game.cellInfo);  
                promoteQorki(clickedCell, game.cellInfo);  
                selectedCell.cellType = emptyCell;  
                game.isPlayerOneTurn = !game.isPlayerOneTurn;  
            }  
        }  
    }  
}  

Cell getCell(int x, int y, Cell cells[8][8]) {  
    int row = y / CELL_SIZE;  
    int col = x / CELL_SIZE;  
    if (row >= 0 && row < 8 && col >= 0 && col < 8) {  
        return cells[row][col];  
    }  
    return { -1, -1, emptyCell }; // return an invalid cell if out of bounds  
}  

void moveQorki(Cell selectedCell, Cell targetCell, Cell cells[8][8]) {  
    if (isMoveValid(selectedCell, targetCell, cells)) {  
        cells[targetCell.row][targetCell.col].cellType = cells[selectedCell.row][selectedCell.col].cellType;  
        cells[selectedCell.row][selectedCell.col].cellType = emptyCell;  
        // Capture opponent Qorki if applicable  
        if (abs(selectedCell.row - targetCell.row) == 2) {  
            int capturedRow = (selectedCell.row + targetCell.row) / 2;  
            int capturedCol = (selectedCell.col + targetCell.col) / 2;  
            removeQorki(cells[capturedRow][capturedCol], cells);  
        }  
    }  
}  

bool isMoveValid(Cell selectedCell, Cell targetCell, Cell cells[8][8]) {  
    if (targetCell.cellType != emptyCell) return false; // Target cell must be empty  

    int rowDiff = targetCell.row - selectedCell.row;  
    int colDiff = targetCell.col - selectedCell.col;  

    // Check if the move is diagonal  
    if (abs(rowDiff) == 1 && abs(colDiff) == 1) {  
        return true;  
    }  

    // Check for jump/capture move  
    if (abs(rowDiff) == 2 && abs(colDiff) == 2) {  
        int middleRow = (selectedCell.row + targetCell.row) / 2;  
        int middleCol = (selectedCell.col + targetCell.col) / 2;  
        Cell middleCell = cells[middleRow][middleCol];  
        if (middleCell.cellType != emptyCell && ((game.isPlayerOneTurn && middleCell.cellType == player2Qorki) ||  
            (!game.isPlayerOneTurn && middleCell.cellType == player1Qorki))) {  
            return true; // Valid jump move  
        }  
    }  
    return false;  
}  

void removeQorki(Cell cell, Cell cells[8][8]) {  
    cells[cell.row][cell.col].cellType = emptyCell;  
}  

void promoteQorki(Cell targetCell, Cell cells[8][8]) {  
    if (targetCell.cellType == player1Qorki && targetCell.row == 7) {  
        cells[targetCell.row][targetCell.col].cellType = player1KingQorki;  
    } else if (targetCell.cellType == player2Qorki && targetCell.row == 0) {  
        cells[targetCell.row][targetCell.col].cellType = player2KingQorki;  
    }  
}
Leave a Comment