Python from Scratch vs Battleship Written in Python
Join the DZone community and get the full member experience.Join For Free
I guess by now you already know who I am. You already read my "Python from Scratch" 1, 2, 3 and 4 , you know that dzone.com has selected me to be one of their Most Valuable Blog writers. You know that I have decided to take the shortcut and instead of reading lesson after lesson I just jumped into a project to find a solution for a game called Battleship. 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 have received many comments and tips. It inspired me a lot that I am doing something right. In this post I will 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 hear tips from others (after all you are already coders while I am only study from scratch)
So let's start the game …
· First I will need to define the board 10x10:
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 Part of a ship Rounded_ship
o Part of a ship Squared_ship
o Part of a ship Start_right_ship
o Part of a ship Start_left_ship
o Part of a ship Start_up_ship
o Part of a ship Start_down_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-
· Create a counter that counts 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 deduct 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 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' …
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 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 marked ship (or part of it)
o If entire ship discovered then all around it
o If part of a ship Squared_ship then only in its corners
o If part of a ship 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 If part of a ship Start_down_ship then only its lower part (left to right)
Moves to paint a ship (or part of it)
· If number of unknown cells equals counter_hide- Paint entire unknown cells as 'part_of_ship'
· If current status is unknown- You may change to a new discovered status (water or ship)
· If current status is water_cell it may be equal only to new status water_cell
· If current status is a ship it may be equal only to new status of the same
· Any other result- FAIL and Print to debug.
Next and most important is the decision engine, 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 feedbacks to my work so far.
Do you think it can be done differently? Better?
Any tip or a good advice?
Scratch (programming language) Python (language) Row (database)
Published at DZone with permission of Hod Benbinyamin. See the original article here.
Opinions expressed by DZone contributors are their own.