I have to do another flowchart, and I still don't understand how to do the assignment. So! Here I am. Again. Assignment instructions are as follows-

"Create the logic for the dice game Pig, in which a player can compete with the computer. The object of the game is to be the first to score 100 points. The user and computer take turns "rolling" a pair of dice following these rules:

On a turn, each player rolls two dice. If no 1 appears, the dice values are added to a running total for the turn, and the player can choose whether to roll again or pass the turn to the other player. When a player passes, the accumulated turn total is added to the player's game total.
If a 1 appears on one of the dice, the player's turn total becomes 0; in other words, nothing more is added to the player's game total for that turn, and it becomes the other player's turn.
If a 1 appears on both of the dice, not only is the player's turn over, but the player's entire accumulated total is reset to 0.
When the computer does not roll a 1 and can choose whether to roll again, generate a random value from 1 to 2. The computer will then decide to continue when the value is 1 and decide to quit and pass the turn to the player when the value is not 1."
Do you have a more specific question? What have you tried so far? Which pieces have you been able to figure out? Which pieces have you gotten stuck on, and why?
It's more the dice section throwing me off, I can't figure out how to get it so it would display 2 separate results from the dice roll, since 2 dice have to be rolled per turn. Since that's pretty much the beginning of it, I didn't want to try the rest yet until I can understand how to do that.

As I stated before though, the book is terrible with my learning style...I learn more in one post from you than I do in an entire chapter from the book. Might be better if it taught an actual language as well instead of just in pseudo code and flowcharts.
Well, how would you set it up if you had just one die to roll and display, then? Say you needed to roll the die, somehow remember the value from the die, then display that remembered value. What would you use? How would the pseudocode look?

Code:
start
   declarations
   num die = 1-6
   num playerCount = 0
   num computerCount = 0
   num total = 100

   roll 2 die
   display die roll result
Okay, you have some of the ideas here, so you're thinking in the right direction. You know what a variable is: you're creating one to store the total the player has gotten so far, another to store the total the computer has gotten, and a third to keep track of what the "winning" total the player and computer are aiming for. You also have a fourth one that if this were code would set die = -5, which I don't think is what you want. Smile In general, a numerical variable can only hold a single value. Perhaps it would help if I gave you a new function called randomInteger. randomInteger takes two arguments, the minimum possible value produced and the maximum possible value produced. It picks an integer between the minimum and maximum inclusive and returns it. So randomInteger(1,52) might return any number between and including 1 and 52. You could use this in a program by saying cardNumber = randomInteger(1,52). However, note that cardNumber would then be a single value. If you wanted another random number, you'd have to call randomInteger again.

A second point: you say "display die roll result", but to a computer, that might be confusing, because you never told it where to remember the result of rolling each of those two die. How might you expand that code to explicitly roll two dice, one at a time, store (remember) their values in some place(s), and then display those two values?
So would it be more like


Code:
start
   declarations
   num die = randomInteger(1,6)
   num playerCount = 0
   num computerCount = 0
   num total = 100
That would get you a single roll of the dice, since as I said, a function like that will run once, return a value, and then store that into die. If you want another roll of the dice, you need to call the function again to get a new value. A function call is a name attached to a bunch of code, in this case code that somehow generates a random number. When you use that function, that code is executed, and it returns a number, which the computer will just store into the variable die. If you need a new random number, you need to ask the computer to run the randomInteger code again, by saying somevariable = randomInteger(min, max) again.
Well I don't think I could do


Code:
num die = randomInteger(1,6) + randomInteger(1,6)


if for the simple fact that even if it works, it would add them together when I need them separate...Hmm, could I put an "and" in there instead of the "+"?
That's valid code, certainly; what do you think that code would produce? It sounds like you really have two steps you need to perform: (1) rolling a die and storing it to a variable (2) rolling a second die and storing it to a variable. Once (1) and (2) are done, you can display each of those variables.
Figures eating pizza would give me an idea.


Code:

start
   declarations
   num die1 = randomInteger(1,6)
   num die2 = randomInteger(1,6)
   num playerCount = 0
   num computerCount = 0
   num total = 100
   num result1
   num result2

   result1 = die1
   result2 = die2

   display result1
   display result2

You have written some code! Smile Is there a question attached to that code? Wink
I was wondering if I finally got that part right...I'm going to try and figure out the rest of this assignment and then edit it into this post (or add a new post, depending on if someone replies or not).
Reikanobi wrote:
I was wondering if I finally got that part right...I'm going to try and figure out the rest of this assignment and then edit it into this post (or add a new post, depending on if someone replies or not).
I'm not going to answer if it's right or not, but I will ask you to think about what you want it to do, and then execute it in your head to see if it does what you want. If it does, then it counts as right. Smile
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement