DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Languages
  4. Python from Scratch vs Battleship Written in Python

Python from Scratch vs Battleship Written in Python

Hod Benbinyamin user avatar by
Hod Benbinyamin
·
May. 30, 12 · Interview
Like (0)
Save
Tweet
Share
5.57K Views

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 …

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?

Stay Tuned
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.

Popular on DZone

  • Spring Boot vs Eclipse Micro Profile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Create a REST API in C# Using ChatGPT
  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam
  • 10 Best Ways to Level Up as a Developer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: