Results 1 to 4 of 4
- 11-24-2012, 08:29 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Simple Mastermind Java Game, Please help
Hello I have an java assignment due where I create a simple Mastermind game. As of now I am I stumped and help would be appreciated. I have two classes and they are as follows;
Java Code:import java.util.Random; public class Mastermind { public char [] secretCode = new char [4]; public void main(String[] args) { Board board = new Board( ); String code = generateCode( ); boolean gameover = false; do { String guess = getPlayerGuess( ); if ( ! guess.equals("QUIT")) { gameover = board.processGuess( guess, code ); board.printBoard( ); } } while ( ! gameover && (board.getCount( ) < 10) && !guess.equals("QUIT") ); if ( gameover ) System.out.println("You guessed the code correctly in " + board.getCount( ) + " moves"); else if (guess.equals("QUIT") ) System.out.println( "Please play again"); else System.out.println("Sorry – too many guesses"); } public String generateCode(); { Random gen = new Random(); int num = 0; char[] colors = {'R','B','G','Y'}; for(int x = 0; x < colors.length; x++) { num = gen.nextInt(4); secretCode[x] = colors[num]; } return secretCode; } } import java.util.*; public class Board { private int numMoves = 0; public Board(char[][] board){ board = new char [10][8]; } public boolean processGuess(String guess, String code){ String result = ""; String guessCopy = guess; String codeCopy = code; for ( int pos = 0; pos < 4; pos++ ) { if ( guess.charAt(pos) == code.charAt(pos) ) { result = result + "P"; code = code.substring(0,pos) + "X" + code.substring(pos+1); guess = guess.substring(0,pos) + "X" + guess.substring(pos+1); } } for ( int pos = 0; pos < 4; pos++ ) { int loc = code.indexOf(guess.charAt(pos)); if (loc != -1 && (guess.charAt(pos) != 'X') ) { result = result +"C"; code = code.substring(0,loc) + "X" + code.substring(loc+1); } else if ( guess.charAt(pos) != 'X' ) { result = result + " "; } } char [] resultCode = result.toCharArray( ); Arrays.sort( resultCode); result = new String(resultCode); board[ board.getCount()-1 ] = guessCopy + " " + result; if ( result == "PPPP" ) return true; else return false; } public void printBoard(){ for (int row = 0; row < numMoves; row++) { for (int col = 0; col < 4; col++) { System.out.print(board[row][col] + " "); } } } public int getCount(){ numMoves++; return numMoves; } public String getPlayerGuess(Scanner input){ System.out.println("Please enter your guess for the secret code or QUIT : " + input ); } }
I need the output to look like:
Please enter your guess for the secret code or “QUIT” : BBGG
BBGG CCP
Please enter your guess for the secret code or “QUIT” : GBBY
BBGG CCP
GBBY CC
Please enter your guess for the secret code or “QUIT” : GRBG
BBGG CCP
GRBY CC
GRBG CCCC
Please enter your guess for the secret code or “QUIT” : BGGR
BBGG CCP
GRBY CC
GRBG CCCC
BGGR CCPP
Please enter your guess for the secret code or “QUIT” : RGGB
BBGG CCP
GRBY CC
GRBG CCCC
BGGR CCPP
RGGB PPPP
You guessed the code correctly in 5 moves
Currently the printBoard() method is giving me the most trouble. ThanksLast edited by Fubarable; 11-24-2012 at 08:31 PM. Reason: code tags added
-
Re: Simple Mastermind Java Game, Please help
Please go into more detail about the problems you are having with your code. The more useful information you can give us, the better we will be able to help you. In a second I will edit your question above in order to add [code] [/code] tags around your posted code so that it is easier to read.
Edit: also the code you posted is *not* formatted but rather is all left-justified. If we can't read your code, we can't understand it. If we can't understand it, we can't help you. Please fix this by editing your initial question, by posting only well-formatted code and by using [code][/code] tags around your posted code (as I have done).
- 11-24-2012, 08:36 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Re: Simple Mastermind Java Game, Please help
Ok thanks alot, I'm lost on how to print the board and printing all of the previous guesses.
- 11-24-2012, 08:52 PM #4
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Re: Simple Mastermind Java Game, Please help
When I try to run the code all I get is cannot find or load class Mastermind so I can't tell if what I'm doing is right or wrong. Here is an outline of the methods I am suppose to use;
Mastermind.java - this class is the driver class which will contain the main( ) method that drives the game. It should declare a Board object (see below). It should also generate the “secret” code consisting of 4 randomly selected colored pegs (R,G,B,Y).
You should define the following method.
String generateCode( ) – this method will generate the 4 peg secret code for the game. Each character of the code should be generated randomly from the 4 colors RGBY.
Board.java - this class represents the board and will be updated after each player guess. It will consist of a data member named board (lowercase) which is a 2D array of char consisting of 10 rows of 8 characters OR a single dimensioned array of Strings. Each row represents a guess (leftmost 4 chars) and a response code (4 chars). Row 0 is the first guess made by the player and its corresponding response. Row 1 is the second guess and its response and so on. This array should be initialized in the constructor for the class Baord().
The Board class should have a private data member of type int named numMoves which should be initialized to 0 in the constructor.
The following methods should be contained in this class.
boolean processGuess( String guess, String code ) - this method should determine the correct response to the given player guess (the string of C’s and P’s). IT should update the correct row of the board with all 8 characters and it should return true if the player’s guess is a winner (the response string is “PPPP”) or false if it is not.
void printBoard( ) - Print the board row by row up to the most recent guess. And result.
int getCount( ) - returns the value of the numMoves variable.
String getPlayerGuess( Scanner input) - this method prompts the user for their guess which should be a string of 4 characters consisting of either R,G,B, or Y. IT then returns this String. It uses the supplied Scanner object to obtain the input.
Similar Threads
-
Simple Java Game -- HELP :)
By Gamersunited22 in forum New To JavaReplies: 7Last Post: 04-04-2012, 06:41 PM -
My Java games! MasterMind, Othello/Reversi, Reaction Timer & Random Number Game
By cawthorne in forum Reviews / AdvertisingReplies: 1Last Post: 01-15-2012, 02:50 PM -
Mastermind game problem
By TheQuad in forum New To JavaReplies: 1Last Post: 04-16-2011, 05:29 PM -
need a simple java game
By Shashwat in forum New To JavaReplies: 1Last Post: 01-03-2011, 02:42 PM -
how to make mastermind game
By javabeginer in forum New To JavaReplies: 10Last Post: 04-14-2009, 02:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks