C. Program Description for Developers

Normtech’s Revenge

This dice game is played using printed statements and generated numbers. The only key that is used is the enter key. Although there is no visual dice being rolled, a number is selected randomly between 1 and 6. With each press of the enter key a new randomly selected number is printed for the player to view. This results represents the roll of a die. All off the previous “rolls” are visible to the user as they are progressing to through the game and then at the end. After all five rolls are made, the 5 values of the die are displayed in a vector and then the summation is displayed. Depending on the sum of their roll values, they either win or lose, receiving a printed result.

List of Variables

  1. n
    • Used to determine the turn of the game. Increases by 1 every time the user rolls the dice.
  2. Roll_1 (Roll_1 – Roll_5)
    • Used to assign the number rolled by the user, or the values returned from the random sample command.
  3. Your_Total
    • The sum of the five rolls are added together and assigned to this variable.

List of Commands

  1. input()
    • Takes the user input within the brackets.
  2. randsample()
    • Returns a single integer value between two defined limits.
  3. fprintf()
    • Prints the string within the brackets.

References

Mathworks. (2018). Random Number Generation. Retrieved November 25, 2018, from https://www.mathworks.com/help/matlab/random-number-generation.html
Mathworks. (2018). N. Retrieved November 26, 2018, from https://www.mathworks.com/help/stats/randsample.html

Twenty-One Sticks

This counting game is composed of only two main parts, a while loop and outside of the while loop.

Before the while loop, there are two variables nSum and turnCounter which are both set to zero. The nSum is the sum of all of the sticks, and the turnCounter is the number of turns that have passed.

The while loop is set so that it keeps repeating the code within as long as the sum is less than 21. Inside the while loop, there are basically two blocks of code, one for the player and one for the AI. The player block features an input statement for the player to enter 1 or 2 sticks which is assigned to the variable num, a while loop and an input statement to keep repeating asking the user to enter 1 or 2 if the user enters something else, a statement num to the nSum, and a statement which increases turnCounter by 1 after the previous lines of code have to be executed. The AI block is very similar, being that instead of having an input statement to enter a number, there is a statement using a random integer generator limited between 1 and 2, which generates 1 or 2 randomly, which is assigned to the variable aNum. Similarly to the player block, there is a summation statement adding aNum to nSum and turnCounter which increases by 1 after AI’s turn. There are break statements which break out of the while loop if  nSum is 21 or greater before the player code block and the AI code block, ensuring that the nSum remains under 21 when using the while loop.

Outside of the while loop, there is a statement using mod which divides the number of turns indicated by the turn counter into 2 and returns the remainder. After that, there is an if statement which determines if whether the user wins or loses, if the remainder is 1, the user loses because it is an odd number and all of the player’s turns are odd, and if the remainder is not 1, then the player wins because all of the AI’s turns are even.

List of Variables

  1. nSum
    • Used to measure the sum of the game. Increases by the number of sticks the player or the AI enters after every turn. Checked after every turn to see if the sum is 21 or greater. If it is, then the program will break out from the while loop.
  2. turnCounter
    • Used to determine the turn (Odd is player, while even in AI). Increases by 1 after the player and AI make their turn. After the sum is 21, turnCounter is divided by 2 and the remainder is assigned to nWin.
  3. num
    • Used to assign the number of sticks the player has entered.
  4. aNum
    • Used to assign the number of sticks the AI has entered
  5. nWin
    • Assigned the remainder of the division of turnCounter by 2. Compared to either even or odd, even as 0, and odd as 1.

List of Commands

  1. input()
    • Takes the user input within the brackets.
  2. mod()
    • Takes the remainder of a division.
  3. randi[]
    • Generates a random integer within a set limit.

References

Mathworks. (2018) Retrieved 2018, from https://www.mathworks.com/help/matlab/ref/mod.html

Mathworks. (2018). Random Number Generation. Retrieved November 25, 2018, from https://www.mathworks.com/help/matlab/random-number-generation.html

 

 

Battleship

This game starts by loading in the board and all the image files we need. Next, the program sets up the board for the computer.

The function used to set the computer board up uses several while loops to count the amount of ships placed and uses if statements to check if there are boats already in the spot that was randomly generated. The function also randomly generates the orientation of the ship by using 1 for left, 2 for right, 3 for up, and 4 for down. Then a switch statement is used to place the ship on the board based on which number was generated. This function is looped through for each type of ship in the main program.

To get the user’s placement of their ships, the program asks for the row and column number using the getRow and getCol functions. Each time the function is used, the only input to the function is the type of ship the program is asking for. The function then stores the user input into the variable that getRow or getCol is set equal to. After that, the main program prompts for the orientation. The main program then uses if statements to check if the row, column and orientation are valid by using the isempty() method. Then the player board is updated by looping from 1 to the ship size (using i and incrementing using i = i+1) using a while loop. For each value, the board is updated to add one space for each time i is incremented. The program has if-elseif-else statements to update the board based on the orientation chosen by the player. There is also a check to make sure invalid input makes the process loop through again. This check is called placed (boolean) that doesn’t update to true unless the row, column, and orientation are valid. This process is looped through for each type of ship.

Next is the player and computer taking turns trying to hit the others ship. The whole process is checked to make sure that the max number of hits(17) has not been reached. Then the player gets to go, with the main program using the function getRowColToHit. This function first loops using a while statement to make sure a selection hasn’t already been made (boolean selectionMade), Then asks for user input on which row and column they would like to hit. Then the isempty() method is used with if-elseif-else statements to check if the selection was within the bounds (1-10), and updates selectionMade once a valid selection is made. Then the main function checks if the player hit or missed using a hidden board that has the same values as the computer’s board but does not update, then updates the computer’s board to reflect if a hit was made or not using the opponentBoardUpdate function. If a hit was made, then hitsOnOpponent increments by 1. This function, opponentBoardUpdate, pushes updates made to the array, Opponent_Board and visually updates the board that the player sees . Then the opponent AI gets to play. First a while loop that checks if a selection was made is used, and an array lastHit is made to store the value of the last hit made on the player board. Then the randi() method is used to generate a row and column that will be used if lastHit is empty to hit a random spot on the board. If lastHit is not empty, then if-elseif-else statements are used to check the locations around lastHit and chooses a spot on player’s board to try and hit. If a hit is made, then hitsOnPlayer increments by 1.

List of Variables

  1. Boat_FrontBack_1 1.
    1. variable for the image of the front and back of the first boat type
  2. Boat_FrontBack_1_hit
    1. variable for the image of the front or back of the boat being hit
  3. Boat_FrontBack_2
    1. variable for the image of the front and back of the second boat type
  4. Boat_FrontBack_2_hit
    1. variable for the image of the front and back of the second boat being hit
  5. Boat_FrontBack_3
    1. variable for the image of the front and back of the third boat type
  6. Boat_FrontBack_3_hit
    1. variable for the image of the front and back of the third boat being hit
  7. Boat_FrontBack_4
    1. variable for the image of the front and back of the fourth boat type
  8. Boat_FrontBack_4_hit
    1. variable for the image of the front and back of the fourth boat being hit
  9. Boat_Mid_hor
    1. variable for the image of the midsection of a horizontal boat
  10. Boat_Mid_hor_hit
    1. variable for the image of the midsection of a horizontal boat being hit
  11. Boat_Mid_vert
    1. variable for the image of the midsection of a vertical boat
  12. Boat_Mid_vert_hit
    1. variable for the image of the midsection of a vertical boat being hit
  13. col
    1. variable for the column that an opponent’s ship will be placed
  14. colToHit
    1. variable used for user input for getting the column to hit
  15. copyofPlayerBoard
    1. used for for resetting the board if there is an overlap of ships during placement
  16. Hidden_Board
    1. 10×10 cell that the player sees for the opponent
  17. Hit
    1. variable for a boat being hit
  18. hitsOnOpponent
    1. int that acted as a counter for how many hits have been registered on the enemy’s boats
  19. hitsOnPlayer
    1. int that acted as a counter for how many hits have been registered on the player’s boats
  20. i
    1. loop variable
  21. lastHit
    1. array of the form [x,y] that determines the last tile that the opponent ai hit that was a ship.
  22. Miss
    1. variable for missing a hit on a boat
  23. Open_Water
    1. variable for the image of open water
  24. Opponent_Board
    1. 10×10 cell that is the opponent’s board and contains all the opponent’s ships
  25. orientation
    1. int for orientation of the boat
  26. overlap
    1. boolean value, determines if there is an overlap of ships
  27. placed
    1. boolean for determining if all ships have been placed
  28. Player_Board
    1. 10×10 cell that is the player’s board and contains all the player’s ships
  29. randomCol
    1. int that stores randomly generated column number
  30. randomRow
    1. int that stores randomly generated row number
  31. row
    1. variable for the row that an opponent’s ship will be placed
  32. rowToHit
    1. variable used for user input for getting the row to hit
  33. selectionMade
    1. boolean value, determines if a selection is made
  34. Ships
    1. 5×4 array that is used with Setup() to place down opponent ships randomly

References

  1. Ossman, K. and Bucks, G. , “MATLAB Courseware: Game Projects” , MathWorks, Retrieved November 26, 2018, from https://www.mathworks.com/academia/highschool/courseware/games/?s_tid=srchtitle