Hi my name is Jakob and I'm new to programming. My project now is to make a game called Mastermind in Java Eclipse. Here is an explanation of what Mastermind is: http://sv.wikipedia.org/wiki/Mastermind
My game is going to be text-/number-based and it's just going to be able to run in the console, nothing advanced like an applet etc.
Would you professional programmers like to help me with this game? Please! :)
Here is how far I've come, not much, but it's a start :P

public class JakobMastermind {

	
	public static void main(String[] args) {
		
//	INTRODUCTION
		System.out.println("Hi and welcome to the game Mastermind!! :)\nMade by Jakob Petersson\n\n");
		
		
//	ANSWER/SECRET
//	(4 arrays for a combination of 4 numbers that will guess a number between 1 and 6)
	
		int[] secret=new int[4];
		
		for(int i=0;i<4;i++){
			secret[i]=(int)(Math.random()*6+1);
		}
		
//	(Type out the answer)
		System.out.println("The secret: "+secret[0]+secret[1]+secret[2]+secret[3]);
		
		
		
//	GUESSES
		
		int[] guess=new int[4];
		
		for(int i=0;i<4;i++){
		do{
		guess[i]=Kbd.readInt("Guess number "+(i+1)+": ");
		}while(!(guess[i]>=1 && guess[i]<=6));
		
		}
		
		System.out.println("Your guess was: "+guess[0]+guess[1]+guess[2]+guess[3]+"\n");

//	CONTROL
		
//		WHITE
			int white=0;
		
			if(guess[0]==secret[1] || guess[0]==secret[2] || guess[0]==secret[3]){
				white=white+1;
			}
			if(guess[1]==secret[0] || guess[1]==secret[2] || guess[1]==secret[3]){
				white=white+1;
			}
			if(guess[2]==secret[0] || guess[2]==secret[1] || guess[2]==secret[3]){
				white=white+1;
			}
			if(guess[3]==secret[0] || guess[3]==secret[1] || guess[3]==secret[2]){
				white=white+1;
			}
		
//		BLACK
			int black=0;
			
			if(guess[0]==secret[0]){
				black=black+1;
			}
			if(guess[1]==secret[1]){
				black=black+1;
			}
			if(guess[2]==secret[2]){
				black=black+1;
			}
			if(guess[3]==secret[3]){
				black=black+1;
			}
		
//		CONTROL - ANSWER
			white=white-black;
			
			if(white>1 && black>=1){
				System.out.print("You have "+white+" white pins");
			}else if(white==1 && black>=1){
				System.out.print("You have "+white+" white pin");
			}else if(white>1 && black==0){
				System.out.println("You have "+white+" white pins.");
			}else if(white==1 && black==0){
				System.out.println("You have "+white+" white pin.");
			}
			
			if(black>1 && white>=1){
			System.out.println(" and "+black+" black pins.");
			}else if(black==1 && white>=1){
				System.out.println(" and "+black+" black pin.");
			}else if(black==1 && white<1){
				System.out.println("You have "+black+" black pin.");
			}else if(black>1 && white<1){
				System.out.println("You have "+black+" black pins.");
			}
		
//	WIN
			
			if(secret[0]==guess[0] && secret[1]==guess[1] && secret[2]==guess[2] && secret[3]==guess[3]){
				System.out.println("\nCongratulations, you've won!! :D");
			}
			
			
			
			
			
	}

}

The code looks good, is there any problem that we can help you with?

The code looks good, is there any problem that we can help you with?

Yes, please do! I've some issues with the black/white pins. Sometime they count one or another too much. For example if the WIN is: 4663 and you ANSWER: 4636 I think it will give you one too many pins of some color, don't know the exact issue, but would you try it and see what's wrong please? :)
I would also like some help with some kind of menu, like when you're done it asks you if you want to play again and I would also like to have a maximum amount of guesses (I think it's 9 or something). I'm kind of a noob so I would love to learn some of this stuff :P

Hi

