War
- Load in Card deck.mat, created a variable called ShuffledDeck which is a vector of 1-52 cards in random indices.
- player=ShuffledDeck[1:26];
- Computer=ShuffledDeck[27:52];
- Set DeckValues=[14 2: 13 14 2: 13 14 2: 13 14 2: 13];
- Use subplot and imshow functions to properly display cards, player card on top, Computer card on the bottom.
- Set variable won=0;
- Print out statement explaining rules and inputs
- While loop that iterates until won no longer equals 0;
- Every iteration of loop is a turn
- Each iteration, checks if player or computer is out of cards, if either is true won=1, loop breaks. Variables P and C are created and assigned to the DeckValues of the first card in the player and Computer arrays respectively. Then checks to see who has the higher value, if P>C player wins, player first card is returned to the array, Computer first card is taken away from Computer array and added to Player array. If C>P, the opposite occurs. If there is a tie, check the 5th index of both arrays, and repeat the value check with two new variables, Pt for player and Ct for the computer. If there is a double tie, this process will repeat
- The loop will iterate until someone has won, and the program will terminate.
Yahtzee
- load in Dice.mat
- creates a cell array score with two columns with the different score categories in column two and the first columns is the numbers 1-19
- Using a double for loop create and fill in three more columns in the cell array score
- create a variable turns set it equal to 48
- starts a while loop
- starts a for loop with three iteration
- Prints which players turn it is
- Rolls 5 dice with random values 1-6 and stores them in roll
- Shows the dice using imshow
- Calls the function rerolling and stores it in roll
- rerolling uses while and for loops to ask the player for which dice to
- reroll and then returns the new values of the dice.
- Asks the player for an input and stores it in categ
- Sorts roll numerically using sort
- Starts a while loop that ends if the array score is filled for that category and player or if the categ is set to 19
- Runs the function ScoreCalc and stores the outputted score in the array score
- ScoreCalc uses ifelse statements to check what categ is and using this returns a point value based on the dice values
- If ScoreCalc fails the player is asked for a different category
- Ends for loop
- Checks if the bonus Yahtzee has been used and if it has adds 100 points to the Yahtzee category
- Runs a for loop to add up the upper scores and store them in sub
Creates variable bonus and checks if sub is greater than 63 if so it sets bonus to 35 - Creates variable lowertotal and using a for loop adds up categories 10-17 storing it in lowertotal
- Adds up sub and bonus into a new variable uppertotal
- Adds up uppertotal and lowertotal into a new variable grandtotal
- Adds the variables to the proper categories in the array score
- Displays the array score
- Subtracts one from the turns variable
- Once turns equals 0 using ifelse statements it checks which player has the greatest score or if players have tied and prints out the results