Untitled

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

#define TOTAL_TIME (1500)  // Całkowity początkowy czas dla każdego gracza w sekundach

volatile unsigned int seconds = TOTAL_TIME;

void delay_ms(unsigned long milliseconds) {
    while (milliseconds--) {
        __delay32(4000); // Zakładając 8 MHz FCY (8 MIPS)
    }
}

int main(void) {
    char buffer[32];  
    LCD_Initialize(); 
    unsigned int remainingSecondsWhite = seconds;
    unsigned int remainingSecondsBlack = seconds;
    int gamePhase = 3;  

    while (true) {
        // Obsługa przycisków funkcyjnych S5 i S6
        if (BUTTON_IsPressed(BUTTON_S5)) {  // Reset gry 
            gamePhase = 3; 
            remainingSecondsWhite = seconds;
            remainingSecondsBlack = seconds;
        } else if (BUTTON_IsPressed(BUTTON_S6)) { // Mat lub remis
            LCD_ClearScreen();
            if (BUTTON_IsPressed(BUTTON_S3)) {  // Sprawdź, który przycisk jest wciśnięty razem z S6
                LCD_PutString("Mat", strlen("Mat"));
            } else {
                LCD_PutString("Remis", strlen("Remis"));
            }
            delay_ms(2000);
            remainingSecondsWhite = seconds;
            remainingSecondsBlack = seconds;
            gamePhase = 3; 
            continue; 
        }

        switch (gamePhase) {

            case 0:  // Koniec czasu
                LCD_ClearScreen();
                if (remainingSecondsWhite < 1) {
                    LCD_PutString("Biale przegrywaja", strlen("Biale przegrywaja")); 
                } else {
                    LCD_PutString("Czarne przegrywaja", strlen("Czarne przegrywaja")); 
                }
                __delay_ms(4000); 
                remainingSecondsWhite = seconds;
                remainingSecondsBlack = seconds;
                gamePhase = 3;  
                break;

            case 1:  // Tura białych
                if (BUTTON_IsPressed(BUTTON_S3)) { // Zmiana tury 
                    gamePhase = 2;
                    break;
                }
                sprintf(buffer, "%02u:%02u biale", remainingSecondsWhite / 60, remainingSecondsWhite % 60);
                LCD_ClearScreen();
                LCD_PutString(buffer, strlen(buffer));
                LCD_PutString("\n", 1); 
                sprintf(buffer, "%02u:%02u czarne", remainingSecondsBlack / 60, remainingSecondsBlack % 60);
                LCD_PutString(buffer, strlen(buffer));
                delay_ms(1000); 
                remainingSecondsWhite--;

                if (remainingSecondsWhite < 1) {
                    gamePhase = 0;
                }
                break;

            case 2:  // Tura czarnych
                if (BUTTON_IsPressed(BUTTON_S4)) { // Zmiana tury
                    gamePhase = 1;
                    break;
                }
                sprintf(buffer, "%02u:%02u biale", remainingSecondsWhite / 60, remainingSecondsWhite % 60);
                LCD_ClearScreen();
                LCD_PutString(buffer, strlen(buffer));
                LCD_PutString("\n", 1); 
                sprintf(buffer, "%02u:%02u czarne", remainingSecondsBlack / 60, remainingSecondsBlack % 60);
                LCD_PutString(buffer, strlen(buffer));
                delay_ms(1000); 
                remainingSecondsBlack--;
                if (remainingSecondsBlack < 1) {
                    gamePhase = 0;
                }
                break;


            case 3: // Kto zaczyna?
                LCD_PutString("Kto zaczyna?\n s3=b s4=cz",32);
                __delay_ms(4000);
                LCD_ClearScreen();
                if (BUTTON_IsPressed ( BUTTON_S3 ) == true) {
                    gamePhase = 1; 
                    break;
                }
                if (BUTTON_IsPressed ( BUTTON_S4 ) == true) {
                    gamePhase = 2;
                    break;
                }
                break;
        }
    }
    return 0;
}
Leave a Comment