Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. def winner(board):
  2. """This function accepts the Tic-Tac-Toe board as a parameter.
  3. If there is no winner, the function will return the empty string "".
  4. If the user has won, it will return "X", and if the computer has
  5. won it will return "O"."""
  6.  
  7. # Check rows for winner
  8. for row in range(3):
  9. if (board[row][0] == board[row][1] == board[row][2]) and\
  10. (board[row][0] != " "):
  11. return board[row][0]
  12.  
  13. # Check columns for winner
  14. for col in range(3):
  15. if (board[0][col] == board[1][col] == board[2][col]) and\
  16. (board[0][col] != " "):
  17. return board[0][col]
  18.  
  19. # Check diagonal (top-left to bottom-right) for winner
  20. for row in range(3) and col in range(3):
  21. if (board[0][0] == board[1][1] == board[2][2]) and\
  22. (board[0][0] != " "):
  23. return board[0][0]
  24.  
  25. # Check diagonal (bottom-left to top-right) for winner
  26. for row in range(3) and col in range(3):
  27. if (board[2][0] == board[1][1] == board[0][2]) and\
  28. (board[2][0] != " "):
  29. return board[2][0]
  30.  
  31. # No winner: return the empty string
  32. return ""
  33.  
  34. def display_board(board):
  35. """This function accepts the Tic-Tac-Toe board as a parameter.
  36. It will print the Tic-Tac-Toe board grid (using ASCII characters)
  37. and show the positions of any X's and O's. It also displays
  38. the column and row numbers on top and beside the board to help
  39. the user figure out the coordinates of their next move.
  40. This function does not return anything."""
  41.  
  42. print " 0 1 2"
  43. print "0: "+board[0][0]+" | "+board[0][1]+" | "+board[0][2]
  44. print " ---+---+---"
  45. print "1: "+board[1][0]+" | "+board[1][1]+" | "+board[1][2]
  46.  
  47. print " ---+---+---"
  48. print "2: "+board[2][0]+" | "+board[2][1]+" | "+board[2][2]
  49. print
  50.  
  51. def make_user_move(board):
  52. """This function accepts the Tic-Tac-Toe board as a parameter.
  53. It will ask the user for a row and column. If the row and
  54. column are each within the range of 0 and 2, and that square
  55. is not already occupied, then it will place an "X" in that square."""
  56.  
  57. valid_move = False
  58. while not valid_move:
  59. row = input("What row would you like to move to (0-2):")
  60. col = input("What col would you like to move to (0-2):")
  61. if (0<=row<=2) and (0<=col<=2) and (board[row][col]==" "):
  62. board[row][col] = 'X'
  63. valid_move = True
  64. else:
  65. print "Sorry, invalid square. Please try again!\n"
  66.  
  67.  
  68. def make_computer_move(board):
  69. """This function accepts the Tic-Tac-Toe board as a parameter.
  70. It will randomly pick row and column values between 0 and 2.
  71. If that square is not already occupied it will place an "O"
  72. in that square. Otherwise, another random row and column
  73. will be generated."""
  74.  
  75. import random
  76. valid_move = False
  77. while not valid_move:
  78. row = random.randrange(3)
  79. col = random.randrange(3)
  80. if board[row][col] == " ":
  81. board[row][col] = "O"
  82. valid_move = True
  83. else:
  84. make_computer_move(board)
  85.  
  86. def main():
  87. """Our Main Game Loop:"""
  88.  
  89. free_cells = 9
  90. users_turn = True
  91. ttt_board = [[" ", " ", " "],[" ", " ", " "],[" ", " ", " "]]
  92.  
  93. while not winner(ttt_board) and (free_cells > 0):
  94. display_board(ttt_board)
  95. if users_turn:
  96. make_user_move(ttt_board)
  97. users_turn = not users_turn
  98. else:
  99. make_computer_move(ttt_board)
  100. users_turn = not users_turn
  101. free_cells -= 1
  102.  
  103. display_board(ttt_board)
  104. if (winner(ttt_board) == 'X'):
  105. print "Y O U W O N !"
  106. elif (winner(ttt_board) == 'O'):
  107. print "I W O N !"
  108. else:
  109. print "S T A L E M A T E !"
  110. print "\n*** GAME OVER ***\n"
  111.  
  112. # Start the game!
  113. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement