• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How would i create a game?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i have a main class, which is this. Im supposed to create 6 classes based on the 6 games and im wondering if i can input "1" for case 1, then have it run a class based on game 1. Is this possible?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, you're asking if Java has a facility whereby you can choose a particular course of action depending on user input? It would be a pretty useless programming language if it didn't. And you already know the answer anyway, since the code you posted is already doing just that.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know how to better word my question. Like if i have object classes, Game 1, 2 ,3 4, 5, and 6, all with different games. How can i have the user input "1" and the Game 1 class will execute?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hoang wrote:I dont know how to better word my question. Like if i have object classes, Game 1, 2 ,3 4, 5, and 6, all with different games. How can i have the user input "1" and the Game 1 class will execute?



Your question is actually just "How can I get the Game 1 class to execute?"

You already know how to do the part, "If the user inputs 1, how do I do something corresponding to 1?" I can see that you know the answer to that question, because you're doing it here:



If you wrote that code, and you understand what it does, then you know the answer to the first part of your question. So that only leaves the second part: "How do I execute Game 1?"

That answer also has to come from you, at least in part. You haven't told us anything about what it means to "execute the class for Game 1", so we don't know what you're actually trying to do. One example might be:


That's not the only possibility of course, just the simplest one that corresponds most directly to what little information you've provided.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i think thats the answer i was looking for. Sorry i couldnt be clearer about how i wanted to execute the class for game 1 because i am currently working on the class now. This is what i have so far as a frame of reference. Thanks for your answer.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so i got the class to execute, but now im having a logic error with my dice game. Im not sure how exactly to repeat the rolls until i get the entered number to repeat the entered amount of times. When i try the way i do here below, nothing comes out. What could i be missing in my logic?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is rather hard to follow, so I didn't study it in detail. This is always wrong though:



That is equivalent to:


which boils down to:


which does exactly nothing.

If you want to increment x, simply do:


If you set some variable to hold the value of x before it gets incremented, you can do:


in which case if x goes from 5 to 6, y will get the value 5. (So, as I said above, if you have x on the LHS instead of y, then x just gets reassigned whatever value it already has.)
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh i see, that makes sense. It still doesnt make my code loop correctly, so something else must be wrong. What also makes my code hard to read out of curiosity?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hoang wrote:What also makes my code hard to read out of curiosity?



Not sure. Might be the indentation's a little weird. Might be because I'm tired.

Also, don't do this:


Declare one variable per line:
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ok. I do both, but sometimes i end up putting all of them on one line because im too lazy to scroll further up and down haha
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,


I agree with Jeff for the solution to the problem. Nevertheless, a good maintainable code use the power of polymophism and design patterns.

You should have an interface called Game and six specific implementation classes Game1, Game2... Game6 implement this interface .

In you subsequent main code use only the Game gm = getGame();

getGame() method should create a specific Game object based on the user input for eg: new Game1 () should be returned if user selects 1 .

Regards
Vikram

 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not learned interfaces or polymorphisms yet. but i am close to getting my loops to work at least.
 
Marshal
Posts: 80508
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is something wrong with putting descriptions of the games in the main method. The main method should simply start off the application, so I think its ideal length is one statement. You can call other methods which give you the choice of games. But those methods should not have the descriptions of the games either. The descriptions are part of the game, and should therefore live in the game classes.
 
Campbell Ritchie
Marshal
Posts: 80508
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hoang wrote:I have not learned interfaces or polymorphisms yet. but i am close to getting my loops to work at least.

That makes it sound as though you are trying to do several things simultaneously. Don’t. Do one thing, get it working and then move on to the next thing.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im not trying to do several things at once. Im still currently working on making my loops work, it just takes me a while.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hoang wrote:Im not trying to do several things at once.


Actually, you are; otherwise Campbell wouldn't have said it. For starters, you have 7 games that you're trying to implement.
Why not one? Design it, code it, test it; and then move on to the next.

Im still currently working on making my loops work, it just takes me a while.


Well, most of your problems (as has already been said) stem from the fact that all your logic is crammed into a single method (main()), which is almost always a bad way to go.

My advice: StopCoding (←click). Work out what you're trying to do on paper and in English (or your native language).
You will never code your way out of a mess.

Winston

PS: Please DontWriteLongLines (←click). It makes your post very hard to read. I'd break them up myself, but there are tons of them.
You can do it yourself with the 'Edit' button. Remember: 80 characters maximum.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my mistake. Im not having trouble on my main method!! Ive been working on game 1 after i figured out the solution to my main. Thats why ive been saying that im working on one thing at a time. By loops, i meant the dice loops in my game1 class. Also, thanks for the tips, ill keep them in mind.
 
Campbell Ritchie
Marshal
Posts: 80508
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hoang wrote: . . . By loops, i meant the dice loops in my game1 class. . . .

And I though you meant those three do loops in the main method. You need to be clear; remember we can’t look over your shoulder and see what you are doing. (I bet you are pleased about that ‍).
And think of a better name for that class than game1.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your help. i thought i mentioned that i finished the main method and moved on to the class but Im have a huge tendency to be unclear. i was wondering why everyone was telling me i was doing more than one thing.
 
Campbell Ritchie
Marshal
Posts: 80508
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don’t code from the main method outwards. Code from the periphery, which means your basic classes, towards the centre.
 
Sheriff
Posts: 28385
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You asked about what makes your code hard to read. Here's an example:



Sure, you are using indentation, but the indentation doesn't match the actual code logic. The "else if" statement controls only the next statement, whereas the indentation implies it controls the next two statements. This can be very misleading and prevent you from finding bugs. Indentation which matches the actual code logic would be like this:


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Indentation which matches the actual code logic would be like this:




And better still would be to use braces, so that it's clear what you intended to be part of the else and what you intended not. (And still using proper indentation of course.)

Always use braces for if, else, for, while, even when they're not strictly necessary.


And of course, proper use of ++ :


 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok, here is my code, i think it looks pretty good i hope.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somewhat better, but the indentation is still pretty icky.

I notice that you have a mixture of spaces and tabs for the indentation, so that's at least part of the problem. You should always use only spaces (never tabs) for indenting. You should be able to set your editor up to indent up to a multiple of N spaces when you hit the tab key, where N is configurable by you, but should be 4 (or better yet, 2, IMHO, but 4 is more standard).
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ok. It always annoyed me that the tab button wouldnt be consistent everytime i pressed it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic