Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from IPython.display import clear_output
- # Displays the current board
- def display_board(board):
- print(f"{board[6]}|{board[7]}|{board[8]}")
- print("-+-+-")
- print(f"{board[3]}|{board[4]}|{board[5]}")
- print("-+-+-")
- print(f"{board[0]}|{board[1]}|{board[2]}")
- # Player 1 chooses X or O, Player 2 is assigned the other
- def player_input():
- PlayerInput=''
- while not PlayerInput=='X' and not PlayerInput=='O':
- PlayerInput=input('Player 1, choose X or O: ').upper()
- Player1=PlayerInput
- if Player1=='X':
- Player2='O'
- else:
- Player2='X'
- return (Player1,Player2)
- def place_marker(board, marker, position):
- board[position-1]=marker
- def win_check(board, mark):
- for c in board:
- if c!=' ':
- if (board[6]==mark and board[7]==mark and board[8]==mark) or (board[3]==mark and board[4]==mark and board[5]==mark) or (board[0]==mark and board[1]==mark and board[2]==mark) or (board[6]==mark and board[3]==mark and board[0]==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[6]==mark and board[4]==mark and board[2]==mark) or (board[8]==mark and board[4]==mark and board[0]==mark):
- return True
- else:
- return False
- def choose_first():
- first=random.randint(1,100)
- if first<=50:
- return True
- elif first>50:
- return False
- def space_check(board, position):
- if board[position-1]==' ':
- return True
- else:
- return False
- def full_check(board):
- for i in board:
- if i==' ':
- return False
- return True
- def player_choice(board):
- choice=input('Choose where you want to place your marker: ')
- intchoice=int(choice)
- while not space_check(board, intchoice):
- print('Space is full. Choose another space.')
- return choice
- def replay():
- playagain=input('Would you like to play again?\n')
- if playagain.lower=='yes':
- return True
- elif playagain.lower=='no':
- return False
- else:
- print('Please type "yes" or "no."')
- def tic_tac_toe():
- print('Welcome to Tic-Tac-Toe!')
- # List representing the board, indexes represent the board via the numpad
- board=[' ',' ',' ',' ',' ',' ',' ',' ',' ']
- # Variables storing whether Player 1 or 2 are X or O
- Player1, Player2 = player_input()
- PlayingGame=True
- playAgain=True
- isPlayer1turn=choose_first()
- while PlayingGame and playAgain:
- if isPlayer1turn:
- print("Player 1's turn.")
- GridPosition=player_choice(board)
- intGrid=int(GridPosition)
- place_marker(board, Player1, intGrid)
- isPlayer1turn=not isPlayer1turn
- display_board(board)
- elif not isPlayer1turn:
- print("Player 2's turn.")
- GridPosition=player_choice(board)
- intGrid=int(GridPosition)
- place_marker(board, Player2, intGrid)
- isPlayer1turn=not isPlayer1turn
- display_board(board)
- if win_check(board, Player1):
- print('Player 1 wins!')
- playAgain=replay()
- if playAgain:
- board=[' ',' ',' ',' ',' ',' ',' ',' ',' ']
- continue
- else:
- print('Game Over!')
- PlayingGame=False
- if win_check(board, Player2):
- print('Player 2 wins!')
- playAgain=replay()
- if playAgain:
- board=[' ',' ',' ',' ',' ',' ',' ',' ',' ']
- continue
- else:
- print('Game Over!')
- PlayingGame=False
- if full_check(board):
- print('Board is full! Tie game!')
- playAgain=replay()
- if playAgain:
- board=[' ',' ',' ',' ',' ',' ',' ',' ',' ']
- continue
- else:
- print('Game Over!')
- PlayingGame=False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement