Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Reading and storing coordinates from a txt using a list of lists (but the data gets overwritten).

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am trying to write a battleship game for an assignment. The ships and board size are read through a txt-file and they look like this;
-------------------------
12
Carrier;3*2;3*3;3*4;3*5;3*6
Battleship;5*6;6*6;7*6;8*6
(....)
-----------------------------
In the code i call a function where I deleted the separators between the coordinates (see listOfxyCoordinates=getXYcoordinates.getXYcoordinatesFromTextFile(line); )
and then I try to store the coordinates.
Before new instances of the shipclass have been made and as you see they consist of a shipType (the name in the file) and the coordinates.
Right after trying to store the list of xycoordinates, the ships are also added to a list.

However when I call readGameOptionsFromTextFile() from another class, the names are all collected,
but the coordinates of the ships have been overwritten.
Only the coordinates of the last ships are still in the list. I tried working with different datatypes where indexing seemed more easy to me, however the List of lists that i use now is more flexible but I just can't figure out
how to store it all effectively.

Any help would be very much appreciated.

Kind regards


 
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I suggest you start by removing the word static from your vocabulary, except in the heading for the main() method. A static variable is a recipe for disaster.
Please put some debugging code into your program. Print out the state of your List before and after you alter it:-That will help you see where things are going wrong. If you decide that line 73 isn't at fault, you can comment out the debugging code and delete it all later:-You have some very old‑fashioned code there. Also I think lines 33‑35 are error‑prone. What will happen if your board size is 15?
Don't use two readers for the same file. You only need one.
 
I'm still in control here. LOOK at this tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic