Last active
February 10, 2016 20:48
-
-
Save michelleye123/48b06ebc24c9adb468a4 to your computer and use it in GitHub Desktop.
A game of Tic Tac Toe for two human people
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
# Feb 2016 | |
# Michelle Ye | |
# RC Int #2 | |
class Board: | |
values = [" "] * 9 | |
keep_playing = 1 | |
def __init__(self, symb1='x', symb2='o'): | |
self.symbol_map = {0: symb1, 1: symb2} | |
def __repr__(self): | |
my_str = "\nBoard: Positions:\n" | |
for i in range(2, -1, -1): | |
my_str += "[%s] [%s] [%s]" % (self.values[i*3], self.values[i*3+1], self.values[i*3+2]) | |
my_str += " [%d] [%d] [%d]\n" % (i*3+1, i*3+2, i*3+3) | |
return my_str | |
def enter_turn(self, position, playerID): | |
symb = self.symbol_map[playerID] | |
if self.values[position-1] == " ": | |
self.values[position-1] = symb | |
print(self) | |
return True | |
else: | |
return "Taken_Error" | |
def check_if_winner(self, playerID): | |
v = self.symbol_map[playerID] | |
for i in range(3): | |
if v in self.values[i*3] and v in self.values[i*3+1] and v in self.values[i*3+2]: | |
return self.announce_winner(playerID, "horizontally") | |
elif v in self.values[i] and v in self.values[i+3] and v in self.values[i+6]: | |
return self.announce_winner(playerID, "vertically") | |
if v in self.values[0] and v in self.values[4] and v in self.values[8] or \ | |
v in self.values[2] and v in self.values[4] and v in self.values[6]: | |
return self.announce_winner(playerID, "diagonally") | |
return 1 # self.keep_playing = 1 | |
def announce_winner(self, playerID, win_type): | |
print ("\nPlayer %s wins %s!" % (self.symbol_map[playerID], win_type)) | |
return 0 # self.keep_playing = 0 | |
def start_a_game(): | |
#start = input("Press Enter to start a new game: ") | |
start="" | |
if start == "": | |
print("\nNew game!") | |
board = Board() | |
print(board) | |
return board | |
else: | |
print("..awkward") | |
def a_turn(board, playerID): | |
while 1: | |
try: | |
go_here = int( input("Player %s's turn. Enter a number: " %(board.symbol_map[playerID]))) | |
if go_here in range(1, 10): | |
return board.enter_turn(go_here, playerID) | |
print("That's not on the board! Please enter an integer between 1 and 9. \nStill ",end="") | |
except ValueError: | |
print("Please enter an integer between 1 and 9. \nStill ",end="") | |
# main loop | |
def A_Game(): | |
board = start_a_game() | |
playerID = 0 | |
turn_count = 1 | |
while board.keep_playing: | |
turn_success = False | |
while turn_success is not True: | |
turn_success = a_turn(board, playerID) | |
if turn_success == "Taken_Error": | |
print("Position already taken. Pick an empty position.") | |
board.keep_playing = board.check_if_winner(playerID) | |
if turn_count == 9 and board.keep_playing: | |
print("\nBoard full. No one is a winner.") | |
exit() | |
turn_count += 1 | |
playerID = not playerID | |
A_Game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment