Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def one_round(player):
- #Distinguish player 1 from player 2
- if player == "p1":
- p = "Player 1"
- elif player == "p2":
- p = "Player 2"
- pin = raw_input("%s move (a/b/c for row | 1/2/3 for column):" % (p)) #receive input for player from console
- #separating row from column
- row = pin[0]
- col = int(pin[1]) - 1
- if row == "a": #changing the row letter into a digit for the index of board
- row = 0
- elif row =="b":
- row = 1
- elif row == "c":
- row = 2
- if board[row][col] == 'X' or board[row][col] == 'O':
- print "Oops this spot has been taken, please pick another"
- pin = raw_input("%s move (a/b/c for row | 1/2/3 for column):" % (p)) #receive input for player from console
- #separating row from column
- row = pin[0]
- col = int(pin[1]) - 1
- if row == "a": #changing the row letter into a digit for the index of board
- row = 0
- elif row =="b":
- row = 1
- elif row == "c":
- row = 2
- if player == "p1": #Determining whether to use X or O
- x = 'X'
- elif player == "p2":
- x = 'O'
- del board[row][col] #Swapping out the dashes for a symbol
- board[row].insert(col, x)
- print str(" ".join(board[0])) + 'n' + str(" ".join(board[1])) + 'n' + str(" ".join(board[2])) #Printing board
- if board[0] == [x, x, x] or board[1] == [x, x, x] or board[2] == [x, x, x] or (board[0][0] == x and board[1][0] == x and board[2][0] == x) or (board[0][1] == x and board[1][1] == x and board[2][1] == x) or (board[0][2] == x and board[1][2] == x and board[2][2] == x) or (board[0][0] == x and board[1][1] == x and board[2][2] == x) or (board[0][2] == x and board[1][1] == x and board[2][0] == x): #Checking for winner
- print '%s wins!' % (p) + 'n' + 'Game Over!'
- return True
- elif board[0][0] != '-' and board[0][1] != '-' and board[0][2] != '-' and board[1][0] != '-' and board[1][1] != '-' and board[1][2] != '-' and board[2][0] != '-' and board[2][1] != '-' and board[2][2] != '-': #Checking for draw
- print 'Draw!' + 'n' + 'Game Over!'
- return True
- else:
- return False
- #---------------------------------------------------------------------------------------------------------------------
- """Tic tac toe game. (Noughts and crosses)"""
- print "Welcome to Tic Tac Toe!" + "n" + " Find a friend and start the game by typing coordinates for a 3x3 grid labelled with the letters a,b,c from top to bottom along the vertical axis and with 1,2,3 from left to right along the horizontal axis." + "n" + "Have Fun!"
- #initialising board and players
- board = [["-", "-", "-"], ["-", "-", "-"], ["-", "-", "-"]]
- p1 = False
- p2 = False
- while (p1 == False and p2 == False): #running game
- p1 = one_round("p1")
- if p1 == True:
- break
- p2 = one_round("p2")
Add Comment
Please, Sign In to add comment