Advertisement
TShiva

Battleship

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