Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone help me check my code?  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have to create a code to play a game of Duck Duck Goose. Here is the exact project

"You are writing a program that simulates a game (such as Duck Duck Goose). The following are the requirements: 1. You must ask the user for the number of participants 2. You must ask the user for the length of the cycle (for example, Duck Duck Goose has a cycle of 3, whereas Eenie Meenie Miney Mo has a cycle of 4) 3. Your project must ONLY display the winner of the game (counting from 0). So, for example, if the winner is the 4 th participant, your program should display 3"

I have a code that I think might do this that technically works.



The code compiles and works but I don't think it is doing what I want it to do. When I input 4 for the number of cycles and 8 for the number of players I get duck as the output, but when I do it on paper I get Goose. It's very possible that I am doing it wrong so I just want to make sure my code is correct and if not what should I do to fix it?
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have other things wrong but for starters I suggest initializing the player array list to integers representing their original position number. The requirements ask that you output this value from the last remaining player. The requirements don't ask for "duck" or "goose".
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:Hi I have to create a code to play a game of Duck Duck Goose. Here is the exact project

"You are writing a program that simulates a game (such as Duck Duck Goose). The following are the requirements: 1. You must ask the user for the number of participants 2. You must ask the user for the length of the cycle (for example, Duck Duck Goose has a cycle of 3, whereas Eenie Meenie Miney Mo has a cycle of 4) 3. Your project must ONLY display the winner of the game (counting from 0). So, for example, if the winner is the 4 th participant, your program should display 3"

Are you sure that's exactly it and all of it?

It seems to me like there's still a lot of missing information. That or whoever gave these specifications assumes that everybody who reads them will know what other unspecified "rules" are supposed to apply. For example, how exactly does someone win in this game? The specs don't explicitly say.

What exactly does a "cycle" mean? Are there constraints to the length of cycles that the program is supposed to accept? That is, does the cycle length need to be at least equal to the number of participants or does it have to be greater? What about constraints on the number of participants? Is the number displayed at the end the original position of the winning participant, before anyone was eliminated?

As I have seen Duck Duck Goose being played by school children, whoever is tagged the "goose" has to chase the old "goose" and try to tag him/her. The person being chased can then take the "goose"'s place in the circle before getting tagged and be considered "safe". If you get tagged before you can get back safely in the circle, you are eliminated. Your specs don't say anything about all this. Is this to be assumed then? If so, then again, what should the program output, the original position of the winner at the start of the game? Same deal with Eenie Meenie, except you just get eliminated, no chasing around the circle.

Seems like the most logical thing for this simulation, now that I think about it, is to eliminate each person on whom a "cycle" ends and keep going until there is only one participant left. That would be the winner of the game. Is this the unstated rule then?

But answer my original question first: Is what you gave as the "requirements" the complete and exact specification that you were given by your instructor?
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:
The code compiles and works but I don't think it is doing what I want it to do.

This may seem pedantic but... Did you mean to say that the code compiles and the program runs? A program can execute but still not actually behave the way it's supposed to. In other words, you can run the program but it doesn't actually work correctly. Is that what you meant to say? Because to say that something works but you don't think it is doing what you want it to do is a bit of a contradiction. If it doesn't do what you want it to do, then it doesn't actually work, does it?

The point I'm trying to make is that program specifications need to be free from ambiguity. If your specs are ambiguous, then your program will most probably reflect that ambiguity. When it comes to specifying program behavior, you need to be very precise.

When I input 4 for the number of cycles and 8 for the number of players I get duck as the output, but when I do it on paper I get Goose. It's very possible that I am doing it wrong so I just want to make sure my code is correct and if not what should I do to fix it?

As noted by someone else, this is not the specified behavior. You're supposed to output a number that indicates which participant won the game. In order for anyone to give you suggestions on how to fix your code, you need to be very clear about how the program should actually behave. Don't make any assumptions that everyone else knows what you and your instructor know that's not explicitly stated in the requirement statement that you provided.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Are you sure that's exactly it and all of it?

I think so. Based on the way I've seen game played, I'd describe it as follows:

If the cycle is c and the number of participants p:
1. Arrange them in a circle.
2. Create a set of cards numbered from 0 to p-1.
3. Starting with card #0, go around the circle in a clockwise (or anti-clockwise) direction, giving each participant the next higher numbered card.
4. Go around the circle in the same direction, removing every 'c'th participant from the circle until there is only one left.
5. Display the number on the card of the remaining player (who is the "winner").

I should perhaps point out that the way we played "eeny meeny miny mo" at primary school, it had a cycle of 16.

@John: If I'm wrong, let us know.

Winston
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry for not being clear but the program I am trying to create is one that will play an "elimination" game. The user inputs the number of players and the number of cycles or the amount it goes around in. Then whoever the program lands on is eliminated. So if I pick eight players and I have four cycles that means the program counts to four then the fourth person is eliminated, then it counts by four again and the eighth person is eliminated. Then it keeps going until there is one winner. I used "duck duck goose" because I could not figure out how to input numbers into the array list based off user input. The program I am trying to create is not like actual game of duck duck goose and I am just using those variables as an example. It would be the same thing if I use eney, meeney, miny, moe or anything else really. I'm sorry I did not specify this before.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:I am sorry for not being clear...

No probs. It sounds pretty much like what I wrote.

So the main thing your program will have to do is keep track of which number the winner was originally given when they started the game.

Tip: Use meaningful names when you write programs. A List called 'a1' doesn't mean anything to anyone trying to help you. What's it meant to contain?

Also: Play the game out on paper and try to translate that to a program. Right now it seems like you're guessing at what you're going to need.

HIH

Winston
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are my tips:
1. The % operator is useful for wrapping an index around to the beginning of a range once it "falls off" of the end.
2. You put "eliminated" participants at the end of the line and just not include them in the next cycle. This involves a "swap" operation.

additionally, if you imagine this as kids sitting in a line. when someone is eliminated, the eliminated kid just keeps switching with the next kid in line until he/she gets to the end of the line. This keeps the original order of the remaining participants intact. Then the next cycle wraps at the last kid who is still eligible. When you have only one kid left, you just announce the "name" of that kid. So if you're supposed to announce the original position of the winner, then what does that tell you what the "name" should be?
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so after a little experimenting I was able to add the numbers of the players positions and I now know what is wrong with my code. My code picks player number 1 every time because of the remove from the array list part of the code I have on line 34. I have to so it removes the second character every time so no matter what it's going to land on the first player. How can I change my code so that it removes the player that the code lands on? Here is my new code.




 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote: 3. Your project must ONLY display the winner of the game (counting from 0). So, for example, if the winner is the 4 th participant, your program should display 3"


Line 6 increments countUp so the "first player", that is player number '0' instead has the number '1'.
 
Dor Burd
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you find an appropriate place to insert something like:

playerToRemove = numberOfMovesMade % totalNumberOfPlayers;

al.remove(playerToRemove);


I'm very much a beginner, but I *think* that might be the solution.
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should really learn how to indent code properly. My teachers would refuse to read that code and would give the lowest grade, even if it worked flawlessly.
 
Dor Burd
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles D. Ward wrote:You should really learn how to indent code properly. My teachers would refuse to read that code and would give the lowest grade, even if it worked flawlessly.


Heh. I'm building up for an exam and one of the things I've been warned about is that the code in the questions won't be indented properly and it will not conform to best practice but I need to be able to read it anyway. I appreciate you and your lack of proper indentation OP.
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dor MyLastName wrote:Could you find an appropriate place to insert something like:

playerToRemove = numberOfMovesMade % totalNumberOfPlayers;

al.remove(playerToRemove);

The problem with modulus is that it is not "totalNumberOfPlayers" but "currentNumberOfPlayers" which is constantly changing.
Better to have something like an "index" to point to the current player and increment it. When it gets to the end, return to the first one.
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:The problem with modulus is … When it gets to the end, return to the first one.
But wrapping around is exactly what % helps you do. The solution I suggested (see my previous reply) moves eliminated participants and adjusts where the "end" is. When you've adjusted the line down to one active participant, then you have your winner.

 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you're not concerned about doing anything with the eliminated participants, you don't even have to move them past the "end" of the line. You just replace the eliminated participant with the next participant and keep going down the line, moving all remaining participants down one spot from their current position.

I haven't actually tried it yet but I'm thinking the bulk of the code for this program, excluding the parts that deal with Scanner, could be written in 10-15 lines of code, maybe less. One for loop to set up the line, one loop to cycle, another loop to move remaining participants up one spot.
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:And if you're not concerned about doing anything with the eliminated participants, you don't even have to move them past the "end" of the line. You just replace the eliminated participant with the next participant and keep going down the line, moving all remaining participants down one spot from their current position.

I haven't actually tried it yet but I'm thinking the bulk of the code for this program, excluding the parts that deal with Scanner, could be written in 10-15 lines of code, maybe less. One for loop to set up the line, one loop to cycle, another loop to move remaining participants up one spot.
He's already using an ArrayList, so removal should already handle this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!