-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kolko-Krzyzyk.py
112 lines (103 loc) · 3.31 KB
/
Kolko-Krzyzyk.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
def display_board(board):
blankBoard="""
___________________
| | | |
| 7 | 8 | 9 |
| | | |
|-----------------|
| | | |
| 4 | 5 | 6 |
| | | |
|-----------------|
| | | |
| 1 | 2 | 3 |
| | | |
|-----------------|
"""
for i in range(1,10):
if (board[i] == 'O' or board[i] == 'X'):
blankBoard = blankBoard.replace(str(i), board[i])
else:
blankBoard = blankBoard.replace(str(i), ' ')
print(blankBoard)
def player_input():
player1 = input("Please pick a marker 'X' or 'O' ")
while True:
if player1.upper() == 'X':
player2='O'
print("You've choosen " + player1 + ". Player 2 will be " + player2)
return player1.upper(),player2
elif player1.upper() == 'O':
player2='X'
print("You've choosen " + player1 + ". Player 2 will be " + player2)
return player1.upper(),player2
else:
player1 = input("Please pick a marker 'X' or 'O' ")
def place_marker(board, marker, position):
board[position] = marker
return board
def space_check(board, position):
return board[position] == '#'
def full_board_check(board):
return len([x for x in board if x == '#']) == 1
def win_check(board, mark):
if board[1] == board[2] == board[3] == mark:
return True
if board[4] == board[5] == board[6] == mark:
return True
if board[7] == board[8] == board[9] == mark:
return True
if board[1] == board[4] == board[7] == mark:
return True
if board[2] == board[5] == board[8] == mark:
return True
if board[3] == board[6] == board[9] == mark:
return True
if board[1] == board[5] == board[9] == mark:
return True
if board[3] == board[5] == board[7] == mark:
return True
return False
def player_choice(board):
choice = input("Please select an empty space between 1 and 9 : ")
while not space_check(board, int(choice)):
choice = input("This space isn't free. Please choose between 1 and 9 : ")
return choice
def replay():
playAgain = input("Do you want to play again (y/n) ? ")
if playAgain.lower() == 'y':
return True
if playAgain.lower() == 'n':
return False
if __name__ == "__main__":
print('Welcome to Tic Tac Toe!')
i = 1
# Choose your side
players=player_input()
# Empty board init
board = ['#'] * 10
while True:
# Set the game up here
game_on=full_board_check(board)
while not game_on:
# Player to choose where to put the mark
position = player_choice(board)
# Who's playin ?
if i % 2 == 0:
marker = players[1]
else:
marker = players[0]
# Play !
place_marker(board, marker, int(position))
display_board(board)
i += 1
if win_check(board, marker):
print("You won !")
break
game_on=full_board_check(board)
if not replay():
break
else:
i = 1
players=player_input()
board = ['#'] * 10