Created
April 9, 2016 02:49
-
-
Save ana0/9f37922fadb17bdf19f8e3779ad97c24 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import random | |
class Board(object): | |
def __init__(self): | |
self.board = [[" "," "," "], [" "," "," "], [" "," "," "]] | |
def print_board(self): | |
topstr = " a b c" | |
linestr = " +-----+-----+-----+" | |
rowstr = "%s | %s | %s | %s |" | |
print topstr | |
for i, row in enumerate(self.board): | |
print linestr | |
print rowstr % (i, self.board[i][0], self.board[i][1], | |
self.board[i][2]) | |
print linestr | |
def set_position(self, position, char): | |
# Sort the input string | |
# This is so players can enter their row, column in any order | |
column = [c.lower() for c in position if c in "abcABC"] | |
row =[c for c in position if c in "012"] | |
# Was it a valid input? | |
if len(column) != 1 or len(row) != 1: | |
print "That doesn't seem to be a valid position\n" | |
return False | |
# Place the marker in the right position | |
column, row = column[0], int(row[0]) | |
if column == "a": | |
column = 0 | |
elif column == "b": | |
column = 1 | |
else: | |
column = 2 | |
# Make sure the space is empty | |
if self.board[row][column] != " ": | |
print "Sorry that spot is already taken\n" | |
return False | |
self.board[row][column] = char | |
return True | |
def look_for_win(self): | |
for i in range(len(self.board)): | |
# Check each row | |
if (self.board[i][0] == self.board[i][1] == self.board[i][2] | |
and self.board[i][0].isalpha()): | |
return self.board[i][0] | |
# Check each column | |
elif (self.board[0][i] == self.board[1][i] == self.board[2][i] | |
and self.board[0][i].isalpha()): | |
return self.board[0][i] | |
# Check diagonals | |
if (self.board[0][0] == self.board[1][1] == self.board[2][2] | |
and self.board[0][0].isalpha()): | |
return self.board[0][0] | |
if (self.board[0][2] == self.board[1][1] == self.board[2][0] | |
and self.board[0][2].isalpha()): | |
return self.board[0][2] | |
return False | |
def check_empty_spaces(self): | |
for row in self.board: | |
for i in row: | |
if i == " ": | |
return True | |
return False | |
class Game(object): | |
def __init__(self): | |
# Boolean triggers the outer game loop | |
self.playing_game = True | |
def playing_a_human(self): | |
# At the moment, the game will force you to play a human | |
print "Would you like to play against another human or the computer?" | |
answer = raw_input("Type 'h' for human and 'c' for computer\n") | |
if answer.strip().lower() == "h": | |
print "You've chosen to play a human!\n" | |
return True | |
elif answer.strip().lower() == "c": | |
print "Playing against the computer is not available yet, sorry!\n" | |
return False | |
else: | |
print "I'm sorry that doesn't seem to be one of the options\n" | |
return False | |
def get_player_name(self, char): | |
name = raw_input("Player %s enter your name\n" % char) | |
return (name, char) | |
def goes_first(self, player_x, player_o): | |
first = random.choice([player_x, player_o]) | |
print "\n%s will go first" % first[0] | |
return first | |
def turn(self, player): | |
move = raw_input("Player %s make your move!\n" % player[0]) | |
return move | |
def win(self, playing): | |
print "Congratilations %s, you won!" % playing[0] | |
print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!" | |
return True | |
def tie(self): | |
print "The game is a tie!" | |
print "One of you should make more mistakes next time!" | |
def play_again(self): | |
answer = raw_input("Would you like to play again? 'y' or 'n'\n") | |
if answer.strip().lower() == "y": | |
print "Let's play again!" | |
return True | |
elif answer.strip().lower() == "n": | |
print "Ok, goodbye!" | |
self.playing_game = False | |
return True | |
else: | |
print "I'm sorry that doesn't seem to be one of the options" | |
return False | |
def tutorial(): | |
print "Welcome to tictactoe! Would you like to hear the instructions?" | |
answer = raw_input("Type 'y' or 'n' and then press Enter\n") | |
if answer.strip().lower() == "y": | |
print "This is the board:" | |
test_board = Board() | |
test_board.print_board() | |
print "You can declare a move by typing the name of the row and column" | |
print "For example typing, '0a' would place your marker like this:" | |
test_board.set_position("x", "0a") | |
test_board.print_board() | |
print "When you have three marks in a row, you win!\n" | |
return True | |
elif answer.strip().lower() == "n": | |
print "ok!\n" | |
return True | |
else: | |
print "I'm sorry that doesn't seem to be one of the options\n" | |
return False | |
def run(): | |
play_instructions = tutorial() | |
while not play_instructions: | |
play_instructions = tutorial() | |
game = Game() | |
# The game will keep looping until users choose to quit | |
while game.playing_game: | |
false_choice = game.playing_a_human() | |
while not false_choice: | |
false_choice = game.playing_a_human() | |
# Sets new player names with every round of the game | |
player_x = game.get_player_name("x") | |
player_o = game.get_player_name("o") | |
board = Board() | |
current_player = game.goes_first(player_x, player_o) | |
board.print_board() | |
# empty_spaces = board.check_empty_spaces() | |
# while empty_spaces: | |
winner = board.look_for_win() | |
while not winner: | |
# Turn loop runs at the same time as testing the integrity | |
# of the move, "playing" is a tuple created by get_player_name | |
move_placed = board.set_position(game.turn(current_player), current_player[1]) | |
while not move_placed: | |
move_placed = board.set_position(game.turn(current_player), current_player[1]) | |
board.print_board() | |
# Swaps turns | |
winner = board.look_for_win() | |
if current_player == player_x and not winner: | |
current_player = player_o | |
elif not winner: | |
current_player = player_x | |
if not board.check_empty_spaces(): | |
break | |
if winner: | |
congratulations = game.win(current_player) | |
else: | |
game.tie() | |
replay = game.play_again() | |
while not replay: | |
replay = game.play_again() | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment