Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rock Paper Scissors in C for #32 #97

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Rock_Paper_Scissors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

// 0 - Rock
// 1 - Paper
// 2 - Scissors
int whoWon(int move1, int move2) {
int winner = 0;
if(move1 != move2) { // Otherwise is a tie
if(move1>move2) { // 1 beats 0, 2 beats 1
if(move1-move2<2) {
winner = 1;
} else {
winner = 2;
}
} else {
if(move2-move1<2) {
winner = 2;
} else {
winner = 1;
}
}
}
return winner;
}

int main(void)
{
int play = 1;
srand(time(NULL)); // Initialization, should only be called once.
while(play == 1) {
printf("Hi! What's your move?\n");
int yourMove = -1;

printf("(R)ock\n");
printf("(P)aper\n");
printf("(S)cissors\n");

char move = getchar();
getchar(); // To skip \n

if(move == 'R') yourMove = 0;
if(move == 'P') yourMove = 1;
if(move == 'S') yourMove = 2;

if(yourMove != -1) {
int myMove = rand();

if(myMove%3 == 0) printf("I play Rock!\n");
if(myMove%3 == 1) printf("I play Paper!\n");
if(myMove%3 == 2) printf("I play Scissors!\n");

int winner = whoWon(yourMove, myMove);
if(winner == 0) printf("Its a tie!\n");
if(winner == 1) printf("You Win!\n");
if(winner == 2) printf("I Win!\n");

} else {
printf("Invalid move, please select a valid one.\n");
}

printf("Wanna play again?(y/n)");

char again = getchar();
getchar(); // To skip \n
printf("a%ca\n",again);

if(again == 'y') {
play = 1;
} else {
play = 0;
}
}
printf("Bye!\n");
return 0;
}