2020-12-20 01:53:52 +01:00
|
|
|
"""
|
|
|
|
| |
|
|
|
|
-|-|-
|
|
|
|
| |
|
|
|
|
-|-|-
|
|
|
|
| |
|
|
|
|
|
|
|
|
"""
|
2020-12-23 23:37:04 +01:00
|
|
|
import random
|
2020-12-20 01:53:52 +01:00
|
|
|
|
2020-12-20 02:13:38 +01:00
|
|
|
class TicTacToe:
|
2020-12-23 23:37:04 +01:00
|
|
|
def __init__(self):
|
2020-12-24 00:33:54 +01:00
|
|
|
self.player1 = Player("X", "Player 1")
|
|
|
|
self.player2 = Player("O", "Player 2")
|
2020-12-23 23:37:04 +01:00
|
|
|
self.board = Board()
|
2020-12-23 20:21:46 +01:00
|
|
|
|
|
|
|
def run(self):
|
2020-12-23 23:37:04 +01:00
|
|
|
while True:
|
|
|
|
self.board.make_move(self.player1.token, self.player1.make_random_move(self.board.emptyFields))
|
|
|
|
self.board.print()
|
2020-12-24 00:33:54 +01:00
|
|
|
if self.board.isWin(self.player1.token, self.player1.name):
|
2020-12-23 21:46:09 +01:00
|
|
|
break
|
|
|
|
|
2020-12-23 23:37:04 +01:00
|
|
|
self.board.make_move(self.player2.token, self.player2.make_random_move(self.board.emptyFields))
|
|
|
|
self.board.print()
|
2020-12-24 00:33:54 +01:00
|
|
|
if self.board.isWin(self.player2.token, self.player2.name):
|
2020-12-23 21:46:09 +01:00
|
|
|
break
|
2020-12-23 20:21:46 +01:00
|
|
|
|
|
|
|
class Board:
|
2020-12-20 02:13:38 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.state = list(" "*9)
|
2020-12-23 23:37:04 +01:00
|
|
|
self.emptyFields = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
2020-12-20 01:53:52 +01:00
|
|
|
|
2020-12-20 02:13:38 +01:00
|
|
|
def print(self):
|
|
|
|
board = ""+ self.state[0] +"|"+self.state[1] +"|"+self.state[2] +" \n-|-|-\n"+self.state[3] +"|"+self.state[4] +"|"+self.state[5] +"\n-|-|-\n"+self.state[6] +"|"+self.state[7] +"|"+self.state[8] +"\n"
|
|
|
|
print(board)
|
|
|
|
|
|
|
|
def make_move(self, token, field):
|
|
|
|
if self.state[field] == " ":
|
|
|
|
self.state[field] = token
|
2020-12-23 23:37:04 +01:00
|
|
|
self.emptyFields.remove(field)
|
2020-12-20 02:13:38 +01:00
|
|
|
else:
|
|
|
|
print("This field's taken.")
|
|
|
|
|
2020-12-24 00:33:54 +01:00
|
|
|
def isWin(self, token, name):
|
2020-12-23 21:46:09 +01:00
|
|
|
if((self.state[0] == token and self.state[1] == token and self.state[2] == token) or
|
|
|
|
(self.state[0] == token and self.state[4] == token and self.state[8] == token) or
|
|
|
|
(self.state[0] == token and self.state[3] == token and self.state[6] == token) or
|
|
|
|
(self.state[1] == token and self.state[4] == token and self.state[7] == token) or
|
|
|
|
(self.state[2] == token and self.state[5] == token and self.state[8] == token) or
|
|
|
|
(self.state[2] == token and self.state[4] == token and self.state[6] == token) or
|
|
|
|
(self.state[3] == token and self.state[4] == token and self.state[5] == token) or
|
|
|
|
(self.state[6] == token and self.state[7] == token and self.state[8] == token)):
|
2020-12-24 00:33:54 +01:00
|
|
|
print(name + " has won.")
|
2020-12-23 21:46:09 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-12-23 20:21:46 +01:00
|
|
|
def get_state():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_action():
|
|
|
|
pass
|
|
|
|
|
2020-12-20 21:46:22 +01:00
|
|
|
class Player:
|
2020-12-24 00:33:54 +01:00
|
|
|
def __init__(self, token, name):
|
2020-12-20 21:46:22 +01:00
|
|
|
self.token = token
|
2020-12-24 00:33:54 +01:00
|
|
|
self.name = name
|
2020-12-23 23:37:04 +01:00
|
|
|
|
|
|
|
def make_random_move(self, emptyFields):
|
|
|
|
return random.choice(emptyFields)
|
2020-12-20 21:46:22 +01:00
|
|
|
|
|
|
|
|
2020-12-20 02:13:38 +01:00
|
|
|
|
|
|
|
game = TicTacToe()
|
2020-12-23 20:21:46 +01:00
|
|
|
game.run()
|
2020-12-20 02:13:38 +01:00
|
|
|
|
2020-12-23 21:00:38 +01:00
|
|
|
|