Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @file game.c
- @author Bryan Lim & Sam Scott
- @date 10 October 2017
- @brief Paper Scissors Rock Game!
- */
- #include "system.h"
- #include "tinygl.h"
- #include "navswitch.h"
- #include "button.h"
- #include "ir_uart.h"
- #include "pacer.h"
- #include "../fonts/font5x7_1.h"
- #include "menu.h"
- #define PACER_RATE 500
- void show_hand (char character){
- char hand[1];
- hand[0] = character;
- tinygl_text (hand);
- }
- static uint8_t compare_hands(char my_hand, char rec_hand){
- /* Basic comparison function where if my_hand is a winning hand
- * the function returns 1. If it's a draw, it returns 2 and if
- * loss, the function retruns 0.
- * @param two chars that are to be compared
- * @return boolean value */
- if (my_hand == 'P' && rec_hand == 'S'){
- return 1;
- } else if (my_hand == 'S' && rec_hand == 'P'){
- return 1;
- } else if(my_hand == 'R' && rec_hand == 'S'){
- return 1;
- } else if(my_hand == rec_hand) {
- return 2;
- }
- return 0;
- }
- int main (void){
- /* Main function to play the game
- * @param void
- */
- //initialising all systems
- system_init ();
- navswitch_init ();
- ir_uart_init ();
- //defining variables in advance
- char PSR[3] = {'P','S','R'};
- char my_hand = PSR[0];
- char rec_hand;
- uint8_t score = 0;
- int i = 0;
- int sent_value = 0;
- int rec_value = 0;
- pacer_init (PACER_RATE);
- start_display();
- int game = 1;
- //main game loop
- while (1){
- while(game <= 3){
- pacer_wait ();
- tinygl_update ();
- navswitch_update ();
- //controls, scrolls between P,S,R
- if (navswitch_push_event_p (NAVSWITCH_EAST)){
- i += 1;
- if(i > 2){
- i = 0;
- }
- } else if (navswitch_push_event_p (NAVSWITCH_WEST)){
- i-=1;
- if(i < 0){
- i = 2;
- }
- }
- //selecting the hand
- my_hand = PSR[i];
- //need to add wait so that the sender will wait to receive before sending
- //as long as IR is ready this will run
- if (ir_uart_read_ready_p ()){
- rec_hand = ir_uart_getc ();
- rec_value = 1;
- }
- //once the navswtich has been pressed it will send
- if (navswitch_push_event_p (NAVSWITCH_PUSH)){
- ir_uart_putc(my_hand);
- sent_value = 1;
- }
- if(sent_value == 1 && rec_value == 1){
- uint8_t result = compare_hands(my_hand, rec_hand);
- if(result < 2){
- score += result;
- game += 1;
- }
- result_screen(result);
- sent_value = 0;
- rec_value = 0;
- my_hand = 'P';
- }
- show_hand(my_hand);
- tinygl_update ();
- }
- end_screen(score);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement