• 0

Help! With Mastermind in C


Question

Hey guys, im developing for my class a simple mastermind game, it consist in 4 integers, from 1 to 4 and they cannot be repited. Now when i have declared this, my program doesnt want to go on! I dont know why.. so I think that the only ones who can help me are you guys!!

So please! can someone take a look to my code, specially in the leerVariables() function??

Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <iostream>

int n1,n2,n3,n4,n5,n6,n7,n8;
int cont=0,respuestasAcertadas=0,numeroIntentos=0,volver=100;
char x,y;

void introduccionPrograma()
{
	printf("Bienvenidos a Master Mind, presione enter para continuar!");
	scanf("%c",&x);
}

void instruccionPrograma()
{
	printf("A continuacion, usted jugara a Master Mind. Este juego consta de \ndos partes:\nUna en la cual el Jugador #1 introduce 4 numeros del 1 al 4 al azar \nY otra parte en la cual el Jugador #2 trata de adivinar la secuencia\nintroducida por el Jugador #1.");
	scanf("%c",&x);
}

void reglasPrograma()
{
	printf("Las reglas son: ");
	printf("\n");
	printf("1) Introducir unicamente numeros enteros del 1 al 4");
	printf("\n");
	printf("2) No repetir numeros al momento de intruducir las combinaciones");
	printf("\n");
	printf("3) No ver la pantalla de su companero al momento que el mismo introduzca \nlas combinaciones");
	printf("\n");
	printf("4) Disfrutar del juego!!");
}

void comenzarPrograma()
{
	printf("Presiona enter cuanto esten listos para comenzar...");
	scanf("%c",&x);
}

void leerVariables1()
{
	printf("Introduzca el primer valor: ");
	scanf("%d",&n1);

	while(cont<volver)
	{
		if(n1>=1 and n1<=4)
		{
			printf("Introduzca el segundo valor: ");
			scanf("%d",&n2);

			while(cont<volver)
			{
				if(n2 != n1 and n2>=1 and n2<=4)
				{
					printf("Introduzca el tercer valor: ");
					scanf("%d",&n3);

					while(cont<volver)
					{
						if(n3 != n1 and n3 != n2 and n3>=1 and n3<=4)
						{
							printf("Introduzca el cuarto valor: ");
							scanf("%d",&n4);
							while(cont<volver)
							{
								if(n4 != n1 and n4 != n2 and n4 != n3 and n4>=1 and n4<=4)
								{
									printf("Excelente, ahora puedes darle el computador al Jugador #2");
									scanf("%c",&x);
								}
								else
									printf("Error!, Vuelva a introdrucir el valor: ");
									scanf("%d",&n4);
									cont=cont+1;
							}	
						}
						else
							printf("Error!, Vuelva a introdrucir el valor: ");
							scanf("%d",&n3);
							cont=cont+1;
					}	
				}
				else
					printf("Error!, Vuelva a introdrucir el valor: ");
					scanf("%d",&n2);
					cont=cont+1;
			}	
		}
		else
			printf("Error!, Vuelva a introdrucir el valor: ");
			scanf("%d",&n1);
			cont=cont+1;
	}	
}

void leerVariables2()
{
		printf("example");
}

int main (int argc, char * const argv[]) 
{
	introduccionPrograma();
	printf("\n");
	printf("\n");
	instruccionPrograma();
	printf("\n");
	printf("\n");
	reglasPrograma();
	printf("\n");
	printf("\n");
	comenzarPrograma();
	printf("\n");
	printf("\n");
	leerVariables1();
	leerVariables2();
}

Link to comment
Share on other sites

18 answers to this question

Recommended Posts

  • 0

There is too many errors

if(x == y and t ==z)
{...}

should be

if(x == y && t ==z)
{...}

				else
					printf("Error!, Vuelva a introdrucir el valor: ");
					scanf("%d",&n2);
					cont=cont+1;

should be

				else
				   {
					printf("Error!, Vuelva a introdrucir el valor: ");
					scanf("%d",&n2);
					cont=cont+1;
					}

thats just to fix your compiler errors...

Link to comment
Share on other sites

  • 0

Hey ekw, I regret what i said before, i did what you told me and at least its getting more "normal", still it doesnt recognize any code after introducirVariables1(), and after introducing the last variable it starts reproducing thousands of times this message "Excelente, ahora puedes darle el computador al Jugador #2" Can you help me in this one please?

Edited by .Dan
Link to comment
Share on other sites

  • 0
Hey ekw, I regret that i said before, i did what you told me and at least its getting more "normal", still it doesnt recognize any code after introducirVariables1(). Can you help me in this one please?

do you mean leerVariables1()?

it compiles and runs fine, what do you mean it doesn't recognize any code?

Link to comment
Share on other sites

  • 0

Ok, so when i compile, everything WORKS, the program runs, but when i get to the last variable(n4) it starts repeting the message "Excelente, ahora puedes darle el computador al Jugador #2" and it keeps going and never goes to the next function called introducirVariables2(), so thats why im stucked on this one.. if you can, run the app so you can see what im telling you!

Thank You very MUCH!

Link to comment
Share on other sites

  • 0

							while(cont<volver)
							{
								if(n4 != n1 && n4 != n2 && n4 != n3 && n4>=1 && n4<=4)
								{
									printf("Excelente, ahora puedes darle el computador al Jugador #2");
									scanf("%c",&x);
								}
								else
								{
									printf("Error!, Vuelva a introdrucir el valor: ");
									scanf("%d",&n4);
									cont=cont+1;
									}
							}

the error is that you are taking in a character, but it is not updating the while loop or changing the if statement. So it is an infinite while loop since it will never reach the else statement.

Link to comment
Share on other sites

  • 0
So what would you recommend me do in this case??

well i dont see the use of char x, is that just so the second player can hit enter and play?

I never played mastermind before, so i dont know how the logic is supposed to work out.

but, i am assuming that you worked out the logic so that once it reached to

 printf("Excelente, ahora puedes darle el computador al Jugador #2");

it is meant to exit.

If that is the case, all you need to do is insert a break; inside of the if statement.

another issue to bring up, you have volver set to 100, it wont finish the while loop until u have 100 errors

Link to comment
Share on other sites

  • 0

Thanks allot ekw, the thing is that i cant get it to work properly, there is something wrong with the code and none of us can detect the error!

Good Night.. and again Thank You!

If you see something later on, and want to share it with me i will read it tomorrow!

Oh, by the way, this mastermind consist of four variables, and integers from 1 to 4 meaning, 1 2 3 and 4.

User1 introduce the sequence in the order he wants with the only condition being that there cannot be any repeated numbers.

Then the program should do a clear screen and User2 comes.

User2 tries to figure the sequence, and once he does it, he gets a congratulation message.

Here you have my last code!

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <iostream>

int n1,n2,n3,n4,n5,n6,n7,n8;
int cont=0,respuestasAcertadas=0,numeroIntentos=0,volver=10;
char x,y;

void introduccionPrograma()
{
	printf("Bienvenidos a Master Mind, presione enter para continuar!");
	scanf("%c",&x);
}

void instruccionPrograma()
{
	printf("A continuacion, usted jugara a Master Mind. Este juego consta de \ndos partes:\nUna en la cual el Jugador #1 introduce 4 numeros del 1 al 4 al azar \nY otra parte en la cual el Jugador #2 trata de adivinar la secuencia\nintroducida por el Jugador #1.");
	scanf("%c",&x);
}

void reglasPrograma()
{
	printf("Las reglas son: ");
	printf("\n");
	printf("1) Introducir unicamente numeros enteros del 1 al 4");
	printf("\n");
	printf("2) No repetir numeros al momento de intruducir las combinaciones");
	printf("\n");
	printf("3) No ver la pantalla de su companero al momento que el mismo introduzca \nlas combinaciones");
	printf("\n");
	printf("4) Disfrutar del juego!!");
}

void comenzarPrograma()
{
	printf("Presiona enter cuanto esten listos para comenzar...");
	scanf("%c",&x);
}

