Question: Python ''' So this should be fun. This is just Tic-Tac-Toe. Or at least a board that can be used to play it. The ttt_board

Python

''' So this should be fun. This is just Tic-Tac-Toe. Or at least a board that can be used to play it. The ttt_board is a dictionary that contains all 9 of the grid squares (columns A-C, rows 1-3). The key is the square "position" and the value is " " (space), "X" or "O".

As with p1, your job is simply to complete the three functions below by putting real logic in place of the "pass" statements. The rest of the logic to "play" the game is already written for you at the bottom. ''' ttt_board = { "A1": " ", "B1": " ", "C1": " ", "A2": " ", "B2": " ", "C2": " ", "A3": " ", "B3": " ", "C3": " " }

def show_board(): ''' This function should simply display the tic-tac-toe board (ttt_board) in a grid. To start with, it will be empty: A B C 1 2 3 Though over time, it will start to contain X's and O's:

A B C 1 X 2 X 3 O O X

Your job here is simply to disply the dictionary as shown. You don't need to use a loop if you don't want to. You can just brute force a bunch of print statements, placing the value of each dictionary element in its proper place.

BUT, if you DO want a challenge (and you probably should want it since its good practice for stuff you will eventually have to do), then you can try to use a couple of "nested" for loops to print the board. Should you choose this challenge, here's a hint:

for row in "123": # loops 3 times...for "1", "2" and "3" ... perhaps do something here ... for col in "ABC": # loops 3 times...for "A", "B" and "C" ... and some stuff here ... If it isn't obvious what's happening, you might just start by printing row and col inside of the inner loop. These likely can be concatenated to give you the "key" to this solution. ''' global ttt_board pass

def play_move(player, position): ''' At it's heart, this funciton simply sets the square at `position` to the value of `player` (either X or O). BUT, you'll need the following extra logic. 1) IF the position isn't one of the keys in ttt_board, then print "BAD POSITION" _AND_ RETURN FALSE (indicating failure) 2) IF the value of the dictionary for the `position` key is NOT " " (a space) then print "POSITION OCCUPIED" _AND_ RETURN FALSE (again, failure) 3) OTHERWISE, set the value of the "square" in ttt_board to `player` _AND_ RETURN TRUE (yeah, success) Those True/False return values are critical as they are used in the main gameplay (the part I already did for you) to determine whether it's the next player's move or not. ''' global ttt_board pass

def clear_board(): ''' And this function is the easiest. Simply set each "square" in the ttt_board dictionary to " " (i.e., a space). YOU MUST use a loop here to iterate over each key and set the value. Don't just "hack" it... ''' global ttt_board pass

''' Everything from here down is already done for you. If you do YOUR job right, then running the script will execute the code below, which will call your functions and let you enjoy a nice game of tic-tac-toe.

DO take a look at the code, though. Try your best to understand it. You will likely need to do some similar logic in the not-too-distant future.

__IF__ you want another challenge, add a function (check_for_winner) to check if there is a winner and return True or False. Then you could change:

if move_count == 9:

to

if move_count == 9 or check_for_winner() == True:

so that the game doesn't just keep droning on if X or O has already won. Even better, you might show different messages if the game ends in a draw versus having X or O win. '''

current_player = 'X' move_count = 0

while True: show_board() while True: pos = input(f"Move for {current_player}: ").strip().upper() success = play_move(current_player, pos) if success == True: # increment the move counter move_count += 1 # exit this "inner" loop to let the next player go break ### END INNER LOOP ###

# if the move counter is at 9, the game is over if move_count == 9: choice = input("Game Over. Play again?? (y/N)").strip().upper() # if the choice here isn't "Y", then exit the main loop. # Otherwise clear the board and continue another loop. if choice != "Y": break else: move_count = 0 clear_board() continue # otherwise, it's the next player's turn, so "toggle" to the other player. if current_player == "X": current_player = "Y" else: current_player = "X"

### END MAIN LOOP ### print("BYE!!")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!