• Post Reply Bookmark Topic Watch Topic
  • New Topic

My code is not doing what is it supposed to and I don't know why  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 program that plays an elimination game. The user has to input the number of players and the number of "cycles" or how much the program counts by. So for example if I input 8 players and 4 cycles then the first player out will be player 4 and then the next one would be player 8, and then it goes on and on until there is one winner. As of now my current plan to change any variables that the cycle lands on to 999 and keep going until there is one winner. I know I posted this a few times before but I made some major changes to my code (almost everything) and I think it should be working now. It gives me an output but it is not correct every time. How can I fix this?

Les Morgan
Posts: 779
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your assignment description, you should be moving through the players and when the cycle ends, that player is eliminated. Right?

Well, do a manual step through your while loop, you are not sequentially stepping through your players like you think you are, your formula(e) are not correct.

You have an array--which you named array (BTW: bad idea)--of players, and you have entered a number of cycles. Your cycles don't change even though your effective number of players do change. What you are trying to implement is a circular list using an array, and a spanning function using your cycles. You have an added problem of having to step through, and not count, players that have been eliminated. I don't see in your code where you do that.

Your array should go from 0 to your number of players minus one.
Your counting or cycle function limit is set--you chose the number of cycles and that has to be set throughout the game.
You are marking the eliminated by adding 999 as their value--ok, not a problem--but you need to step over them when they are eliminated.

I would look into something along this line:

1--initialize all the players to the same value--I like 0.
2--make a flagged exit while loop: while(!bExit)
3--in the body of that loop I would put the halting condition--at the end of the loop: if(playerCount==1) bExit=true;

So now you need something to keep track of where you are in your player array--playerNumber should run from 0 to playerCount-1, that means you have to have a condition that makes your index circular: if(playerNumber>playerCount-1) playerNumber=0; there you have a circular index.

You need to account for your cycle: Cycle should run from 1 to cycle. You need it to refresh also and do something if it reaches it's count. also remember don't count eliminated players. so if(playerArray[PlayerNumber]==0) check cycle if you are on the end of the cycle, then cycle = 1, mark the player eliminated and decrement the playerCount, and increment the playerNumber. But if you are not on the end of the cycle just increment the cycle counter.

But what if PlayerNumber != 0 ? Well, then you have to go on to the next player by just incrementing the playerNumber, but nothing else should change because you are on the same cycle because the player has already been eliminated.

If I have kept track in my mind correctly, the loop should exit when your playerCount == 1 and you can find which player by running the array through and seeing which one still is 0.

If you want to track how many cycles you have to go before someone wins, then make a counter and increment it every time you have to loop your cycle back to 1.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!