• 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
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Dice Game in java

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dice Game Write a program that plays a simple dice game between the computer and the user. When the program runs, a loop should repeat 10 times. Each iteration of the loop should do the following: Generate a random integer in the range of 1 through 6. This is the value of the computer's die. . Generate another random integer in the range of 1 through 6. This is the value of the user's die. The die with the highest value wins. (In case of a tie, there is no winner for that particular roll of the dice.) As the loop iterates, the program should keep count of the number of times the computer wins, and the number of times that the user wins. After the loop performs all of its iterations, the program should display who was the grand winner, the computer or the user. Computername is a specially-formed String that consists of 3 letters (can be upper-case or lower-case), followed by a dash, followed by 4 numbers. For example, "abc-1234".  All computer name follow this pattern, otherwise they are invalid. Use this UML diagram.


Sample output:

Enter computer name: yza-3512
Enter user name: Johny
This simulates the rolling of a 6 sided die and a 6 sided die
Rolling the dice 10 times
Die1  Die2
3   6
5   4
6   6
6   5
1   4
4   4
6   6
1   2
3   5
2   6
Computer=8 user=2
Grand winner is computer yza-3512

Or

Grand winner is user xyz

Or

There is tie between computer xyz-1234 and user Han

Can Someone help me on this code and check if everything is correct


my codes is:



 
Master Rancher
Posts: 170
24
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Could you format your code please? In the post editor there is a button that says 'code' that will give you two tags: [code] and [/code]. Your code needs to go between those tags. It will look much better, and will be easier to read then. Thanks!
 
olu idowu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where is my code!
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I reverted your last edit.  It looks like you deleted you code by accident.  It's back now.
 
olu idowu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone Help me, please?
 
Knute Snortum
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can stop editing your post; I will fix it for you.  Then on to answering your question.
Content minimized. Click to view
 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Good to see somebody who knows how to format code well I hope I have made the code and the UML look better by wrapping them with the code button, as JJ suggested.
Please go back to the UML diagram Unfortunately you haven't made the die match that UML diagram. You have all sorts of fields which aren't in the UML. I think a die has only one field, viz. number of faces. That is what the UML calls sides. You have called it MAX, which would be acceptable, but if you spell it MAX rather than max, make that field final. I can understand that a Die would have a field which the UML calls value, and that is the current face showing, but I wouldn't give it any more fields. You are going to have to give it more fields, however, because that is what the UML says. Otherwise you will lose marks. All the other fields you have in the current Die class should be removed, along with the methods associated with them.

At this point I am going to disagree with the UML and tell you to query it with your teacher, but this will be the subject of a future post. Watch this space.
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a copy of the UML you showed us. Remember you must follow the UML to get full marks, even if I think the UML isn't ideal.Did you copy the class name correctly? I would expect it to be Die.
I worry when I see setXXX() methods for everything. Yes, I know the books tell you to write them, but I don't think they are usually good OO design. You are able to change the player's name on that Die. Yes, it can happen that player 1 starts a game and player 2 finishes it. But would you want to change the number of sides? If it says so in the UML, however, you will have to implement those methods.

Minor quibble: You have constructors in that UML and they are called “method mmbers”, but constructors are neither methods nor members of a Java® class.
Moderate quibble: You are shown two constructors which might neither of them get an object into a consistent state. If you call new Die(6) you will probably get a die without user names; if you call new Die("XYZ‑12345", "Olu") you might not necessarily set the sides. It is however easy enough to write code in that constructor to initialise sides. Please compare the constructors you were told to use with what you have written.

This where I start seeing problems. You have been given an overspecified assignment, which means you don't get marked for your ability to write object code, but your ability to follow a UML diagram. I think you are thereby forced into less than perfect design. I don't think a die “knows” who is rolling it. I think it would be better design to have a Player class, which will have the name. I think a player HAS A die and a die DOESN'T HAVE a player. Also, dice don't remember past scores. The players or the game would have to remember those scores. Similarly the game will determine a winner, not a die.
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like arithmetic with Math#random(), as explained here. You have declared a Random object (good) but you haven't used it.
 
Marshal
Posts: 15630
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a terrible design.

Providing setters for side and value is terrible.

The side attribute would be a fixed property of a die and therefore would either be defined as a constant for all die or as an immutable property for each specific die and passed in to the constructor.

The value is a state that will be maintained by the die object and would be reassigned every time roll() is called. Outside code has no business setting this value for the Die object. The value can either be randomly set on object creation, be set to a fixed default value, or it wouldn't be set yet and an IllegalStateException would be thrown if you called getValue() before roll() has been called at least once after creating the object.
 
Saloon Keeper
Posts: 7175
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No wonder I wasn't much help to you last night. You started a new thread with zero context about your problem.

I don't get what the requirements mean by a "value". If it means the side of the die, well, you've got two dice and only one value, what then?

It also seems like roll() plays the entire game including computing and printing the winners.

You've got a Random object, which is very good, but you aren't using it. And you've got sides which you aren't using either.

The requirement call for checking if the computer name follows the correct format, but it doesn't say what to do if it doesn't.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so lets start form top
why using methods in construct?
Max in original code must be max(we use MAX if its final)
why make roll int . void is better option and using winner method in roll because to determine winner for every round i didn see a use for boolean for winner here is your code that i change (i try to minimize changes so i didnt remove setter methods extra variable and ....
(in your original code because you start the game in construct the setter method has no use .here you can use them before you use roll methods  )


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

sahan foraty wrote:why make roll int . void is better option


I would disagree that void is a better option. I prefer semantics that allow me to write code like this:

 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic