Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- def battleship():
- board = []
- size = int(raw_input("Tell me the size of the field:"))
- turns = int(raw_input("Tell me the turns you will use:"))
- print
- for x in range(size):
- board.append(["O"] * size)
- def print_board(board):
- for row in board:
- print " ".join(row)
- print "Let's play Battleship!"
- print_board(board)
- print
- 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)
- for turn in range(turns):
- guess_row = int(raw_input("Guess Row:"))-1
- guess_col = int(raw_input("Guess Column:"))-1
- print
- if guess_row == ship_row and guess_col == ship_col:
- print "Congratulations! You sunk my battleship!"
- break
- else:
- if ( 0 > guess_row > len(board) - 1) or (0 > guess_col > len(board[0]) - 1):
- print "Oops, that's not even in the ocean."
- elif (board[guess_row][guess_col] == "X"):
- print "You guessed that one already."
- else:
- print "You missed my battleship!"
- board[guess_row][guess_col] = "X"
- print "Turns taken: %i; Turns left: %i" % (turn + 1, turns - turn - 1)
- print_board(board)
- print
- if turn == turns - 1:
- print "Game Over"
- battleship()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement