100% found this document useful (1 vote)
58 views3 pages

Notebook For The Milestone Project 1

This document defines functions for playing a game of tic-tac-toe. It includes functions to display the game board, place markers on the board, check for a win, and check if the board is full. The full_tic_tac_toe_game function runs a full game by alternating turns between players, checking for a win or draw after each turn, and allowing the players to replay.

Uploaded by

xSoft.Hearten?
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
58 views3 pages

Notebook For The Milestone Project 1

This document defines functions for playing a game of tic-tac-toe. It includes functions to display the game board, place markers on the board, check for a win, and check if the board is full. The full_tic_tac_toe_game function runs a full game by alternating turns between players, checking for a win or draw after each turn, and allowing the players to replay.

Uploaded by

xSoft.Hearten?
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

from IPython.

display import clear_output

def display_board(board):
clear_output()
print('Here is the current board')
print(' | | ')
print(' '+board[1]+' | '+board[2]+' | '+board[3]+' ')
print(' | | ')
print('-----------')
print(' | | ')
print(' '+board[4]+' | '+board[5]+' | '+board[6]+' ')
print(' | | ')
print('-----------')
print(' | | ')
print(' '+board[7]+' | '+board[8]+' | '+board[9]+' ')
print(' | | ')

def player_input():
marker = ''

while not (marker == 'X' or marker == 'O'):


marker = input('Player 1: Do you want to be X or O? ').upper()

if marker == 'X':
return ('X', 'O')
else:
return ('O', 'X')

def place_marker(board, marker, position):


board[position] = marker

def win_check(board,mark):

return ((board[7] == mark and board[8] == mark and board[9] == mark) or


(board[4] == mark and board[5] == mark and board[6] == mark) or
(board[1] == mark and board[2] == mark and board[3] == mark) or
(board[7] == mark and board[4] == mark and board[1] == mark) or
(board[8] == mark and board[5] == mark and board[2] == mark) or
(board[9] == mark and board[6] == mark and board[3] == mark) or
(board[7] == mark and board[5] == mark and board[3] == mark) or
(board[9] == mark and board[5] == mark and board[1] == mark))

import random

def choose_first():
if random.randint(0, 1) == 0:
return 'Player 2'
else:
return 'Player 1'

def space_check(board, position):

return board[position] == ' '


def full_board_check(board):
for i in range(1,10):
if space_check(board, i):
return False
return True

def player_choice(board):
position = 0

while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):


position = int(input('Choose your next position: (1-9) '))

return position

def replay():
aswer= ' '
while not (aswer =='Yes' or aswer == 'No'):
aswer=input('Please insert Yes if you want to play else insert No')

if aswer=='yes':
return True
else:
pass

def full_tic_tac_toe_game():#1
print('Welcome to Tic Tac Toe!')#2
#3
theboard=['#','1','2','3','4','5','6','7','8','9']#4
#5
while True:#6
display_board(theboard)#7
whos_turn=choose_first()#8
print(whos_turn+' will go first')#9
want_to_play=' '#10
while want_to_play =='Yes' or want_to_play == 'No':#11
want_to_play = input('Are you ready?')#12
if want_to_play=='yes':#13
game_on=True#14
else:#15
game_on=False#16
#17
while game_on:#18
if whos_turn == 'player_1':#19
display_board(theboard)#20
player_choice(theboard)#21
place_marker(theboard,player1_marker, position)#22
#23
if win_check(theBoard, player1_marker):#24
display_board(theboard)#25
print('Congratulations! Player_1 you have won the game!')#26
game_on = False#27
else:#28
if full_board_check(theboard):#29
display_board(theboard)#30
print('No one has won the game because it is a draw!')#31
break#32
else:#33
turn = 'Player 2'#34
#35
else:#36
#37
display_board(theboard)#38
position = player_choice(thboard)#39
place_marker(theboard, player2_marker, position)#39
#40
if win_check(theboard, player2_marker):#41
display_board(theboard)#42
print('Congratulations! Player_2 you have won the game!')#43
game_on = False#44
else:#45
if full_board_check(theboard):#46
display_board(theboard)#47
print('No one has won the game because it is a draw!')#48
break#49
else:#50
turn = 'Player 1'#51
#52
if replay == True:#53
full_tic_tac_toe_game()#54
else:#55
pass#56

You might also like