Randit() Battleship


#1

I have a query related to the battleship project regarding the random integer function syntax.

This is the solution code:-

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

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

According to my understanding this should be the code:
def random_row(board):
return randint(0, len(board) - 1)

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

If i’m wrong please can someone please make me understand why?


#2

with your code, what happens when you make the board a rectangle? (4 rows, 8 columns for example)


#3

My Code:

from random import randint

board = []

for x in range(0, 4):
  board.append(["O"] * 8)

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

print_board(board)

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

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

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

guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

# Write your code below!
if guess_row==ship_row and guess_col==ship_col:
  print "Congratulations! You sank my battleship!"
else:
  print "You missed my battleship!"
  board[guess_row][guess_col]='X'
  print_board(board)

Output:-

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
1
2
Guess Row: 2
Guess Col: 5
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 X O O
O O O O O O O O

#4

that will not work properly, sure, it will go fine most of the time, but sometimes it will fail. I am going to flip the board (4 by 8, you had a 8 by 4 board) because that bug is more obvious:

from random import randint

board = []

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

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

print_board(board)

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

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

for i in range(50):
    ship_row = random_row(board)
    ship_col = random_col(board)
    print ship_row, ship_col

guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

# Write your code below!
if guess_row==ship_row and guess_col==ship_col:
  print "Congratulations! You sank my battleship!"
else:
  print "You missed my battleship!"
  board[guess_row][guess_col]='X'
  print_board(board)

now i implemented a loop:

for i in range(50):
    ship_row = random_row(board)
    ship_col = random_col(board)
    print ship_row, ship_col

this mimics 50 players or 50 played games (whatever)

see the problem? Our ship can have a column, which doesn’t even exist on the board. That is a problem.

now, with a 8 by 4 board you still have a bug, any idea what it is?


#5

Since we are working with lists, I’ve often wondered why the author chose randint as opposed to randrange.

>>> from random import randint, randrange
>>> randint(5)
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    randint(5)
TypeError: randint() missing 1 required positional argument: 'b'
>>> c = 0
>>> x = 0
>>> while x < 5:
	x = randint(0, 5)
	c += 1

	
>>> c
2
>>> x
5
>>> 

Don’t try this with randrange or you will get an infinite loop.

>>> randrange(5)
3
>>> x = 0
>>> c = 0
>>> while x < 4:
	x = randrange(5)
	c += 1

	
>>> c
1
>>> x
4
>>>

See the differences? randrange is ideally suited to work with lists, very much like its cousin, range. It doesn’t have two positional arguments required, and we don’t have to decrement the top of the range since the function does it for us.