aspose file tools*
The moose likes Beginning Java and the fly likes can anyone please briefly teach me how to write a classic game? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can anyone please briefly teach me how to write a classic game?" Watch "can anyone please briefly teach me how to write a classic game?" New topic
Author

can anyone please briefly teach me how to write a classic game?

jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
a classic game~Cross the River: Cannibals and Missionaries
by just using loop, selection (if,else....), scanner or other simple java ~
btw, i know how to win the game~haha~ but dont know how to present it by java ~need to grab input from player~
thanks a lot everyone!!!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

jack parker wrote:a classic game~Cross the River: Cannibals and Missionaries...

It would take too long to explain it all here. I suggest you try the Java Tutorials.

Once you've read them, try writing a program and come back if you have any problems.

Winston


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

Joined: Oct 02, 2003
Posts: 11499
    
  16

Your question is too broad.

Do you need to know how to download/install the JDK?
Do you need to know how to write a "Hello, world." type program?
Do you need to know how to compile a program?
Do you need to know what a class is?
Do you need to know how to lay out your program?
Do you need help working through the logic?

We really can't help you unless you tell us what kind of help you need. Detailed, specific, focused questions work best, not broad "Here is my homework problem - write it for me" requests.


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:Your question is too broad.

Do you need to know how to download/install the JDK?
Do you need to know how to write a "Hello, world." type program?
Do you need to know how to compile a program?
Do you need to know what a class is?
Do you need to know how to lay out your program?
Do you need help working through the logic?

We really can't help you unless you tell us what kind of help you need. Detailed, specific, focused questions work best, not broad "Here is my homework problem - write it for me" requests.

i am sorry,, i know the basic stuff
and i just wanna know for example how to show one round by one for the user

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

jack parker wrote:
i am sorry,, i know the basic stuff


That really doesn't mean anything.


and i just wanna know for example how to show one round by one for the user


So...

You're asking how to repeat a task multiple times? If so, google for java looping tutorial. Otherwise, go back and re-read Fred's post very carefully, and then try again to ask a more focused question.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

I would really recommend you read our HowToAskQuestionsOnJavaRanch FAQ. It give tips like "show us what you've done". We really can't give you advice, because we don't know what you've done.

For example, if i said "I need help getting from Orlando to Las Vegas", the advice you need would vary depending on whether you need help packing a suitcase, buying plane tickets, fixing a flat tire you got on the side of the road in Missouri...you get the idea.

"how to show one round by one for the user "

this could mean you don't know how to print to the screen (command window), you want help writing a GUI, you need help figuring out the algorithm, or you have it almost working, but it's doing one strange thing. How can we help you if you don't tell us what kind of help you REALLY need?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred rosenberger wrote:
For example, if i said "I need help getting from Orlando to Las Vegas", the advice you need would vary depending on whether you need help packing a suitcase, buying plane tickets, fixing a flat tire you got on the side of the road in Missouri...you get the idea.


This is one of the awesomest analogies I've ever seen to illustrate this very common point. I may just steal it!
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
thanks first , i will try the game by myself a couple more times~~
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43


why jgrasp said i was wrong in the lowest line,,, after the while~
error: cannot find symbol
the three s~ THANKS !!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

It's going to seem like I'm picking on you, but I'm really trying to help you get the best experience you can here on the Ranch.

First, when you post java source code, please use the'code' tags. after you paste it, highlight it call and click the 'code' button - just like you wanted to make something italic or bold. It preserves indentation and color-highlights things, making it MUCH easier to read. I put them in for you in your post.

Second, it is of GREAT help if you can post a complete example that demonstrates your problem. By doing so, people can cut-and-paste your source directly into a text file and compile it to see the error you get. Just giving a snippet means we have to go to a lot of effort just to get the compiler error you are seeing since...

Third, when asking about a compiler error, please post the entire compiler error. It usually tells you exactly what the problem is. I was able to play around with your code to get the same error:




So now, looking at this, I know that the compiler doesn't know what the variable 's' is in your while statement. That is because of the scope of the variable. Since you declared it inside the block defined by the curly braces on lines 1 and 8, the variable goes out of scope on line 9...it doesn't exist anymore. The simple solution is to declare it outside of your loop...so before your "do" statement, have this:

String s;

Then inside the loop, you'd simply need

s = sc.next();

As a side note, you NEVER want to compare strings with == or !=. It doesn't do what you think it does. You really need to use



or

jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
thanks fred rosenberger first! i can fix it now!
the question is
1. At the start of the game, there are THREE cannibals and THREE missionaries on one side
of a river.
2. Your task is to move all cannibals and missionaries to the other side of the river using boat.
3. The boat can only take ONE or TWO people each time.
4. When there are more cannibals than missionaries on one side of the river, they eat them and
you lose the game.

