Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. from random import randint
  2.  
  3. board_large = []
  4. board_small = []
  5. player_one = {
  6. "name": "Player 1",
  7. "wins": 0,
  8. "lose": 0
  9. }
  10. player_two = {
  11. "name": "Player 2",
  12. "wins": 0,
  13. "lose": 0
  14. }
  15. total_turns = 0
  16. win_state_change = 0
  17.  
  18.  
  19. def build_board_large(board):
  20. """create a 5 x 5 board"""
  21. for item in range(10):
  22. board.append(["O"] * 10)
  23.  
  24.  
  25. def build_board_small(board): # create a 3 x 3 board
  26. """create a 3 x 3 board"""
  27. for item in range(10):
  28. board.append(["O"] * 10)
  29.  
  30.  
  31. def show_board(board_one, board_two):
  32. """make the lists look more like a simple battleship graph"""
  33. print "Board One"
  34. for row in board_one:
  35. print " ".join(row)
  36. print "Board Two"
  37. for row in board_two:
  38. print " ".join(row)
  39.  
  40.  
  41. def load_game(board_one, board_two):
  42. """each time the players decides to play again, start again from a fresh slate"""
  43. print "Let's play Battleship!"
  44. print "Turn 1"
  45. del board_one[:]
  46. del board_two[:]
  47. build_board_large(board_one)
  48. build_board_small(board_two)
  49. show_board(board_one, board_two)
  50. ship_col_large = (lambda x: randint(1, len(x)))(board_one)
  51. ship_row_large = (lambda x: randint(1, len(x[0])))(board_one)
  52. ship_col_large1 = (lambda x: randint(1, len(x)))(board_one)
  53. ship_row_large1 = (lambda x: randint(1, len(x[0])))(board_one)
  54. ship_col_small = (lambda x: randint(1, len(x)))(board_two)
  55. ship_row_small = (lambda x: randint(1, len(x[0])))(board_two)
  56. ship_col_small1 = (lambda x: randint(1, len(x)))(board_two)
  57. ship_row_small1 = (lambda x: randint(1, len(x[0])))(board_two)
  58. print "Board 1 ship column (on board one): " + str(ship_row_large)
  59. print "Board 1 ship row (on board one): " + str(ship_col_large)
  60. print "Board 2 ship column (on board one): " + str(ship_row_large1)
  61. print "Board 2 ship row (on board one): " + str(ship_col_large1)
  62. print "Board 3 ship column (on board two): " + str(ship_row_small)
  63. print "Board 3 ship row (on board two): " + str(ship_col_small)
  64. print "Board 4 ship column (on board two): " + str(ship_row_small1)
  65. print "Board 4 ship row (on board two): " + str(ship_col_small1)
  66. return {
  67. 'ship_col_large': ship_col_large,
  68. 'ship_row_large': ship_row_large,
  69. 'ship_col_large1': ship_col_large1,
  70. 'ship_row_large1': ship_row_large1,
  71. 'ship_col_small': ship_col_small,
  72. 'ship_row_small': ship_row_small,
  73. 'ship_col_small1': ship_col_small1,
  74. 'ship_row_small1': ship_row_small1
  75. }
  76.  
  77. ship_points = load_game(board_large, board_small) # assign the new ship locations to the new dictionary
  78.  
  79.  
  80. def player_turns():
  81. """alternate between player turns by checking for odd numbers"""
  82. if total_turns % 2 == 0:
  83. return player_two
  84. else:
  85. return player_one
  86.  
  87.  
  88. def play_again():
  89. """if user wants to play again, restart / reload game"""
  90. global total_turns
  91. global ship_points
  92. answer = str(raw_input("Type yes to play again."))
  93. if answer == "yes":
  94. total_turns = 0 # reset / start over from player one again
  95. show_board(board_large, board_small)
  96. ship_points = load_game(board_large, board_small)
  97. else:
  98. exit()
  99.  
  100.  
  101. def best_out_of(win_state, player):
  102. """check the game statistics"""
  103. global total_turns
  104. if win_state == 1 and player["wins"] < 2: # only do a check if player one the current game
  105. print "%s wins this game!" % (player["name"])
  106. play_again()
  107. elif win_state == 0 and total_turns == 6:
  108. print "This match was a draw"
  109. play_again()
  110. elif win_state != 0 and total_turns == 6:
  111. play_again()
  112. elif player["wins"] >= 2: # check who won best out of 3
  113. print "%s wins best out of 3" % (player["name"])
  114. play_again()
  115. elif player["lose"] >= 2:
  116. print "%s lost best out of 3" % (player["name"])
  117. play_again()
  118. else:
  119. play_again()
  120.  
  121.  
  122. def input_check(ship_row, ship_col, player, board):
  123. """check/handle the players guesses of the ship points"""
  124. global win_state_change
  125. guess_col = 0
  126. guess_row = 0
  127. while True:
  128. try:
  129. guess_row = int(raw_input("Guess Row:")) - 1
  130. guess_col = int(raw_input("Guess Col:")) - 1
  131. except ValueError:
  132. print "Enter a number only."
  133. continue
  134. else:
  135. break
  136. match = guess_row == ship_row - 1 and guess_col == ship_col - 1
  137. not_on_small_board = (guess_row < 0 or guess_row > 9) or (guess_col < 0 or guess_col > 9)
  138. not_on_large_board = (guess_row < 0 or guess_row > 9) or (guess_col < 0 or guess_col > 9)
  139.  
  140. if match:
  141. win_state_change = 1 # notes that someone has won the current game
  142. player["wins"] += 1
  143. print "Congratulations! You sunk my battleship!"
  144. best_out_of(win_state_change, player)
  145. elif not match and player == player_two: # check the current player to then correlate with the correct board size
  146. if not_on_small_board:
  147. print "Oops, that's not even in the ocean."
  148. elif board[guess_row][guess_col] == "X":
  149. print "You guessed that one already."
  150. else:
  151. print "You missed my battleship!"
  152. board[guess_row][guess_col] = "X"
  153. win_state_change = 0
  154. show_board(board_large, board_small)
  155. elif not match and player == player_one:
  156. if not_on_large_board:
  157. print "Oops, that's not even in the ocean."
  158. elif board[guess_row][guess_col] == "X":
  159. print "You guessed that one already."
  160. else:
  161. print "You missed my battleship!"
  162. board[guess_row][guess_col] = "X"
  163. win_state_change = 0
  164. show_board(board_large, board_small)
  165. else:
  166. return win_state_change
  167.  
  168. """Start the game logic"""
  169. for games in range(3):
  170. games += 1 # 3 games total
  171. for turns in range(6): # 6 turns total = 3 turns for each player
  172. total_turns += 1
  173. if player_turns() == player_one:
  174. print "It's player Ones's turn"
  175. input_check(
  176. ship_points['ship_row_large'],
  177. ship_points['ship_col_large'],
  178. ship_points['ship_row_large1'],
  179. ship_points['ship_col_large1'],
  180. player_one, board_large
  181. )
  182. elif player_turns() == player_two:
  183. print "It's player Two's turn"
  184. input_check(
  185. ship_points['ship_row_small'],
  186. ship_points['ship_col_small'],
  187. ship_points['ship_row_small1'],
  188. ship_points['ship_col_small1'],
  189. player_two, board_small
  190. )
  191. else:
  192. break
  193. if total_turns == 6 and player_turns() == player_one:
  194. best_out_of(win_state_change, player_one)
  195. elif total_turns == 6 and player_turns() == player_two:
  196. best_out_of(win_state_change, player_two)
  197. else:
  198. continue
  199. if games == 3:
  200. print "The game has ended."
  201. # print "Wins: %s" % (player_one[entry])
  202. exit()
  203. else:
  204. continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement