Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. """
  2. TODO
  3. Make multiple battleships:
  4. You will need to be careful because you need to make sure that you do not place battleships on top of each other on the game board.
  5. You will also want to make sure that you balance the size of the board with the number of ships so the game is still challenging and fun to play.
  6.  
  7. Make battleships of different sizes:
  8. This is trickier than it sounds. All the parts of the battleship need to be vertically or horizontally touching.
  9. You will need to make sure you do not accidentally place part of a ship off the side of the board.
  10.  
  11. Make your game a two-player game:
  12. Use functions to allow your game to have more features like rematches, statistics and more!
  13. """
  14.  
  15. import os
  16. import time
  17. from random import randint
  18.  
  19. #cls function
  20. clear = lambda: os.system('cls')
  21.  
  22. #declares board as an empty list
  23. board = []
  24.  
  25. ships = {
  26. "Carrier": 5,
  27. "Battleship": 4,
  28. "Cruiser": 3,
  29. "Submarine": 3,
  30. "Destroyer": 2
  31. }
  32.  
  33. turn = 0
  34.  
  35. for x in range(11):
  36. board.append(["O"] * 10)
  37.  
  38. def print_menuscreen():
  39. print
  40. print " Battleship! Written by samillwong" + "\n" + " ",
  41. line()
  42. print
  43. print " Arrange your ships on the grid by inputting the coordinates" + "\n" + " (e.g. A6) and orientation (e.g. H)." + "\n"
  44. print " Take turns firing a salvo at your enemy by inputting the" + "\n" + " coordinate you wish to fire at. (e.g. C3)" + "\n"
  45. print " The first player to sink all opposing ships wins." + "\n" + "\n" + " ",
  46. line()
  47. print
  48. ask_username()
  49.  
  50. def userlogin():
  51. user = raw_input("Username: ")
  52. passw = raw_input("Password: ")
  53. f = open("users.txt", "w+")
  54. for line in f.readlines():
  55. us, pw = line.strip().split("|")
  56. if (user in us) and (passw in pw):
  57. print "Login successful!"
  58. return True
  59. print "Wrong username/password"
  60. return False
  61.  
  62. def ask_username():
  63. username = ""
  64. while username == "":
  65. username = raw_input(" Enter your username to start: ")
  66. #temp
  67. print "\n" + " Welcome to Battleship, " + username + "!"
  68. time.sleep(3)
  69.  
  70. #WORK IN PROGRESS
  71. """
  72. f = open("players.txt", "a+")
  73. f.seek(0)
  74. first_char = f.read(1)
  75. if not first_char:
  76. print f.write("List of players:")
  77. else:
  78. f.seek(0)
  79.  
  80. for line in f.readlines():
  81. if username in line:
  82. print "\n" + " Welcome back, " + username + "!"
  83. time.sleep(3)
  84. else:
  85. print "\n" + " Welcome to Battleship, " + username + "!"
  86. time.sleep(3)
  87. f.write(username)
  88. f.close()
  89. """
  90.  
  91. def print_board(board):
  92. print_header()
  93.  
  94. #print horizontal grid numbers
  95. print " ",
  96. for i in range(10):
  97. print " " + str(i+1) + " ",
  98. print "\n"
  99.  
  100. #print horizontal lines and cell dividers
  101. for i in range(10):
  102. if i != 9:
  103. print " ",
  104. print " -----------------------------------------------------------"
  105. for j in range(11):
  106. if j != 11:
  107. print " | ",
  108. print
  109.  
  110. print " -----------------------------------------------------------"
  111.  
  112. def print_header():
  113. print "\n" + " Turn", turn + 1
  114. print " ",
  115. line()
  116.  
  117. def line():
  118. print "-----------------------------------------------------------------" #65 dashes
  119.  
  120. def random_row(board):
  121. return randint(1, len(board) - 1)
  122.  
  123. def random_col(board):
  124. return randint(1, len(board[0]) - 1)
  125.  
  126. clear()
  127. print_menuscreen()
  128. clear()
  129. print "\n" + " Let's play Battleship!"
  130. print_board(board)
  131.  
  132. ship_row = random_row(board)
  133. ship_col = random_col(board)
  134.  
  135. #that aint cheating, thats debugging :^)
  136. print "Debug:", "x =", ship_row + 1,",", "y =", ship_col + 1
  137.  
  138. for turn in range(3):
  139. turn = turn + 1
  140. guess_row = int(raw_input("\n" + " Guess Row: ")) - 1
  141. guess_col = int(raw_input("Guess Column: ")) - 1
  142.  
  143. if guess_row == ship_row and guess_col == ship_col:
  144. clear()
  145. print "\n" + "Congratulations! You sunk my battleship!" + "\n"
  146. print_header()
  147. print_board(board)
  148. break
  149. else:
  150. if (guess_row >= 5) or (guess_col >= 5):
  151. clear()
  152. print "Oops, that's not even in the ocean."
  153. elif(board[guess_row][guess_col] == "X"):
  154. clear()
  155. print "You guessed that one already."
  156. else:
  157. if turn == 3:
  158. clear()
  159. print "\n" + "You ran out of turns, game over." + "\n" + "\n" + "Turn 4" + "\n" + "------------------------------"
  160. else:
  161. clear()
  162. print "\n" + "You missed my battleship!"
  163. board[guess_row][guess_col] = "X"
  164. print "\n" + "Turn", turn + 1, "\n" + "------------------------------"
  165.  
  166. # Print (turn + 1) here!
  167. print_board(board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement