Skip to content

Commit 8b538c6

Browse files
authored
Add files via upload
0 parents  commit 8b538c6

File tree

2 files changed

+259
-0
lines changed

2 files changed

+259
-0
lines changed

Milestone Project 1/Advance.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os
2+
import random
3+
4+
theBoard = [" "]*10
5+
available =[str(num) for num in range(0,10)]
6+
players = [0,'X','O']
7+
8+
def display_board(a,b):
9+
print('Available TIC-TAC-TOE\n'+
10+
' moves\n\n '+
11+
a[7]+'|'+a[8]+'|'+a[9]+' '+b[7]+'|'+b[8]+'|'+b[9]+'\n '+
12+
'----- -----\n '+
13+
a[4]+'|'+a[5]+'|'+a[6]+' '+b[4]+'|'+b[5]+'|'+b[6]+'\n '+
14+
'----- -----\n '+
15+
a[1]+'|'+a[2]+'|'+a[3]+' '+b[1]+'|'+b[2]+'|'+b[3]+'\n')
16+
17+
'''
18+
def display_board(a,b):
19+
print(f'Available TIC-TAC-TOE\n moves\n\n {a[7]}|{a[8]}|{a[9]} {b[7]}|{b[8]}|{b[9]}\n ----- -----\n {a[4]}|{a[5]}|{a[6]} {b[4]}|{b[5]}|{b[6]}\n ----- -----\n {a[1]}|{a[2]}|{a[3]} {b[1]}|{b[2]}|{b[3]}\n')
20+
'''
21+
22+
def place_marker(avail,board,marker,position):
23+
board[position] = marker
24+
avail[position] = ' '
25+
26+
27+
def win_check(board,mark):
28+
29+
return ((board[7] == board[8] == board[9] == mark) or # across the top
30+
(board[4] == board[5] == board[6] == mark) or # across the middle
31+
(board[1] == board[2] == board[3] == mark) or # across the bottom
32+
(board[7] == board[4] == board[1] == mark) or # down the middle
33+
(board[8] == board[5] == board[2] == mark) or # down the middle
34+
(board[9] == board[6] == board[3] == mark) or # down the right side
35+
(board[7] == board[5] == board[3] == mark) or # diagonal
36+
(board[9] == board[5] == board[1] == mark)) # diagonal
37+
38+
39+
40+
def random_player():
41+
return random.choice((-1, 1))
42+
43+
def space_check(board,position):
44+
return board[position] == ' '
45+
46+
def full_board_check(board):
47+
return ' ' not in board[1:]
48+
49+
50+
def player_choice(board,player):
51+
position = 0
52+
53+
while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):
54+
try:
55+
position = int(input('Player %s, choose your next position: (1-9) '%(player)))
56+
except:
57+
print("I'm sorry, please try again.")
58+
59+
return position
60+
61+
62+
def replay():
63+
64+
return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')
65+
66+
67+
while True:
68+
os.system("cls")
69+
70+
print("Welcome to Tic Tac Toe! ")
71+
toggle = random_player()
72+
player = players[toggle]
73+
print(f"For this round player {player} will go first")
74+
75+
game_on =True
76+
input("Hit Enter to Continue : ")
77+
while game_on:
78+
display_board(available,theBoard)
79+
position = player_choice(theBoard,player)
80+
place_marker(available,theBoard,player,position)
81+
82+
if win_check(theBoard,player):
83+
display_board(available,theBoard)
84+
print(f"Congratulations ! player {player} wins!..")
85+
game_on = False
86+
87+
else:
88+
if full_board_check(theBoard):
89+
display_board(available,theBoard)
90+
print("The game is draw ..!!")
91+
break
92+
else:
93+
toggle *=-1
94+
player = players[toggle]
95+
os.system("cls")
96+
97+
theBoard = [" "]*10
98+
available = [str(num) for num in range(0,10)]
99+
100+
if not replay():
101+
break

