Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Making Battleship with code in python 2 at codeacademy
- board = []
- for i in range(5):
- board.append(["O"] * 5)
- def print_board(board_in):
- for row in board:
- 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]
- # so the .join function will give out O O O O O which is joining each element in the list with " " in between.
- print_board(board)
- from random import randint
- def random_row(board):
- return randint(0, len(board) - 1)
- def random_col(board):
- return randint(0, len(board[0]) - 1)
- ship_row = random_row(board)
- ship_col = random_col(board)
- # print ship_row
- # print ship_col
- # ^ for debugging
- for turn in range(4):
- print "Turn", turn + 1
- guess_row = int(raw_input("Guess Row: "))
- guess_col = int(raw_input("Guess Col: "))
- if guess_col == ship_col and guess_row == ship_row :
- print "Congratulations! you sank my battleship!"
- else:
- if guess_row not in range(5) or \
- guess_col not in range(5): # The \ sign says continue the if statement on next line
- print "Oops, that\'s not even in the ocean"
- if turn == 3:
- print "Game Over"
- elif board[guess_row][guess_col] == "X":
- print "You guessed that one already"
- if turn == 3:
- print "Game Over"
- else:
- print "You missed my battleship!"
- board[guess_row][guess_col] = "X"
- if turn == 3:
- print "Game Over"
- print_board(board)
Add Comment
Please, Sign In to add comment