i dont really get it.
how to print m or c in each side by the user typing m or each round
and the boat "\-----/", "\"this symbol has an error (illegal escape character)
and why when the user do not type"m", "c", "n", it cant print "Illegal input!", for example, i type a, it still go on but not ask me to type again.
thanks so much !


there are things that i have done

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

jack parker wrote:...and the boat "\-----/", "\"this symbol has an error (illegal escape character)...

That one I'll give you for free: try using "\\-----/". Java Strings treat a single '\' character as the start of an escape sequence, and "\-" is plainly invalid.

As to the rest, here's my advice:

Rather than just coding, think about the problem. Do you know how to solve it? If not, research it a bit.
Turn off your computer, get out a pencil and paper and go through it yourself.
What are you doing, and why?
When you have a solution, write down the steps that got you there in English (or your native language), NOT Java.

Then turn your computer back on and start coding. You'll find it's a lot easier when you understand the problem.

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Winston Gutkowski wrote:"\-" is plainly invalid.

Only to someone who has been writing code for years...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
You need to look carefully at the bit about equals. Write down with the pencil Winston told you to use, under which circumstances that will return true and under which false.
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
thanks guys~
i can fix the boat and the illegal input~
but still figuring out how the M & C stuff works..
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
jack parker wrote:thanks guys~
i can fix the boat and the illegal input~
but still figuring out how the M & C stuff works..

but how can i communicate with java that if i input 2 c
then right hand side print out 2c and left hand side print out 1c and 2m?
Jonathan Bjerche
Greenhorn

Joined: Nov 14, 2011
Posts: 9
If you want to create games in java and you are a total newbie I would recommend you download Greenfoot (google it).

We have used it alot on our school for learning java, and I created a pretty good mario-like game when I almost had no programming-experience at all.
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
jack parker wrote:
jack parker wrote:thanks guys~
i can fix the boat and the illegal input~
but still figuring out how the M & C stuff works..

but how can i communicate with java that if i input 2 c
then right hand side print out 2c and left hand side print out 1c and 2m?

it is so hard..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
What have you changed, and what stage are you at now?
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Campbell Ritchie wrote:What have you changed, and what stage are you at now?

i fixed the boat and the illegal input. (the do-while part)
but the M&C stuff is still thinking
do the sequence of codes correct?
confused that how can i communicate with java, when i input m & c, how can he know and change the position of M/C.

thanks.!
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Jonathan Bjerche wrote:If you want to create games in java and you are a total newbie I would recommend you download Greenfoot (google it).

We have used it alot on our school for learning java, and I created a pretty good mario-like game when I almost had no programming-experience at all.

how can i use it
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Winston Gutkowski wrote:
As to the rest, here's my advice:

Rather than just coding, think about the problem. Do you know how to solve it? If not, research it a bit.
Turn off your computer, get out a pencil and paper and go through it yourself.
What are you doing, and why?
When you have a solution, write down the steps that got you there in English (or your native language), NOT Java.

Then turn your computer back on and start coding. You'll find it's a lot easier when you understand the problem.

Winston


This really is the best advice anyone could offer, and it applies to any software development no matter how simple or complex.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

jack parker wrote:how can i use it

start by reading the documentation.
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
jack parker wrote:
Campbell Ritchie wrote:What have you changed, and what stage are you at now?

i fixed the boat and the illegal input. (the do-while part)
but the M&C stuff is still thinking
do the sequence of codes correct?
confused that how can i communicate with java, when i input m & c, how can he know and change the position of M/C.

thanks.!

can anyone just give me some tips
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

jack parker wrote:can anyone just give me some tips

As far as I can see you've been given several.

You really do need to understand the problem and also how to write information to the console and receive it from the keyboard. And unfortunately for that, there is no substitute for reading the tutorials, the API for java.util.Scanner, or a decent book. Learning a language as complex as Java by 'gorilla coding' isn't going to work, and you're only likely to get more and more frustrated.

Winston
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Winston Gutkowski wrote:
jack parker wrote:can anyone just give me some tips

As far as I can see you've been given several.

You really do need to understand the problem and also how to write information to the console and receive it from the keyboard. And unfortunately for that, there is no substitute for reading the tutorials, the API for java.util.Scanner, or a decent book. Learning a language as complex as Java by 'gorilla coding' isn't going to work, and you're only likely to get more and more frustrated.

Winston

But seems I cant find a way out
I do try to read notes or something, maybe I am not a java guy...
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
jack parker wrote:
Winston Gutkowski wrote:
jack parker wrote:can anyone just give me some tips

As far as I can see you've been given several.

You really do need to understand the problem and also how to write information to the console and receive it from the keyboard. And unfortunately for that, there is no substitute for reading the tutorials, the API for java.util.Scanner, or a decent book. Learning a language as complex as Java by 'gorilla coding' isn't going to work, and you're only likely to get more and more frustrated.

Winston

But seems I cant find a way out
I do try to read notes or something, maybe I am not a java guy...

