I'm currently learning Java. I'm trying to get a good grasp on determining what becomes a class. I've started translating old games in to simple Java programs.
I'm looking for any critique of use of objects. Pretty much any advice as to what could make this better in the long run.
(This code has no input error checking, I know this. It should but I want to make sure I'm on the right path first.)
- I feel that codebreaker doesn't need to be its own class.
- I feel that the board should be a class?
- Is there a better way than making a temp copy of my
ArrayList
to do the checking.
Game.java
package com.secryption.mastermind;
public class Game {
private int numberOfPegs = 0; //number of pegs the player wants to use
private String playerName = "";
int numberOfGuesses = 0;
boolean isAlive = true;
public static void main(String[] args) {
Game game1 = new Game();
game1.setupGame();
}
private void setupGame() { //here is where we get the name, pegs etc.
ScreenHelper sh1 = new ScreenHelper();
CodeBreaker cb1 = new CodeBreaker();
setPlayerName(sh1.getUserInput("Enter your name: "));
String pg = sh1.getUserInput("How many pegs would you like?");
setNumberOfPegs(Integer.parseInt(pg));
CodeMaker cm1 = new CodeMaker(numberOfPegs);
cm1.createBoard();
System.out.println(cm1.printBoard());
System.out.println("You may begin guessing now. Enter your guess as colors. For example with 4 pegs.");
System.out.println("rybl = Position 0=red, Position 1=yellow etc.");
System.out.println("r=red, y=yellow, b=black, w=white, l=blue, g=green, p=purple");
while(isAlive) {
String guess = cb1.makeGuess();
System.out.println("Your guess: " + guess);
String result = cm1.checkGuess(guess);
numberOfGuesses++;
if(result.equals("BBBB")) {
System.out.println("You win " + playerName + "! It took you " + numberOfGuesses + " to crack the code.");
isAlive = false;
} else {
System.out.println("Result: " + result);
}
}
}
public void setNumberOfPegs(int i) {
numberOfPegs = i;
}
public int getNumberOfPegs() {
return numberOfPegs;
}
public void setPlayerName(String n) {
playerName = n;
}
public String getPlayerName() {
return playerName;
}
}
CodeMaker.java
package com.secryption.mastermind;
import java.util.ArrayList;
public class CodeMaker {
private int boardLength = 0;
private String[] colors = {"r", "g", "l", "y", "p", "b", "w"};
private ArrayList<String> board = new ArrayList<String>();
private ArrayList<String> tempBoard = new ArrayList<String>();
private String result = "";
public CodeMaker(int i) {
boardLength = i;
}
public int getBoardLength() {
return boardLength;
}
public void createBoard() {
for(int i = 0; i < boardLength; i++) { // get random colors for each of teh pegs
board.add(randomColor()); // add to arraylist. just add color
}
}
private String randomColor() {
int a = (int) (Math.random() * 7);
String r = colors[a];
return r;
}
public String printBoard() {
for ( String s : board) {
result = result + s;
}
return result;
}
private void makeTempBoard() {
tempBoard.clear();
for( String w : board) {
tempBoard.add(w);
}
}
public String checkGuess(String g) {
String result = "";
String piece = "";
makeTempBoard();
//System.out.println(tempBoard.size());
for(int i = 0; i < g.length(); i++) { //check each pair of guess. position/color.. should change this
String subA = g.substring(i, i + 1);
//System.out.println("The guess letter: " + subA);
for(int b = 0; b < tempBoard.size(); b++) { //take each part of the guess and check it against each board peg
//System.out.println("The tempBoad character: " + b);
if(subA.equals(tempBoard.get(b)) && (i == b)) {
piece = "B";
tempBoard.set(b, "*"); //set to * so we don't get double results.
break;
}
else if(subA.equals(tempBoard.get(b))) {
piece = "W";
//same here, remove item from arraylist.
tempBoard.set(b, "*");
break;
} else {
piece = "-";
} //end else
}//end if
result = piece + result;
}//end for
return result;
}//end method
}
CodeBreaker.java
package com.secryption.mastermind;
public class CodeBreaker {
public String makeGuess() {
ScreenHelper sh2 = new ScreenHelper();
String guess = sh2.getUserInput("Enter a guess: ");
return guess;
}
}
ScreenHelper.java
package com.secryption.mastermind;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ScreenHelper {
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0 ) return null;
} catch (IOException e) {
System.out.println("IOException: " + e);
}
return inputLine;
}
}