Guest User

Untitled

a guest
Jun 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. # Making Battleship with code in python 2 at codeacademy
  2. board = []
  3.  
  4. for i in range(5):
  5. board.append(["O"] * 5)
  6.  
  7. def print_board(board_in):
  8. for row in board:
  9. print " ".join(row) #.join function joins the elements of a list by the thin before the .join, example here the row is ["O", "O", etc]
  10. # so the .join function will give out O O O O O which is joining each element in the list with " " in between.
  11.  
  12. print_board(board)
  13. from random import randint
  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. # print ship_row
  24. # print ship_col
  25. # ^ for debugging
  26. for turn in range(4):
  27. print "Turn", turn + 1
  28. guess_row = int(raw_input("Guess Row: "))
  29. guess_col = int(raw_input("Guess Col: "))
  30.  
  31. if guess_col == ship_col and guess_row == ship_row :
  32. print "Congratulations! you sank my battleship!"
  33. else:
  34. if guess_row not in range(5) or \
  35. guess_col not in range(5): # The \ sign says continue the if statement on next line
  36. print "Oops, that\'s not even in the ocean"
  37. if turn == 3:
  38. print "Game Over"
  39. elif board[guess_row][guess_col] == "X":
  40. print "You guessed that one already"
  41. if turn == 3:
  42. print "Game Over"
  43. else:
  44. print "You missed my battleship!"
  45. board[guess_row][guess_col] = "X"
  46. if turn == 3:
  47. print "Game Over"
  48. print_board(board)
Add Comment
Please, Sign In to add comment