Skip to content

Code for final CS2A project at Foothill College Winter 2019. Project based on Mastermind word game. This version is for 1-player and the computer chooses the word.

Notifications You must be signed in to change notification settings

melissa-perez/cs2a-mastermind-final-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mastermind

Link to presentation: https://www.youtube.com/watch?v=XjHVpYH9AQk&t=53s

Introduction

code output

Mastermind is a two player game where a player chooses a 4 letter word and guides the second player to a correct guess or forfeit. To help the second player, a code is shown as a sequence of asterisks, hyphens, and spaces. These correspond to a correct letter and proper position, a correct letter and incorrect position, or a incorrect letter, respectively, of the guess attempt. The position of these symbols is not one-to-one with the results.

In this implementation, Player 1 will be designated as the computer. It will then select a random word from a file-read list. The user will be Player 2. These two players alternate their turns until the word is guessed or not.

Designing

I decided that all the code would be contained in the Game class. The main has the looping mechanisms to continue repeating the game until the player quits or guesses correctly, and if the player would like to play a new game. Apart from that, all input and output is maintained inside the class. Variables that I believed should not be changed were made into static constant variables. This is because I didn't want the constant variables to keep being initialized at each new Game instance being created. The word bank of all the words was created in this manner, as File I/O is a costly expense. All player input is submitted through a check since that would be the main point of errors barring all game logic handling.

Implementation

Most functions are self explanatory. I did want to go over the few I touched in the video since those are the ones driving the game.

ComputerTurn() This function acts as the mediator between the player and game.

  • The input of Q, surpasses all input since the player will want to quit immediately.
  • The input of H, follow since the player the player will want to view the help. If it's the beginning of the game add, display help once.
  • Now follows the result of having to check the response created, if it happens to be true then the game has ended.
  • If not, the last option is to show the player the code and move to the playerTurn.
  • At this point, set the old guess to the empty string, regardless since the player will have to input a new response OR the game will end during the checkpoint in the main.

PlayerTurn() This function receives the player's input and with the SanitizeInput function, provide the game with a correct word value.

  • Parse the given string input and make sure it's a string value.
  • Call on the helper function to validate this string.
  • Check if this good input is the extra commands H or Q, so the number of attempts doesn't increase.
  • If the input was bogus, then erase the input and prompt the player to try again back at the main.

CreateResponse() This function builds the code string to show the player how close their guess was.

  • Create copies of the game word and the player guess in order to modify them and maintain the original variables.
  • The two loops will iterate the words in different steps, at each i=0..4, the jth value will cycle from 0 to 4.
  • i will be accessing the values of the chosen game word letter-wise.
  • Lump the string made together and set the response.

iterating

Improvements

In regards to main, I will most likely move the CreateResponse function as a private function and only call it from inside the ComputerTurn(). I didn't want to create a continuation style approach and give ComputerTurn too much power, but I feel that is next course of action if I were to hide more details from the client. I would also change the inner loop of checking the input to an empty string with another bool character within the class.

The header will change accordingly and add the necessary new variables and getters to access them in the main. The logic in CreateResponse could also be handled better and may not need an extra copy set of the current guess and the actual word. I don't feel that it is intuitive enough to explain without writing down, and that is not the purpose of having good code.

Aside from changing th current code, a Player class could be added if the game were to include the ability of having two human players. It would require some restructuring of the current code however. The PlayerTurn and ComputerTurn methods would need to be condensed into similar abilities. Or perhaps include function overloading. There are many ways to achieve this to make the game more robust. I would ideally love to include a graphical interface and perhaps have a file output of streaks and guess attempts.

iterating

About

Code for final CS2A project at Foothill College Winter 2019. Project based on Mastermind word game. This version is for 1-player and the computer chooses the word.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages