-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
59 lines (48 loc) · 1.52 KB
/
tictactoe.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
# Tic-Tac-Toe
# By: Loran Mills
def main():
player = "x"
board = create_board()
while not (win(board) or tie(board)):
display_board(board)
take_turn(player, board)
player = next_player(player)
display_board(board)
print("Great Game! Well Played!")
def create_board():
board = []
for i in range(9):
board.append(i + 1)
return board
def display_board(board):
print()
print(f"{board[0]} | {board[1]} | {board[2]}")
print("--+---+--")
print(f"{board[3]} | {board[4]} | {board[5]}")
print("--+---+--")
print(f"{board[6]} | {board[7]} | {board[8]}")
print()
def win(board):
return (board[0] == board[1] == board[2] or
board[3] == board[4] == board[5] or
board[6] == board[7] == board[8] or
board[0] == board[3] == board[6] or
board[1] == board[4] == board[7] or
board[2] == board[5] == board[8] or
board[0] == board[4] == board[8] or
board[2] == board[4] == board[6])
def tie(board):
for i in range(9):
if board[i] != "x" and board[i] != "o":
return False
return True
def take_turn(player, board):
place = int(input(f"{player}'s turn to chose a space (1-9): "))
board[place - 1] = player
def next_player(current_player):
if current_player == "" or current_player == "x":
return "o"
elif current_player == "o":
return "x"
if __name__ == "__main__":
main()