Upload Lab 6 Files
++ Lab 6 Files Signed-off-by: cdez <cdez@cdezselfhosted.pw>
This commit is contained in:
parent
136c80d1ac
commit
7b34fcad4d
4 changed files with 1121 additions and 0 deletions
186
Lab06/Lab_6_provided.c
Normal file
186
Lab06/Lab_6_provided.c
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
// header files
|
||||
#include "Console_IO_Utility.h"
|
||||
|
||||
// global constants
|
||||
// none
|
||||
|
||||
// function prototypes
|
||||
int generateRandChoice();
|
||||
char playGame();
|
||||
char whoWins();
|
||||
|
||||
// main program
|
||||
int main()
|
||||
{
|
||||
// show title
|
||||
// function: printString, printEndline(s)
|
||||
printEndline();
|
||||
printString( "Rock / Paper / Scissors" );
|
||||
printEndline();
|
||||
|
||||
|
||||
// initialize variables/functions
|
||||
int userInput;
|
||||
char winner;
|
||||
|
||||
|
||||
// get user input
|
||||
// Display options of user input
|
||||
// functions: printString, printEndline
|
||||
printString("Options to choose:");
|
||||
printEndline();
|
||||
printString("0 - Scissors");
|
||||
printEndline();
|
||||
printString("1 - Paper");
|
||||
printEndline();
|
||||
printString("2 - Rock");
|
||||
printEndline();
|
||||
|
||||
// get users value for rock/paper/scissors
|
||||
// function: promptForInteger, printEndline
|
||||
userInput = promptForInteger("Enter your value: ");
|
||||
printEndline();
|
||||
|
||||
// check for valid user input, if valid,
|
||||
if (userInput > -1 && userInput < 3)
|
||||
{
|
||||
// use playGame
|
||||
winner = playGame(userInput);
|
||||
// display winner.
|
||||
// functions: printString, printCharacter, printEndline
|
||||
printString("The winner is: ");
|
||||
printCharacter(winner);
|
||||
printEndline();
|
||||
}
|
||||
else
|
||||
{
|
||||
// else, return zero and end the program,
|
||||
// print "Sorry no such option! Try again"
|
||||
printString("Sorry no such option! Try again");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// supporting functions
|
||||
|
||||
/*
|
||||
Name: playGame
|
||||
Process: plays rock paper scissors game, generates random choice for AI player
|
||||
and returns who won the game
|
||||
Function input/parameters: previously chosen players input
|
||||
rock / paper / scissors represented by 2 / 1 / 0
|
||||
Function output/parameters: none
|
||||
Function output/returned: character which represents who won the game
|
||||
Function input/keyboard: none
|
||||
Device output/file: none
|
||||
Dependencies: generateRandChoice, whoWins
|
||||
*/
|
||||
|
||||
char playGame(int userInputValue)
|
||||
{
|
||||
// initialize variables
|
||||
int computerChoice;
|
||||
|
||||
// calculate computerChoice
|
||||
computerChoice = generateRandChoice();
|
||||
|
||||
// return who whoWins
|
||||
return whoWins(userInputValue, computerChoice);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Name: whoWins
|
||||
Process: based on user input and randomly generated value decides who wins
|
||||
via series of decision making statements
|
||||
Function input/parameters: previously chosen players input
|
||||
rock / paper / scissors represented by 2 / 1 / 0
|
||||
and randomly generated number of the same range
|
||||
Function output/parameters: none
|
||||
Function output/returned: character which represents who won the game
|
||||
'U' - user; 'R' - random (AI); 'F' - friendship.
|
||||
Function input/keyboard: none
|
||||
Device output/file: none
|
||||
Dependencies: none
|
||||
*/
|
||||
|
||||
char whoWins(int playerInputValue, int computerChoice)
|
||||
{
|
||||
// define variables
|
||||
char winnerCharacter;
|
||||
// get function input
|
||||
// given by values: playerInputValue, computerChoice
|
||||
|
||||
// do calculations/comparisons
|
||||
if (playerInputValue == computerChoice)
|
||||
{
|
||||
// check to see if the player and computer picked the same choice
|
||||
winnerCharacter = 'F';
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise, check which values the player chose versus the computer.
|
||||
if(playerInputValue == 0 && computerChoice == 1)
|
||||
{
|
||||
// player loses, computer wins
|
||||
winnerCharacter = 'R';
|
||||
}
|
||||
else if (playerInputValue == 1 && computerChoice == 0)
|
||||
{
|
||||
// player wins, computer loses
|
||||
winnerCharacter = 'U';
|
||||
}
|
||||
else if (playerInputValue == 2 && computerChoice == 0)
|
||||
{
|
||||
// player wins, computer loses
|
||||
winnerCharacter = 'U';
|
||||
}
|
||||
else if (playerInputValue == 0 && computerChoice == 2)
|
||||
{
|
||||
// player loses, computer wins
|
||||
winnerCharacter = 'R';
|
||||
}
|
||||
else if (playerInputValue == 1 && computerChoice == 2)
|
||||
{
|
||||
// player wins, computer loses
|
||||
winnerCharacter = 'U';
|
||||
}
|
||||
else
|
||||
{
|
||||
// player loses, computer wins. (playerInputValue == 2, computerChoice == 1)
|
||||
winnerCharacter = 'R';
|
||||
}
|
||||
}
|
||||
|
||||
// return output
|
||||
return winnerCharacter;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: generateRandChoice
|
||||
Process: based on user input and randomly generated value decides who wins
|
||||
via series of decision making statments
|
||||
Function input/parameters: previously chosen players input
|
||||
rock / paper / scissors represented by 2 / 1 / 0
|
||||
and randomly generated number of the same range
|
||||
Function output/parameters: none
|
||||
Function output/returned: character which represents who won the game
|
||||
'U' - user; 'R' - random (AI); 'F' - friendship.
|
||||
Function input/keyboard: none
|
||||
Device output/file: none
|
||||
Dependencies: none
|
||||
*/
|
||||
int generateRandChoice()
|
||||
{
|
||||
// initialize variables
|
||||
int randNum;
|
||||
|
||||
// computations
|
||||
// initialize random seed
|
||||
srand(time(0));
|
||||
// generate random number between 0 and 2 (inclusive)
|
||||
randNum = rand() % 2 + rand() % 2;
|
||||
|
||||
// return result
|
||||
return randNum;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue