forked from TomRichtr/training-creations_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac-toe.py
163 lines (123 loc) · 4.28 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#clear output function import
from IPython.display import clear_output
# Displaying of the game board
def display_board(board):
clear_output ()
print ("-------------")
print ("| " + board [7] + " | " + board [8] + " | " + board [9] + " |")
print ("-------------")
print ("| " + board [4] + " | " + board [5] + " | " + board [6] + " |")
print ("-------------")
print ("| " + board [1] + " | " + board [2] + " | " + board [3] + " |")
print ("-------------")
#Player one chooses marker and player two gets opposite marker
def player_input():
marker = ""
while marker != "X" and marker != "O":
marker = input ("Player 1, choose your marker X/O!").upper () [0]
player1 = marker
if player1 == "X":
player2 = "O"
else:
player2 = "X"
return (player1,player2)
#Function defining where to place a marker on board.
def place_marker(board,marker,position):
board [position] = marker
#A function checking if chosed place on board is empty.
def space_check(board, position):
if board [position] == " ":
return True
else:
return False
# A function checking if the board isnt full and so there is a tie.
def full_board_check(board):
if " " not in board [1:11:1]:
return True
else:
return False
#A function defining which player goes first.
import random
def choose_first():
if random.randint (1,2) == 1:
return ("Player 1")
else:
return ("Player 2")
#Function checking if the specific marker didnt reach a victory conditions
def win_check(board, marker):
if board [1:4:1] == [marker,marker,marker]:
return True
elif board [4:7:1] == [marker,marker,marker]:
return True
elif board [7:10:1] == [marker,marker,marker]:
return True
elif board [2:9:3] == [marker,marker,marker]:
return True
elif board [3:10:3] == [marker,marker,marker]:
return True
elif board [1:10:4] == [marker,marker,marker]:
return True
elif board [3:8:2] == [marker,marker,marker]:
return True
elif board [1:8:3] == [marker,marker,marker]:
return True
else:
return False
#Function taking input of players, a number, defining where the marker should be placed.
def player_choice(board):
position = 0
while position not in (1,2,3,4,5,6,7,8,9) or space_check(board,position) == False:
position = int (input ("Where (1-9) you want to place your marker?"))
return position
#Function asking a players if to continue with another game.
def replay():
if input ("Play again? Y/N.").upper () [0] == "Y":
return True
else:
return False
#actuall game
print ("Welcome to Tic Tac Toe game!")
while True:
board = [" "] * 10
player1,player2 = player_input()
print (f"Player's 1 marker is {player1} and player's 2 marker is {player2}!")
turn = choose_first()
print (f"{turn} starts!")
game = input (f"{turn} Can we start? Y/N")
if game.upper () [0] == "Y":
game_on = True
else:
game_on = False
while game_on == True:
if turn == "Player 1":
display_board(board)
position = player_choice (board)
place_marker (board,player1,position)
if win_check(board,player1) == True:
display_board(board)
print ("Player 1 won!")
game_on = False
else:
if full_board_check(board) == True:
display_board(board)
print ("Its a tie")
game_on = False
else:
turn = "Player 2"
elif turn == "Player 2":
display_board(board)
position = player_choice (board)
place_marker (board,player2,position)
if win_check(board,player2) == True:
display_board(board)
print ("Player 2 won!")
game_on = False
else:
if full_board_check(board) == True:
display_board(board)
print ("Its a tie")
game_on = False
else:
turn = "Player 1"
if replay () != True:
break