Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 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. for turn in range(5):
  24.   print "Turn", turn + 1
  25.   guess_row = int(raw_input("Guess Row: "))
  26.   guess_col = int(raw_input("Guess Col: "))
  27.  
  28.   if guess_row == ship_row and guess_col == ship_col:
  29.     print "Congratulations! You sank my battleship!"
  30.     break
  31.   else:
  32.     if guess_row not in range(5) or \
  33.       guess_col not in range(5):
  34.       print "What ocean are you fighting in?"
  35.     elif board[guess_row][guess_col] == "X":
  36.       print( "You guessed that location already." )
  37.     else:
  38.       print "You missed my battleship!"
  39.       board[guess_row][guess_col] = "X"
  40.     if (turn == 3):
  41.       print "Game Over"
  42.     print_board(board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement