C. Program Description for Developers

War

  1. Load in Card deck.mat, created a variable called ShuffledDeck which is a vector of 1-52 cards in random indices.
  2. player=ShuffledDeck[1:26];
  3. Computer=ShuffledDeck[27:52];
  4. Set DeckValues=[14 2: 13 14 2: 13 14 2: 13 14 2: 13];
  5. Use subplot and imshow functions to properly display cards, player card on top, Computer card on the bottom.
  6. Set variable won=0;
  7. Print out statement explaining rules and inputs
  8. While loop that iterates until won no longer equals 0;
  9. Every iteration of loop is a turn
  10. 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
  11. The loop will iterate until someone has won, and the program will terminate.

 

Yahtzee

  1. load in Dice.mat
  2. 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
  3. Using a double for loop create and fill in three more columns in the cell array score
  4. create a variable turns set it equal to 48
  5. starts a while loop
  6. starts a for loop with three iteration
  7. Prints which players turn it is
  8. Rolls 5 dice with random values 1-6 and stores them in roll
  9. Shows the dice using imshow
  10. Calls the function rerolling and stores it in roll
    1. rerolling uses while and for loops to ask the player for which dice to
  11. reroll and then returns the new values of the dice.
  12. Asks the player for an input and stores it in categ
  13. Sorts roll numerically using sort
  14. 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
  15. Runs the function ScoreCalc and stores the outputted score in the array score
    1. ScoreCalc uses ifelse statements to check what categ is and using this returns a point value based on the dice values
  16. If ScoreCalc fails the player is asked for a different category
  17. Ends for loop
  18. Checks if the bonus Yahtzee has been used and if it has adds 100 points to the Yahtzee category
  19. 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
  20. Creates variable lowertotal and using a for loop adds up categories 10-17 storing it in lowertotal
  21. Adds up sub and bonus into a new variable uppertotal
  22. Adds up uppertotal and lowertotal into a new variable grandtotal
  23. Adds the variables to the proper categories in the array score
  24. Displays the array score
  25. Subtracts one from the turns variable
  26. 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