This is what I've made of it, usually I don't code an entire program, but this was fun, and the code was good. I modified the program quite a bit, you don't have to use all the things I did, but you can learn from it I hope.

- I made a do {} while {} loop around the entire program, to repeat the guessing
- I changed the way pins are checked
- I changed the printing of the number of pins
- I changed the random number generating

import java.io.*;
import java.util.*;

public class Main {
	private BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
	private int[] guess = new int[4];
	private int[] secret = new int[4];
	private int turn = 1;

	public static void main(String[] args) throws IOException {
		new Main ();

	}
	Main() throws IOException {

		//	intro //
		System.out.println ("Hi and welcome to the game Mastermind!! :)\nMade by Jakob Petersson\n\n");

		//	secret = {(w,x,y,z): w,x,y,z € {1,2,3,4,5,6}} //
		for (int i = 0; i < secret.length; i++)
			secret[i] = new Random ().nextInt (6) + 1;

		//	print the answer //
		System.out.println ("The secret: " + secret[0] + secret[1] + secret[2] + secret[3]);

		do {
			// ask for quess //

			for (int i = 0; i < 4; i++) {
				do {
					System.out.println ("Guess number " + (i + 1) + ": ");
					guess[i] = Integer.parseInt (reader.readLine ());
				} while (!(guess[i] >= 1 && guess[i] <= 6));

			}

			System.out.println ("Your guess was: " + guess[0] + guess[1] + guess[2] + guess[3] + "\n");

			// white and black pins//
			boolean[] white = new boolean[4], black = new boolean[4];
			Arrays.fill (white, false);
			Arrays.fill (black, false);

			for (int i = 0; i < white.length; i++) {
				black[i] = guess[i] == secret[i];

				if (!black[i]) {
					int j = 0;
					for (int k = 0; k < 3; k++, j++) {
						if (j == i)
							j++;
						white[i] = white[i] || (guess[i] == secret[j]);
					}
				}
			}

			int whitePins = 0, blackPins = 0;
			for (int i = 0; i < white.length; i++) {
				if (white[i])
					whitePins++;
				if (black[i])
					blackPins++;
			}

			System.out.println ("You have " + whitePins + " white pin" + (whitePins == 1 ? "" : "s") + " and " + blackPins + " black pin"
					+ (blackPins == 1 ? "" : "s"));
			
			turn ++;

		} while (!(secret[0] == guess[0] && secret[1] == guess[1] && secret[2] == guess[2] && secret[3] == guess[3]) && turn <= 8);
		System.out.println ("\nCongratulations, you've won!! :D");

	}
}

I'm really sorry, but you kind of failed somewhere in the correction or printing of the pins (which is my biggest problem). For example it gives you 3 white and 1 black pin if you guess 2222 and the answer is 2615. It's just supposed to give 1 black pin and 0 white since it's just one correct number and that number is in the right place.

Hi

This is what I've made of it, usually I don't code an entire program, but this was fun, and the code was good. I modified the program quite a bit, you don't have to use all the things I did, but you can learn from it I hope.

- I made a do {} while {} loop around the entire program, to repeat the guessing
- I changed the way pins are checked
- I changed the printing of the number of pins
- I changed the random number generating

import java.io.*;
import java.util.*;

public class Main {
	private BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
	private int[] guess = new int[4];
	private int[] secret = new int[4];
	private int turn = 1;

