Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. from random import randint
  2.  
  3. # RC TTT - Kevin Johnson
  4.  
  5. game_board = [["-", "-", "-"], ["-", "-", "-"], ["-", "-", "-"]]
  6.  
  7. def print_board(board):
  8. for row in board:
  9. print " ".join(row)
  10.  
  11. def check_win_tie(board):
  12. # horizontal
  13. win_one = [board[0][0], board[0][1], board[0][2]]
  14. win_two = [board[1][0], board[1][1], board[1][2]]
  15. win_three = [board[2][0], board[2][1], board[2][2]]
  16. # vertical
  17. win_four = [board[0][0], board[1][0], board[2][0]]
  18. win_five = [board[0][1], board[1][1], board[2][1]]
  19. win_six = [board[0][2], board[1][2], board[2][2]]
  20. # diagonal
  21. win_seven = [board[0][0], board[1][1], board[2][2]]
  22. win_eight = [board[0][2], board[1][1], board[2][0]]
  23.  
  24. wins = [win_one, win_two, win_three, win_four, win_five, win_six, win_seven, win_eight]
  25.  
  26. for win in wins:
  27. moves_in_win = []
  28. for position in win:
  29. moves_in_win.append(position)
  30.  
  31. if moves_in_win == ["X","X","X"]:
  32. print_board(board)
  33. print("X Wins!")
  34. quit()
  35.  
  36. elif moves_in_win == ["O","O","O"]:
  37. print_board(board)
  38. print("O Wins!")
  39. quit()
  40.  
  41. # tie
  42. current_positions = []
  43. for row in board:
  44. for position in row:
  45. current_positions.append(position)
  46.  
  47. if "-" not in current_positions:
  48. print_board(board)
  49. print ("Tie!")
  50. quit()
  51.  
  52. in_progress = True
  53. while in_progress == True:
  54.  
  55. print_board(game_board)
  56.  
  57. # player
  58. input = "X"
  59.  
  60. input_validated = False
  61. while input_validated == False:
  62.  
  63. index_row = int(raw_input(input + ' Row:')) - 1
  64. index_col = int(raw_input(input + ' Column:')) - 1
  65.  
  66. if ((index_row < 0 or index_col < 0) or (index_row > 2 or index_col > 2)):
  67. print("Not on Board")
  68.  
  69. elif game_board[index_row][index_col] is not "-":
  70. print("Already Taken")
  71.  
  72. else:
  73. input_validated = True
  74. game_board[index_row][index_col] = input
  75.  
  76. check_win_tie(game_board)
  77.  
  78. # computer
  79. input = "O"
  80.  
  81. computer_validated = False
  82. while computer_validated == False:
  83.  
  84. random_row = randint(1,3) - 1
  85. random_col = randint(1,3) - 1
  86.  
  87. if (game_board[random_row][random_col]) is "-":
  88. computer_validated = True
  89. game_board[random_row][random_col] = input
  90. else:
  91. computer_validated = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement