Skip to content

Instantly share code, notes, and snippets.

@KyleRConway
Created November 27, 2018 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleRConway/e55d75d90b5d47bc5cb50b4eefa5a3a9 to your computer and use it in GitHub Desktop.
Save KyleRConway/e55d75d90b5d47bc5cb50b4eefa5a3a9 to your computer and use it in GitHub Desktop.
# Requirements
# --------------------
# 1. Tic-Tac-Toe
# 2. Two-Player
# - take turns
# - human input
# 3. Terminal-based
# 4. Recognize and report win-state and winner (when/if reached)
import os
from random import randint
# Start with Player #1
player = 1
# Make tic-tac-toe grid to show game board
global grid
grid = [[0,0,0],[0,0,0],[0,0,0]]
# Name columns
columns = " a b c"
# create function to show game-state in better format
## Add row/column naming for player input
def show_grid():
print(columns)
i = 0
for row in grid:
print(i, end=' ')
print(row)
i += 1
# Get players
def startgame():
start_message = '''
welcome to tic-tac-toe
--------------------------
'''
print(start_message)
global player1
global player2
player1 = "P1: " + input("Type your name, Player #1: ")
player2 = "P2: " + input("Type your name, Player #2: ")
clearscreen()
# create player input
def play(player):
show_grid()
if player == 1:
player_name = player1
else:
player_name = player2
play = input("\nWhat's your move, {}? (e.g. a1) ".format(player_name))
moves = [player, play]
# replace letters with numbers
play = play.replace('a','0')
play = play.replace('b','1')
play = play.replace('c','2')
error_message = '''
***********************
Please enter a column: 'a', 'b', or 'c'
followed by a row: '0', '1', or '2'
Examples --> a1, b0, c2
***********************
'''
space_taken = '''
***********************
Space already taken!
Make another selection!
***********************
'''
try:
space = grid[int(play[1])][int(play[0])]
grid[int(play[1])][int(play[0])] = space
except IndexError:
clearscreen()
print(error_message)
return(player)
except ValueError:
clearscreen()
print(error_message)
return(player)
# check entry for space already occupied
if grid[int(play[1])][int(play[0])] > 0:
clearscreen()
print(space_taken)
return(player)
else:
# Modify Output for player1 v. player2
if player == 1:
grid[int(play[1])][int(play[0])] = 1
else:
grid[int(play[1])][int(play[0])] = 2
if player == 2:
player = 1
else:
player = 2
clearscreen()
return player
def game_state_check():
wins = " WINS THE GAME"
p1_wins = str(player1) + wins
p2_wins = str(player2) + wins
# check win in rows
for row in grid:
if row[0] == row[1] == row[2] == 1:
show_grid()
print(p1_wins)
return True
elif row[0] == row[1] == row[2] == 2:
show_grid()
print(p2_wins)
return True
else:
continue
# check win in columns
for i in range(3):
column_nums = []
for row in grid:
column_nums.append(row[i])
if len(set(column_nums)) == 1:
if column_nums[0] == 1:
print(p1_wins)
return True
exit
elif column_nums[0] == 2:
print(p2_wins)
return True
else:
continue
# check diagonal wins
if grid[0][0] == grid[1][1] == grid[2][2] and not grid[1][1] == 0:
if grid[1][1] == 1:
print(p1_wins)
return True
else:
print(p1_wins)
return True
elif grid[0][2] == grid[1][1] == grid[2][0] and not grid[1][1] == 0:
if grid[1][1] == 1:
print(p2_wins)
return True
else:
print(p2_wins)
return True
# Check Draw
drawn = False
row_w_0_count = 0
for i in range(3):
if [j for j in grid[i] if 0 in grid[i]]:
row_w_0_count += 1
else:
continue
if row_w_0_count == 0:
print("DRAWN GAME!!!")
return True
else:
return False
def clearscreen():
os.system('clear')
######################
# RUN GAME
######################
startgame()
while not game_state_check():
player = play(player)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment