[Episode 1] Beginners tried Numeron AI with python

Last time (self-introduction, so you don't need to read it) 1st 2nd 3rd Final story

About this article

Let's first explain the flow of playing against Numeron AI. As I explained last time, I'm a beginner with 1 month of programming experience rather than python ... so there may be some deficiencies. We hope you will see it with warm eyes. Also, since I am a beginner myself, I would like to write it in an easy-to-understand manner for other beginners, so please refrain from redundant explanations.

Program flow

1: player and com each set their own 3-digit number

At this time, the player inputs the 3 digits of the player, and the 3 digits of com are randomly determined.

2: The player calls the number (the player must be the first player)

Calls are made by input. After inputting, the following processing is required.

3: Display xEAT, yBITE and the result for it

3.1: If 3EAT, the first move wins

In other words, the process ends there.

3.2: Move the turn to call otherwise to com

It means that the turn changes with each call. (Hereafter, xEAT and yBITE are referred to as x-y)

4: com calls a 3-digit number

The number to be called at this time should be the number that can narrow down the candidates even in the worst result (details will be described later).

5: Display x-y and result for it

5.1: If 3-0, com wins

5.2: Otherwise, move the turn first

Since it is the same process, the explanation is omitted

6: Repeat steps (2) to (5) until 3-0 appears.

It will be the flow.

Then, I will explain "the number that can narrow down the most candidates even in the worst result"

AI description of com

What does "the number that can narrow down the most candidates even in the worst result" mean?

Explanation with a concrete example

When the first call is 012 and 0-1 there are 252 candidates left.

If you call 345

If you call 345 on the second call There are results [2-0], [1-0], [0-2], [0-1], [1-1], [0-0], respectively. 6 ways for 2-0 48 ways for 1-0 18 ways for 0-2 96 ways for 0-1 1-1 is 12 ways 72 ways for 0-0 You can narrow down the number of candidates. In other words, if you call [345] in the second call, you can narrow down to 96 ways even in the worst result. If we call this kind of thing the worst number of candidates The worst candidate for 345 is 96.

If you call 314

If you call 314 on the second call 2-0, 2 ways 30 ways for 1-0 2-0, 2 ways 1-2 is 2 ways 12 ways for 1-1 30 ways for 1-0 80 ways for 0-0 And the worst candidate number is 80.

So if you call 345 and 314 314 has a smaller number of worst candidates ⇒ Excellent call The computer decides to call 314 preferentially. Also, if the worst number of candidates is the same, we will call at random.

Handling exceptions

Of course, the worst number of candidates is not optimal because we are only considering the situation of one minion. However, there are times when we do something that is not optimal without thinking about such difficult things. That is when there are only "123" and "132" remaining candidates I think most humans call one or the other. Lucky if you hit. Even if you miss it, you can get the next correct answer. However, the above AI may call 453. Because 123, 132 and 453 have the worst number of candidates of 1. Therefore, we will include the following processing

Priority of numbers to call

1: The number with the smallest number of worst candidates 2: If there are multiple such things, preferentially call the one that may be 3-0 3: Still random if more than one

I will select the numbers like this. The above is the flow of this program. When you write it in a sentence, you can see the bones of the program and it becomes easier to add meat.

Finally

From the next time, I will write the program concretely. I would like to know a lot more, so I would appreciate your guidance and encouragement.

Recommended Posts

[Episode 2] Beginners tried Numeron AI with python
[Episode 3] Beginners tried Numeron AI with python
[Episode 0] Beginners tried Numeron AI with python
[Episode 1] Beginners tried Numeron AI with python
[Final story] Beginners tried Numeron AI with python
3. 3. AI programming with Python
I tried fp-growth with python
I tried scraping with Python
Make Puyo Puyo AI with Python
Mayungo's Python Learning Episode 1: I tried printing with print
I tried gRPC with Python
I tried scraping with python
Mayungo's Python Learning Episode 3: I tried to print numbers with print
I tried running prolog with python 3.8.2.
python beginners tried to find out
I tried SMTP communication with Python
I tried to solve the ant book beginner's edition with python
Mayungo's Python Learning Episode 7: I tried printing with if, elif, else
INSERT into MySQL with Python [For beginners]
I tried scraping Yahoo News with Python
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
I tried recursion with Python ② (Fibonacci sequence)
[Python] Read images with OpenCV (for beginners)
Mayungo's Python Learning Episode 8: I tried input
WebApi creation with Python (CRUD creation) For beginners
How Python beginners get started with Python with Progete
Build AI / machine learning environment with Python
[For beginners] Try web scraping with Python
#I tried something like Vlookup with Python # 2
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
Mayungo's Python Learning Episode 2: I tried to put out characters with variables
I refactored "I tried to make Othello AI when programming beginners studied python"
I tried to predict next year with AI
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Causal reasoning and causal search with Python (for beginners)
I tried "differentiating" the image with Python + OpenCV
Scraping with Python
Python with Go
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
Python beginners tried to code some energy drinks
I tried L-Chika with Raspberry Pi 4 (Python edition)
Integrate with Python
I tried Jacobian and partial differential with python
I tried to get CloudWatch data with Python
I tried using mecab with python2.7, ruby2.3, php7
AES256 with python
~ Tips for Python beginners from Pythonista with love ① ~
I tried function synthesis and curry with python
Tested with Python
I tried to output LLVM IR with Python
Machine learning beginners tried to make a horse racing prediction model with python
python starts with ()
with syntax (Python)
I tried running faiss with python, Go, Rust
I tried to automate sushi making with python