import numpy as np
from TicTacToe import TicTacToe
PLAYER = {
"None": 0,
"First": 1,
"Second": 2
}
JUDGE = {
"None": "none",
"OUT_OF_RANGE": "out_of_range",
"OVERLAP": "overlap",
"WIN": "win",
"DRAW": "draw"
}
class TTTConsole:
def __init__(self, player1s_myturn, player2s_myturn, Is_shown=False):
self.PLAYER_STR = ["", "先手", "後手"]
self.PLAYER_MARK = [" ", "○", "×"]
self.Players_myturn = [None, player1s_myturn, player2s_myturn]
self._ttt = TicTacToe()
self._showNumber = True
self.Is_shown = Is_shown
def Run(self):
if self.Is_shown:
self.ShowTitle()
self.Battle()
if self.Is_shown:
self.ShowResult()
return self._ttt.Judge, self._ttt.Player
def ShowTitle(self):
print("拡張〇×ゲーム")
def Battle(self):
self._ttt.Init()
while True:
if self.Is_shown:
print("")
self.ShowBoard()
print("{}の番".format(self.PlayerStr()))
self._ttt.Set(self.Players_myturn[self._ttt.Player](self._ttt.GetBoard()))
if self.Is_shown:
print("--> {}".format(self._ttt.LastSet + 1))
if self._ttt.Judge != JUDGE["None"]:
break
def ShowBoard(self):
print("--- ターン", self._ttt.Turn, " ---")
flip = -1 if self._ttt.Player == PLAYER["Second"] else 1
for row in range(self._ttt.BOARD_ROWS):
print(" ", end="")
for col in range(self._ttt.BOARD_COLS):
pos = row * self._ttt.BOARD_COLS + col
pIdx = PLAYER["First"] if self._ttt.Board[pos] * flip > 0 else PLAYER["Second"] if self._ttt.Board[pos] * flip < 0 else PLAYER["None"]
print(self.PLAYER_MARK[pIdx], end="")
if self._showNumber:
mark = " " if self._ttt.Board[pos] == 0 else "{}".format(abs(self._ttt.Board[pos]))
print(mark, end="")
if col < self._ttt.BOARD_COLS - 1:
print(" | ", end="")
print("")
if (row < self._ttt.BOARD_ROWS - 1):
if self._showNumber:
print(" -----+------+-----")
else:
print(" ---+----+---")
def ShowResult(self):
msg = ""
if self._ttt.Judge == JUDGE["WIN"]:
msg = "{}の勝利".format(self.PlayerStr())
elif self._ttt.Judge == JUDGE["DRAW"]:
msg = "引き分け"
elif self._ttt.Judge == JUDGE["OUT_OF_RANGE"]:
msg = "{}の反則負け(範囲外)".format(self.PlayerStr())
elif self._ttt.Judge == JUDGE["OVERLAP"]:
msg = "{}の反則負け(重ね置き)".format(self.PlayerStr())
print("")
self.ShowBoard()
print(msg)
def PlayerStr(self):
return self.PLAYER_STR[self._ttt.Player]