Writing a program to play this game?

In summary, the conversation is about a game where two players pick a four-digit number with no digit repetition and take turns guessing each other's number. The first player to guess the other's number correctly wins. The conversation also discusses the development of a program to play this game and the challenges faced in creating an efficient algorithm and evaluation function. Possible solutions and improvements are suggested, such as using a lookup table and reducing the complexity of the game tree.
  • #1
Millennial
296
0
Recently, I have came across an interesting game that can be played fairly easily between two people. Here is how it is played: Both you and your opponent pick a four digit integer with no digit repetition. Then, you perform specific "guesses" so as to determine what their number is. A guess is a four digit integer with no digit repetition as well. Your opponent compares your guess with his own number, and the result he gives you contains one -1 for each digit that both his number and your guess contains, but not in the same place; while it contains one +1 for each digit that both his number and your guess contains in the same place. As an example, assume your opponent's number is 1234 and your guess is 2135. 1,2 and 3 are in both the number and the guess, and among them, only 3 is contained in the same place in both numbers. Hence, the information your opponent gives you about this guess will be -2 +1 (3 digits common, the place of one digit is common as well). Note that you don't take the sum of these and you type it out as -2 +1, - and + is nothing more than notation. It could be A2B1, but I prefer using - and +.

After playing it for a while, the idea occurred to me to write a program that plays this game. I don't mean a program that picks a number and has you guess it, I mean a program that tries to guess what you picked. I have already written such a program, but there are problems with its speed such that I had to make some compromises to run it at acceptable speeds (maximum of 3-4 seconds per guess by the computer.) Given the game's apparent simplicity, I have been searching for ways to make the algorithm more efficient; but these ways have eluded me so far.

The algorithm I currently use performs an exhaustive search over the vector of currently possible numbers, using an evaluation function on each and choosing one out of random from the ones with the maximum score. The first guess is taken to be random automatically; because even though guessing a number with a 0 in it gives a higher score, a player that knows the first guess will have a 0 in it can exploit this by picking a number that lowers efficiency of the guess. After the program is given the results for the guess, it eliminates the numbers not satisfying that guess from the vector of currently possible numbers.

The main problem has been the evaluation function itself. It is the most natural evaluation function I could think of, and it is basically the average count of numbers this particular guess would eliminate. This is calculated by considering each individual possible results of the guess (there are 15) and calculating the value of (numbers eliminated by this result) * (numbers remaining after the result) / (current count of possible numbers), and summing them all. The count of numbers remaining after this particular result is equal to the numbers giving this result out of the currently possible numbers, and is necessary to weight the average accordingly. This particular evaluation function relies on the ability to find the number of numbers that will be remaining after a particular result, which is also a check over the current vector of possible numbers. If no sort of optimization or compromise is used here, the second move can take up to 3-4 minutes to make; as the computer has to perform over one billion runs of the function that checks if a particular number satisfies a particular result.

Can anyone think of a better algorithm or a better evaluation function than the ones I am using? A better algorithm is important for the more general case I am considering, where you pick x-digit numbers in base y and x < y with no digit repetition, and try to find the number using the same method as this game. While the program's playing strength currently is certainly a lot higher than mine, a generalization demands a better algorithm to avoid outrageous runtime.

Edit: I have described the algorithm verbally instead of posting the code, so anybody regardless of the programming languages they have knowledge of may state their opinions/suggestions.
 
Technology news on Phys.org
  • #2
Let me see if I understand this. You pick a number, say 1234. Does the computer pick a number at random, say 2135, and ask you to enter a string that shows correct digits and correct places, which would be -2 + 1 in this case?

What does the computer do next? How does it have a list of currently possible numbers? Is it all of the four digit numbers with a 1 in any position, a 2 in any position, a 3 in any position, or a 5 in any position?
 
  • #3
A minor detail first: there are just 14 possible results. -1+3 does not work: the 4th correct digit has to be at the right place as well.
Is 0 as first digit allowed? For simplicity I assume it is.

I don't understand why/where you want to perform billions of runs. Searching the whole game tree is certainly expensive, but you just have to do it (at most) once - you can store the result to automate at least the first 2, probably the first 3 or even all guesses. This reduces the complexity a lot.

It is certainly useful to randomize the first guess, but you can adjust the following guesses accordingly. This is probably clearer with an example:
Imagine your opponent would choose random numbers, so you don't have to randomize. Your first guess can be 0123. Then you can have a lookup table "if the result is -2 +1, then continue with guess 0256" (random example).
If you have to randomize, and your first guess becomes 7498, then you can replace every "0" in your original table by "7", every "1" by "4" and so on. If the result is -2+1, your next guess would be something like 7931 (where 3 and 1 are random again). You keep the structure, but not the numbers.
 
  • #4
It seems to me the game you describe is the same as the board game "Master Mind", which just uses colored pegs instead of numbers, so if you are looking for how other people have "solved" the game you describe you may want to include "master mind" in your search as well.
 
  • #5


I find this game and the idea of writing a program to play it very interesting. It is a great way to explore different algorithms and optimization techniques.

One suggestion I have is to consider using a heuristic search algorithm, such as A* or Monte Carlo tree search, instead of an exhaustive search. These types of algorithms can be more efficient and can often find good solutions faster than exhaustive search.

In terms of the evaluation function, it seems like you are already using a reasonable approach. However, you may want to consider incorporating some domain knowledge into the function. For example, you could give higher weights to certain digits or patterns that are more likely to occur in the number. This could potentially improve the performance of the algorithm.

Another idea is to use machine learning techniques to train a model that can predict the next best guess based on previous guesses and their results. This could potentially lead to a more efficient and accurate algorithm.

Overall, I think there are many potential avenues to explore in terms of improving the algorithm for this game. It will require a combination of creativity, domain knowledge, and technical skills. I wish you the best of luck in your research and development!
 

1. How do I begin writing a program to play a game?

To begin writing a program to play a game, you will first need to understand the rules and mechanics of the game. Then, you can outline the steps needed to play the game and start writing your code.

2. What programming language should I use to create a game-playing program?

The choice of programming language depends on the game and your personal preference. Some popular languages for game development include Java, C++, Python, and Unity.

3. How do I incorporate artificial intelligence into my game-playing program?

Incorporating artificial intelligence into a game-playing program requires knowledge of algorithms and data structures. You will need to determine the best approach for the specific game and implement it using the chosen programming language.

4. How do I test and improve my game-playing program?

To test and improve your game-playing program, you can use techniques such as unit testing, playtesting, and gathering feedback from other programmers. Additionally, continuously analyzing and optimizing your code can help improve its performance.

5. Are there any resources or tools available to help with creating a game-playing program?

Yes, there are various resources and tools available to help with creating a game-playing program. These include game development engines, online tutorials and courses, and forums where you can seek advice and guidance from other programmers.

Similar threads

  • Precalculus Mathematics Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
775
  • Programming and Computer Science
Replies
30
Views
4K
Replies
9
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
14
Views
929
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
394
Replies
29
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
Back
Top