• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dice game issue

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody- I'm a new kid here,

As most of the beginners I’m experiencing some issues with my first Java program…

Basically I need to write a Pig Dice Game. Rules are as below:

· It’s a two players game- first to reach 100 or more points wins
· Players take turn- on each turn player rolls six-sided dice
· If the player rolls 1, then the player gets no new points and it becomes other player’s turn
· If the player rolls 2 through to 6, he/she can:
o Roll again
o Hold- at this point the sum of all rolls is added to the player’s score and it becomes the other player’s turn
· The first player is a human and the other is the computer
· When it’s the human turn, the program should show the score of both players and the previous roll. Allow human to put “r” to roll again and “h” to hold
· The computer should play according to the following rule:
o Keep rolling when it is the computer’s turn until it has accumulated 20 or more points. Then hold, If the computer wins or rolls 1, then the turn ends immediately.
· Allow the human to roll first.

Guys below is the code I came up with after I don’t know how many hours spent, that’s the best I could do so far…

The small success for me is the fact that finally the file complies and I don’t get any error messages, however the problem is that I’m not getting any output- just the message Process Completed (I use the Jcreator). Can you PLEASE have a look and help me out with that?
I know I still need to come up with the victory conditions- at the moment they are put as a comment- after putting them in I started to get again errors, but at least I want to have a peace of mind that the main part is done. I’m close to a nervous breakdown, because this thing is driving me mad… J

Any help will be much appreciated!!
Lucas


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please note the Code Button; code is difficult to read otherwise. I have been back to your code and edited it and you can see how much better it looks.

You ought not to need a humanTurn and a computerTurn field. Since humanTurn == !computerTurn at all times, you only need one. Put the initialisations for all fields into a constructor.
You are calling new Assignment(); Apart from the fact that you have a poor name for your class, you are at no point telling it to start the game. You need a method which starts the game, and you call that.
You will almost certainly want more classes.

Never use == true or == false, because it is poor style, and if you write = by mistake instead of == you can get nasty errors. I think you have in fact made that mistake somewhere.
 
Lucas Lata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richie!

Thank you for your help! Yes, I wanted to use editing function however I didn't know how to do it.

My lecturer said that everything should be done within one class only- forgot to put that in the requirements.

What do you mean by that? "Put the initialisations for all fields into a constructor." Are you trying to say that I should put boolean humanTurn !==computer turn in the HumanGame and ComputerGame method?

Also I don't know what to use as a trigger to start the game- any hints?

I just wanted to say that I'm really green, green beginner...

Thank you
Lucas
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lucas,

Welcome to Javaranch And he is Ritchie not "Richie"

You are calling new Assignment(); Apart from the fact that you have a poor name for your class, you are at no point telling it to start the game. You need a method which starts the game, and you call that.



1. This point tells two things a) Your class name is not related to its activity. b) You have just called default constructor Assignment() that calls the implicit constructor that does nothing. So you should either implement your own constructor that calls the method to start the game or you may make any method in the class and call that method by creating the object.

Considering you will have No-arg constructor in the class something like this - Please decide the best yourself.


Between, I noticed you have used method name HumanGame(); Method name should start with lowercase - just a convention.


Never use == true or == false, because it is poor style, and if you write = by mistake instead of == you can get nasty errors. I think you have in fact made that mistake somewhere



You have used while(computerTurn =true). This line does nothing it just assigning true to computerTurn . You must want to compare the values. So either you should use while(computerTurn ==true) ( That i won't suggest to use ) or you may use while(computerTurn) simply.(That i will suggest to do )

So just change these things in your program and let us know where you reach.

Between Why do you take Boolean Class. I think you should use primitive instead of wrapper classes unless necessary. You may use boolean instead of Boolean.


Regards,
Patricia
 
Lucas Lata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Patricia and Ritchie,

Just joined your forum and already managed to offend its members- Ritchie sincere aplogies for misspelling your name.

Patricia, Ritchie,

GREAT THANKS! It works now!! I do appreciate your advice. After I fired it off I've noticed that there are some issues with the order of tasks being carried out. I will try to fix them myself, if I get stuck I will let you know.

Guys you are GREAT!!! THANK YOU!!!

Lucas

PS. You saved one more guy from being sent to a mental institute!!!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucas Lata wrote: . . . already managed to offend its members

No you haven't offended anybody.

Ritchie sincere aplogies for misspelling your name.

Think nothing of it Lots of people get my name wrong.



Patricia, Ritchie, . . . THANK YOU!!!

Lucas

PS. You saved one more guy from being sent to a mental institute!!!

You're welcome.
And you will have seen how using == true and == false might lead to dangerous errors.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing you may need some advice on how to write your programs in the future. You posted 92 lines of code with "there's no output". To me, that says you wrote 92 lines of code before you tested anything.

When I write code, I try to write as little as possible before going through the compile/test/debug cycle. Sometimes, I'll write as little as one line. That really helps you focus in on the specific problem/task you are currently working on.

This does sometimes require you to write code you later throw away, like this:


My final code won't have either of those print statements, but it lets me follow the logic and flow of the code as i write it.

Just something to consider on your next program.
 
Lucas Lata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!

Hope everybody is well!

Ok, with your help I managed to nearly complete my project.

There is still small issue I need to overcome.

As stated before this ia a game between computer and a human. They swap their turns (as explained in the beginning) now I need to come up with the way to show the turn score and general score(. Winner will be the one who irst score >=100 points. And I don't know how to add points from each human or computer turn to humanTotalScore (or computerTotalScore). With the start of each turn humanTurnPoints or computerTurnPoints have to have value of 0. After human decides to hold- still the points won in a particular turn have to be added humanTotalScore.

I know it's more logical than programming issue, but can you please help?

Thanks in advance
Lucas
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Lucas Lata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Patricia,

Thank you for that! Just to explain you why I created two different variables.

humanTurnPoints- is to show points scored by a human in his/her turn (exactly the same idea is behind computerTurnPoints)- after you hold or loose your turn, human(computer)TurnPoints are reset to 0.

At the same time human(computer)TotalPoints sum up all the points scored in every turn by a human or a machine. And that's the problem for me...

Thanks for what you've suggested- humanTotalPoints does show all points scored during the game!

However I need to seaprately show points scored during the each turn.

I was trying to modify the code you gave me and I put an extra line:



Is there anything you'd suggest?

Thank you
Lucas

PS. Just FYI I'm blond- and no offence to the other blonds:P
 
Lucas Lata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Can you please let me know what should I do? I managed to reset humanTurnPOints to 0 after each time I throw 1 or pick "h", could you please give me an idea how to make the humanTotalPoints store all the humanTurnPoints won in each turn so far?

Thank you!
Lucas
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lucas,

What is it doing currently? Please explain it in the form of desired output.

Regards,
Patricia
 
Lucas Lata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Patricia,

Basically I would like to have the grand total points dispalying all points won by human. So those will be points won in all human turns.

So it will look something like that:
Points won in the human turn: X
Grand total points: X+X+X+X etc (depending how many human turns we had)

When human throw 1 or choose to hold- this means that this is the end of human turn and beginning of computer turn. So each time new turn starts human turn points are reset to 0- I managed to crack this by craeting an extra method:



Analogically the same will aplly to computer, however I will do it myself after I manage to crack the human stats.

Hope this helps.

Thank you
Lucas
 
Lucas Lata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Patricia and Ritchie!

Guys I've made it!!!

It works now the way it should be so Mission Accomplished!

Thanks guys for your help!!! Wouldn't be able to do it without you!!!

In case you are interested I'm posting my code...

Lucas
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good..... (Considering its your very first program)
 
Lucas Lata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic