Python from scratch – Battleships

 

 

Have you written battleships using python?
 
Keep on reading my post on Python from scratch – Battleships on my journey into python.
 

Preview of Python from scratch journey

I guess by now you already know who I am.
 
You already read my “Python from Scratch” 1, 2, 3 and 4
 
By now, you know that dzone.com has selected me to be one of their Blog writers.
 
You know that I have decided to take the shortcut and instead of reading lesson after a lesson I just jumped into a project, to find a solution for a game called Battleships.
 
In case you do not know this game and in case you want to learn the rules please read my last post which describes the game and rules and what I have ahead to accomplish.
 
I got many comments and tips to my act.
 
It inspired me a lot that I am doing something right.
 
In this post, I will try to share with you the way I want to design the project.
 
The way I believe is a good and clear solution.
 
This is my vision but I am open to hearing tips from others (after all you are already coders while I am only study from scratch).
 
So let’s start the game.
 
Let’s do Python from scratch – Battleships a real thing!
 

 

Battleships constant definitions

  • First I will need to define the board 10×10:
o       Rows will be called ‘row_1’, ‘row_2′ …’row_10’
o       Columns will be ‘column_1’, ‘column_2′ …’column_10’
 
·     To define the various ships:
o       Battleship (4 cells in a row)
o       Cruiser (3 cells in a row)
o       Destroyer (2 cells in a row)
o       Submarine (1 cell only)
 
·    Status of a cell:
o       Unknown
o       water_cell
o       Rounded_ship – part of a ship
o       Squared_ship – part of a ship
o       Start_right_ship – part of a ship
o       Start_left_ship – part of a ship
o       Start_up_ship – part of a ship
o       Start_down_ship part of the ship
o       Part_of_ship
 
·      The initial data
o       Input of row’s numbers will be called ‘input_row1′, input_row2’ …
o       Input of column’s numbers will be called ‘input_column1’, input_column2’…
 
 
Once I have all the constant and initial data ready and set I need to start my moves.
 
First I will start with the easy moves that need basic analysis
 

Battleships Counters

 
·                    Create a counter that counts the number of cells which have not discovered yet
o       At first, it equals the input
o       Any cell which is part of a ship will be deducted from that counter
o       Counter for rows will be called ‘counter_hide_row1’, ‘counter_hide_row2’ …
o       Counter for columns will be called ‘counter_hide_column1′, counter_hide_column2’ …
 
·                    Create a counter that counts the number of already painted cells (part of a ship)
o       Counter for rows will be called ‘counter_painted_row1’, ‘counter_painted_row2’ …
o       Counter for columns will be called ‘counter_painted_column1’  …
 
The easy and important move is to paint any place with water.
 
This way I eliminate these cells
 

Moves to mark water cell

 
·                    Read input_rowX and where it is 0- to mark with water_cell the entire row
 
·                    Read input_columnX  and where it is 0- to mark with water_cell the entire column
 
·                    If the counter of already painted cells equals input (and count_hide equals 0) then:
o       Mark with water_cell the entire unknown cells in that row
o       Mark with water_cell the entire unknown cells in that column
 
·                    Mark with water_cell adjacents of the marked ship (or part of it)
o       If the entire ship discovered then all around it
o       If part of a ship Squared_ship then only in its corners
o      Start_right_ship then only its right part (top to bottom)
o       If part of a ship Start_left_ship then only its left part (top to bottom)
o       If part of a ship Start_up_ship then only its upper part (left to right)
o       ship Start_down_ship then only its lower part (left to right)
 
 
Moves to paint a ship (or part of it)
·                    If the number of unknown cells equals counter_hide- Paint entire unknown cells as ‘part_of_ship’
 

Battleships Verification

·                    If the current status is unknown- You may change to a newly discovered status (water or ship)
 
·                    If the current status is water_cell it may be equal only to new status water_cell
 
·                   the current status is a ship it may be equal only to a new status of the same
 
·                    Any other result- FAIL and Print to debug.
 

Battleships – Decision engine

Next and most important is the decision engine.
 
It needs to find ‘Part_of_Ship’ cells.
 
This is an advanced analysis.
 
I will keep it to the next post.
 
For now, I would like to get feedback on my work so far in my journey of Python from scratch – Battleships
Do you think it can be done differently? Better?
Any tip or good advice?
 

Leave a Reply