Tuesday, May 22, 2012

Python from scratch has been deffered

I was in the middle of my struggle to solve the battleship case using Python I am learning these days.
I was good, I had a good lead and things were working quite well to me when I saw a message in HN community about a great site must see- www.42floors.com .
So I clicked the link and what I saw there is "Life Changing"
42floors is most of all the best UI and User Friendly site I have ever seen.
It is so easy to navigate, search and read information inside this site.
Also, the idea behind the site is so appealing to me, I was always thinking to myself "How do businesses find their space?" and how the opposite happens- "How do locations make themselves available to others?"

It took me few minutes to know that I must delay my Python from scratch study and try to understand how the hell this site is working.
So I posted Ask HN question and waited for the answers.

From the answers I realized that for me to be even closer to that level of site it may take years but in the other hand I can still go step by step and learn what seems to me now the future- Front-End programming (JS + CSS + HTML5)

You are probably laughing and saying- Dude, what's up with you? this is so old news.
Well, for me this is a big news.
Yeah yeah I know it looks like I was far away when all this revolution took place comparing to the time I was writing sites using FrontPage or Geocities.

Anyway, the day after I registered to code academy following one of the suggestion and I started their code year course which contains JS first, later CSS and last HTML5.
I am doing well and the course is so far very good to understand and I really feel like catching it, I even going into the Q&A pages helping others like me to solve their issues.

So for now this 'Python from Scratch' Blog series is temporary deferred.
I would start a new series of writing and will call it 'JavaScript from Scratch'
I would like to thank all the Python users who read and commented on my posts.
And if you find my new journey interesting- I ask you to follow me and drop a comment or two.


Tuesday, May 8, 2012

Python from scratch vs Battleships

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 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 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 hear tips from others (after all you are already coders while I am only study from scratch)

So let's start the game …

Constant definitions:
·                    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       Unknown
o       water_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
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-

Counters
·                    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'


Verification
·                    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?