#include #include // for rand and srand #include // for time #include // memset and strcmp struct player { int choice; // rock, paper or scissors int wins; int losses; int ties; }; char *choices[] = { "rock", "paper", "scissors" }; void roshambo (struct player p[2]) { int win_matrix[3][3] = { /* rock paper scissors */ { -1, 1, 0 }, /* rock */ { 0, -1, 1 }, /* paper */ { 1, 0, -1 } /* scissors */ }; int winner,loser; winner = win_matrix[p[0].choice][p[1].choice]; if (winner == -1) { printf ("It's a tie! Human: %s Computer: %s\n", choices[p[0].choice], choices[p[1].choice]); p[0].ties++; p[1].ties++; return; } if (winner == 0) loser = 1; else loser = 0; printf ("Human: %s Computer: %s Winner: %s\n", choices[p[0].choice], choices[p[1].choice], winner == 0 ? "Human" : "Computer"); p[winner].wins++; p[loser].losses++; } int main () { struct player player[2]; char buffer[16]; // Input buffer int i; /* Set player values to all 0, the quick way */ memset (player, 0, sizeof(struct player) * 2); srand(time(NULL)); while (printf ("Enter choice: ") && scanf("%15s",buffer)) { for (i = 0; i < 3; ++i) { if (!strcmp (choices[i], buffer)) { player[0].choice = i; break; } } if (i == 3) { printf ("Invalid choice.\n"); continue; } player[1].choice = rand() % 3; roshambo (player); printf ("Score:\nHuman: %d/%d/%d Computer: %d/%d/%d\n", player[0].wins, player[0].losses, player[0].ties, player[1].wins, player[1].losses, player[1].ties); } return 0; }