Tic-tac-toe series #3: Updating the board with user input

Image for post
Image for post
Where we’re headed today

Learning incrementally

Where we left off

Image for post
Image for post
Yesterday’s program

Today’s goal

Mini-step #1: Map 1–9 to i, j

Image for post
Image for post
board = [
['_', '_', '_'],
['_', '_', '_'],
['_', '_', '_']
]
board = [
[0|0, 0|1, 0|2],
[1|0, 1|1, 1|2],
[2|0, 2|1, 2|2]
]
def convert_selection(selection):
selection -= 1
return (selection // 3, selection % 3)

Mini-step #2: Update the board

def place_piece(selection, board):
board[selection[0]][selection[1]] = "X"

Mini-step #3: Add these new functions into the game

# ttt.py

def convert_selection(selection):
selection -= 1
return (selection // 3, selection % 3)


def place_piece(selection, board):
board[selection[0]][selection[1]] = "X"


def print_board(board):
for row in board:
print(row)


def select_square():
selection = int(input("Select a square: "))
if not 1 <= selection <= 9:
raise ValueError
return selection


board = [["_" for _ in range(3)] for _ in range(3)]
print_board(board)
try:
selection = convert_selection(select_square())
place_piece(selection, board)
except ValueError:
print("Sorry, please select a number 1-9")
print_board(board)
board = [["_" for _ in range(3)] for _ in range(3)]
print_board(board)
try:
place_piece(convert_selection(select_square()), board)
except ValueError:
print("Sorry, please select a number 1-9")
print_board(board)
$ python ttt.py
['_', '_', '_']
['_', '_', '_']
['_', '_', '_']
Select a square: 6
['_', '_', '_']
['_', '_', 'X']
['_', '_', '_']

Mini-step #4: Re-prompt the user

board = [["_" for _ in range(3)] for _ in range(3)]
while True:
print_board(board)
try:
selection = convert_selection(select_square())
place_piece(selection, board)
except ValueError:
print("Sorry, please select a number 1-9")
python ttt.py 
['_', '_', '_']
['_', '_', '_']
['_', '_', '_']
Select a square: 4
['_', '_', '_']
['X', '_', '_']
['_', '_', '_']
Select a square: 2
['_', 'X', '_']
['X', '_', '_']
['_', '_', '_']
Select a square: 6
['_', 'X', '_']
['X', '_', 'X']
['_', '_', '_']
Select a square:

Bonus: Refactoring to main()

if __name__ == "__main__":
main()
# ttt.pydef main():
board = [["_" for _ in range(3)] for _ in range(3)]
while True:
print_board(board)
try:
selection = convert_selection(select_square())
place_piece(selection, board)
except ValueError:
print("Sorry, please select a number 1-9")
def convert_selection(selection):
selection -= 1
return (selection // 3, selection % 3)
def place_piece(selection, board):
board[selection[0]][selection[1]] = "X"
def print_board(board):
for row in board:
print(row)
def select_square():
selection = int(input("Select a square: "))
if not 1 <= selection <= 9:
raise ValueError
return selection
if __name__ == "__main__":
main()

Mini-step #5: Switching players

def main():
board = [["_" for _ in range(3)] for _ in range(3)]
is_x = True
while True:
player = "X" if is_x else "O"
print_board(board)
try:
selection = convert_selection(select_square())
place_piece(selection, player, board)
except ValueError:
print("Sorry, please select a number 1-9")
is_x = not is_x
...def place_piece(selection, player, board):
board[selection[0]][selection[1]] = player
def main():
board = [["_" for _ in range(3)] for _ in range(3)]
is_x = True
while True:
print_board(board)
try:
selection = convert_selection(select_square())
place_piece(selection, is_x, board)
except ValueError:
print("Sorry, please select a number 1-9")
is_x = not is_x
...def place_piece(selection, is_x, board):
board[selection[0]][selection[1]] = "X" if is_x else "O"
$ python ttt.py
Select a square: 5
['_', '_', '_']
['_', 'X', '_']
['_', '_', '_']
Select a square: 1
['O', '_', '_']
['_', 'X', '_']
['_', '_', '_']
Select a square: 9
['O', '_', '_']
['_', 'X', '_']
['_', '_', 'X']
Select a square: 2
['O', 'O', '_']
['_', 'X', '_']
['_', '_', 'X']
Select a square: 3
['O', 'O', 'X']
['_', 'X', '_']
['_', '_', 'X']
Select a square: 6
['O', 'O', 'X']
['_', 'X', 'O']
['_', '_', 'X']
Select a square: 7
['O', 'O', 'X']
['_', 'X', 'O']
['X', '_', 'X']
Select a square:

Wrapping up

About Bennett

Top writer in Technology | Backend Web Developer | bennettgarner.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store