[Logo]
Forums Register Login
Loading files
So I have a file that looks like:
Troll,1
Jinn,1
Wall
Troll,0
Goblin,1
Troll,1
Wall
Troll,0
Goblin,1
Jinn,0

Per the instructions
"You will need to check whether the first field is Troll,  Jinn, Wall, etc. to determine the type of opponent to create. Once the opponent is created, add it to the opponents array list."

This is the only thing I have come up with??

It looks good so far as I can tell, you haven't stated what the requirements are for the opposition (maybe that's unimportant for now). You are creating an Obstruction but you aren't doing anything with the 'o' variable. Can you describe what you're stuck on?
Well, honeslty I just wanted to see if that was the correct way to do it. I only had little pieces to go off of, but I do get a arrayindexoutofbounds:1 exception.
Id have more to add on like reporting line number errors, but I want to get this working first

In your text file, "Wall" doesn't have a 2nd parameter - no comma.
If a line starts with "Troll", first you create an Obstacle("Troll"), Next you create 4 Trolls that get added to the opponents list. So with four lines in the file starting with "Troll" you add 16 Trolls in total. Is this really according to the assignment?
Well, I showed the instructor and he didn’t say anything about it being wrong. The only hints he gave me were

“You might want to check the length of the array or do something different when it comes to a wall”
Instructor is spot on.
How will checking the length if the array help me add in a well? Because wouldnt it then be
This is the line that's giving you grief, opp[1] does not exist for "Wall".
Right, because all thats there in the list is a wall. So I cant add a wall
Which implies that you need to check the value of opp[0] before constructing an Obstacle.
Is that not what I’m doing? Checking the value of opp[0] for troll, jinn, and wall before adding them

How many elements will be in opp if you read a "Wall"?  Or, what is the length of opp?
 

Cody Biggs wrote:Is that not what I’m doing? Checking the value of opp[0] for troll, jinn, and wall before adding them



Well, from the code earlier, your Obstacle creation code is on line 10, and you don't check the value of opp[0] until line 12 onwards.
 

Knute Snortum wrote:
How many elements will be in opp if you read a "Wall"?  Or, what is the length of opp?



Well from the list I posted in the first post it would be 4 before it hit the first wall
That's not quite the answer. What would you get if you insert
gives me
2
2
1
2
2
2
1
2
2
2
It IS a bit confusing. Another way to look at this is:

suppose that you call the constructor of the Obstacle, and you just send it the line just read in, or the splitted array from this line, i.e:

and let that constructor itself find out what to do with it.  How would that constructor look like?
 

Piet Souris wrote:It IS a bit confusing. Another way to look at this is:

suppose that you call the constructor of the Obstacle, and you just send it the line just read in, or the splitted array from this line, i.e:

and let that constructor itself find out what to do with it.  How would that constructor look like?

That would work unless you are not supposed to create any Obstacle if the input is "Wall".
 

Carey Brown wrote:That would work unless you are not supposed to create any Obstacle if the input is "Wall".


Is the wall considered as not obstacle?
The wall is considered an obstacle
 

Cody Biggs wrote:The wall is considered an obstacle


Alright, so we are getting somewhere with requirements.

Also in your initial code you have have hardcoded values which you passing to Troll constructor, these as I see reassemble the values (numeric) which appear next to each Troll in a file, that is why you have them 4 created, again, in hardcoded way. That is of course not correct too. This is what Piet hinted to you why you creating 16 of them (Trolls) in total.

1. What you most likely need to do is, to have an interface or abstract class Obstacle, where each concrete obstacles either extend or implement (based on either it is abstract class or interface you choose to go with).

2.Then based on the obstacle you find out during the file read you create concrete object and add to Obstacles array or list or other data structure you need to. Factory method might would be helpful to have so you wouldn't pollute class where main logic is laid down with lots of if's/else's.

3. During the creation of concrete obstacle you need to take into account, that some (actually just one at this moment of requirements) obstacles have just a name of them (i.e. Wall), meaning have no numeric value after the comma, which you didn't tell us yet what it means.

3.1 So, when you create concrete obstacle, you need to either know beforehand that there is no second parameter (numerical) or let constructor figure out that there is no such numeric value - Piet hinted about that too.

