Battleship, take home version


#1

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
This is turn 1.
Guess Row:4
Guess Col:4
You missed my battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O X
This is turn 2.
Guess Row:0
Guess Col:0
You missed my battleship!
X O O O O
O O O O O
O O O O O
O O O O O
O O O O X
This is turn 3.
Guess Row:2
Guess Col:3
You missed my battleship!
X O O O O
O O O O O
O O O X O
O O O O O
O O O O X
This is turn 4.
Guess Row:1
Guess Col:4
You missed my battleship!
X O O O O
O O O O X
O O O X O
O O O O O
O O O O X
This is turn 5, your last turn!
Guess Row:3
Guess Col:1
Congratulations! You sunk my battleship!
X O O O O
O O O O X
O O O X O
O W O O O
O O O O X
>>> 1/25+1/24+1/23+1/22+1/21
0.21821852060982494
>>>

In five guesses I had a 21.8 per-cent chance of guessing correctly. Lo and behold, I did it this time. Usually it's the computer that wins.

from random import randint

def new_board(size):
    return [["O"] * size for x in range(size)]

def print_board(board):
    for row in board:
        print (" ".join(row))

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

print ("Let's play Battleship!")

side = 5
board = new_board(side)
print_board(board)

ship_row = random_row(board)
ship_col = random_col(board)

turn = 0
while turn < side:
    turn += 1
    if turn < side:
        print ("This is turn %d." % (turn))
    else:
        print ("This is turn %d, your last turn!" % (turn))
    while True:
        try:
            guess_row = int(input("Guess Row:"))
            guess_col = int(input("Guess Col:"))
            break
        except ValueError:
            print ("You have to enter a number.")
    if guess_row == ship_row and guess_col == ship_col:
        print ("Congratulations! You sunk my battleship!")
        board[ship_row][ship_col] = "W"
        print_board(board)
        break
    elif guess_row not in range(side) or guess_col not in range(side):
        print ("Oops, that's not even in the ocean.")
        turn -= 1
    elif board[guess_row][guess_col] == "X":
        print ("You guessed that one already.")
        turn -= 1
    else:
        print ("You missed my battleship!")
        board[guess_row][guess_col] = "X"
        print_board(board)
        if turn > side - 1:
            print ("Game Over.")
            print ("My battleship was at Row %d, Column %d." % (ship_row, ship_col))
            board[ship_row][ship_col] = "M"
            print_board(board)

19. Track hits across different ships without using classes?
#2

Cool! I tried it on IDLE


#3

That's where I wrote the revision (Python 3.5), keeping as close to the original as I could. It works fine in the lab, (2.7) as well. The only thing missing is a Play Again loop.


#4

thanks anyway @mtf :smiley:


#5

With a game loop…

from random import randint

def new_board(size):
    return [["O"] * size for x in range(size)]

def print_board(board):
    for row in board:
        print (" ".join(row))

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)
def battle_ship(side):
    print ("Let's play Battleship!\n")

    board = new_board(side)
    print_board(board)

    ship_row = random_row(board)
    ship_col = random_col(board)

    turn = 0
    while turn < side:
        turn += 1
        if turn < side:
            print ("This is turn %d." % (turn))
        else:
            print ("This is turn %d, your last turn!" % (turn))
        while True:
            try:
                guess_row = int(input("Guess Row:"))
                guess_col = int(input("Guess Col:"))
                break
            except ValueError:
                print ("You have to enter a number.")
        if guess_row == ship_row and guess_col == ship_col:
            print ("Congratulations! You sunk my battleship!")
            board[ship_row][ship_col] = "W"
            print_board(board)
            break
        elif guess_row not in range(side) or guess_col not in range(side):
            print ("Oops, that's not even in the ocean.")
            turn -= 1
        elif board[guess_row][guess_col] == "X":
            print ("You guessed that one already.")
            turn -= 1
        else:
            print ("You missed my battleship!")
            board[guess_row][guess_col] = "X"
            print_board(board)
            if turn > side - 1:
                print ("Game Over.")
                print ("My ship was at Row %d, Column %d." % (ship_row, ship_col))
                board[ship_row][ship_col] = "M"
                print_board(board)
def game_loop():
    while True:
        board_size = int(input("Enter a board size (0 to quit): "))
        if board_size: battle_ship(board_size)
        else: break
Code
from random import randint

def new_board(size):
    return [["O"] * size for x in range(size)]

def print_board(board):
    for row in board:
        print (" ".join(row))

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

def battle_ship(side):
    print ("Let's play Battleship!\n")
    
    board = new_board(side)
    print_board(board)

    ship_row = random_row(board)
    ship_col = random_col(board)

    turn = 0

    while turn < side:
        turn += 1
        if turn < side:
            print ("This is turn %d." % (turn))
        else:
            print ("This is turn %d, your last turn!" % (turn))
        while True:
            try:
                guess_row = int(input("Guess Row:"))
                guess_col = int(input("Guess Col:"))
                break
            except ValueError:
                print ("You have to enter a number.")
        if guess_row == ship_row and guess_col == ship_col:
            print ("Congratulations! You sunk my battleship!")
            board[ship_row][ship_col] = "W"
            print_board(board)
            break
        elif guess_row not in range(side) or guess_col not in range(side):
            print ("Oops, that's not even in the ocean.")
            turn -= 1
        elif board[guess_row][guess_col] == "X":
            print ("You guessed that one already.")
            turn -= 1
        else:
            print ("You missed my battleship!")
            board[guess_row][guess_col] = "X"
            print_board(board)
            if turn > side - 1:
                print ("Game Over.")
                print ("My battleship was at Row %d, Column %d." % (ship_row, ship_col))
                board[ship_row][ship_col] = "M"
                print_board(board)

def game_loop():
    while True:
        board_size = int(input("Enter a board size (0 to quit): "))
        if board_size: battle_ship(board_size)
        else: break


#6

Hey I didn't know Codecademy labs existed! cool!


#7

Be sure to click Share and copy the URL (bookmark for good measure) or you could lose your work in the vacuum. There is not index or log of these pages. Once you lose the URL, your work is gone (unless you like searching your browser history).

Copy code before running or refreshing the page. One crash and all is lost.


#8

This is turn 13.
Guess Row:4
Guess Col:14
Congratulations! You sunk my battleship!
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O X O O O O O O O O W O O O O O
O O O O O O O O O O O O O X O O O O O O
O O O O O O O O O O O O O O O O X O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O X O O O O O O O O O O O O O O
O O O O O O O O O O X O O O O O O O O O
O O O O O O O O O O O X O O O O O O O O
O O O O O O O O O O O O X O O O O O O O
O O O O O O O O O O O O O X O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O X O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O X O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O X
O O O O O O O O X O O O O O O O O O O O
Enter a board size (0 to quit):

#10