	public static void main(String[] args) throws IOException {
		new Main ();

	}
	Main() throws IOException {

		//	intro //
		System.out.println ("Hi and welcome to the game Mastermind!! :)\nMade by Jakob Petersson\n\n");

		//	secret = {(w,x,y,z): w,x,y,z € {1,2,3,4,5,6}} //
		for (int i = 0; i < secret.length; i++)
			secret[i] = new Random ().nextInt (6) + 1;

		//	print the answer //
		System.out.println ("The secret: " + secret[0] + secret[1] + secret[2] + secret[3]);

		do {
			// ask for quess //

			for (int i = 0; i < 4; i++) {
				do {
					System.out.println ("Guess number " + (i + 1) + ": ");
					guess[i] = Integer.parseInt (reader.readLine ());
				} while (!(guess[i] >= 1 && guess[i] <= 6));

			}

			System.out.println ("Your guess was: " + guess[0] + guess[1] + guess[2] + guess[3] + "\n");

			// white and black pins//
			boolean[] white = new boolean[4], black = new boolean[4];
			Arrays.fill (white, false);
			Arrays.fill (black, false);

			for (int i = 0; i < white.length; i++) {
				black[i] = guess[i] == secret[i];

				if (!black[i]) {
					int j = 0;
					for (int k = 0; k < 3; k++, j++) {
						if (j == i)
							j++;
						white[i] = white[i] || (guess[i] == secret[j]);
					}
				}
			}

			int whitePins = 0, blackPins = 0;
			for (int i = 0; i < white.length; i++) {
				if (white[i])
					whitePins++;
				if (black[i])
					blackPins++;
			}

			System.out.println ("You have " + whitePins + " white pin" + (whitePins == 1 ? "" : "s") + " and " + blackPins + " black pin"
					+ (blackPins == 1 ? "" : "s"));
			
			turn ++;

		} while (!(secret[0] == guess[0] && secret[1] == guess[1] && secret[2] == guess[2] && secret[3] == guess[3]) && turn <= 8);
		System.out.println ("\nCongratulations, you've won!! :D");

	}
}

You're right, I hadnt thought of more than 1 pin of the same number (in the mastermind game, you can only use each number once). I'll look into it, but it'll definitely make things more difficult.

Yup, changed the pin checking a bit.

// white and black pins//
			boolean[] white = new boolean[4], black = new boolean[4], used = new boolean[4];
			Arrays.fill (white, false);
			Arrays.fill (black, false);
			Arrays.fill (used, false);

			for (int i = 0; i < white.length; i++) {
				black[i] = guess[i] == secret[i];

				if (!black[i]) {
					int j = 0;
					for (int k = 0; k < 3; k++, j++) {
						if (j == i)
							j++;
						white[i] = white[i] || (guess[i] == secret[j] && !used[j]);
						if (white[i]) {
							used[i] = true;
							break;
						}
					}
				}
			}

I added a variable that checked if the current pin had already been visited and counted.

It still doesn't work! For example if you have 2563 and guess 2222 it gives you 1 black and 3 white pins, but it's just supposed to give 1 black. And if you guess 2345 it gives you 1 black and 1 white when it's supposed to give 1 black and 2 white. Can you fix this please? Thanks for everything you've done so far, I really appreciate it! :)
I would also like to have something that prints out "This was guess number 1, you have 9 guesses left" and after all guesses (10) it says "Game over!" and a loop that asks the player "Would you like to play again?". Do you know how to do that?

Yup, changed the pin checking a bit.

// white and black pins//
			boolean[] white = new boolean[4], black = new boolean[4], used = new boolean[4];
			Arrays.fill (white, false);
			Arrays.fill (black, false);
			Arrays.fill (used, false);

			for (int i = 0; i < white.length; i++) {
				black[i] = guess[i] == secret[i];

				if (!black[i]) {
					int j = 0;
					for (int k = 0; k < 3; k++, j++) {
						if (j == i)
							j++;
						white[i] = white[i] || (guess[i] == secret[j] && !used[j]);
						if (white[i]) {
							used[i] = true;
							break;
						}
					}
				}
			}

I added a variable that checked if the current pin had already been visited and counted.

Well, I think you should look into that yourself. I made a lot of code, try to understand it, and ask yourself: What do I want? What should the program do? What steps should it take to do that?
Make those steps.

If you run into problems, ask me, I'll help you

Why do I get an error on kbd.???

This thread is OLD. Please start a new thread with your problem. Be sure to post the full text of the error message and the code that caused it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.