Things go in some chaotic way as I see (I've done that many times too). So what I usually do when that happens - I delete code and start over. Would suggest that to you too, just this time take smaller steps.

By the way, Obstacles sounds like a Dutch word, but it is English, I thought where I heard this word...
I think I confused a bit opponents with obstacles (note: for the time being ignore my initial post). So you have Wall as an obstacle, and the rest are opponents?
 

Carey Brown wrote:That would work unless you are not supposed to create any Obstacle if the input is "Wall".


That is why. Carey, I think you meant: unless you are not supposed to create any Obstacle if the input is not "Wall" ?
 

Liutauras Vilda wrote:

Carey Brown wrote:That would work unless you are not supposed to create any Obstacle if the input is "Wall".


That is why. Carey, I think you meant: unless you are not supposed to create any Obstacle if the input is not "Wall" ?


Without clear requirements I couldn't tell if logic is "is" or "is not".
Yeah.

@OP

Where this opponents coming from and what is the type of them? It cannot be seen from the method you showed, apparently it is created somewhere else, above.
Here are the Instructions

and Classes

Obstacle


Game Entity


Troll


Jinn


Wall


Enemy


Player


Game


Warrior


Collector
This results in numVials being set to zero. Why not just set it to zero? Here and other places you mix in both direct access to a field and indirect access through a getter.
You could. I’ve changed up a few methods of the player and enemy class from not paying attention
 

Liutauras Vilda wrote:By the way, Obstacles sounds like a Dutch word, but it is English, I thought where I heard this word...


The Dutch word is 'obstakel' (single, plural 'obstakels'), with the emphasize on 'sta'.
Still left with the problem that Obstacle has no one-argument constructor...
True. At this point im so confused
(1 like)
I.s.o. looking for obstacle with single argument constructor, you should use the Wall object.
Use new Wall() for creating a wall, it will create an obstacle with strength 10:

As per your info:

Very true.

However,  the discussion started with the misery of creating an Obstacle in case of a wall. Two questions arise:

1) must OP really create an Obstacle? Why? The opponents are created anyway.
2) Or, if it really is necessary, might the omission of that constructor be one of those details that must be fixed?

I bet the first option!
(0 likes, 1 cow)
I agree with number 1:

When creating a Wall you are allready creating the obstacle (or opponent). It makes no sense to create another obstacle for that same wall...

In general it makes sense to:
* For Producing/Construction classes: Specific classes are used (Troll, Wall, ...)
* In the parts of the code that Consume/use classes, use Generic Interfaces or - in this case - parent classes (Obstacle)

As a tip it can be considered "cleaner" if you seperate the creation of your functional objects(logical/functional part) from the reading/parsing of the file (technical part).
To make this more clear an example which is pretty similar:

Here you see : first read the file, then create the objects, also the object creation is encapsulated in a factory which returns objects for consumers, but creates/produces specific objects


To give you an idea of what a factory could look like

Ruben, great contribution, clearly cow worthy Welcome back, I see you an old-timer.
@OP

1. Your methods isAlive() in all classes are incorrect at the moment.
2. Method drink() in Player class is also incorrectly implemented. There is a warning even issued by the instructor. I see you tried something, but unfortunately it isn't correct - re-read and give a try again.
3. Method relinquishVials() also incorrectly implemented. Read once again what is required to return.

Instructions place X wrote:Game
This class controls the game itself. Most of it has been implemented for you. This is all you need to do to this class for a grade of ‘C’.



Instructions place Y wrote:For a grade of C:
Just implement the above classes and interfaces and load the opponents from a file.



Grading system kind of contradicting a bit. Not really clear what you need to do for Grade "C". Of course you supposed to be interested only in the description of grade "A".
Minor comment:  In class Game you have...
Instead, try...
That way if you want to change the implementation of the List<>, you can.
Y'all I have to apologize.... me and not paying attention made this way harder on y'all. I was an idiot, and was not paying attention to my line which was creating a obstacle, even though it was pointed out probably 5 times. I misunderstood what the instructor was telling me, and made everything a mess that should have been avoided.......again im sorry to everyone

this is what I have to make it work. Which is as straight forward as it should have been if I wasnt an idiot............


Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 1078 times.

All times above are in ranch (not your local) time.
The current ranch time is
May 27, 2018 21:39:00.