Guest User

ttt.py

a guest
Nov 10th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. '''
  4. TicTacToe v1.0 by Benni1000
  5. '''
  6.  
  7. import os
  8. import platform
  9. import random
  10.  
  11. class Player():
  12.     def __init__(self,name,cc):
  13.         self.name = name
  14.         self.cc = cc
  15.  
  16. def isNumber(s):
  17.     try:
  18.         fl = int(s)
  19.     except ValueError, TypeError:
  20.         return False
  21.     else:
  22.         return True
  23.  
  24. def setupPlayers():
  25.     players = []
  26.     for i in range(0,2):
  27.         n = raw_input("Please enter the name of Player %d: "%(i+1))
  28.         cc = raw_input("Should Player %d be ComputerControlled? (Y/N): "%(i+1))
  29.         computerControlled = False
  30.         if cc != "Y" and cc != "y" and cc != "N" and cc != "n":
  31.             print "Invalid Choice Player %d is not ComputerControlled!"%(i+1)
  32.         elif cc == "Y" or cc == "y":
  33.             print "Player %d is ComputerControlled!"%(i+1)
  34.             computerControlled = True
  35.         elif cc == "N" or cc == "n":
  36.             print "Player %d is not ComputerControlled!"%(i+1)
  37.         players.append(Player(n,computerControlled))
  38.     return players
  39.  
  40. def drawField(f):
  41.     print "[%s][%s][%s]"%(f[0],f[1],f[2])
  42.     print "[%s][%s][%s]"%(f[3],f[4],f[5])
  43.     print "[%s][%s][%s]"%(f[6],f[7],f[8])
  44.  
  45. def cls():
  46.     if platform.system().lower == "windows": os.system("cls")
  47.     else: os.system("clear")
  48.  
  49. def computerTurn(field):
  50.     while True:
  51.         r = random.randrange(1,10)
  52.         if field[r-1] == r:
  53.             return r
  54.  
  55. def getGameState(f):
  56.     z = "X"
  57.     for i in range(1,3):
  58.         if i == 2:
  59.             z = "O"
  60.         if f[0] == z and f[1] == z and f[2] == z: return i
  61.         if f[3] == z and f[4] == z and f[5] == z: return i
  62.         if f[6] == z and f[7] == z and f[8] == z: return i
  63.         if f[0] == z and f[3] == z and f[6] == z: return i
  64.         if f[1] == z and f[4] == z and f[7] == z: return i
  65.         if f[2] == z and f[5] == z and f[8] == z: return i
  66.         if f[0] == z and f[4] == z and f[8] == z: return i
  67.         if f[2] == z and f[4] == z and f[6] == z: return i
  68.     return 0
  69.  
  70. while True:
  71.     field = [1,2,3,4,5,6,7,8,9]
  72.     players = setupPlayers()
  73.     turn = 1
  74.     count = 1
  75.     cls()
  76.     while True:
  77.         st = getGameState(field)
  78.         if st != 0:
  79.             drawField(field)
  80.             print "%s won!\nPress [ENTER] to continue..."%(players[st-1].name)
  81.             raw_input()
  82.             cls()
  83.             break
  84.         if count > 9:
  85.             drawField(field)
  86.             print "DRAW! Nobody won.\nPress [ENTER] to continue..."
  87.             raw_input()
  88.             cls()
  89.             break
  90.         drawField(field)
  91.         print "%s's turn!"%(players[turn-1].name)
  92.         if players[turn-1].cc:
  93.             ch = computerTurn(field)
  94.         else:
  95.             ch = raw_input("Please enter a number between 1 and 9: ")
  96.         cls()
  97.         if isNumber(ch):
  98.             ch = int(ch)
  99.             if ch < 10 and ch > 0:
  100.                 if field[ch-1] != ch:
  101.                     print "Error: Field alredy taken!"
  102.                 else:
  103.                     if turn == 1:
  104.                         field[ch-1] = "X"
  105.                         turn = 2
  106.                     else:
  107.                         field[ch-1] = "O"
  108.                         turn = 1
  109.                     count += 1
  110.             else:
  111.                 print "Error: The number is not between 1 and 9!"
  112.         else:
  113.             print "Error: That is NOT a number!"
Advertisement
Add Comment
Please, Sign In to add comment