• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pig Game  RSS feed

 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a pig game (dice game) for my CS 140 class. The rules for the games are as follows. There are two players a human and an AI. The goal of the game is to be the first to get one hundred points or more. Players take turns rolling a six-sided die. If a one is rolled then the player gets no points and it becomes the other player's turn. If it is two through six then the player can do two things. Either roll again or hold to gain all of those points. Now my job is to make a program that gives the human player options to roll again with 'r' or hold with 'h.' Also, the AI has a cap of twenty points before they give the human their turn. Now my code so far is as follows:


When the game runs, it only runs through the human turn and the AI's turn, but then stops after that. My question is how do I keep the game going? Any help will be great.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah Osornio wrote:I am creating a pig game (dice game) for my CS 140 class. The rules for the games are as follows. There are two players a human and an AI. The goal of the game is to be the first to get one hundred points or more. Players take turns rolling a six-sided die. If a one is rolled then the player gets no points and it becomes the other player's turn. If it is two through six then the player can do two things. Either roll again or hold to gain all of those points. Now my job is to make a program that gives the human player options to roll again with 'r' or hold with 'h.' Also, the AI has a cap of twenty points before they give the human their turn. Now my code so far is as follows:


The first thing you need to do is use the [ code] [ /code] tags so that your code is formatted properly and everyone else can read it properly. Once you do that, you'll see that some of your while loops are not doing what you think they're doing. I've added comments to your code to show you where.



