Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.3 kB
2
Indexable
Never
#include <xc.h>
#include <libpic30.h>
#include <stdio.h>
#include <string.h>
#include "lcd.h"
#include "buttons.h"

#define TOTAL_TIME (1500) // Total initial time for each player in seconds

volatile unsigned int remainingSecondsWhite = TOTAL_TIME;
volatile unsigned int remainingSecondsBlack = TOTAL_TIME;
volatile int gamePhase = 3;  // Start in "Who Starts?" phase
//volatile char buffer[32];  // Moved to main for better scope control

void delay_ms(unsigned long milliseconds) {
    while (milliseconds--) {
        __delay32(4000); // Assuming 8 MHz FCY (8 MIPS)
    }
}

int main(void) {
    char buffer[32]; 
    LCD_Initialize();

    while (true) {
        if (BUTTON_IsPressed(BUTTON_S5)) { // Reset game 
            gamePhase = 3; 
            remainingSecondsWhite = TOTAL_TIME;
            remainingSecondsBlack = TOTAL_TIME;
        } else if (BUTTON_IsPressed(BUTTON_S6)) { // Checkmate or Draw (Handled outside switch)
            LCD_ClearScreen();
            LCD_PutString((BUTTON_IsPressed(BUTTON_S3)) ? "Mat" : "Remis", 5);
            delay_ms(2000);
            remainingSecondsWhite = TOTAL_TIME;
            remainingSecondsBlack = TOTAL_TIME;
            gamePhase = 3; 
            continue; 
        }

        switch (gamePhase) {
            case 0: // Time's up
                LCD_ClearScreen();
                LCD_PutString((remainingSecondsWhite < 1) ? "Biale przegrywaja" : "Czarne przegrywaja", 18);
                delay_ms(4000); 
                remainingSecondsWhite = TOTAL_TIME;
                remainingSecondsBlack = TOTAL_TIME;
                gamePhase = 3;  
                break;

            case 1: // White's turn
            case 2: // Black's turn
                // Combined display logic (no swapping lines)
                sprintf(buffer, "biale %02u:%02u\nczarne %02u:%02u", 
                        remainingSecondsWhite / 60, remainingSecondsWhite % 60,
                        remainingSecondsBlack / 60, remainingSecondsBlack % 60);
                LCD_ClearScreen();
                LCD_PutString(buffer, strlen(buffer));
                delay_ms(1000); 

                // Update time for the current player
                if (gamePhase == 1) {
                    remainingSecondsWhite--;
                    if (remainingSecondsWhite < 1) gamePhase = 0; 
                } else {
                    remainingSecondsBlack--;
                    if (remainingSecondsBlack < 1) gamePhase = 0; 
                }

                // Switch turns if respective button is pressed
                if (gamePhase == 1 && BUTTON_IsPressed(BUTTON_S3)) gamePhase = 2;
                if (gamePhase == 2 && BUTTON_IsPressed(BUTTON_S4)) gamePhase = 1;
                break;

            case 3: // Who Starts?
                LCD_PutString("Kto zaczyna?\ns3=biale s4=czarne", 32); 
                delay_ms(4000);  
                LCD_ClearScreen();
                if (BUTTON_IsPressed(BUTTON_S3)) {
                    gamePhase = 1;
                } else if (BUTTON_IsPressed(BUTTON_S4)) {
                    gamePhase = 2;
                }
                break;
        }
    }
    return 0;
}
Leave a Comment