williamsb14's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
3 (0 per day)
Joined:
24-November 12
Profile Views:
83
Last Active:
User is offline Nov 29 2012 10:39 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   williamsb14 has not set their status

Posts I've Made

  1. In Topic: Mastermind Java Help (Non gui)

    Posted 24 Nov 2012

    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.
  2. In Topic: Mastermind Java Help (Non gui)

    Posted 24 Nov 2012

    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 );
    
    }
    }
    
    

    Ok thanks alot, I'm lost on how to print the board and printing all of the previous guesses.
    When I try to run it all I get is that it could not find the class Mastermind so it's hard to track my progress.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

williamsb14 hasn't added any friends yet.

Comments

williamsb14 has no profile comments yet. Why not say hello?