Note that you need to remove the spaces when you use the code tags. Question to the moderators, what's the escape sequence for a '['?
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing you need to do is use the [ code] [ /code] tags so that your code is formatted properly and everyone else can read it properly. Once you do that, you'll see that some of your while loops are not doing what you think they're doing. I've added comments to your code to show you where.

Note that you need to remove the spaces when you use the code tags. Question to the moderators, what's the escape sequence for a '['?


So is my problem with the booleans used for the while loops or the values I gave to the while loops? For example: while(computerTotal >= 100) or while(computerTotal >= 100)
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a more fundamental problem than that. Your problem is that your while loops don't do anything. Their body consists of a semi-colon, which is a valid statement but has no effect what-so-ever.
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:It's a more fundamental problem than that. Your problem is that your while loops don't do anything. Their body consists of a semi-colon, which is a valid statement but has no effect what-so-ever.

but if I remove the semi-colons the code reports an error for the while statements.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the while-loop needs to have a body. If you don't use a code block ({ and }) then Java assumes that the next statement it sees is the while loop body. In your case, that's the empty statement represented by the lone semi-colon.

My advice would be that whenever you write a while-loop, an if-loop or any other compound statement, always follow it by a code block.



But back to your problem, what do you want those while loops to do?
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:That's because the while-loop needs to have a body. If you don't use a code block ({ and }) then Java assumes that the next statement it sees is the while loop body. In your case, that's the empty statement represented by the lone semi-colon.

My advice would be that whenever you write a while-loop, an if-loop or any other compound statement, always follow it by a code block.



But back to your problem, what do you want those while loops to do?


well, the while loops are actually suppose to be for the do-while, but I know that the way I did it was incorrect. My teacher gave us a format to follow, but I got confused on some thing. I will post what he gave the class below


The problem that I have is that I don't understand when he says "set turn over to true and exit do-while loop." So that is why there are so many while statements.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did suspect that might be the case. When you say your teacher gave you a format to follow, do you mean he gave you some template code to complete, or just gave you a hint as to how to solve it?

My suggestion would be to take a step back and think about the problem from the beginning. Come up with a plan of exactly what you want to do (as in what steps you would take if you were doing this by hand) If you were given template code use that as a starting point, otherwise start with a very basic program (equivalent to a hello world program).

Then, build up from that starting point and add one small bit at a time. Keep everything very simple, and build up the complex steps gradually. Each time you add a new bit, test it immediately. If you have been taught about writing methods then I would suggest you split the program up into different methods to keep it simple.

Edit: I didn't read your post properly! I see you have put the template pseudo-code in there.
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:Yes, I did suspect that might be the case. When you say your teacher gave you a format to follow, do you mean he gave you some template code to complete, or just gave you a hint as to how to solve it?

My suggestion would be to take a step back and think about the problem from the beginning. Come up with a plan of exactly what you want to do (as in what steps you would take if you were doing this by hand) If you were given template code use that as a starting point, otherwise start with a very basic program (equivalent to a hello world program).

Then, build up from that starting point and add one small bit at a time. Keep everything very simple, and build up the complex steps gradually. Each time you add a new bit, test it immediately. If you have been taught about writing methods then I would suggest you split the program up into different methods to keep it simple.

Edit: I didn't read your post properly! I see you have put the template pseudo-code in there.


No, template code just that worksheet. Yes, I see what you mean, and I have tried doing that. In fact, that is how I got this code (the one before was worse). However, I believe that my problem lies mostly with the while loops at the end. Should I just delete them or would be best to keep some of them?
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah Osornio wrote:I am creating a pig game (dice game) for my CS 140 class. ...When the game runs, it only runs through the human turn and the AI's turn, but then stops after that. My question is how do I keep the game going? Any help will be great.


It might help if you work out your logic using pseudocode (plain English). Get a clear understanding of the solution first, then implement it in code. For example:




I'm assuming that the human always rolls first, and that the computer keeps rolling until it sees a 1 or already gained 20 points.



 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote:
Noah Osornio wrote:I am creating a pig game (dice game) for my CS 140 class. ...When the game runs, it only runs through the human turn and the AI's turn, but then stops after that. My question is how do I keep the game going? Any help will be great.


It might help if you work out your logic using pseudocode (plain English). Get a clear understanding of the solution first, then implement it in code. For example:




I'm assuming that the human always rolls first, and that the computer keeps rolling until it sees a 1 or already gained 20 points.




yes, the human always goes first. Also, I never thought of having it in English form (it sure does make more sense). Thanks, I will try making a code with that in mind.
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah Osornio wrote:yes, the human always goes first. Also, I never thought of having it in English form (it sure does make more sense). Thanks, I will try making a code with that in mind.

Using pseudocode let's you workout and even test your solution without having to worry about the coding details. I tell my students to solve the problem first, then code. That being said, I think the pseudocode I provided is good, or pretty close to what you need.
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote:
Noah Osornio wrote:yes, the human always goes first. Also, I never thought of having it in English form (it sure does make more sense). Thanks, I will try making a code with that in mind.

Using pseudocode let's you workout and even test your solution without having to worry about the coding details. I tell my students to solve the problem first, then code. That being said, I think the pseudocode I provided is good, or pretty close to what you need.

seems to be the case. Now I understand that the while loops are actually the end of do-while loop which explains somethings.
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah Osornio wrote:seems to be the case. Now I understand that the while loops are actually the end of do-while loop which explains somethings.

Yes. There are two types of WHILE loops. WHILE (checks the condition first) and DO-WHILE (checks the condition at the end).
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote:
Noah Osornio wrote:seems to be the case. Now I understand that the while loops are actually the end of do-while loop which explains somethings.

Yes. There are two types of WHILE loops. WHILE (checks the condition first) and DO-WHILE (checks the condition at the end).

okay, thank you for your help. The way you organized it also helps because I didn't know how to place in the code for both the human and the computer when they are less then 100, but placing it in the first while loop makes sense. Does that mean there should be another while loop for those who reach over one hundred or is that nested inside?
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah Osornio wrote:okay, thank you for your help. The way you organized it also helps because I didn't know how to place in the code for both the human and the computer when they are less then 100, but placing it in the first while loop makes sense. Does that mean there should be another while loop for those who reach over one hundred or is that nested inside?

In the pseudocode that I provided, the outer (main) WHILE loop ends when one or both scores are greater than or equal to 100. When you break out of that loop, you just have to calculate the winner by comparing the two scores. There is no need for additional loops.
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote:
Noah Osornio wrote:okay, thank you for your help. The way you organized it also helps because I didn't know how to place in the code for both the human and the computer when they are less then 100, but placing it in the first while loop makes sense. Does that mean there should be another while loop for those who reach over one hundred or is that nested inside?

In the pseudocode that I provided, the outer (main) WHILE loop ends when one or both scores are greater than or equal to 100. When you break out of that loop, you just have to calculate the winner by comparing the two scores. There is no need for additional loops.

So, if it goes to the winner by default if the limit is reached. One more question if you don't mind. Do I need any booleans to make this work or can I get by with only int? Also, what does "<>" mean?
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah Osornio wrote:So, if it goes to the winner by default if the limit is reached. One more question if you don't mind. Do I need any booleans to make this work or can I get by with only int? Also, what does "<>" mean?


Whether you use boolean variables or not is up to you. I don't think you need them based on my pseudocode. And "<>" is another way of saying "not equal to". In Java, that's: !=
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote:
Noah Osornio wrote:So, if it goes to the winner by default if the limit is reached. One more question if you don't mind. Do I need any booleans to make this work or can I get by with only int? Also, what does "<>" mean?


Whether you use boolean variables or not is up to you. I don't think you need them based on my pseudocode. And "<>" is another way of saying "not equal to". In Java, that's: !=

I thought so based on the last statement. Now I wrote the new code, but for some reason the compiler says my else statement is wrong? I checked my previous one and performed the same steps, but I am still getting this error for some reason.

 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah Osornio wrote:I thought so based on the last statement. Now I wrote the new code, but for some reason the compiler says my else statement is wrong? I checked my previous one and performed the same steps, but I am still getting this error for some reason.




The error is here. You cannot place a statement in between the IF and ELSE clauses of an IF-THEN-ELSE statement. You probably meant to put that inside the previous IF clause.
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote:


The error is here. You cannot place a statement in between the IF and ELSE clauses of an IF-THEN-ELSE statement. You probably meant to put that inside the previous IF clause.

whoops! yeah that was the error thanks again.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some other problems with the code. Start with the top-most problem and work down:

 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Here are some other problems with the code. Start with the top-most problem and work down:


yeah, I just fixed those. However, for when I declared choice == 'h' the code want me to initialize it at the beginning of my code. If I do this will I have only 'h' as an option or will 'r' still be in use?
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nevermind, it works either way. However, I am experiencing a problem in game where when I roll a number. The numbers don't add up, and if I hold the number will hold, but when I roll its says that if I hold I gain as many numbers as I had in storage. Is there a reason for this?
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, the game seems to work now so here is the final product.


I thank everyone who was so kind to take time out of their day to help me out!
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah Osornio wrote:nevermind, it works either way. However, I am experiencing a problem in game where when I roll a number. The numbers don't add up, and if I hold the number will hold, but when I roll its says that if I hold I gain as many numbers as I had in storage. Is there a reason for this?


In your latest code, you never update humanTotal or computerTotal. For starters...
 
Noah Osornio
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I'm still encountering one error. When the computer reaches twenty points they go only one turn because they surpass the amount. Is there anyway to fix it where the AI goes a max of twenty a turn and not twenty then once a turn? I fixed a lot from the old code.

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by twenty then once? As far as I can see the computer will stop when it scores 20 or more. Are you wanting to limit it to a maximum of 20 points?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!