import java.util.Scanner;
import java.util.ArrayList;
/*
* Developed and programmed by Clement Boiteux
* Project started on 2/28/2021
* Future updates: Implement a GUI, new gamemodes and features, audio, settings, User Login
*/
public class Mastermind {
private String[] pastGuesses;
int pastGuessCounter = 0;
private ArrayList<User> users = new ArrayList<User>();
private String[] colors;
private String[] guess;
private String[] availableColors = {"Red", "Blue", "Green", "Yellow", "Black", "White"};
private String[] indicators = {"Black", "White"};
private int tries = 0;
Scanner scanner = new Scanner(System.in);
public Mastermind() {
pastGuesses = new String[10];
colors = new String[4];
guess = new String[4];
}
public Mastermind(int numPastGuesses, int numColors, int numGuesses) {
pastGuesses = new String[numPastGuesses];
colors = new String[numColors];
guess = new String[numGuesses];
}
public void getGuessHistory() {
for (int i = 0; i < pastGuesses.length; i++) {
System.out.println(pastGuesses[i]);
}
}
public void chooseRandomColors() {
for (int i = 0; i < colors.length; i++) {
colors[i] = availableColors[(int) (Math.random()*availableColors.length)];
}
}
public void revealColors() {
for (int i = 0; i < colors.length; i++) {
System.out.println(colors[i]);
} // Cheat code
System.out.println();
}
public void resetHighScores() {
for (int i = 0; i < users.size(); i++) {
users.remove(i);
i--;
}
if (users.isEmpty()) {
System.out.println("High scores list has been reset!\n");
}
}
public void selectColors() {
showAvailableColors();
System.out.println("Please choose your 4 colors: ");
for (int i = 0; i < colors.length; i++) {
System.out.println("Color #" + (i + 1) + ":");
String color = scanner.next();
if (color.equalsIgnoreCase("red")) {
colors[i] = "Red";
}
else if (color.equalsIgnoreCase("blue")) {
colors[i] = "Blue";
}
else if (color.equalsIgnoreCase("yellow")) {
colors[i] = "Yellow";
}
else if (color.equalsIgnoreCase("black")) {
colors[i] = "Black";
}
else if (color.equalsIgnoreCase("white")) {
colors[i] = "White";
}
else if (color.equalsIgnoreCase("green")) {
colors[i] = "Green";
}
}
System.out.println();
for (int i = 0; i < colors.length; i++) {
if (colors[i] == null) {
System.out.println("You did not input valid values. Try again.\n");
selectColors();
}
}
}
public void startGame() {
boolean isOptionValid = true;
System.out.println("Which would you prefer?");
System.out.println("1. Select your own colors");
System.out.println("2. Generate random colors");
int choice = scanner.nextInt();
if (choice == 1) {
selectColors();
}
else if (choice == 2) {
chooseRandomColors();
}
else {
System.out.println("Invalid option. Select an option again.");
isOptionValid = false;
startGame();
// If this happens, do not start the game
}
if (isOptionValid) { // In development
setGuess();
tries++;
checkEquivalency();
}
}
public void setGuess() {
revealColors();
showAvailableColors();
System.out.println("Guess each of the four colors: ");
for (int i = 0; i < guess.length; i++) {
System.out.println("Color #" + (i + 1) + ":");
String color = scanner.next();
if (color.equalsIgnoreCase("red")) {
guess[i] = "Red";
}
else if (color.equalsIgnoreCase("blue")) {
guess[i] = "Blue";
}
else if (color.equalsIgnoreCase("yellow")) {
guess[i] = "Yellow";
}
else if (color.equalsIgnoreCase("black")) {
guess[i] = "Black";
}
else if (color.equalsIgnoreCase("white")) {
guess[i] = "White";
}
else if (color.equalsIgnoreCase("green")) {
guess[i] = "Green";
}
}
String s = "";
for (int i = 0; i < guess.length; i++) {
s += guess[i];
if (i < guess.length - 1) {
s += ", ";
}
}
System.out.println(s);
pastGuesses[pastGuessCounter] = s;
if (pastGuessCounter < pastGuesses.length-1) {
pastGuessCounter++;
}
System.out.println();
for (int i = 0; i < colors.length; i++) {
if (guess[i] == null) {
System.out.println("You did not input valid values. Try again.\n");
setGuess();
}
}
System.out.println();
}
public void checkEquivalency() {
boolean isEquivalent = false;
if (colors.length == guess.length) { // Should not be a problem, array length doesn't magically change
for (int i = 0; i < colors.length; i++) {
if (colors[i].equals(guess[i])) {
isEquivalent = true;
}
else if (!colors[i].equals(guess[i])) {
isEquivalent = false;
break;
}
}
if (isEquivalent) {
endMenu();
}
else {
markIndicators();
getGuessHistory();
setGuess();
}
}
}
public void endMenu() {
System.out.println("Good job! You have solved the code! Would you like to: ");
System.out.println("1. Be featured on the high scores list");
System.out.println("2. Play again");
System.out.println("3. Quit game");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.println("First Name:");
String name = scanner.next();
System.out.println("Date completed ([month]/[day]/[year] format)");
String date = scanner.next();
users.add(new User(name, date, tries));
}
else if (choice == 2) {
startGame();
}
else if (choice == 3) {
System.out.println("Have a nice day! Thank you for playing!");
}
else {
System.out.println("Invalid option. Please enter again.");
endMenu();
}
}
public void printUserList() {
for (int i = 0; i < users.size(); i++) {
System.out.println(users.get(i));
}
if (users.isEmpty()) {
System.out.println("Either no one has played the game yet or high scores have been cleared.\n");
}
System.out.println("Would you like to:");
System.out.println("1. Return to Main Menu");
System.out.println("2. Reset high scores");
int choice = scanner.nextInt();
if (choice == 1) {
showMenu();
}
else if (choice == 2) {
resetHighScores();
showMenu();
}
else {
System.out.println("Invalid option. Please enter again.");
printUserList();
评论0