-
Notifications
You must be signed in to change notification settings - Fork 1
/
tic-tac-toe.py
144 lines (125 loc) · 4.19 KB
/
tic-tac-toe.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'''
********************************
Tic-Tac-Toe GAME
Developer: Austin Joyal
********************************
'''
import sys
import time
import random
global position
position = -1
board = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
wins = ((0,1,2), (3,4,5), (6,7,8),
(0,3,6), (1,4,7), (2,5,8),
(0,4,8), (2,4,6))
# printing the initial board showing positions
def initial_board():
print('This is the view of the board. Note each position.\nFIRST ROW 1->3\nSECOND ROW 4->6\nTHIRD ROW 7->9')
print(' _'*3)
for x in(0,3,6):
print('|' + board[x] + '|' + board[x+1] + '|' + board[x+2] + '|')
print(' =+=+=')
# printing board
def print_board():
dummy = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
for x, i in enumerate(board):
if i == 'X' or i == 'O':
dummy[x] = i
print(' _'*3)
for x in(0,3,6):
print('|' + dummy[x] + '|' + dummy[x+1] + '|' + dummy[x+2] + '|')
print(' =+=+=')
def score(board):
for w in wins:
b = board[w[0]]
if b in 'XO' and all (board[i] == b for i in w):
return b, [i+1 for i in w]
return None
def over():
return all (b in 'XO' for b in board)
def available(board):
return [ b for b in board if b not in 'XO']
def computer_turn(symbol, board):
o = 'O' if symbol =='X' else 'X'
print("\nComputer thinking...")
time.sleep(5)
block = None
options = [int(s)-1 for s in available(board)]
for choice in options:
brd = board[:]
brd[choice] = symbol
if score(brd):
break
if block is None:
brd[choice] = o
if score(brd):
block = choice
else:
choice = block if block is not None else random.choice(options)
board[choice] = symbol
return choice+1
def player_turn(symbol, board):
options = available(board)
while True:
timeout = 10
print("\nYOUR TURN\nPlace {0}. Positions remaining {1} ".format(symbol, ' '.join(options)))
position = input()
if str(position) in options:
break
print( "Wrong input." )
board[int(position)-1] = symbol
return position
def computer(symbol):
print_board()
print('\nComputer selects {}'.format(computer_turn(symbol, board)))
return score(board)
def player(symbol):
print_board()
print('\nYou selected {}'.format(player_turn(symbol, board)))
return score(board)
def first_player(symbol):
print_board()
print('Player1 selects {}'.format(player_turn(symbol, board)))
return score(board)
def second_player(symbol):
print_board()
print('Player2 selects {}'.format(player_turn(symbol, board)))
return score(board)
print(__doc__)
initial_board()
option = input('\nSelect one among the following:\n1. Single player\n2. Multi player ')
if option == 1:
choice = raw_input("\n'X' or 'O' ? (Use capitals) ")
while not over():
if choice is 'X':
s = computer('O')
else:
s = computer('X')
if s:
print_board()
print("\nYOU LOSE :(\nComputer wins along positions: {} \n Better Luck next time".format(s[1]))
break
if not over():
s = player(choice)
if s:
print_board()
print("\n*********Congratulations!!********\n YOU win along positions: {}".format(s[1]))
break
else:
print('\nReally close. The game ended in a draw. :(')
elif option == 2:
while not over():
s = first_player('X')
if s:
print_board()
print("\n*********Congratulations!!********\n Player {0} wins along positions: {1}".format(s[0], s[1]))
break
if not over():
s = second_player('O')
if s:
print_board()
print("\n*********Congratulations!!********\n Player {0} wins along positions: {1}".format(s[0], s[1]))
break
else:
print('\nReally close. The game ended in a draw. :(')