aspose file tools*
The moose likes Beginning Java and the fly likes how to write the game-CrossingRiver (2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to write the game-CrossingRiver (2)" Watch "how to write the game-CrossingRiver (2)" New topic
Author

how to write the game-CrossingRiver (2)

jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
here are the newest things i've done.

I just wanna ask how to grab the string that the user input and then adjust the value?
If the user enter m in input1 and c in input2, what should I do to adjust?
Is the flow / sequence of the codes correct?
Or just focus on the line 35-62.
That is all what I am thinking in mind....
I really cannot figure out how to do the FOR LOOP part(grab the user input and move the m&c)
I just wanna know how to Interact with java in this case , what solution should I use..


is other things correct?
Thanks a lot again!


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

You are back to asking very vague, broad, poorly focused questions. Also, please realize that many folks around her are not going to be familiar with your other thread. They will come to this one and think "What the heck is this guy talking about? Oh well, I have better things to do than try and figure out what he wants".

Try and ask SPECIFIC questions. Try NOT to post 100+ lines of code and say "what is wrong with this"? Try NOT to say "I don't know how to do the for-loop stuff" and give NO explanation of what "the for-loop stuff' is.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
fred rosenberger wrote:You are back to asking very vague, broad, poorly focused questions. Also, please realize that many folks around her are not going to be familiar with your other thread. They will come to this one and think "What the heck is this guy talking about? Oh well, I have better things to do than try and figure out what he wants".

Try and ask SPECIFIC questions. Try NOT to post 100+ lines of code and say "what is wrong with this"? Try NOT to say "I don't know how to do the for-loop stuff" and give NO explanation of what "the for-loop stuff' is.

OK, I am sorry.
I just wanna ask how to grab the string that the user input and then adjust the value?
If the user enter m in input1 and c in input2, what should I do to adjust?
Is the flow / sequence of the codes correct?
Or just focus on the line 35-62.
That is all what I am thinking in mind.... Is it better or the same :S
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
I just wanna know how to Interact with java in this case , what solution should I use..
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

jack parker wrote:[quote=fred rosenberger
I just wanna ask how to grab the string that the user input and then adjust the value?


So, are you asking how to get user input?

Or are you asking how to change a String's value?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

jack parker wrote:I just wanna know how to Interact with java in this case , what solution should I use..


That's about as vague as a question can get.
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Jeff Verdegan wrote:
jack parker wrote:[quote=fred rosenberger
I just wanna ask how to grab the string that the user input and then adjust the value?


So, are you asking how to get user input?

Or are you asking how to change a String's value?

Yeah, Or are you asking how to change a String's value?<~can I do this??
because I wanna how can I Present that Left M and Left C went to Right become Right M & Right C~
For example~

Crossing the River
---------------------------------
Round 0:
Left Right
MMMCCC
-------
\-----/
First passenger (c for cannibal, m for missionary): h
Illegal input!
First passenger (c for cannibal, m for missionary): m
Second passenger (c for cannibal, m for missionary, n for none): c
---------------------------------
Round 1:
Left Right
MMCC MC
-------
\-----/
First passenger (c for cannibal, m for missionary): c
Second passenger (c for cannibal, m for missionary, n for none): c
Illegal input!
Second passenger (c for cannibal, m for missionary, n for none): n
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

Personally, I would approach it this way...

I'd create something like a RiverSide class. I'd make two - a left and a right. Each can hold various things - like Cannibals or Missionaries. I'd have a boat object which can hold up to two things, and can't move if it has nothing in it.

You may make a River class that holds two RiverSide objects.

Each time a RiverSide changes it's state (i.e. someone comes in or someone leaves), I'd have it check if the cannibals outnumber the missionaries, and if so, flag this as an illegal state.

the RiverSide class can also print who is on it - so you can ask your leftSide and it will say "MMMCCC" and your Right side will say "". So you may ask your River class what the state is - it would then query each side, and then give you the output you want.

I am really not sure what you are trying to do in your code. While you can't change a String, you can change what a String reference points to. So if you have

String myString = "ABC"

you can later set myString to point to "DEF". The original string still exists, but your reference now gives you something else.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

jack parker wrote:
I just wanna ask how to grab the string that the user input and then adjust the value?

So, are you asking how to get user input?

Or are you asking how to change a String's value?

Yeah, Or are you asking how to change a String's value?<~can I do this??


No. Assuming by "change a String's value" you mean "change the contents of a String object", you can't do that because Strings are immutable. You can, however, take a String reference variable and point it to a new String object. And that new String object may be based on the old String object, such as changing it all to uppercase, or removing or changing certain characters.

because I wanna how can I Present that Left M and Left C went to Right become Right M & Right C~
For example~

Crossing the River
---------------------------------
Round 0:
Left Right
MMMCCC
-------
\-----/
First passenger (c for cannibal, m for missionary): h
Illegal input!
First passenger (c for cannibal, m for missionary): m
Second passenger (c for cannibal, m for missionary, n for none): c
---------------------------------
Round 1:
Left Right
MMCC MC
-------
\-----/


Okay, so you really shouldn't think about this as "changing the value of a String". What you want to do is change the contents of the data structure that represents who is where, and then just display that data structure's contents.

This means you first need to think about how you're going to represent "2 Ms and 2 Cs on the left, and 1M and 1C on the right". There's no point in going further until that's sorted out.
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
fred rosenberger wrote:Personally, I would approach it this way...

I'd create something like a RiverSide class. I'd make two - a left and a right. Each can hold various things - like Cannibals or Missionaries. I'd have a boat object which can hold up to two things, and can't move if it has nothing in it.

You may make a River class that holds two RiverSide objects.

Each time a RiverSide changes it's state (i.e. someone comes in or someone leaves), I'd have it check if the cannibals outnumber the missionaries, and if so, flag this as an illegal state.

the RiverSide class can also print who is on it - so you can ask your leftSide and it will say "MMMCCC" and your Right side will say "". So you may ask your River class what the state is - it would then query each side, and then give you the output you want.

I am really not sure what you are trying to do in your code. While you can't change a String, you can change what a String reference points to. So if you have

String myString = "ABC"

you can later set myString to point to "DEF". The original string still exists, but your reference now gives you something else.

I am in a fog about this. Are you saying about "Method" in java?
I am sorry. !
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Jeff Verdegan wrote:
jack parker wrote:
I just wanna ask how to grab the string that the user input and then adjust the value?

So, are you asking how to get user input?

Or are you asking how to change a String's value?

Yeah, Or are you asking how to change a String's value?<~can I do this??


No. Assuming by "change a String's value" you mean "change the contents of a String object", you can't do that because Strings are immutable. You can, however, take a String reference variable and point it to a new String object. And that new String object may be based on the old String object, such as changing it all to uppercase, or removing or changing certain characters.

because I wanna how can I Present that Left M and Left C went to Right become Right M & Right C~
For example~

Crossing the River
---------------------------------
Round 0:
Left Right
MMMCCC
-------
\-----/
First passenger (c for cannibal, m for missionary): h
Illegal input!
First passenger (c for cannibal, m for missionary): m
Second passenger (c for cannibal, m for missionary, n for none): c
---------------------------------
Round 1:
Left Right
MMCC MC
-------
\-----/


Okay, so you really shouldn't think about this as "changing the value of a String". What you want to do is change the contents of the data structure that represents who is where, and then just display that data structure's contents.

This means you first need to think about how you're going to represent "2 Ms and 2 Cs on the left, and 1M and 1C on the right". There's no point in going further until that's sorted out.

YES..how you're going to represent "2 Ms and 2 Cs on the left, and 1M and 1C on the right".<<<<this is what I want to ask, can you give me some hints?
What should I type in the for loop to achieve this ↑>
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

jack parker wrote:
YES..how you're going to represent "2 Ms and 2 Cs on the left, and 1M and 1C on the right".<<<<this is what I want to ask, can you give me some hints?


This is exactly what Fred is talking about in the post where you responded that you're "in a fog". This really has very little to do with Java per se and is more about data structures, abstraction, and problem solving. If you're completely in the dark about this, you need to study some basic Computer Science.

I see two main approaches: One is to have something representing each "side" of the river, and each of those things contains indications of who is on that "side". The second approach is to just represent each missionary and each cannibal, and then each of them has an indication of where he is. I agree with Fred's suggestion to take the first approach. Either way, you're going to be defining at least oen new class to represent the concept of "side of the river" or "person who needs to cross the river", and maybe more.

What should I type in the for loop to achieve this ↑>


Approaching it as "what should I type?" is exactly the wrong way to learn how to program. We're not at Hogwarts learning the incantation for getting missionaries across a river. You need to think about "what data structures best represent the objects--both physical and abstract--in my problem domain?" and once you have a handle on that, then you translate those concepts into Java syntax.
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Jeff Verdegan wrote:
jack parker wrote:
YES..how you're going to represent "2 Ms and 2 Cs on the left, and 1M and 1C on the right".<<<<this is what I want to ask, can you give me some hints?


This is exactly what Fred is talking about in the post where you responded that you're "in a fog". This really has very little to do with Java per se and is more about data structures, abstraction, and problem solving. If you're completely in the dark about this, you need to study some basic Computer Science.

I see two main approaches: One is to have something representing each "side" of the river, and each of those things contains indications of who is on that "side". The second approach is to just represent each missionary and each cannibal, and then each of them has an indication of where he is. I agree with Fred's suggestion to take the first approach. Either way, you're going to be defining at least oen new class to represent the concept of "side of the river" or "person who needs to cross the river", and maybe more.

What should I type in the for loop to achieve this ↑>


Approaching it as "what should I type?" is exactly the wrong way to learn how to program. We're not at Hogwarts learning the incantation for getting missionaries across a river. You need to think about "what data structures best represent the objects--both physical and abstract--in my problem domain?" and once you have a handle on that, then you translate those concepts into Java syntax.

Do I just choose one of the two ways?
WTF...I am such an idiot in java!!!
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
jack parker wrote:
Jeff Verdegan wrote:
jack parker wrote:
YES..how you're going to represent "2 Ms and 2 Cs on the left, and 1M and 1C on the right".<<<<this is what I want to ask, can you give me some hints?


This is exactly what Fred is talking about in the post where you responded that you're "in a fog". This really has very little to do with Java per se and is more about data structures, abstraction, and problem solving. If you're completely in the dark about this, you need to study some basic Computer Science.

I see two main approaches: One is to have something representing each "side" of the river, and each of those things contains indications of who is on that "side". The second approach is to just represent each missionary and each cannibal, and then each of them has an indication of where he is. I agree with Fred's suggestion to take the first approach. Either way, you're going to be defining at least oen new class to represent the concept of "side of the river" or "person who needs to cross the river", and maybe more.

What should I type in the for loop to achieve this ↑>


Approaching it as "what should I type?" is exactly the wrong way to learn how to program. We're not at Hogwarts learning the incantation for getting missionaries across a river. You need to think about "what data structures best represent the objects--both physical and abstract--in my problem domain?" and once you have a handle on that, then you translate those concepts into Java syntax.

Do I just choose one of the two ways?
WTF...I am such an idiot in java!!!

It seems that I haven't learnt what exactly the two class stuff yet..
Do most of the above codes correct? Just missing something in the For Loop?
Or they are wrong?
Thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

jack parker wrote:
Do I just choose one of the two ways?


Well, hopefully not blindly. The idea is you think about how you might use each representation, and determine which one best suits the problem at hand and the rest of your approach to solving it. Sometimes it's no more than an educated guess, and sometimes you get halfway down the path of one approach and finally realize that it's all wrong, so you have to scrap it and start over.

Both Fred and I suggested the first approach. I think perhaps you're panicking and not taking the time to read closely enough and think about what we're writing, otherwise you'd have noticed that. I realize that you feel frustrated and overwhelmed, but if you're going to have any hope of getting through this, you absolutely must take your time to read, think, try to understand, and then try to move forward on your own.

WTF...I am such an idiot in java!!!


At the moment I'd say your difficulty has less to do with Java and more to do with thinking abstractly about problem solving. You're trying to rush right into "what do I type to get these guys across the river" but that's not how it's done.

Don't worry, most beginners go through this. It just takes a while to get used to a new way of thinking. The main thing is to take it slowly and worry about one little piece at a time.

So start by defining a class that represents a side of the river and holds missionaires and cannibals. That's it. Just the bare bones. Don't even think about drawing C and M on the screen yet.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

jack parker wrote:
It seems that I haven't learnt what exactly the two class stuff yet..


You haven't learned about classes in Java? You can't really get anything done until you do. That will be covered very early in any text or tutorial.

Do most of the above codes correct? Just missing something in the For Loop?


You're jumping all over the place and getting ahead of yourself. If you can't get your representation clear, there's no point in even trying to write any code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Have you got rid of the == trues and == falses yet?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8398
    
  23

Jeff Verdegan wrote:You're jumping all over the place and getting ahead of yourself. If you can't get your representation clear, there's no point in even trying to write any code.

'Fraid we've been through this on OP's other thread, but he seems intent on ploughing ahead regardless of all advice.

@Jack - for the (I think it is) 6th and final time: You will not get very far by 'gorilla programming' (ugh...me got problem...code...).
You must understand what you're doing and why you're doing it; and the only way to do that is to read a good book (Head First Java is supposed to be quite decent) and follow the exercises, or go through the Java tutorials (which you've already been given links to) - assuming you're not on a Computer Science course already.
Picking something that is clearly beyond your current capabilities and trying to force a solution out of it is likely to be a very frustrating experience that probably won't teach you a lot.

@Jeff: I still can't think of you as anything but 'jverd' , but your advice is, as usual, top-notch.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Jeff Verdegan wrote:You're jumping all over the place and getting ahead of yourself. If you can't get your representation clear, there's no point in even trying to write any code.

'Fraid we've been through this on OP's other thread, but he seems intent on ploughing ahead regardless of all advice.

@Jack - for the (I think it is) 6th and final time: You will not get very far by 'gorilla programming' (ugh...me got problem...code...).


I think I'm going to just back off and lurk for a while. If Jack starts following the advice to do less typing and more thinking, I may pipe up again, but otherwise I'm just beating my head against a wall. (Still gotta figure out the emoticons in this place.)

@Jeff: I still can't think of you as anything but 'jverd' , but your advice is, as usual, top-notch.


Thanks. And I'm glad I can finally stop wondering whether "Winston" is your first or last name.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8398
    
  23

Jeff Verdegan wrote:...but otherwise I'm just beating my head against a wall. (Still gotta figure out the emoticons in this place.)



Row 3 three, second from the end in the Emoticons tab at the top.

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

Jack,

You really don't need to quote an entire post when you reply - especially on that contains a quote of a quote of a quote of a reply. Just include the relevant parts.
jack parker wrote:Do most of the above codes correct? Just missing something in the For Loop? Or they are wrong?

The reason nobody can help you is that we don't know what your code is supposed to be DOING. I keep looking at it, and say "What the heck???"

Tell us in clear, plain, detailed English, what you want your code to do. At the very least, put more comments in your code so it is readable by someone who has not idea what it does - which is basically everyone but you, and will be you in a few weeks when you come back to it.

Seriously, this one line alone is enough to make me run away screaming:


What is that supposed to represent? What the heck is "rightM", and why doest it matter if it is (or isn't) three? Why do you still compare something to 'false'? As Campbell has pointed out, you should NEVER compare to 'true' or 'false'. You simply look at what it is (or what the negative of it is).
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred rosenberger wrote:Why do you still compare something to 'false'? As Campbell has pointed out, you should NEVER compare to 'true' or 'false'. You simply look at what it is (or what the negative of it is).


@Jack: And in case that's not clear (though I think it this may have been spelled out in your other thread):



But again, focus less on what to type and more on developing a clear picture of how to solve your problem without Java.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Winston Gutkowski wrote: . . . Row 3 three, second from the end in the Emoticons tab at the top.

Winston
No, no, no. Don’t tell him. I hate that emoticon, and the less I see of it the better.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Jeff Verdegan wrote: . . .


This is what I am glad Jeff Verdegan never wrote:. . .
You can see that the latter tiny difference can introduce nasty errors into your app.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8398
    
  23

Campbell Ritchie wrote:
This is what I am glad Jeff Verdegan never wrote:. . .
You can see that the latter tiny difference can introduce nasty errors into your app.

Yeah, I know: you don't like == true. I hope you understand why, on occasion, I will use == false though.

Oh, and sorry about the . Jeff's a good man though. I'm sure he won't abuse it.

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote: I hope you understand why, on occasion, I will use == false though.


Because you hate freedom and babies and puppies?

Oh, and sorry about the [censored] . Jeff's a good man though. I'm sure he won't abuse it.


MWUAH HA HA HA!!!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8398
    
  23

Jeff Verdegan wrote:
Winston Gutkowski wrote: I hope you understand why, on occasion, I will use == false though.

Because you hate freedom and babies and puppies?

No it's like...all them bangs, man...all that bangin'...negative vibes, man...it does me 'ead in....

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Shut up, or I shall have to use a certain emoticon
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Thank you all of you!! yeah i gotta admit that I am too rush on it.
I will read all of your comments clearly.
But I gotta go to school right now. [bangwall]
 
 
subject: how to write the game-CrossingRiver (2)