Extra creddit Help


#1

my code

from random import randint

board = []

for x in range(10):
board.append(["O"] * 10)

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

print "Welcome to Karlijn and Anika self made Battleship! (if you guess something that's not in the ocean it's: Game over! And there's no feedback when missed one of the four ships.)"
print_board(board)

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

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

ship_row_1 = random_row(board)
ship_col_1 = random_col(board)
print ship_row_1
print ship_col_1

ship_row_2 = random_row(board)
ship_col_2 = random_col(board)
print ship_row_2
print ship_col_2

ship_row_3 = random_row(board)
ship_col_3 = random_col(board)
print ship_row_3
print ship_col_3

ship_row_4 = random_row(board)
ship_col_4 = random_col(board)
print ship_row_4
print ship_col_4

for turn in range(10):
print "This is turn:"
print turn+1
guess_row = input("Please enter your guess row:")
guess_col = input("Please enter your guess Col:")

if guess_row == ship_row_1 and guess_col == ship_col_1:
    print "Good for you, You've got a ship!"
if guess_row == ship_row_2 and guess_col == ship_col_2:
    print "Good for you, You've got a ship!"
if guess_row == ship_row_3 and guess_col == ship_col_3:
    print "Good for you, You've got a ship!"
if guess_row == ship_row_4 and guess_col == ship_col_4:
    print "Good for you, You've got a ship!"    
    break

else:
    if (guess_row < 0 or guess_row > 9) or (guess_col < 0 or guess_col > 9):
        print "Oops, not in the ocean (Game over)."
    elif(board[guess_row][guess_col] == "X"):
        print "You can't guess the same again!."
    board[guess_row][guess_col] = "X"
    print (turn+1)
    print_board(board)
if turn == 9:
    print ("You lost, Loser!")

How do i create a feedback that the player won ( the 4 ships are hit)


#2

Like this,

board = "OOOOO",
        "OXOOO",
        .....

Ok we know how the board is stored now how do we access any information in it?

board[1][1]
# OUTPUT: 'X'

Ok, so it looks like we can easily iterate over every position of the board and when it finds an 'X' we can just add it to a list.

Then when that list equals the amount of tiles our ships take up we can just break the game and print a message.

Like so,

hold = []
for item in board:
    for tile in item:
        if tile == 'X':
            hold.append(tile)
if len(hold) == tiles_used:
    break # Or return if in a function, or create a custom exception to raise 
    # You can also put your print functions here

Easy enough?

Now let's make it a function and get used to making functions because more scripts you will want to be able to import and having everthing at the global level will make that hard.

board = [['O' for j in range(5)] for i in range(5)]

def check_win(state, to_win):
    return True if len([tile for item in state for tile in item if tile == 'X']) == to_win else False

check_win(board, 4)
# OUTPUT: False
# It will return this because we have yet to even place a hit on the board

This function can disseminate into

hold = []
for item in board:
    for tile in item:
        if tile == 'X':
            hold.append(tile)
if len(hold) == total_ship_tiles:
    print("You win!")
else:
    Print("You did not win yet")

As you can see it is quite simple to make the function when you know what you need to do.