Mastermind - Java

Permabanned
Joined
19 Dec 2006
Posts
1,382
for my coursework, due in in 4 days, and i dont understand half of java LOL....

I have to create a mastermind game, only allowed to do it in BlueJ, and instead of the colours i have to use numbers 1-9.

So can i just change the code from colours to numbers ? only theres 6 colours in the original game and 9 numbers.......so im stuck with how to implement the other 3 numbers ?

Argh i wish i hadnt left it till the last minute!
 
So basically, you need the program to create a array say, with 6 positions each with a random number between 1 and 9 in? Then the user inputs various combinations of numbers and the program returns how many are in the correct position untill the exact combination is found?
 
mastermind.JPG


That is the specification, I think what you typed is pretty much it!
 
eracer2006 said:
currently editing it so the 6 colours = 1,2,3,4,5,6 ...

Why dont you post us what you've done so far, along with a description of what isnt working, and then we can help you to work out what the bug is?
 
Hi, here is a working mastermind game i quickly knocked up in c++, hope it helps with the logic etc. you can compile it and play if you really want i think it works :P
Code:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int* newGame()
{
	srand ( time(NULL) );

	int* gameBoard = new int[4];
	for (int i = 0; i < 4; i++)
	{
		gameBoard[i] = (rand() % 10);
	}
	return gameBoard;
}

void printArray(int* arrayToPrint)
{
   for ( int i = 0; i < 4; i++ )
	 cout << *(arrayToPrint+i) << " ";
   cout << endl;
   cout << endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
	int correctPlaces = 0;
	int wrongPlaces = 0;
	int won = 0;
	int* gameNumbers = newGame();
	int enteredNumbers[4] = {0};
	printArray(gameNumbers); //delete this if you want the solution hidden
	
	
	while (won != 1)
	{
		cout << "Guess four numbers pressing enter after each entry" << endl;
		
		cin >> enteredNumbers[0];
		cout << endl;
		cin >> enteredNumbers[1];
		cout << endl;
		cin >> enteredNumbers[2];
		cout << endl;
		cin >> enteredNumbers[3];

		for (int i = 0; i < 4; i++)
		{
			if (enteredNumbers[i] == gameNumbers[i])
			{
				correctPlaces += 1;
			}

			for (int j = 0; j < 4; j++)
			{
				if (enteredNumbers[j] == gameNumbers[i] && enteredNumbers[i] != gameNumbers[i] && enteredNumbers[j] != gameNumbers[j])
				{
					wrongPlaces += 1;
					break;
				}
			}
		}

		cout << "C = " << correctPlaces << "  W = " << wrongPlaces << endl;
		if (correctPlaces == 4)
		{
			won = 1;
		}
		correctPlaces = 0;
		wrongPlaces = 0;
	}

	cout << "Congratulations you have won!" << endl;

	return 0;
}
 
Ladforce said:
Hi, here is a working mastermind game i quickly knocked up in c++, hope it helps with the logic etc. you can compile it and play if you really want i think it works :P
Code:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int* newGame()
{
	srand ( time(NULL) );

	int* gameBoard = new int[4];
	for (int i = 0; i < 4; i++)
	{
		gameBoard[i] = (rand() % 10);
	}
	return gameBoard;
}

void printArray(int* arrayToPrint)
{
   for ( int i = 0; i < 4; i++ )
	 cout << *(arrayToPrint+i) << " ";
   cout << endl;
   cout << endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
	int correctPlaces = 0;
	int wrongPlaces = 0;
	int won = 0;
	int* gameNumbers = newGame();
	int enteredNumbers[4] = {0};
	printArray(gameNumbers); //delete this if you want the solution hidden
	
	
	while (won != 1)
	{
		cout << "Guess four numbers pressing enter after each entry" << endl;
		
		cin >> enteredNumbers[0];
		cout << endl;
		cin >> enteredNumbers[1];
		cout << endl;
		cin >> enteredNumbers[2];
		cout << endl;
		cin >> enteredNumbers[3];

		for (int i = 0; i < 4; i++)
		{
			if (enteredNumbers[i] == gameNumbers[i])
			{
				correctPlaces += 1;
			}

			for (int j = 0; j < 4; j++)
			{
				if (enteredNumbers[j] == gameNumbers[i] && enteredNumbers[i] != gameNumbers[i] && enteredNumbers[j] != gameNumbers[j])
				{
					wrongPlaces += 1;
					break;
				}
			}
		}

		cout << "C = " << correctPlaces << "  W = " << wrongPlaces << endl;
		if (correctPlaces == 4)
		{
			won = 1;
		}
		correctPlaces = 0;
		wrongPlaces = 0;
	}

	cout << "Congratulations you have won!" << endl;

	return 0;
}

Being pedantic, thats not valid ISO C++....
 
Ladforce said:
its just visual studio win32 console application :) what is not valid about it?

Well:

1. stdafx isnt a standard header
2. Neither are stdio.h, stdlib.h and time.h. They should be replaced with cstdio, cstdlib and ctime respectively
3. The entry point for a c++ application is not int _tmain(int argc, _TCHAR* argv[]), it should be int main(int argc, char* argv[]). C++ has no TCHAR type.
4. You've also got a memory leak since the memory allocation for the board is never deleted.
 
Ladforce said:
so when visual studio says create a c++ project its actually not creating proper c++? Good point about the board.

Its a kind of 'extended' c++. You couldnt, for example, copy that source file to a linux box and recompile it and run it, for example - the compiler would barf on the nonstandard header and entry point, for example.

In a true MS way, though they dont realise that other OS's exist. In an ideal world you'd say to visual studio 'create my a c++ project' and the resulting files could simply be copied to another OS, recompiled and run without modifcation....
 
Ladforce said:
Ahh I see, thanks for the explanation. By deleting the memory allocation, would delete gameNumbers be ok?

Because you used the array form of new you need the array form of delete:

delete [] gameNumbers ;
 
Visage said:
Why dont you post us what you've done so far, along with a description of what isnt working, and then we can help you to work out what the bug is?

There isnt a bug, the bug is me not knowing java well enough to write it, and failing my year at uni if i fail the coursework!
 
eriedor said:
Can I ask what course/uni is this?
Plymouth, first year programming module for a bunch of computing students. Not sure exactly which computing courses, but definitely comp sci, some web development course (no idea on the names) and a bunch of networking nerds (computer systems & networking) :)
 
Back
Top Bottom