Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Global Variables
- board = [['-', '-', '-'],
- ['-', '-', '-'],
- ['-', '-', '-']]
- player = "X"
- flag = False
- flag2 = False
- #Copy your functions from the previous activities here:
- #print_board, is_valid_move, place_player, take_turn
- def print_board():
- n = 0
- print()
- print(' 0 1 2')
- print()
- for i in board:
- print(n, end=' ')
- n += 1
- for j in i:
- print(j, end=' ')
- print()
- print()
- #returns true if a row,col on the board is open
- def is_valid_move(row, col):
- if row not in (0,1,2) or col not in (0,1,2):
- print("Please enter a valid move")
- return False
- if board[row][col] != "-":
- print("That spot is already taken")
- return False
- return True
- #places player on row,col on the board
- def place_player(player, row, col):
- board[row][col] = player
- #Asks the user to enter a row and col until the user enters a valid location
- #Adds user location to the board, and prints the board
- def take_turn(player):
- print(f"{player}'s Turn")
- valid = False
- while not valid:
- row = int(input("Enter a row : "))
- col = int(input("Enter a column : "))
- valid = is_valid_move(row, col)
- place_player(player, row, col)
- print_board()
- #Write your check win functions here:
- def check_col_win(player):
- for c in range(3):
- if board[0][c] == board[1][c] == board[2][c] == player:
- return True
- return False
- def check_row_win(player):
- for r in range(3):
- if board[r][0] == board[r][1] == board[r][2] == player:
- return True
- return False
- def check_diag_win(player):
- if board[0][0] == board[1][1] == board[2][2] == player:
- return True
- if board[0][2] == board[1][1] == board[2][0] == player:
- return True
- return False
- def check_win(player):
- return (
- check_row_win(player) or
- check_col_win(player) or
- check_diag_win(player)
- )
- def check_tie():
- for row in board:
- if "-" in row:
- return False
- return True
- #Start of program
- print("\t\tWelcome to Tic Tac Toe!")
- print_board()
- '''
- DO NOT CHANGE: Tests
- each test should print true if your function is written correctly:
- '''
- #Check row win
- place_player(player, 0,0)
- place_player(player, 0,1)
- place_player(player, 0,2)
- print_board()
- print("Check Row Win: ", check_win(player))
- #Check col win
- board = []
- for i in range(3):
- board.append(["-","-","-"])
- place_player(player, 0,0)
- place_player(player, 1,0)
- place_player(player, 2,0)
- print_board()
- print("Check Col Win: ", check_win(player))
- #check diag win
- board = []
- for i in range(3):
- board.append(["-","-","-"])
- place_player(player, 0,0)
- place_player(player, 1,1)
- place_player(player, 2,2)
- print_board()
- print("Check Diag Win: ", check_win(player))
- #Check Tie
- board = []
- for i in range(3):
- board.append(["-","-","-"])
- place_player("X", 1,1)
- place_player("X", 0,2)
- place_player(player, 0,0)
- place_player("O", 0,1)
- place_player("O", 2,2)
- place_player("O", 1,0)
- place_player("X", 1,2)
- place_player("X", 2,1)
- place_player("O", 2,0)
- print_board()
- print("Check Tie: ", check_tie())
Advertisement
Add Comment
Please, Sign In to add comment