Mastermind with php

Solved
renjie -  
 k.hicham -
Salam alaykum

I want to program the game mastermind in PHP. It has 3 levels
1-> Beginner: which does not allow the repetition of numbers (the 4 numbers must be distinct)
2-> Expert: which allows the repetition of numbers
3-> Intelligent: the possibility of leaving a case empty
But I haven't found the functions that can help me in this script to allow the repetition of values as well as the possibility of an empty case.

Thank you in advance for helping me,
Configuration: Windows XP Internet Explorer 7.0

14 réponses

renjie
 

no one wants to help me
4
fiddy Posted messages 441 Registration date   Status Contributeur Last intervention   1 847
 
Hi

Sorry, I didn't see your post.
I'm not giving you the PHP syntax, but a method to choose 4 distinct digits.
I assume there are 6 different colors (0,1,2,3,4,5).
You create an array of 6 elements initialized to 0, 1, 2, 3, 4, 5.
You shuffle this array by swapping randomly chosen indices. And you take the first 4 elements of the array.

If you want 4 digits that can be identical, you draw a random number. For the empty slot, you can arbitrarily choose 7 as the empty slot. If the randomly drawn number is 7, then you initialize the slot to null.

I hope I answered your question

Have a good evening
--

Google is your friend
1
renjie
 
salamoelikom

thank you very much for your help
for the 4 digits it is mandatory to use rand() which chooses the numbers by default without me knowing them (the computer chooses them) so the method you gave me is useless in this case,
and for the empty field I will try and show you the result

thanks again for your response
0
fiddy Posted messages 441 Registration date   Status Contributeur Last intervention   1 847
 
Yes, rand() chooses numbers randomly.
But you said that in the beginning, all the numbers need to be different. With rand(), there's nothing to assure you that you'll only draw different numbers.

--

Google est votre ami
0
kab_hicham
 
salam:
Hello, Renjie & Fiddy;
it's true that rand() does not necessarily generate different numbers, but we can use a little 'trick' to test if we have two variables or more with the same value, if so:

ALGO:
each time rand() generates a value we store it in the array,
we initialize the counter c to 0,
and we check by going through all the previous elements of the array if we have an identical value that already exists to this drawn value.
we increment the counter c,
and finally if the counter has been incremented
we decrement the index i to redo the rand().

PHP:
srand();
// echo "launch rand";
for($i=0; $i<4; $i++)
{
$haz[$i]=rand(0,9);
$c=0;
for($j=0; $j<$i; $j++)
{
if($haz[$i]==$haz[$j])
$c++;
}
if($c>0) $i--;
}

I hope my 'trick' is understandable.
See you soon ------ k.hicham ------ESTE
0
fiddy Posted messages 441 Registration date   Status Contributeur Last intervention   1 847
 
Hello kab_hicham,

Yes, it seems to be the most logical method. However, the problem with this one is that it’s not a good habit to adopt when there are a lot of numbers to draw. Indeed, the compiler might end up picking the same numbers multiple times. Furthermore, it’s an algorithm that doesn’t have good complexity.
The method of initializing an array and shuffling it by index has a complexity of O(n).

Best regards
--

Google is your friend
0
kab_hicham
 
Salam,
hello Fiddy;

I understand well what you mean ("we initialize the arrays with four digits and mix them by index") but yet you haven't understood the goal of the game, the numbers drawn should not be initialized by the programmer, only the machine has the right to randomly choose the four digits in the range [0-9].


For our case, we only have 4 digits to draw, which makes the probability of falling on already existing digits very small.

I hope you have already played this type of game "Mastermind" to fully understand the principle. You said it

google is your friend, have fun with PHP
see you soon ------ k.hicham ------ESTE
0
fiddy Posted messages 441 Registration date   Status Contributeur Last intervention   1 847
 
Hi kab_hicham,
I think you didn't understand my algorithm well then.

You initialize an array. The program shuffles it by taking two indices at random. And we perform several iterations.
The chosen values are therefore also random values.

Best regards
--

Google is your friend
0
renjie
 
salamoelikom;

hello hicham and fiddy thank you for your contributions I think the best method to avoid repeating the values chosen by rand is to put them in a file and that way we can keep the same values.
0
fiddy Posted messages 441 Registration date   Status Contributeur Last intervention   1 847
 
Hi

renjie. Uh, with your solution, I don't see where the random aspect is. Unless I didn't understand what you meant.

Best regards
--

Google is your friend
0
renjie
 
salamoelikom;

I am talking about the repetitions of values that each time the player repeats the game (before they exceed 10 times), the values chosen by rand() must remain the same (it's not the same problem as the repetition of numbers) but still it is the solution to Mastermind to avoid the change of rand() values because every time the program is executed, rand() takes new values.
0
fiddy Posted messages 441 Registration date   Status Contributeur Last intervention   1 847
 
Re,

If you want him to keep the same values, you randomly pick them yourself and put them in your program. After all, putting that in a file just complicates things. But you're the programmer ;)

Best regards
--

Google is your friend
0
renjie
 
salamoelikom,

it's not complicated, it's simple to work with the files, but there is a better solution...
0
k.hicham
 
Salam:

Hello, Renjie & Fidddy;
I initially thought that the discussed issue was about the fact that rand() must draw 4 random digits without repetition. :s

Well, now it's another problem that has arisen, the fact that the four drawn digits must remain the same during the game (10 attempts), yes what Renjie told you:(" to avoid the repetition of values chosen by rand, put them in a file and this will allow us to keep the same values.") is feasible and clear, but you must not ignore that the local file where you are going to put your values must not be accessible by the user, and subsequently your 4 digits will be revealed, and thus we lose the principle of the game. "Master Mind"

For this, I propose two solutions: either you put them in a file but encrypted with one of the functions (md5(), crypt(), etc.), and we have plenty of others.
Otherwise, if you don't want to implement encryption, you need to put your file on a server (hosting) so that you can protect your 4 digits peacefully :).

I hope I stayed objective in my analysis; this is my point of view and it is open to discussion.
See you soon ------ K.Hicham ------ESTE
0