From 8368c2e76d56c844e20bd65cb5f10d28530a50dc Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Fri, 12 Sep 2014 22:02:42 +0200 Subject: [PATCH] Added menu to tictactoe program --- tictactoe.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/tictactoe.py b/tictactoe.py index c8cd073..7d3b14e 100644 --- a/tictactoe.py +++ b/tictactoe.py @@ -86,23 +86,24 @@ class AI(Player): #Check for win chance for x,y in board.get_empty_fields(): boardcopy.set(x,y,self) - # Check for win - if boardcopy.get_winner() == self: - return (x,y) # Check for chance chance = self.count_win_chances(boardcopy) - enemy.count_win_chances(boardcopy) - moves.append( (chance,(x,y)) ) + # Check for win + if boardcopy.get_winner() == self: + chance += 1000 + # Check if enemy win boardcopy.set(x,y,enemy) if boardcopy.get_winner() == enemy: - return (x,y) + chance += 500 boardcopy.set(x,y,Board.empty) + moves.append( (chance,(x,y)) ) moves.sort(key=lambda x: -x[0]) @@ -119,7 +120,7 @@ class AI(Player): class Human(Player): def play(self, enemy, board): while True: - s = input("Player %s's turn:") + s = input("Player %s's turn:" % self.rep) try: (x,y) = map(int, s.split(",")) return (x,y) @@ -129,9 +130,25 @@ class Human(Player): def rotate(l): return l[1:] + l[:1] +def menu(prompt,choices): + print("> %s" % prompt) + for i,choice in enumerate(choices): + print("%d. %s" % (i,choice)) + return int(input("Your choice: ")) + if __name__ == "__main__": - board = Board(size=6) - players = [ AI("X"), AI("O") ] + c = menu("Playmode",["Ai vs Ai","Ai vs Player","Player vs Player"]) + if c == 0: + players = [ AI("X"), AI("O") ] + elif c == 1: + players = [ Human("X"), AI("O") ] + elif c == 2: + players = [ Human("X"), Human("O") ] + else: + import sys + sys.exit(0) + + board = Board(size=3) print(str(board))