Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. import random
  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 "Let's play Battleship!"
  13. print_board(board)
  14.  
  15. def random_row(board):
  16.     return random.randint(0,len(board)-1)
  17.  
  18. def random_col(board):
  19.     return random.randint(0,len(board[0])-1)
  20.  
  21. ship_row = random_row(board)
  22. ship_col = random_col(board)
  23.  
  24. for turn in range(4):
  25.     print "Turn: " + str(turn+1)
  26.     guess_row = input("Guess Row: (0-4)")
  27.     guess_col = input("Guess Col: (0-4)")
  28.  
  29.     if guess_row == ship_row and guess_col == ship_col:
  30.         print "Congratulations! You sunk my battleship(S)!"
  31.         board[ship_row][ship_col] = "S"
  32.         print_board(board)
  33.         break
  34.     else:
  35.         if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
  36.             print "Oops, that's not even in the ocean."
  37.         elif(board[guess_row][guess_col] == "X"):
  38.             print "You guessed that one already."          
  39.         else:
  40.             print "You missed my battleship!"
  41.             board[guess_row][guess_col] = "X"
  42.             print_board(board)
  43.             if turn == 3:
  44.                 print "Game Over (S = Battleship)"
  45.                 board[ship_row][ship_col] = "S"
  46.                 print_board(board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement