Skip to main content
input checking
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

Write a function for input checking

The main logic (now incorporated into main battleship_game function) is full of checks to make sure that the input is valid, abstracting the input checking to another function will condensate the top-level logic.

Costumize

After you define a battleship_game function, you can have fun customizing it by passing all kinds of different parameters: the board size, the number of turns, whether to run in debugging mode or not...

This will allow you to play slightly different battleship games by using the same code.

Write a function for input checking

The main logic (now incorporated into main battleship_game function) is full of checks to make sure that the input is valid, abstracting the input checking to another function will condensate the top-level logic.

Costumize

After you define a battleship_game function, you can have fun customizing it by passing all kinds of different parameters: the board size, the number of turns, whether to run in debugging mode or not...

This will allow you to play slightly different battleship games by using the same code.

the range for you
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

First define, then "do stuff"

Organization is a nice property of code. Now you mix function definitions and code that "does things" (namely code that interacts with the user).

I would define at first all of the functions, and then run them.

Use enumerate

Use the range that fits your needs best

>>> help(range) Help on class range in module builtins: class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return a sequence of numbers from start to stop by step. ... 

Using the second version of range will avoid incrementing afterwards:

for turn in range(1, 5): print "Turn", turn 

I also find it more obvious this way that turns are 1-indexed.

Use enumerate

First define, then "do stuff"

Organization is a nice property of code. Now you mix function definitions and code that "does things" (namely code that interacts with the user).

I would define at first all of the functions, and then run them.

Use enumerate

Use the range that fits your needs best

>>> help(range) Help on class range in module builtins: class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return a sequence of numbers from start to stop by step. ... 

Using the second version of range will avoid incrementing afterwards:

for turn in range(1, 5): print "Turn", turn 

I also find it more obvious this way that turns are 1-indexed.

default param
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

Also, columns is only ever used inside print_board so I would define it as a default parameter, to make it clear that the rest of the program does not need it:

def print_board(board, columns = [" 0", "1", "2", "3", "4"]): print "\t\t", " ".join(columns) for row_num, row in enumerate(board): print "\t\t", row_num, " ".join(row) 

Use enumerate

def print_board(board, columns = [" 0", "1", "2", "3", "4"]): print "\t\t", " ".join(columns) for row_num, row in enumerate(board): print "\t\t", row_num, " ".join(row) 
board = [["-"] * 5 for _ in range(5)] 

Remove unused definitions

row_num is defined but never used, so delete the line:

row_num = ["0", "1", "2", "3", "4"] 

Use enumerate

def print_board(board): print "\t\t", " ".join(columns) for row_num, row in enumerate(board): print "\t\t", row_num, " ".join(row) 
board = [["-"] * 5 for _ in range(5)] 

Also, columns is only ever used inside print_board so I would define it as a default parameter, to make it clear that the rest of the program does not need it:

def print_board(board, columns = [" 0", "1", "2", "3", "4"]): print "\t\t", " ".join(columns) for row_num, row in enumerate(board): print "\t\t", row_num, " ".join(row) 

Use enumerate

def print_board(board, columns = [" 0", "1", "2", "3", "4"]): print "\t\t", " ".join(columns) for row_num, row in enumerate(board): print "\t\t", row_num, " ".join(row) 
board = [["-"] * 5 for _ in range(5)] 

Remove unused definitions

row_num is defined but never used, so delete the line:

row_num = ["0", "1", "2", "3", "4"] 
list comprehension è `_`
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138
Loading
avoid nothing.
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138
Loading
avoid globals
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138
Loading
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138
Loading