Extra credit


#1



Hey, I'm tring to make evolve the initial BS game so this bit of code don't have much to do with the initial project.
It's just a test, this bit of script only prints ships but I would like to make this bit work before doing other steps.
Sorry for the english, I tried to be as explicit as I can be. If you guys have a question go ahead.

I think there is something wrong with the way I'm trying to reset the map in the last function.


"""
Atempt to creat a part of a Battleship game.
I want this script to print 4 boats on a map whitout having any
overlaping between the elements of the boats. It's half working. If it prints a map,
the conditions gonna be fulfilled and on all the printed maps it won't gonna be any
overlaping. That happens if the conditions of map_check() are luckily fulfiled at 
the first generation of the ships. If not it's crashsing. After a couple of external 
tests with simplified version of the code it seems that the resseting of the map is not working.
"""


from random import randint


# stores an empty version of the map
def resset_board():
	board = [
	[" ","1","2","3","4","5","6","7","8","9"],
	["1"," "," "," "," "," "," "," "," "," "],
	["2"," "," "," "," "," "," "," "," "," "],
	["3"," "," "," "," "," "," "," "," "," "],
	["4"," "," "," "," "," "," "," "," "," "],
	["5"," "," "," "," "," "," "," "," "," "],
	["6"," "," "," "," "," "," "," "," "," "],
	["7"," "," "," "," "," "," "," "," "," "],
	["8"," "," "," "," "," "," "," "," "," "],
	["9"," "," "," "," "," "," "," "," "," "]
	]
	return board


# the map
board = resset_board()


# function to print the map whitout the quotes
def print_board(board):
    print "\n"
    print "\n"
    for row in board:
        print " ".join(row)
    print "\n"
    print "\n"


# rand orientation. Vertical or Horizontal. Used by Ship_build
def random_orientation():
	orientation = randint(0, 1)
	if orientation == 0:
		return "H"
	else:
		return "V"


# Modify the list to creat the ships and place them.
# First it use random_oriantation() to give an oriantation.
# The next part of the function gonna pick a random 
# index in the lists stored in board who gonna be the initial
# spowning point of a ship. Then in function of the rand of
# random_oriantation the following part of the script gonna creat the rest of
# the ship. For the horizontal ones it gonna modify the two following indexes 
# in the same list.
# For the vertical ones, it gonna modify the same indexes in the 
# two following lists.
# Each boat gonna be composed of 3 time the same number.
def ship_build(number):

	ship_row = 0
	ship_col = 0

	o = random_orientation()

	if o == "H":

		ship_row = randint(1, len(board) - 1)
		ship_col = randint(1, 7)
		board[ship_row][ship_col] = number
		board[ship_row][ship_col + 1] = number
		board[ship_row][ship_col + 2] = number

	elif o == "V":

		ship_row = randint(1, 7)
		ship_col = randint(1, len(board) - 1)
		board[ship_row][ship_col] = number
		board[ship_row + 1][ship_col] = number
		board[ship_row + 2][ship_col] = number




# Counts elements in a 2 dimensional list.
# If not composed of 3 elements, it means that there is an overlaping of boats.
def ship_check(ship_number):


	n = len(board)
	m = len(board[0])
	count = 0
	for i in range(n):
		for j in range(m):
			if board[i][j] == ship_number:
				count += 1
	return count





# Using ship_check, it checks if the right number of numbers composing the boats
# are stored in board.
# if not it ressets the board by calling resset_board to store a clean map in
# board. Then it buils 4 ships on a clean map before recalling map_check.
# it should do that until the conditions of the IF are True.
# If the conditions are fulfiled, it prints the board by calling print_board(board).

# Each ship is composed of 3 numbers. The XY of the map have 2 time those numbers also.
# So, to be sure that I have 4 full boats whitout overlaping on the map 
# I have to check if the number of the boat is == to 5 and not ==3.
def map_check():
	if ((ship_check("1") == 5) and (ship_check("2") == 5) and (ship_check("3") == 5) and (ship_check("4") == 5)) == True:		
		print_board(board)
	else:
		resset_board()
		ship_build("1")
		ship_build("2")
		ship_build("3")
		ship_build("4")
		map_check()




	
map_check()


#2

That function returns something, but you aren't doing anything with that return value

Personally I'd define the board like this instead of writing out every coordinate:

def create_board(width=10, height=10):
    return [[' '] * width for _ in range(height)]

And then I'd have a print function adding the labels as it prints. The main advantage is that it's easy to change size, it can even be done as the program is running.


#3

Actually yes, it's used in map_check()

I don't want to define like that for debugging purposes. It's easier for me to visualise the lists and the ships getting in to it if it's displayed like that (in my head).

I don't want to change the size of the map, this script is just a preliminary version of the game. I'm trying to figure things out.
And here, I try to figure out why map_check() is not working properly.

Thanks for the quick answer btw :slight_smile: It's just that it confuses me a bit. Did you checked the code till the end?
Did you get my logic and maybe I'm totally off base ? Really, nothing ironical in here, I just try to understand and I'm a big noob.


#4

You're trying to reset the board.

You call the function that creates a new board.
But you don't do anything with that new board. You ignore what it returns.


#5

What do you mean by I ignore what it returns. It seems to me that I use it in this function.


#6

You use it. Yes. But you don't do anything with its result.

If you have a line like this:

5 + 5

Then yes, you are computing a sum, but you're not using the result (10) to do anything, 10 just gets discarded and that line will have no effect.

You create a new board. What do you want to do with the new board? Do that.