Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Tic Tac Toe v002.2
- # Originally written by Scott Beardwood with modification by Lee Crocker
- # This version has further mods (boardTest function) by Darryl Medley for TWiT Coding 101
- import random
- def drawboard():
- print
- print " %s| %s| %s" % (board[1],board[2],board[3])
- print "__|__|__"
- print " %s| %s| %s" % (board[4],board[5],board[6])
- print "__|__|__"
- print " %s| %s| %s" % (board[7],board[8],board[9])
- print
- return
- wins = [
- [1,2,3], [4,5,6], [7,8,9],
- [1,4,7], [2,5,8], [3,6,9],
- [1,5,9], [3,5,7] ]
- def boardTest(testType, mark1, mark2):
- for w in wins: # loop through all potential win combos
- # create a sub-list containing the marks (values) of the 3 board list spots
- # we want to test using the w sub-list of wins as indexes of the board list
- boardMarks = [board[x] for x in w] # use "list comprehension" to create list
- if testType == "isWinner":
- if boardMarks.count(mark1) == 3:
- return True # all 3 spots are X's or 0's
- else: # testMode is canWin or blockMove
- if (boardMarks.count(mark1) == 2) and (boardMarks.count(mark2) == 0):
- # 2 mark1s and a no mark2 qualifies for a winning or blocking move
- # We need to return the unmarked board #. Use a "generator" to get the index.
- return w[next((idx for idx, mrk in enumerate(boardMarks) if mrk != mark1))]
- return 0 # no tests returned true so return false
- #Start of main game.
- #
- game = 0
- random.seed()
- while (game == 0):
- board = ["-","1","2","3","4","5","6","7","8","9"]
- print "Welcome to Tic Tac Toe"
- print
- player1 = raw_input("Player one's name? ")
- player2 = raw_input("Player two's name? (type 'python' for computer opponent) ")
- movesplayed = 0
- playersturn = 1
- gamewon = False
- while (movesplayed < 9):
- drawboard()
- if playersturn == 1:
- nextmove = raw_input("Your Move %s ? " % (player1))
- if board[(int(nextmove))] == nextmove:
- board[(int(nextmove))] = "X"
- if boardTest("isWinner","X",""):
- gamewon = True
- winner = player1
- break
- playersturn = 0
- movesplayed = movesplayed+1
- else:
- print "the move was invalid or space has been taken"
- else:
- if player2.lower() == "python":
- checkWin = boardTest("canWin","0","X")
- stopwin = boardTest("blockMove","X","0")
- if not checkWin:
- if not stopwin:
- nextmove = str(random.randrange(1,9))
- else:
- nextmove = str(stopwin)
- print "Python's move was %s " % (nextmove)
- else:
- nextmove = str(checkWin)
- print "Python's move was %s " % (nextmove)
- else:
- nextmove = raw_input("Your Move %s ? " % (player2))
- if board[(int(nextmove))] == nextmove:
- board[(int(nextmove))] = "0"
- if boardTest("isWinner","0",""):
- gamewon = True
- winner = player2
- break
- playersturn = 1
- movesplayed = movesplayed+1
- else:
- print "the move was invalid or space has been taken"
- if gamewon:
- drawboard()
- print "Congratulations %s! You won this game." % winner
- else:
- print "Oh well %s the game with %s was a draw....." % (player1,player2)
- print
- if raw_input("Play again? (Y or N) ").lower() == 'n':
- game = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement