from ConsolePlay import TTTConsole
import numpy as np
from copy import deepcopy
from reinforcement.MinMaxAct import minMaxAct
def checkReach(board, Is_me = True):
board = board.copy
lines = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]])
if not Is_me:
board *= -1
for check_line in lines:
line_state = np.array([board[i] for i in check_line])
# 消え始めてから
if np.any(line_state == 2) and np.any(line_state == 3):
index = np.argmin(line_state)
if board[check_line[index]] == 0:
return check_line[index]
# 序盤にリーチしてたとき
if np.any(line_state == 1) and np.any(line_state == 2) and not np.any(board == 3):
index = np.argmin(line_state)
if board[check_line[index]] == 0:
return check_line[index]
return None
def HumanTurn(board):
pos = 0
while True:
print("どこに置きますか? (1:左上 - 9:右下): ")
pos = int(input())
if 1 <= pos <= 9 and board[pos - 1] == 0:
break
return pos - 1
def RandTurn(board):
pos = 0
while True:
pos = np.random.randint(0, 8)
if board[pos] == 0:
break
return pos
def SimpleCPU(board):
win_pos = checkReach(board, Is_me=True)
if win_pos is not None:
return win_pos
difencive_pos = checkReach(board, Is_me=False)
if difencive_pos is not None:
return difencive_pos
while True:
pos = np.random.randint(0, 8)
if board[pos] == 0:
break
return pos
if __name__ == '__main__':
play = TTTConsole(HumanTurn, RandTurn, Is_shown=True)
play.Run()