Milestone Project 1/Tic_tac.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import os
2+
import random
3+
4+
def display_board(board):
5+
"""
6+
Display the Tic Tac Toe Board on screen
7+
8+
"""
9+
os.system("cls")
10+
print("\t\t**Tic Tac Toe Board**\n")
11+
print(" | | ")
12+
print(" {} | {} | {} ".format(board[7],board[8],board[9]))
13+
print(" | | ")
14+
print("--------------------")
15+
print(" | | ")
16+
print(" {} | {} | {} ".format(board[4],board[5],board[6]))
17+
print(" | | ")
18+
print("--------------------")
19+
print(" | | ")
20+
print(" {} | {} | {} ".format(board[1],board[2],board[3]))
21+
print(" | | ")
22+
print("\n")
23+
24+
25+
def player_input():
26+
"""
27+
OUTPUT : (player 1 marker, player 2 marker)
28+
"""
29+
30+
marker =""
31+
while not( marker =='X' or marker =='O'):
32+
marker = input("Player 1 choose 'X' or 'O' : ").upper()
33+
34+
if marker == 'O':
35+
return ('O','X')
36+
else:
37+
return ('X','O')
38+
39+
40+
def place_marker(board,marker,position):
41+
'''
42+
Place your marker on the board at the given position
43+
'''
44+
board[position] = marker
45+
46+
47+
def win_check(board , mark):
48+
"""
49+
WIN TIC TAC TOE ?
50+
51+
"""
52+
return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the top
53+
(board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle
54+
(board[1] == mark and board[2] == mark and board[3] == mark) or # across the bottom
55+
(board[7] == mark and board[4] == mark and board[1] == mark) or # down the middle
56+
(board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle
57+
(board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side
58+
(board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal
59+
(board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal
60+
61+
62+
def choose_first():
63+
"""
64+
Randomly choose the player who goes first
65+
"""
66+
67+
flip = random.randint(0,1)
68+
69+
if flip ==0:
70+
return 'Player 1'
71+
else:
72+
return 'Player 2'
73+
74+
75+
def space_check(board,position):
76+
"""
77+
Check for empty space
78+
"""
79+
80+
return board[position] == " "
81+
82+
83+
def full_board_check(board):
84+
"""
85+
Check for the board is full or not
86+
"""
87+
88+
for i in range(1,10):
89+
if(space_check(board,i)):
90+
return False
91+
return True
92+
93+
94+
def player_choice(board):
95+
position = 0
96+
while position not in [1,2,3,4,5,6,7,8,9] and not space_check(board,position):
97+
position = int(input("Choose position : (1-9) "))
98+
99+
return position
100+
101+
102+
def replay():
103+
choice = input("Do You want to play Again? Yes or No : ")
104+
return choice == 'Yes'
105+
106+
107+
108+
if __name__ == "__main__":
109+
print("\t\tWelcome to Tic Tac Toe Game\n")
110+
while True:
111+
the_board =[" "]*10
112+
player1_marker, player2_marker =player_input()
113+
114+
turn =choose_first()
115+
print(turn, " will go first \n")
116+
play_game = input("Ready to play? y or n :")
117+
if play_game =='y':
118+
game_on = True
119+
else:
120+
game_on = False
121+
122+
while game_on:
123+
if turn =='Player 1':
124+
125+
display_board(the_board)
126+
position = player_choice(the_board)
127+
place_marker(the_board,player1_marker,position)
128+
if win_check(the_board,player1_marker):
129+
display_board(the_board)
130+
print("PLAYER 1 WON..!!\n")
131+
game_on = False
132+
else:
133+
if full_board_check(the_board):
134+
display_board(the_board)
135+
print("TIE GAME")
136+
game_on =False
137+
else:
138+
turn = 'Player 2'
139+
else:
140+
141+
display_board(the_board)
142+
position = player_choice(the_board)
143+
place_marker(the_board,player2_marker,position)
144+
if win_check(the_board,player2_marker):
145+
display_board(the_board)
146+
print("PLAYER 2 WON..!!\n")
147+
game_on = False
148+
else:
149+
if full_board_check(the_board):
150+
display_board(the_board)
151+
print("TIE GAME")
152+
game_on =False
153+
else:
154+
turn = 'Player 1'
155+
156+
if not replay():
157+
break
158+

0 commit comments

Comments
 (0)