• 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
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Creating a board using 2D arrays

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.cs.sun.ac.za/courses/cs114/


Hey guys I need help with my CS project I have no idea how to create the board the instructions for the game are in the link above
 
Marshal
Posts: 67939
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think you are going about the problem the wrong way. I think you should divide the assignment into small parts. Maybe, start by reading the input file with ...D...pT. in and next, work out how to create different objects to represent the different pieces or obstacles you might meet in the squares.
 
Sheriff
Posts: 14990
250
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ikaneng Thekiso wrote:http://www.cs.sun.ac.za/courses/cs114/

... CS project...


From those instructions:

Object Oriented Programming (OOP) will not be allowed for this project. Your project must be written in a single java file.


Yikes!

No OOP allowed, a single .java file. Way to teach your CS students how to write good programs!  
 
Ranch Hand
Posts: 116
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow that's a new one.

Also I opened one of the sample projects they provided and immediately saw this method:

This code scares me a lot

On topic: There are tons of requirements there and it uses a lot of custom code written for this school, it seems, so I would recommend attempting an implementation yourself and then asking for help, or asking how to do a specific thing you have to do for the assignment. It's unlikely that someone is going to read your entire assignment and design it for you. There's definitely some stuff there you can start with - can you read in the file using command-line arguments? Can you do something with it after reading the file?
 
Rancher
Posts: 858
20
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off you are going to have to look and see how you want to implement each of your game tiles, that is going to depend on how you want to implement the requirements of the project

Second look to see how you want to implement your 2D array.  Since it is a set size, a array of whatever objects you decide for your game squares should be fine.

Third, your going to have to make a loader to load your array with the appropriate game tiles

and etc...

I see from others one of the comments is you are not allowed to use OOP?  I have to laugh at that, there are many ways to put everything in 1 file and not have a god class.  Here are a few examples:

In your code you can make entire classes within your class, they are called inner classes:

In many languages you have the concept of visibility, in Java you have file visibility, so you can actually put multiple classes in the same file:

I would hope the instruction you are receiving is at Jr High or possibly HS level and your not at some college or university or it's not in a book that you actually paid for.


ikaneng Thekiso wrote:http://www.cs.sun.ac.za/courses/cs114/


Hey guys I need help with my CS project I have no idea how to create the board the instructions for the game are in the link above

 
Junilu Lacar
Sheriff
Posts: 14990
250
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lee Morgan wrote:there are many ways to put everything in 1 file and not have a god class.

.
I wouldn't even know where to start ranting if by "No OOP" they simply meant that you just had to keep everything in one .java file. There's just all kinds of wrong in that, the least of which is that it implies they have no idea what "OOP" even means.
 
Junilu Lacar
Sheriff
Posts: 14990
250
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one of the resource files given, in "as is" condition:

I'm sorry, but this kind of instruction is why airplanes fall out of the skies and kill people.
 
Saloon Keeper
Posts: 11441
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were you, I'd ask about the no Object Orientation allowed requirement. Maybe they only meant that they want everything in one file. It's possible to do everything in one file and still use object oriented code, as Les pointed out.

Not that I understand THAT requirement, seeing as you are allowed to include other resources, but whatever.

Ignore the code they gave you. It's better to write your own from scratch.

As for the Board, you could let it consist of an array of arrays of lists. Each list contains all the entities that can appear in a single square.
 
ikaneng Thekiso
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




hey guys I really appreciate all the help ,this is what I did but I am still encountering problems ......may someone maybe contact me on discord Kingika  #1976
 
Sheriff
Posts: 6801
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is what I did but I am still encountering problems


What problems are you getting, exactly?  Error?  Copy the error message and the stack trace (all the text afterwards) and post it here.  Nothing behaving as you would like?  Describe the behavior and post what you want the program to do.

may someone maybe contact me on discord Kingika  #1976


The whole point of this forum is that other people can see and learn from what you've done and what other people suggest.
 
ikaneng Thekiso
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool,so I want the code to read from a text file (the board of the game)
I SUPPLIED examples in my post above
 
Campbell Ritchie
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with your file reading code? Apart from the fact that you aren't closing the reader correctly. This Java™ Tutorials section shows you the best way to close the reader. You can find out about reading files in the Java™ Tutorials, too.
Remember you are dependant on the file containing the right number of lines of the right length. But if there are any such errors, it isn't your fault and you can put the blame on whoever wrote the text file.

The names in that program are poorly‑chosen. Don't call an array map. Most of the other names are hardly any better. Please indent your code correctly; your indentation is a lot better than many people's but it is still inconsistent. I think it has misled you into writing the println() call in the wrong place. Get a decent text editor (e.g. NotePad++) and set up its options for automatic indentation and conversion of tab→4 spaces. Also you will find bracket matching, syntax highlighting and a right margin marker useful.
Don't put everything in the main method. If you did that because you are simply trying out code, I suggest you use JShell instead.
 
Knute Snortum
Sheriff
Posts: 6801
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ikaneng Thekiso wrote:cool,so I want the code to read from a text file (the board of the game)
I SUPPLIED examples in my post above


But you didn't supply the errors you are encountering.  I can see compile-time errors from the code you posted.  I'd be very surprised if it executes.
 
ikaneng Thekiso
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:

ikaneng Thekiso wrote:cool,so I want the code to read from a text file (the board of the game)
I SUPPLIED examples in my post above


But you didn't supply the errors you are encountering.  I can see compile-time errors from the code you posted.  I'd be very surprised if it executes.



7 7
.......
.......
....x..
....x..
....x..
...xx..
...sx.t

I want to read from a 2D array text file looking like this.This is the code that I wrote. I am creating a game board. The first line represents the dimensions of the board so the program must only read from the second line onwards.

I have attempted numerous ways of doing this such as using the scanner function etc. I was told by a friend of mine that my approach is wrong. How would I fix this code to do that particular function?
 
Knute Snortum
Sheriff
Posts: 6801
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pick one of your best attempts and post the code here.  Make sure it compiles.
 
ikaneng Thekiso
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Pick one of your best attempts and post the code here.  Make sure it compiles.







So this is my best attempt it is like the one above but I fixed the syntax errors etc ,and I edited line 13 and 14 to read the dimensions of the array from 1 line instead of 2 lines.The problem I'm having with my code is that nothing is happening.I think it is the problem with my main class being empty.How would I fix that?
 
Campbell Ritchie
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't make everything static. Didn't I tell you a long time ago, to divide the process into stages? Did you? Have you confirmed that you can read the file at all?
Yes, you have got an empty main() method.
Did you know there are methods of the String object allowing you to divide a String into a char[] or a String[]? (For the latter method use "" as a delimiter and you will get an array of one‑letter Strings.)
 
Knute Snortum
Sheriff
Posts: 6801
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would probably have been easier using Scanner, but let's work with what we have.

* You method doesn't skip the header
* It reads the column but doesn't skip the space between the columns and row
* When you read column and row, remember that you are reading the ASCII code.  The ASCII code for 0 is 48, so you would need to subtract that from the column and row variable.

Remember to put lots of System.out.println() or the like in your code to test whether you are getting the correct values.

Can I convince you to rewrite the method using Scanner?  It might be easier.
 
Don't play dumb with me! But you can try this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!