void leerVariables1()
{
	printf("Introduzca el primer valor: ");
	scanf("%d",&n1);

	while(cont<volver)
	{
		if(n1>=1 && n1<=4)
		{
			printf("Introduzca el segundo valor: ");
			scanf("%d",&n2);

			while(cont<volver)
			{
				if(n2 != n1 && n2>=1 && n2<=4)
				{
					printf("Introduzca el tercer valor: ");
					scanf("%d",&n3);

					while(cont<volver)
					{
						if(n3 != n1 && n3 != n2 && n3>=1 && n3<=4)
						{
							printf("Introduzca el cuarto valor: ");
							scanf("%d",&n4);
							while(cont<volver)
							{
								if(n4 != n1 && n4 != n2 && n4 != n3 && n4>=1 && n4<=4)
								{
									printf("Excelente, ahora proceda a darle el cumputador al Jugador#2");
									break;
								}
								else
								{
									printf("Error!, Vuelva a introdrucir el valor: ");
									scanf("%d",&n4);
									cont=cont+1;
								}
							}	
						}
						else
						{
							printf("Error!, Vuelva a introdrucir el valor: ");
							scanf("%d",&n3);
							cont=cont+1;
						}
					}	
				}
				else
				{
					printf("Error!, Vuelva a introdrucir el valor: ");
					scanf("%d",&n2);
					cont=cont+1;
				}
			}	
		}
		else
		{
			printf("Error!, Vuelva a introdrucir el valor: ");
			scanf("%d",&n1);
			cont=cont+1;
		}
	}	
}

void leerVariables2()
{
	printf("Hello World!");
}

int main (int argc, char * const argv[]) 
{
	introduccionPrograma();
	printf("\n");
	printf("\n");
	instruccionPrograma();
	printf("\n");
	printf("\n");
	reglasPrograma();
	printf("\n");
	printf("\n");
	comenzarPrograma();
	printf("\n");
	printf("\n");
	leerVariables1();
	leerVariables2();
}

Edited by .Dan
Link to comment
Share on other sites

  • 0
Thanks allot ekw, the thing is that i cant get it to work properly, there is something wrong with the code and none of us can detect the error!

Good Night.. and again Thank You!

If you see something later on, and want to share it with me i will read it tomorrow!

Oh, by the way, this mastermind consist of four variables, and integers from 1 to 4 meaning, 1 2 3 and 4.

User1 introduce the sequence in the order he wants with the only condition being that there cannot be any repeated numbers.

Then the program should do a clear screen and User2 comes.

User2 tries to figure the sequence, and once he does it, he gets a congratulation message.

