Created
November 14, 2019 22:10
-
-
Save VincentRoma/cd5aaf3236727da255be10bd7d093395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
w, h = 3, 3 | |
def create_empty_board (): | |
# fill in code here to create the board of size 3 x 3 | |
new_board = [[" " for x in range(w)] for y in range(h)] | |
return new_board | |
def draw_board (board): | |
size = len(board) | |
col_indicator = " " | |
for i in range(0, size): | |
col_indicator = col_indicator + " " + str(i) | |
print(col_indicator) | |
row_separator = " " + ("-" * size * 4) | |
print(row_separator) | |
for row_no in range(0,len(board)): | |
one_row = str(row_no) + " | " | |
for cell in board[row_no]: | |
if cell == '-': | |
one_row += " " | |
else: | |
one_row += cell | |
one_row += " | " | |
print(one_row) | |
print(row_separator) | |
def valid_move (board, col_no, row_no): | |
# returns True if move is valid, False otherwise | |
if board[col_no][row_no] is " ": | |
return True | |
return False | |
def player_move (board, current_player, col_no, row_no): | |
# get valid move from player and return the board with that move indicated | |
board[col_no][row_no]=current_player | |
return board | |
def win (board): | |
# if there is a winning streak on the board, return the player who won, | |
# otherwise return None | |
for i in range(w): | |
if(board[i][0] is not " " and board[i][0]==board[i][1] and board[i][0]==board[i][2]): | |
return board[i][0] | |
if(board[0][i] is not " " and board[0][i]==board[1][i] and board[0][i]==board[2][i]): | |
return board[0][i] | |
if(board[0][0] is not " " and board[0][0]==board[1][1] and board[0][0]==board[2][2]): | |
return board[0][0] | |
if(board[2][0] is not " " and board[2][0]==board[1][1] and board[2][0]==board[0][2]): | |
return board[2][0] | |
return None | |
def full (board): | |
# return True if every slot is full, otherwise return False | |
empty_spaces = 0 | |
for i in range(w): | |
for j in range(h): | |
if board[i][j] is " ": | |
empty_spaces+=1 | |
if empty_spaces: | |
return False | |
return True | |
def next_player (current_player): | |
# based on current player, return the next player (so that play alternates) | |
if current_player is "x": | |
return "y" | |
return "x" | |
def play_ttt (): | |
board = create_empty_board () | |
draw_board (board) | |
is_game_over = False | |
current_player = 'x' | |
# Fill in the rest of the code that handles game play | |
while not is_game_over: | |
input = raw_input("It's "+current_player+" turn to play: ") | |
y = int(input.split(",")[0]) | |
x = int(input.split(",")[1]) | |
if x < 3 and y < 3: | |
if valid_move(board,x,y): | |
player_move(board,current_player,x,y) | |
else: | |
print "ALREADY TAKEN" | |
current_player = next_player(current_player) | |
draw_board(board) | |
else: | |
print "MOVE OUT OF RANGE" | |
winner = win(board) | |
if full(board): | |
print "GAME OVER" | |
is_game_over = True | |
if winner is not None: | |
print winner + " WON THE GAME" | |
is_game_over = True | |
play_ttt() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment