[DOCKER] Play with Othello (Reversi)

What is this?

Let's play Othello in Python.

Board initialization and display

The board will be held in a one-dimensional array of size 64, and the values will be as follows.

--0: Free space ―― 1: Kurokoma (expressed by *) ―― 2: Shirakoma (expressed by o)

python3


import numpy as np
def create_board():
    a = np.zeros(64, dtype=int)
    a[27] = a[36] = 1
    a[28] = a[35] = 2
    return a
def print_board(a):
    print('  a b c d e f g h')
    for i in range(8):
        print(i+1, end=' ')
        print(' '.join('.*o'[j] for j in a[i*8:][:8]))

a = create_board()
print_board(a)
>>>
  a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . * o . . .
5 . . . o * . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .

Place a piece

Create a function that puts its own piece w (1 or 2) at position p (0 to 63). Since the eight vertical, horizontal, and diagonal data are one-dimensional data, look in the direction deviated from the current position by [-9, -8, -7, -1, 1, 7, 8, 9]. This is checked with a combination of [-1, 0, 1] and [-8, 0, 8](excluding 0 and 0 pairs). If you take out the array (b) for each direction and multiply by 0-1 of whether it is the opponent's piece (b == 3-w) cumulatively, the pieces that can be taken are represented by 1, so if you take the sum (sum) , You can see the number of pieces n that can be taken. Let's put it in position 20 ("e3").

python3


def put_piece(a, p, w, puton=True, chk=True):
    t, x, y = 0, p%8, p//8
    for di, fi in zip([-1, 0, 1], [x, 7, 7-x]):
        for dj, fj in zip([-8, 0, 8], [y, 7, 7-y]):
            if not di == dj == 0:
                b = a[p+di+dj::di+dj][:min(fi, fj)]
                n = (b==3-w).cumprod().sum()
                if b.size <= n or b[n] != w: n = 0
                t += n
                if puton:
                    b[:n] = w
    if puton:
        if chk: assert(a[p] == 0 and t > 0)
        a[p] = w
    return t
put_piece(a, 20, 1)
print_board(a)
>>>
  a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . . * . . .
4 . . . * * . . .
5 . . . o * . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .

Find the best position

To find a good hand, add the following points with appropriate weight.

――You can get a lot. ――There are fewer places to hit the opponent

python3


def best(a, w):
    from math import exp
    r, b, c = [], a.copy(), 1+exp(-np.count_nonzero(a)/16)
    for i in range(64):
        if b[i] != 0: continue
        t = put_piece(b, i, w, True, False)
        if t == 0:
            b[i] = 0
            continue
        u = sum(b[j]==0 and put_piece(b, j, 3-w, False) > 0 for j in range(64))
        r.append((t-c*u+np.random.rand()*0.5, i))
        b = a.copy()
    return sorted(r)[-1][1] if r else -1

Try to play

p to pass, q to end.

python3


if __name__ == '__main__':
    a = create_board()
    w = 1
    while np.count_nonzero(a) < 64:
        print_board(a)
        s = input('> ')
        if not s or s=='q': break
        if s != 'p':
            try:
                x, y = ord(s[0])-97, int(s[1])-1
                put_piece(a, x+8*y, w)
            except:
                continue
        p = best(a, 3-w)
        if p >= 0:
            put_piece(a, p, 3-w)
    print_board(a)
    n1, n2 = (a==1).sum(), (a==2).sum()
    print('%d - %d %s' % (n1, n2,
        'You win' if n1 > n2 else
        'You lose' if n1 < n2 else 'Draw'))
>>>
  a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . * o . . .
5 . . . o * . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
> e3
  a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . . * o . .
4 . . . * o . . .
5 . . . o * . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
> f4
  a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . . * o . .
4 . . . * * o . .
5 . . . o o o . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
> g3
  a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . . * * * .
4 . . o o o o . .
5 . . . o o o . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
> c5
  a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . o * * * .
4 . . o o o o . .
5 . . * o o o . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
> 

You should read it a little further.

I prepared Docker

bash


docker run -it --rm tsutomu7/reversi python reversi.py

When doing it with a browser, please do as follows.

bash


firefox http://localhost:8888 &
docker run -it --rm -p 8888:8888 tsutomu7/reversi

that's all

Recommended Posts

Play with Othello (Reversi)
Play with Prophet
Play with 2016-Python
Play with CentOS 8
Play with Pyramid
Play with Fathom
Let's play with 4D 4th
Let's play with Amedas data-Part 1
Play with reinforcement learning with MuZero
Play with push notifications with imap4lib
Let's make Othello with wxPython
Let's play with Amedas data-Part 4
Play with Jupyter Notebook (IPython Notebook)
[Python] Play with Discord's Webhook.
Play RocketChat with API / Python
Let's play with Amedas data-Part 3
Let's play with Amedas data-Part 2
Play with ASE MD module
Othello made with python (GUI-like)
Play with A3RT (Text Suggest)
Play with numerical calculation of magnetohydrodynamics
Play with Poincare series and SymPy
Let's make Othello AI with Chainer-Part 1-
Let's play with Excel with Python [Beginner]
Play with Pythonista UI implementation [Action implementation]
Play with PIR sensor module [DSUN-PIR]
Othello game development made with Python
Play around with Linux partitions ~ Continued ~
Let's make Othello AI with Chainer-Part 2-
Spark play with WSL anaconda jupyter (2)
Write Reversi AI with Keras + DQN
Play with Turtle on Google Colab
Play with demons because it's setsubun
Play video with sound with python !! (tkinter / imageio)
[Introduction to WordCloud] Let's play with scraping ♬
Play audio files from Python with interrupts
Play handwritten numbers with python Part 2 (identify)
Play like a web app with ipywidgets
Play around with the pythonista3 ui module
[Complement] [PySide] Let's play with Qt Designer
Fractal to make and play with Python
I want to play with aws with python
Play with Pythonista UI implementation [Screen elements]
Play with MoleculeNet's PDBBind and DeepChem's RDKitGridFeaturizer
Play audio files with interrupts using PyAudio
Play with puns using the COTOHA API
Load csv with pandas and play with Index
Othello app (iOS app) made with Python (Kivy)