// This program is a java version of the game mastermind import java.util.Scanner; import java.util.Random; public class Mastermind { private static int code[] = new int [4]; private static int board[][] = new int [12][6]; private static String name; private static int currentRow; /** * This is the constructor * * @param tempName String value input to store in name */ public Mastermind( String tempName ) { name = tempName; currentRow = 0; System.out.printf("\nWelcome to Mastermind, %s!\n\n", name); System.out.print("You will try to guess a four-digit code that has been randomly generated.\n"); System.out.print("The numbers you can use are between 1 and 6. I won't check to see\n"); System.out.print("if you only use those numbers, however.\n\n"); System.out.print("When you enter a guess, enter four numbers separated by spaces in the order\n"); System.out.print("you think I have them.\n\n"); System.out.print("After each attempt, I will tell you how many numbers are in the\n"); System.out.print("correct positions, and how many are present but in the wrong positions.\n\n"); System.out.print("You have 12 attempts to guess my code. Good luck!\n"); genCode(); } /** * This method creates the master code to break */ public static void genCode() { Random randomNumbers = new Random(); code[0] = 1 + randomNumbers.nextInt(6); code[1] = 1 + randomNumbers.nextInt(6); code[2] = 1 + randomNumbers.nextInt(6); code[3] = 1 + randomNumbers.nextInt(6); } /** * This method displays the master code */ public static void showCode() { System.out.println("Here is the code: "); System.out.printf( " %d %d %d %d\n\n",code[0],code[1],code[2],code[3]); } /** * This method asks the player to enter their four-digit attempt */ public static void getAttempt() { System.out.println("\n------------------------------------------"); System.out.printf("Enter 4 numbers for attempt #%d: ", currentRow + 1); Scanner i = new Scanner( System.in ); board[currentRow][0] = i.nextInt(); board[currentRow][1] = i.nextInt(); board[currentRow][2] = i.nextInt(); board[currentRow][3] = i.nextInt(); } /** * This method displays all attempts so far for the game */ public static void showAttempts() { System.out.printf("\nHere are your previous guesses, %s\n\n", name); for( int i = 0; i <= currentRow; i++) { System.out.printf(" Attempt %d: %d %d %d %d ", i+1, board[i][0], board[i][1], board[i][2], board[i][3]); if (board[i][4] > 0) { for ( int j = 1; j <= board[i][4]; j++) { System.out.print("+"); } } if (board[i][5] > 0) { for ( int k = 1; k <= board[i][5]; k++) { System.out.print("-"); } } System.out.println(""); } } /** * This method identifies the number of exact and inexact code matches * * @return number of exact matches in current attempt */ public static int checkAttempt() { int exact = 0; int inexact = 0; int accountpos[] = {0,0,0,0}; for (int i = 0; i < 4; i++) { if (board[currentRow][i] == code[i]) { exact++; accountpos[i] = 1; } } for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { if (board[currentRow][j] == code[k] && accountpos[k]==0 && j!=k) { inexact++; accountpos[k] = 1; } } } System.out.printf("\nResults for attempt #%d: \n\n", currentRow + 1); System.out.printf(" Numbers in correct position: %d\n", exact); System.out.printf(" Numbers in wrong position: %d\n", inexact); board[currentRow][4] = exact; board[currentRow][5] = inexact; return exact; } /** * This method displays a success or fail message based on exact matches * * @param correct number of exact matches on latest attempt */ public static void showResults( int correct ) { if (correct == 4) { System.out.println("\nIt looks like you won the game!"); System.out.println("Congratulations!!!!!"); } else { System.out.println("\nI'm sorry, it looks like you didn't win."); } showCode(); } /** * This method actually plays the game */ public static void playGame() { int count = 1; int correct = 0; while (count <= 12) { getAttempt(); correct = checkAttempt(); if (correct == 4) break; showAttempts(); currentRow++; count++; } showResults(correct); } }