Hmm sounds easy, but the way your approaching it is very convoluted (variables in spanish doesn't help also haha!)

heres my implementation, feel free to use it or modify it!

#include <stdio.h>
#include <stdlib.h>
void clearScreen()
{
	//whatever you want to do
}
int checkSequence(int * sequence, int i)
{
	int j;
	for(j = 0; j < i; j++)
	{
		if(sequence[j]==sequence[i])
			return 1;
	}
	return 0;
}
void starting_player(int * sequence, int sequenceLength)
{
	int i;
	printf("<Player 1 Begins>\n");
	for(i = 0; i < sequenceLength;i++)
	{
	printf("Enter number %d: ",i+1);
	scanf("%d",sequence+i);
	while(checkSequence(sequence,i))
	{
		printf("[Error 000: Number already Exists!]\n");
		printf("Re-enter number %d: ", i+1);
		scanf("%d",sequence+i);
	}
	}	
}
void ending_player(int * sequence, int sequenceLength)
{
	int i = 0;
	int guess;
	printf("<Player 2 Begins>\n");
	while(i < sequenceLength)
	{
	printf("Guess number %d: ",i+1);
	scanf("%d", &guess);
	if(sequence[i] == guess)
		i++;
	else
	{
		printf("WRONG!\n");
		i=0;//start over
	}
	}
	printf("Congratulations!! You have cracked the sequence\n");
}
int main()
{
	int playAgain = 1;
	int sequenceLength;
	while(playAgain)
	{
	printf("Enter length of sequence: ");
	scanf("%d", &sequenceLength);
	int sequence[sequenceLength];
	starting_player(sequence, sequenceLength);
	clearScreen();
	ending_player(sequence, sequenceLength);
	printf("Do you wish to play again?\n");
	printf("0: no\n");
	printf("1: yes\n");
	scanf("%d",&playAgain);
	}

}

it has some basic functionality, feel free to ask if you have any questions regarding my code.

good night!

Link to comment
Share on other sites

  • 0

OMG! ekw, your code is running wonderfull, now, if I also want to restrict the numbers that the user introduces from 1 to 4 ONLY, what should i do?

Link to comment
Share on other sites

  • 0
OMG! ekw, your code is running wonderfull, now, if I also want to restrict the numbers that the user introduces from 1 to 4 ONLY, what should i do?

I'm so happy right now!! I actually did IT!! Thanks allot ekw, you show me the path to accomplish my goal!!

Thanks Allot!!

EDIT:

Well kind of.. i would like to know if there is a way to repeat as many times as necessary this two functions:

while(verificarSecuencia(secuencia,n))
		{
			printf("[Error 000: Numero ya Existe!]\n");
			printf("Re-introduzca el Numero %d: ", n+1);
			scanf("%d",secuencia+n);
		}
	while(verificarSecuencia2(secuencia,n))
		{
			printf("[Error 001: Numero excede el Limite!]\n");
			printf("Re-introduzca el Numero %d: ", n+1);
			scanf("%d",secuencia+n);
		}

Edited by .Dan
Link to comment
Share on other sites

  • 0
Well kind of.. i would like to know if there is a way to repeat as many times as necessary this two functions:

while(verificarSecuencia(secuencia,n))
		{
			printf("[Error 000: Numero ya Existe!]\n");
			printf("Re-introduzca el Numero %d: ", n+1);
			scanf("%d",secuencia+n);
		}
	while(verificarSecuencia2(secuencia,n))
		{
			printf("[Error 001: Numero excede el Limite!]\n");
			printf("Re-introduzca el Numero %d: ", n+1);
			scanf("%d",secuencia+n);
		}

When do you want to repeat them?

As for the "guessing" do you want to limit the number of times they can guess?

Link to comment
Share on other sites

  • 0

I want to repeat them everytime the users assigns a number for the sequence, this is because in all the sequence numbers cant be repeated and they cant be more than 4 and less than 1, i already edited your code for the program to satisfy this condition, but when I assing the same number twice the program doesnt recognizes the error.

Here is my new code:

#include <stdio.h>
#include <stdlib.h>

int volverJugar = 1;
int comenzar = 1;
int largoSecuencia=4;
int m;
int n;
int adivinar;

void pantallaIntroduccion()
{
	printf("< Bienvenido a MasterMind >");
	printf("\n");
	printf("El juego consiste en:");
	printf("\n");
	printf("< Jugador 1 >");
	printf("\n");
	printf("   - Introducir una secuencia de numeros enteros del 1 al 4. ");
	printf("\n");
	printf("   - Los numeros de la secuencia no pueden ser repetidos. ");
	printf("\n");
	printf("< Jugador 2 >");
	printf("\n");
	printf("   - Adivinar en el menor numero de turnos la secuencia introducida por el \nJugador 1. ");
	printf("\n");
	printf("\n");
	printf("< Listos para comenzar? >");
	printf("\n");
	printf("Presiona la Tecla < 1 > y < Enter > : ");
	scanf("%d",&comenzar);
}

int verificarSecuencia(int * secuencia, int n)
{
	for(m = 0; m < n; m++)
	{
		if(secuencia[m]==secuencia[n])
			return 1;
	}
	return 0;
}

int verificarSecuencia2(int * secuencia, int n)
{
	for (n > 1; n < 4; n++)
	{
		if(secuencia[n]>=1 && secuencia[n]<=4)
			return 0;
	}
	return 1;
}

void primerJugador(int * secuencia, int largoSecuencia)
{
	printf("< Comienza Jugador 1 >\n");
	for(n = 0; n < largoSecuencia; n++)
	{
		printf("Introduzca el numero %d: ",n+1);
		scanf("%d",secuencia+n);
		while(verificarSecuencia(secuencia,n))
		{
			printf("[Error 000: Numero ya Existe!]\n");
			printf("Re-introduzca el Numero %d: ", n+1);
			scanf("%d",secuencia+n);
		}
		while(verificarSecuencia2(secuencia,n))
		{
			printf("[Error 001: Numero excede el Limite!]\n");
			printf("Re-introduzca el Numero %d: ", n+1);
			scanf("%d",secuencia+n);
		}
		while(verificarSecuencia(secuencia,n))
		{
			printf("[Error 000: Numero ya Existe!]\n");
			printf("Re-introduzca el Numero %d: ", n+1);
			scanf("%d",secuencia+n);
		}
		while(verificarSecuencia2(secuencia,n))
		{
			printf("[Error 001: Numero excede el Limite!]\n");
			printf("Re-introduzca el Numero %d: ", n+1);
			scanf("%d",secuencia+n);
		}
	}
}

void segundoJugador(int * secuencia, int largoSecuencia)
{
	int n = 0;
	printf("< Comineza Jugador 2 >\n");
	while(n < largoSecuencia)
	{
		printf("Adivine el numero %d de la secuencia: ",n+1);
		scanf("%d", &adivinar);
		if(secuencia[n] == adivinar)
			n++;
		else
		{
			printf("INCORRECTO!\n");
			n=0; //Vuelve a Comenzar.
		}
	}
	printf("Felicidades!! Usted adivino la Secuencia\n");
}

int main (int argc, char * const argv[]) 
{
	pantallaIntroduccion();
	printf("\n");
	printf("\n");
	int secuencia[largoSecuencia];
	while(volverJugar)
	{
		primerJugador(secuencia, largoSecuencia);
		system("CLEAR");
		segundoJugador(secuencia, largoSecuencia);
		printf("\n");
		printf("Desea volver a Jugar?\n");
		printf("0: No\n");
		printf("1: Si\n");
		scanf("%d",&volverJugar);
		system("CLEAR");
	}
}

What I want to do is repeat the errors as many times as possible so the user cant assign any repeated number and any number that is less than one and more than 4.

Thanks Again!

EDIT:

I also noticed than when playing again, the condition that it needs to be more than 4 and less than 1 doesnt works.

Edited by .Dan
Link to comment
Share on other sites

  • 0
EDIT 2:

Sorry more than 1 and less than 4.

if you want the numbers to be between 1 to 4, just change checkSequence() to check if its between 1 to 4

int checkSequence(int * sequence, int i)
{
	int j;
	for(j = 0; j < i; j++)
	{
		if(sequence[i] >=4 || sequence[i] <=1||sequence[j]==sequence[i])
			return 1;
	}
	return 0;
}
...
	while(checkSequence(sequence,i))
	{
		printf("[Error 000: Number already Exists and\or not between 1 and 4]\n");
		printf("Re-enter number %d: ", i+1);
		scanf("%d",sequence+i);
	}

if you wish to make it more specific, do a return 2 if its <= 1 return 3 if its >=4

and change the while to do something like this

while(i = checkSequence(sequence,i))
{
	   if(i == 1)
		  ...
	   if(i==2)
		  ...
etc
}

Link to comment
Share on other sites

  • 0

Nice one ekw!

Been watching this thread and its reminded me of the old days here at Neowin (sadly long gone), genuine help and patience with the OP. Good on ya in a difficult situation, and very clever to boot :)

Link to comment
Share on other sites

  • 0

I wouldn't say those days are long gone but I think there's more of a 'google it for yourself' mentality. There's still plenty of helpful people on Neowin although some of the OPs could do a better job of asking for help, especially in the programming forum.

Simply listing unformatted code and stating it doesn't work doesn't put me in the most helpful frame of mind.

Link to comment
Share on other sites

  • 0

ekw, you are the best men! thank you very much for your time and support, this couldnt have been accomplished without your help! KUDOS to you my friend!!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.