Day2
This commit is contained in:
30
day2/rockpaperscissors.c
Normal file
30
day2/rockpaperscissors.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER_SIZE 5
|
||||
|
||||
#define score_play(p) (p - 'W')
|
||||
#define win(o, m) (o == m ? 3 : ((o - m == 1 || o - m == -2) ? 0 : 6))
|
||||
#define score_game(o, m) (score_play(m) + win((o - 'A'), (m - 'X')))
|
||||
|
||||
int main() {
|
||||
char buf[BUFFER_SIZE], *p, c, opponent, me;
|
||||
memset(buf, 0, BUFFER_SIZE);
|
||||
p = buf;
|
||||
unsigned score = 0;
|
||||
|
||||
while ((c = getchar()) != EOF) {
|
||||
*p++ = c;
|
||||
if (c == '\n') {
|
||||
sscanf(buf, "%c %c", &opponent, &me);
|
||||
score += score_game(opponent, me);
|
||||
printf("%u\n", score);
|
||||
|
||||
memset(buf, 0, BUFFER_SIZE);
|
||||
p = buf;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%u\n", score);
|
||||
}
|
||||
Reference in New Issue
Block a user