Java project

Status
Niet open voor verdere reacties.

programmie

Nieuwe gebruiker
Lid geworden
3 apr 2017
Berichten
1
Beste programmeurs,
Ik ben bezig met een project en het lukt aardig.
nu moet ik in mastermind zelf de method mastermindboard maken en die aanroepen in mastermindhuman en mastermindcomputer.
Daarnaast ook de instance variables in de mastermind zetten ipv in mastermindhuman en mastermindcomputer.
Hieronder mijn klassen. Zou iemand mij hierbij kunnen helpen?

[JS]public class MastermindHuman {
private int count1;
private int count2;

private int moves = 0;
private String colors = "ABCDEF";
private String colorcode = "";
private String answer;
private int rightGuess;
private int wrongGuess;
Random rdm = new Random();

boolean guessed = false;

ArrayList<String> answers = new ArrayList<String>();
ArrayList<String> rightCount = new ArrayList<String>();
ArrayList<String> wrongCount = new ArrayList<String>();

public void playhuman() {
// random generator
for (int i = 0; i < 4; i++) {
colorcode = colorcode + colors.charAt(rdm.nextInt(colors.length()));
}
System.out.println("I've got a secret code on my mind. Can you guess it?");
System.out.println("");
System.out.println(colorcode); // cheat code

while (guessed == false) {
System.out.println("Enter your answer here : ");
Scanner scanInput = new Scanner(System.in);
answer = scanInput.nextLine();

if (answer.length() < 4) {
System.out.println("That guess isn't long enough! Try again...");
}
if (answer.length() >= 4) {

answer = answer.substring(0, 4);
// zorgt ervoor dat alleen de eerste 4 karakters gebruikt worden
answer = answer.toUpperCase();
System.out.println(answer);

if (answer.matches("[a-fA-F]+")) { // wanneer het antwoord matched met a tot f of A tot F

exactSpot(); // aanroepen methode juiste plek
rightOnWrongPlace(); // aanroepen methode verkeerde plek
System.out.println("");
moves++;
mastermindBoard();
if (moves == 9) {
System.out.println("I'm sorry, you lost....");
System.out.println("");
System.out.println(colorcode);
break;
}
if (rightGuess == 4) {
boolean isCorrect = true;
System.out.println("You got it !! ");
System.out.println("");
break;

}

} else {
System.out.println("You can't use that as a colour. Please use one of these: ABCDEF");
}
}

}
}

public void exactSpot() {
rightGuess = 0;
count1 = 0;
count2 = 1;
String answer1 = answer.substring(count1, count2);
String colorcode1 = colorcode.substring(count1, count2);
for (int i = 0; i < 4; i++) {
// als van het antwoord plaats x gelijk is aan de code plaats x, dan
// wordt exactspot opgehoogd met 1. 4 x erdoor loopen
if (answer1.equals(colorcode1)) {
rightGuess++;
}
count1++;
count2++;
}
rightCount.add("" + rightGuess);
System.out.println("You've got " + rightGuess + " colours exactly right");
}

public void rightOnWrongPlace() {
wrongGuess = 0;
count1 = 0;
count2 = 1;
for (int i = 0; i < 4; i++) {
// als de code een letter bevat van het antwoord, en niet op de
// perfecte plek staat dan wordt wrongspot opgehoogd met 1. 4x
// doorheen loopen.
if (colorcode.contains(answer.substring(count1, count2))
&& !(answer.substring(count1, count2).equals(colorcode.substring(count1, count2)))) {
wrongGuess++;
}
count1++;
count2++;
}
wrongCount.add("" + wrongGuess);
System.out.println("You've got " + wrongGuess + " colours right but in the wrong spot");
}

public void mastermindBoard() {
answers.add(answer);
System.out.println("*** Mastermind Board***");
for (int bord = 0; bord < answers.size(); bord++) {
System.out.println(
bord + 1 + " " + answers.get(bord) + " " + rightCount.get(bord) + " " + wrongCount.get(bord));
}
System.out.println("***********************");
System.out.println("");
}

}
[/JS]


[JS]public class MastermindComputer {
private String right;
private String wrong;

private int moves = 0;
private String colours = "ABCDEF";
private String answer;
Random rdm = new Random();

boolean guessed = false;

ArrayList<String> answers = new ArrayList<String>();
ArrayList<String> rightCount = new ArrayList<String>();
ArrayList<String> wrongCount = new ArrayList<String>();

public void playcomputer() {
int moves = 1;

System.out.println("Please think of a code of 4 letters. Press <return> when you are ready...(use ABCDEF)");
Scanner scInput = new Scanner(System.in);
scInput.nextLine();

System.out.println("");
while (guessed == false) {
answer = ""; // random input computer reset
for (int i = 0; i < 4; i++) {
// creeert 4 character lang antwoord en wijst dat toe aan answer
answer = answer + colours.charAt(rdm.nextInt(colours.length()));

}
System.out.println("Guess " + moves + ": " + answer);
System.out.println("How many colours are exactly right?");
right = scInput.nextLine();

if (right.matches("[01234]") && right.length() == 1) {
if (right.equals("4")) {
wrong = "0";
System.out.println("Yay, I got it !!");
System.out.println("");
break;
} else {
rightCount.add(right);
System.out.println("How many colours are right but in the wrong spot?");
wrong = scInput.nextLine();
if (wrong.matches("[01234]") && wrong.length() == 1) {
wrongCount.add(wrong);
moves++;
mastermindBoard();
} else {
System.out.println("Please put in 0-4");
}
}
} else {
System.out.println("Please put in 0-4");
}

}

}

private void mastermindBoard() {
answers.add(answer);
System.out.println("*** Mastermind Board***");
for (int bord = 0; bord < answers.size(); bord++) {
System.out.println(
bord + 1 + " " + answers.get(bord) + " " + rightCount.get(bord) + " " + wrongCount.get(bord));
}
System.out.println("***********************");
System.out.println("");

}
}
[/JS]

[JS]public Mastermind() {
MastermindHuman mmh = new MastermindHuman();
mmh.playhuman();

MastermindComputer mmc = new MastermindComputer();
mmc.playcomputer();
}

public void play(){

}



}[/JS]
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan