Guest User

Untitled

a guest
Jul 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. def one_round(player):
  2. #Distinguish player 1 from player 2
  3. if player == "p1":
  4. p = "Player 1"
  5. elif player == "p2":
  6. p = "Player 2"
  7. pin = raw_input("%s move (a/b/c for row | 1/2/3 for column):" % (p)) #receive input for player from console
  8. #separating row from column
  9. row = pin[0]
  10. col = int(pin[1]) - 1
  11.  
  12. if row == "a": #changing the row letter into a digit for the index of board
  13. row = 0
  14. elif row =="b":
  15. row = 1
  16. elif row == "c":
  17. row = 2
  18.  
  19.  
  20. if board[row][col] == 'X' or board[row][col] == 'O':
  21. print "Oops this spot has been taken, please pick another"
  22. pin = raw_input("%s move (a/b/c for row | 1/2/3 for column):" % (p)) #receive input for player from console
  23. #separating row from column
  24. row = pin[0]
  25. col = int(pin[1]) - 1
  26.  
  27. if row == "a": #changing the row letter into a digit for the index of board
  28. row = 0
  29. elif row =="b":
  30. row = 1
  31. elif row == "c":
  32. row = 2
  33.  
  34. if player == "p1": #Determining whether to use X or O
  35. x = 'X'
  36. elif player == "p2":
  37. x = 'O'
  38.  
  39. del board[row][col] #Swapping out the dashes for a symbol
  40. board[row].insert(col, x)
  41.  
  42. print str(" ".join(board[0])) + 'n' + str(" ".join(board[1])) + 'n' + str(" ".join(board[2])) #Printing board
  43.  
  44.  
  45. 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
  46. print '%s wins!' % (p) + 'n' + 'Game Over!'
  47. return True
  48. 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
  49. print 'Draw!' + 'n' + 'Game Over!'
  50. return True
  51. else:
  52. return False
  53. #---------------------------------------------------------------------------------------------------------------------
  54. """Tic tac toe game. (Noughts and crosses)"""
  55. 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!"
  56. #initialising board and players
  57. board = [["-", "-", "-"], ["-", "-", "-"], ["-", "-", "-"]]
  58. p1 = False
  59. p2 = False
  60. while (p1 == False and p2 == False): #running game
  61. p1 = one_round("p1")
  62. if p1 == True:
  63. break
  64. p2 = one_round("p2")
Add Comment
Please, Sign In to add comment