# -*- coding: latin-1 -*- #Richard Albert Nichols III (http://www.gate.net/~inhahe) #todo: # mbtlr alone work if only 1 possibility import random random.seed() quit = False print l = raw_input("Who goes first: ").strip().lower() if not l: first = 1 else: first = l not in ["you", "u", "y", "computer", "the computer", "pc"] def showboard(): print """ %s ³ %s ³ %s ÄÄÄÅÄÄÄÅÄÄÄ %s ³ %s ³ %s ÄÄÄÅÄÄÄÅÄÄÄ %s ³ %s ³ %s """ % tuple([" XO OX"[x+first*3] for y in board for x in y]) wins = 0 cats=0 while 1: board = [[0,0,0],[0,0,0],[0,0,0]] player = first won=False for move in range(9): if player: showboard() catswon=False while 1: place = raw_input("Make your move: ").lower().strip() if not place: catswon=True print "Cat's game" break if place in ["quit", "q", "x", "exit", "bye", "stop"]: quit=True break if " " in place: place = "".join([x[0] for x in place.split()]) x, y = None, None if "t" in place: y=0 if "b" in place: y=2 if "r" in place: x=2 if "l" in place: x=0 if "m" in place or "c" in place: if x==None: x=1 if y==None: y=1 try: assert board[y][x]==0 except: print "Invalid move.\n" continue break if quit: break if catswon: break board[y][x]=2 else: won=False went=False for pair in [[2,0],[0,2]]: for y in [0,1,2]: if map(board[y].count, [1,2]) == pair: board[y][board[y].index(0)] = 1 won=pair[0] went=True break else: for x in [0,1,2]: c = [r[x] for r in board] if map(c.count, [1,2]) == pair: board[c.index(0)][x] = 1 won=pair[0] went=True break else: d = [board[i][i] for i in [0,1,2]] if map(d.count, [1,2]) == pair: i = d.index(0) board[i][i] = 1 won=pair[0] went=True break else: d = [board[i][2-i] for i in [0,1,2]] if map(d.count, [1,2]) == pair: i = d.index(0) board[i][2-i] = 1 won=pair[0] went=True break if went: break if not went: if move==0: board[random.choice([0,2])][random.choice([0,2])] = 1 elif move==1: if max([board[y][x] for y in [0,2] for x in [0,2]]): board[1][1]=1 else: board[random.choice([0,2])][random.choice([0,2])] = 1 elif move==2: fy = [1 in board[y] for y in [0,1,2]].index(1) fx = [board[fy][x] for x in [0,1,2]].index(1) if board[fy][1]==2: x, y = fx, 2-fy elif board[1][fx]==2: x, y = 2-fx, fy else: while 1: x, y = random.choice([[fx, 2-fy], [2-fx,fy]]) if not board[y][x]: break board[y][x]=1 elif move==3: if board[1][1]==1: x,y=1,1 while board[y][x]: x, y = random.choice([[0,1],[2,1],[1,0],[1,2]]) board[y][x]=1 else: fy = [1 in board[y] for y in [0,1,2]].index(1) fx = [board[fy][x] for x in [0,1,2]].index(1) if board[fy][1]==2: x, y = fx, 2-fy elif board[1][fx]==2: x, y = 2-fx, fy else: x, y = random.choice[[fx, 2-fy], [2-fx,fy]] board[y][x]=1 elif move==4: while 1: x, y = random.choice([[0,0],[0,2],[2,0],[2,2]]) if board[y][x]: continue if board[2-y][x] and board[1][x]: continue if board[y][2-x] and board[y][1]: continue break board[y][x]=1 else: while 1: x=random.randint(0,2) y=random.randint(0,2) if not board[y][x]: break board[y][x]=1 if won: break player = not player if won or quit: break if quit: break if won: wins+=1 showboard() print "I won!" print "My wins:",wins print "Your wins: 0" print "Cats:",cats print "-----------" print "I go first." first=0 else: showboard() cats+=1 i = raw_input("Who goes first: ").strip().lower() if not i: first = 1 else: if i in ["bye","exit", "quit", "q", "x"]: break first = i not in ["you", "u", "y", "computer", "the computer", "pc"] print print "My wins:",wins print "Your wins: 0" print "Cats:",cats #if this can be done more efficiently (with exactly teh same functionality), #email me at inhahe@gmail.com because I'd like to know how.