Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. /** @file game.c
  2. @author Bryan Lim & Sam Scott
  3. @date 10 October 2017
  4. @brief Paper Scissors Rock Game!
  5. */
  6.  
  7. #include "system.h"
  8. #include "tinygl.h"
  9. #include "navswitch.h"
  10. #include "button.h"
  11. #include "ir_uart.h"
  12. #include "pacer.h"
  13. #include "../fonts/font5x7_1.h"
  14. #include "menu.h"
  15.  
  16. #define PACER_RATE 500
  17.  
  18. void show_hand (char character){
  19. char hand[1];
  20. hand[0] = character;
  21. tinygl_text (hand);
  22. }
  23.  
  24.  
  25.  
  26. static uint8_t compare_hands(char my_hand, char rec_hand){
  27. /* Basic comparison function where if my_hand is a winning hand
  28. * the function returns 1. If it's a draw, it returns 2 and if
  29. * loss, the function retruns 0.
  30. * @param two chars that are to be compared
  31. * @return boolean value */
  32.  
  33. if (my_hand == 'P' && rec_hand == 'S'){
  34. return 1;
  35. } else if (my_hand == 'S' && rec_hand == 'P'){
  36. return 1;
  37. } else if(my_hand == 'R' && rec_hand == 'S'){
  38. return 1;
  39. } else if(my_hand == rec_hand) {
  40. return 2;
  41. }
  42. return 0;
  43. }
  44.  
  45.  
  46.  
  47. int main (void){
  48. /* Main function to play the game
  49. * @param void
  50. */
  51.  
  52. //initialising all systems
  53. system_init ();
  54. navswitch_init ();
  55. ir_uart_init ();
  56. //defining variables in advance
  57. char PSR[3] = {'P','S','R'};
  58. char my_hand = PSR[0];
  59. char rec_hand;
  60. uint8_t score = 0;
  61. int i = 0;
  62. int sent_value = 0;
  63. int rec_value = 0;
  64. pacer_init (PACER_RATE);
  65. start_display();
  66. int game = 1;
  67. //main game loop
  68. while (1){
  69. while(game <= 3){
  70. pacer_wait ();
  71. tinygl_update ();
  72. navswitch_update ();
  73. //controls, scrolls between P,S,R
  74. if (navswitch_push_event_p (NAVSWITCH_EAST)){
  75. i += 1;
  76. if(i > 2){
  77. i = 0;
  78. }
  79. } else if (navswitch_push_event_p (NAVSWITCH_WEST)){
  80. i-=1;
  81. if(i < 0){
  82. i = 2;
  83. }
  84. }
  85. //selecting the hand
  86. my_hand = PSR[i];
  87. //need to add wait so that the sender will wait to receive before sending
  88. //as long as IR is ready this will run
  89. if (ir_uart_read_ready_p ()){
  90. rec_hand = ir_uart_getc ();
  91. rec_value = 1;
  92. }
  93. //once the navswtich has been pressed it will send
  94. if (navswitch_push_event_p (NAVSWITCH_PUSH)){
  95. ir_uart_putc(my_hand);
  96. sent_value = 1;
  97. }
  98. if(sent_value == 1 && rec_value == 1){
  99. uint8_t result = compare_hands(my_hand, rec_hand);
  100. if(result < 2){
  101. score += result;
  102. game += 1;
  103. }
  104. result_screen(result);
  105. sent_value = 0;
  106. rec_value = 0;
  107. my_hand = 'P';
  108. }
  109. show_hand(my_hand);
  110. tinygl_update ();
  111. }
  112. end_screen(score);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement