-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
66 lines (53 loc) · 1.62 KB
/
play.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Bunaren TicTacToe AI Engine
# Created by Fransiscus Emmanuel Bunaren
# This file is for playing against AI
from engine import Engine
from board import Board
import os
def main():
board = Board()
print("Bunaren TicTacToe AI")
print("========================")
symbol = input("Choose your symbol (X/O) : ").upper()
engine = Engine()
engine.setSymbol("O" if symbol == "X" else "X")
# Computer Starts first if X is chosen by player
if symbol == "O":
board.move(engine.bestMove(board.board))
board.print()
while not board.isOver():
pos = input("Draw " + symbol + " at [row column] : ").split()
pos = list(map(int, pos))
pos = [i-1 for i in pos]
pos = pos[0] * 3 + pos[1]
while not board.isValid(pos):
print("Invalid Position")
pos = input("Draw " + symbol + " at [row column] : ").split()
pos = list(map(int, pos))
pos = [i-1 for i in pos]
pos = pos[0] * 3 + pos[1]
board.move(pos)
if board.isOver():
break
# Computer's Turn
board.move(engine.bestMove(board.board))
board.print()
print()
board.print()
print("\nGame Over!")
result = board.isOver()
if result == "Draw":
print("It's a draw!")
elif result == symbol:
print("You win!")
else:
print("You lose!")
# Play Again?
play_again = input("\nPlay again? [y/n] : ").lower()
if play_again == "y":
os.system("cls")
main()
else:
print("Thanks for playing!")
input("Press enter to quit...")
main()