-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtttmultiplayer.py
59 lines (54 loc) · 1.42 KB
/
tttmultiplayer.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
def gameboard():
print("\n %s| %s| %s" % (board[6],board[7],board[8]))
print("__|__|__")
print(" %s| %s| %s" % (board[3],board[4],board[5]))
print("__|__|__")
print(" %s| %s| %s\n" % (board[0],board[1],board[2]))
def checkwin(w):
if board[0] == w and board[1] == w and board[2] == w:
return True
if board[3] == w and board[4] == w and board[5] == w:
return True
if board[6] == w and board[7] == w and board[8] == w:
return True
if board[0] == w and board[3] == w and board[6] == w:
return True
if board[1] == w and board[4] == w and board[7] == w:
return True
if board[2] == w and board[5] == w and board[8] == w:
return True
if board[6] == w and board[4] == w and board[2] == w:
return True
if board[0] == w and board[4] == w and board[8] == w:
return True
return False
board = ["1","2","3","4","5","6","7","8","9"]
turn = 1
moves = 0
result = False
while (moves < 9):
gameboard()
if turn == 1: #X's turn
move = input("Choose your move, X: ")
if board[(int(move)-1)] == move:
board[(int(move)-1)] = "X"
if checkwin("X"):
result = True
winner = "X."
break
moves += 1
turn = 0
else: #O's turn
move = input("Choose your move, O: ")
if board[(int(move) - 1)] == move:
board[(int(move) - 1)] = "O"
if checkwin("O"):
result = True
winner = "O."
break
moves += 1
turn = 1
if result:
print("Congratulations", winner,"You win!\n")
else:
print("It's a draw!\n")