Advertisement
Darryl-Medley

TicTacToe 2.2

Apr 15th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. #Tic Tac Toe v002.2
  2. # Originally written by Scott Beardwood with modification by Lee Crocker
  3. # This version has further mods (boardTest function) by Darryl Medley for TWiT Coding 101
  4. import random
  5. def drawboard():
  6.     print
  7.     print " %s| %s| %s" % (board[1],board[2],board[3])
  8.     print "__|__|__"
  9.     print " %s| %s| %s" % (board[4],board[5],board[6])
  10.     print "__|__|__"
  11.     print " %s| %s| %s" % (board[7],board[8],board[9])
  12.     print
  13.     return  
  14.  
  15. wins = [
  16.     [1,2,3], [4,5,6], [7,8,9],
  17.     [1,4,7], [2,5,8], [3,6,9],
  18.     [1,5,9], [3,5,7] ]
  19.  
  20. def boardTest(testType, mark1, mark2):
  21.     for w in wins:   # loop through all potential win combos
  22.         # create a sub-list containing the marks (values) of the 3 board list spots
  23.         # we want to test using the w sub-list of wins as indexes of the board list
  24.         boardMarks = [board[x] for x in w]   # use "list comprehension" to create list
  25.  
  26.         if testType == "isWinner":
  27.             if boardMarks.count(mark1) == 3:
  28.                 return True   # all 3 spots are X's or 0's
  29.         else:   # testMode is canWin or blockMove
  30.             if (boardMarks.count(mark1) == 2) and (boardMarks.count(mark2) == 0):
  31.                 # 2 mark1s and a no mark2 qualifies for a winning or blocking move
  32.                 # We need to return the unmarked board #. Use a "generator" to get the index.
  33.                 return w[next((idx for idx, mrk in enumerate(boardMarks) if mrk != mark1))]
  34.            
  35.     return 0   # no tests returned true so return false
  36.            
  37.        
  38. #Start of main game.
  39. #
  40. game = 0
  41. random.seed()
  42. while (game == 0):
  43.  
  44.     board = ["-","1","2","3","4","5","6","7","8","9"]
  45.     print "Welcome to Tic Tac Toe"
  46.     print
  47.     player1 = raw_input("Player one's name? ")
  48.     player2 = raw_input("Player two's name? (type 'python' for computer opponent) ")
  49.     movesplayed = 0
  50.     playersturn = 1
  51.     gamewon = False
  52.     while (movesplayed < 9):
  53.         drawboard()
  54.         if playersturn == 1:
  55.             nextmove = raw_input("Your Move %s ? " % (player1))
  56.             if board[(int(nextmove))] == nextmove:
  57.                 board[(int(nextmove))] = "X"
  58.                 if boardTest("isWinner","X",""):
  59.                     gamewon = True
  60.                     winner = player1
  61.                     break
  62.                 playersturn = 0
  63.                 movesplayed = movesplayed+1
  64.             else:
  65.                 print "the move was invalid or space has been taken"
  66.         else:
  67.             if player2.lower() == "python":
  68.                 checkWin = boardTest("canWin","0","X")
  69.                 stopwin = boardTest("blockMove","X","0")
  70.                 if not checkWin:
  71.                     if not stopwin:
  72.                         nextmove = str(random.randrange(1,9))
  73.                     else:
  74.                         nextmove = str(stopwin)
  75.                     print "Python's move was %s " % (nextmove)
  76.                 else:
  77.                     nextmove = str(checkWin)
  78.                     print "Python's move was %s " % (nextmove)
  79.             else:
  80.                 nextmove = raw_input("Your Move %s ? " % (player2))
  81.                
  82.             if board[(int(nextmove))] == nextmove:
  83.                 board[(int(nextmove))] = "0"
  84.                 if boardTest("isWinner","0",""):
  85.                     gamewon = True
  86.                     winner = player2
  87.                     break
  88.                 playersturn = 1
  89.                 movesplayed = movesplayed+1
  90.             else:
  91.                 print "the move was invalid or space has been taken"
  92.        
  93.     if gamewon:
  94.         drawboard()
  95.         print "Congratulations %s! You won this game." % winner
  96.     else:
  97.         print "Oh well %s the game with %s was a draw....." % (player1,player2)
  98.        
  99.     print
  100.        
  101.     if raw_input("Play again? (Y or N) ").lower() == 'n':
  102.         game = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement