Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. from random import randint
  2.  
  3. board = []
  4.  
  5. def clear_board(board):
  6.     board = []
  7.     for x in range(5):
  8.         board.append(["O"] * 5)
  9.     return board
  10.  
  11. def print_board(board):
  12.     for row in board:
  13.         print " ".join(row)
  14.  
  15. print "Let's play Battleship!"
  16. print_board(board)
  17.  
  18. def random_row(board):
  19.     return randint(0, len(board) - 1)
  20.  
  21. def random_col(board):
  22.     return randint(0, len(board[0]) - 1)
  23.  
  24. answer = "Y"
  25.  
  26. while answer == "Y":
  27.     clear_board
  28.     print_board(board)
  29.     ship_row = random_row(board)
  30.     ship_col = random_col(board)
  31.  
  32.     for turn in range(4):
  33.         guess_row = int(raw_input("Guess Row:"))
  34.         guess_col = int(raw_input("Guess Col:"))
  35.  
  36.         if guess_row == ship_row and guess_col == ship_col:
  37.             print "Congratulations! You sunk my battleship!"
  38.             break
  39.         else:
  40.             if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
  41.                 print "Oops, that's not even in the ocean."
  42.             elif(board[guess_row][guess_col] == "X"):
  43.                 print "You guessed that one already."
  44.             else:
  45.                 print "You missed my battleship!"
  46.                 board[guess_row][guess_col] = "X"
  47.             if turn == 3:
  48.                 print "Game Over"
  49.         print "Turns: ", turn + 1
  50.         print_board(board)
  51.     answer = str(raw_input("Play again? (Y/N) "))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement