Checkers

Hikma
mail@pastecode.io avatar
unknown
c_cpp
8 days ago
5.8 kB
17
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;  
};  

// 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(Cell cells[8][8]);  
Cell getCell(int x, int y);  
void moveQorki(Cell selectedCell, Cell targetCell, Cell cells[8][8]);  
void handleQorkiMove(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]);  

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()) {  
        BeginDrawing();  
            ClearBackground(RAYWHITE);  
            drawBoard(game.board);  
            drawCellsOnBoard(game.cellInfo);  
            drawQorki(game.cellInfo);  
            updateGame(game.cellInfo);  
        EndDrawing();  
    }  

    CloseWindow();  
    return 0;  
}  

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

    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 cell[8][8]) {  
    for (int row = 0; row < 8; row++)  
        for (int col = 0; col < 8; col++) {  
            Cell currentCell = cell[row][col];  
            Color cellColor = getCellColor(currentCell);  
            DrawRectangle(col * currentCell.cellSize, row * currentCell.cellSize, 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 cell[8][8]) {  
    for (int row = 0; row < 8; row++) {  
        for (int col = 0; col < 8; col++) {  
            Cell currentCell = cell[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(Cell cells[8][8]) {  
    // Logic to update the game state, handle input, etc.  
    // This would typically include detecting user input for selecting/moving Qorkis  
}  

Cell getCell(int x, int y) {  
    int row = y / CELL_SIZE;  
    int col = x / CELL_SIZE;  
    if (row >= 0 && row < 8 && col >= 0 && col < 8) {  
        return { row, col, cells[row][col].cellType };  
    }  
    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;  
        // Add logic for king promotion if needed  
    }  
}  

void handleQorkiMove(Cell selectedCell, Cell targetCell, Cell cells[8][8]) {  
    moveQorki(selectedCell, targetCell, cells);  
}  

bool isMoveValid(Cell selectedCell, Cell targetCell, Cell cells[8][8]) {  
    // Implement validation logic for moves  
    return true; // Placeholder: adjust logic to perform actual validation  
}  

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