AM I suppose to use charAt in this case???
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

jack parker wrote:AM I suppose to use charAt in this case???

Don't think so; however, you might be better off using nextLine() for your input. That way the user has to press the Enter key before anything happens. You still may have to deal with the fact that they entered something wrong though. I'm also not sure that do...while is the best way to validate your input.

Winston
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Winston Gutkowski wrote:
jack parker wrote:AM I suppose to use charAt in this case???

Don't think so; however, you might be better off using nextLine() for your input. That way the user has to press the Enter key before anything happens. You still may have to deal with the fact that they entered something wrong though. I'm also not sure that do...while is the best way to validate your input.

Winston

thank you! do...while is for the user may input "not c, m, OR not n in second input"
is that correct?
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
jack parker wrote:
Winston Gutkowski wrote:
jack parker wrote:AM I suppose to use charAt in this case???

Don't think so; however, you might be better off using nextLine() for your input. That way the user has to press the Enter key before anything happens. You still may have to deal with the fact that they entered something wrong though. I'm also not sure that do...while is the best way to validate your input.

Winston

thank you! do...while is for the user may input "not c, m, OR not n in second input"
is that correct?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

jack parker wrote:thank you! do...while is for the user may input "not c, m, OR n in second input"
is that correct?

I understand why you're doing it, but:does exactly the same thing, and is a lot clearer in my view.

But I really think you need to stop simply coding and read the tutorials.

Winston
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Winston Gutkowski wrote:
jack parker wrote:AM I suppose to use charAt in this case???

Don't think so; however, you might be better off using nextLine() for your input. That way the user has to press the Enter key before anything happens. You still may have to deal with the fact that they entered something wrong though. I'm also not sure that do...while is the best way to validate your input.

Winston

the same question
but if I dont use charAt, how can i communicate with java that if i input 2 c
then right hand side print out 2c and left hand side print out 1c and 2m?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

jack parker wrote:but if I dont use charAt, how can i communicate with java that if i input 2 c

But you don't; you input each passenger separately. And as I said earlier, use nextLine().

Don't complicate things before you've even got it working.

Winston

jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Winston Gutkowski wrote:
jack parker wrote:but if I dont use charAt, how can i communicate with java that if i input 2 c

But you don't; you input each passenger separately. And as I said earlier, use nextLine().

Don't complicate things before you've even got it working.

Winston


but is that the same in this case? next or nexLine
because next grab a string and nexLine grab strings
if the user input a string only
sorry for my dumb question..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

jack parker wrote:but is that the same in this case? next or nexLine...

Obviously not, or I wouldn't have suggested the latter.

because next grab a string and nexLine grab strings
if the user input a string only...

Do you understand what next() actually does? It takes a string up to, but not including a delimiter (which, by default, is whitespace). This is not the "usual" input style that I (and I suspect many others) are used to.

For most interactive console programs, the usual pattern is:
1. Program asks for input.
2. User types something in and presses the 'Enter' key.
3. Program processes that input and may ask for more.

The equivalent to step 2 is nextLine(); and yes, the String it returns may contain more than 1 token. But since you've already told them what you want ('m' or 'c'), if they type anything else, it's an error.

Don't complicate things by allowing them to type '2c' or '2 c' in order to indicate "2 cannibals" (at least not YET).
Get things working the way you've asked for them (ie, one passenger at a time).

Winston
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Winston Gutkowski wrote:
jack parker wrote:but is that the same in this case? next or nexLine...

Obviously not, or I wouldn't have suggested the latter.

because next grab a string and nexLine grab strings
if the user input a string only...

Do you understand what next() actually does? It takes a string up to, but not including a delimiter (which, by default, is whitespace). This is not the "usual" input style that I (and I suspect many others) are used to.

For most interactive console programs, the usual pattern is:
1. Program asks for input.
2. User types something in and presses the 'Enter' key.
3. Program processes that input and may ask for more.

The equivalent to step 2 is nextLine(); and yes, the String it returns may contain more than 1 token. But since you've already told them what you want ('m' or 'c'), if they type anything else, it's an error.

Don't complicate things by allowing them to type '2c' or '2 c' in order to indicate "2 cannibals" (at least not YET).
Get things working the way you've asked for them (ie, one passenger at a time).

Winston

Oh, I see. Thanks for the clear explanation!
Then I add Line in the two do while loop!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

jack parker wrote:Then I add Line in the two do while loop!

Assuming you mean change next() to nextLine(), then yes; but I suspect your problems won't end there. You really need to read the tutorials.

Winston
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Winston Gutkowski wrote:
jack parker wrote:Then I add Line in the two do while loop!

Assuming you mean change next() to nextLine(), then yes; but I suspect your problems won't end there. You really need to read the tutorials.

Winston

Thanks
Should I read the I/O from the Command Line? If I need to due with this classic game.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: can anyone please briefly teach me how to write a classic game?