tdunham99

battleship

Jul 10th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. from random import randint
  2.  
  3. board = []
  4.  
  5. for x in range(0, 5):
  6.   board.append(["O"] * 5)
  7.  
  8. def print_board(board):
  9.   for row in board:
  10.     print " ".join(row)
  11.  
  12. print_board(board)
  13.  
  14. def random_row(board):
  15.   return randint(0, len(board) - 1)
  16.  
  17. def random_col(board):
  18.   return randint(0, len(board[0]) - 1)
  19.  
  20. ship_row = random_row(board)
  21. ship_col = random_col(board)
  22.  
  23. # Everything from here on should be in your for loop
  24. # don't forget to properly indent!
  25. for turn in range(4):
  26.     guess_row = int(raw_input("Guess Row: "))
  27.     guess_col = int(raw_input("Guess Col: "))
  28.  
  29.     if guess_row == ship_row and guess_col == ship_col:
  30.         print "Congratulations! You sank my battleship!"
  31.         break
  32.     else:
  33.         if (guess_row not in range(5)) or (guess_col not in range(5)):
  34.             print("Oops, that's not even in the ocean.")
  35.         elif board[guess_row][guess_col] == "X":
  36.             print("You guessed that one already.")
  37.         else:
  38.             print "You missed my battleship!"
  39.             board[guess_row][guess_col] = "X"
  40.     print_board(board)
  41.     turn += 1
  42.     print "Turn: %d" % turn
  43.     if turn > 3:
  44.         print "Game Over"
  45.         board[ship_row][ship_col] = "S"
  46.         print_board(board)
  47.         print('"S" = Ship')
Advertisement
Add Comment
Please, Sign In to add comment