Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.7 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_WHITE (5)  
#define TOTAL_TIME_BLACK (5)  

volatile unsigned int secondsWhite = TOTAL_TIME_WHITE;
volatile unsigned int secondsBlack = TOTAL_TIME_BLACK;

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

    while(true) {
        switch(gamePhase) {
            
            if (BUTTON_IsPressed ( BUTTON_S6 ) == true) {
                gamePhase = 3; // Reset gry przytrzymaniem przycisku S6
                remainingSecondsWhite = secondsWhite;
                remainingSecondsBlack = secondsBlack;
            }

            case 0:  // Koniec czasu
                LCD_ClearScreen();
                if(remainingSecondsWhite < 1) {
                    LCD_PutString("Biale przegrywaja", strlen("Biale przegrywaja")); 
                } else {
                    LCD_PutString("Czarne przegrywaja", strlen("Czarne przegrywaja")); 
                }
                __delay32(4000000); // Czekamy 10 sekund
                gamePhase = 3;  // Reset do ekranu startowego

            case 1:  // Tura białych
                if (BUTTON_IsPressed(BUTTON_S4) == true) { // Zamiana przycisków w grze
                    remainingSecondsWhite += 2;
                    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));
                __delay32(4000000); 
                remainingSecondsWhite--;
                if(remainingSecondsWhite < 1) {
                    gamePhase = 0;
                }
                break;

            case 2:  // Tura czarnych
                if (BUTTON_IsPressed ( BUTTON_S3 ) == true) { // Zamiana przycisków w grze
                    remainingSecondsBlack = remainingSecondsBlack + 2;
                    gamePhase = 1;
                    break;
                }
                sprintf(buffer, "%02u:%02u czarne", remainingSecondsBlack / 60, remainingSecondsBlack % 60);
                LCD_ClearScreen();
                LCD_PutString(buffer, strlen(buffer));
                LCD_PutString("\n", 1); 
                sprintf(buffer, "%02u:%02u biale", remainingSecondsWhite / 60, remainingSecondsWhite % 60);
                LCD_PutString(buffer, strlen(buffer));
                __delay32(4000000); 
                remainingSecondsBlack--;
                if(remainingSecondsBlack < 1) {
                    gamePhase = 0;
                }
                break;


            case 3: // Kto zaczyna?
                LCD_PutString("Kto zaczyna?\n s3=czarne s4=biale",32);
                __delay32(4000000);
                LCD_ClearScreen();
                if (BUTTON_IsPressed ( BUTTON_S3 ) == true) {
                    gamePhase = 2; // Czarne zaczynają po naciśnięciu S3
                    break;
                }
                if (BUTTON_IsPressed ( BUTTON_S4 ) == true) {
                    gamePhase = 1; // Białe zaczynają po naciśnięciu S4
                    break;
                }

            default:
                break;
        }
    }
    return 0;
}
Leave a Comment