OK, programming peeps … here’s a puzzle for you. This is my “end of game doesn’t work” bug, but it’s a weird one. I would be thrilled if anyone had any insight at all to why this particularly odd bug is happening.
CODE AND EXPLANATION FOLLOW
Assume you’re playing with three players … player 1, 2 and 3; also known in the Array of players as 0, 1 and 2 (whoever made arrays like that should be shot, btw, just saying as a non-programmer). The goal of the game is to move your player into slot 10 (or higher). These terrainSlots are also = victory points, and you can move past 10 so player 2 could have 11, but player 3 or 4 on their turn could move past them to win, so the winner might have 13.
Script Works Correctly: If any single player gets to 10 or higher, game ends perfectly.
Example: If player 2 gets 12 points, but player 3 manages to roar past on his last turn and score 14, the game ends perfectly with player 3 declared the winner … that is, case 1 in the function declareWinnerEndGame () function. In other words, the Debug.Log (WhoIsWinning()); output in this case shows 2.
Example 2: If player 1 gets 10 points, and both players 2 and 3 fail to get to 10 (or more) on their final turns, the game ends perfectly with player 1 declared the winner … that is, case 1 in the function declareWinnerEndGame () function. In other words, the Debug.Log (WhoIsWinning()); output in this case shows 0.
Script Works Correctly: If players 2 and 3 both TIE and score 12, and player 1 doesn’t get to 10 or only gets to 11, the game ends perfectly with a tie declared between 2 and 3 … that is, case 2 in the function declareWinnerEndGame () function. In other words, the Debug.Log (WhoIsWinning()); output in this case shows 1,2.
PLAYER 1 TIE BUG: If player 1 is involved in a tie, either with player 2 or 3, or with both 2 and 3, player 1 is declared the single winner; case 1 in the function declareWinnerEndGame () function. In other words, the Debug.Log (WhoIsWinning()); output in this case shows only 0.
This is the bug and it’s causing me to tear the little hair I actually have remaining completely out.
So, here’s the two functions in the script that pushes the players into an Array and compares their scores to declare a winner.
Challenge? Please tell me what I am doing wrong and let me know (please!). Thank you!
3-2-1 … go!
function WhoIsWinning() : Array {
// first check to see how many players reached the objective
var numPlayersReachedObjective = 0;
var whichPlayerReachedObjective = -1;
for (var i = 0; i < GameController.instance.numberOfPlayers; ++i) {
if (playersTerrainSlot[i] > 9) {
++numPlayersReachedObjective;
whichPlayerReachedObjective = i;
}
}
if (numPlayersReachedObjective == 1) {
var result = new Array();
result.Push(whichPlayerReachedObjective);
return result;
}
else {
// cycle through each player and find the player with the highest score
var winners : Array = new Array();
var highest = -1;
for(var j = 0; j < GameController.instance.numberOfPlayers; ++j) {
if (currentScore[j] > highest) {
winners.Clear();
winners.Push(j);
highest = currentScore[j];
}
else if (currentScore[j] == highest) {
winners.Push(j);
}
}
return winners;
}
}
// ----- declare winner after allowing each *remaining* player in sequence one final turn to also try to reach objective
function declareWinnerEndGame () {
// AUGUST 11 TO TRY TO FIGURE OUT END GAME BUG
Debug.Log ("## Printing the winners array to see if there is an error here, now in ---> function declareWinnerEndGame()");
Debug.Log ("## Trying to see the result in the Array ---> function WhoIsWinning()");
Debug.Log (WhoIsWinning());
// grab the winner(s) from WhoIsWinning()
var theWinnerTemp = WhoIsWinning();
var theWinner : int[] = theWinnerTemp.ToBuiltin(int);
messageBackground.active = true;
switch (theWinner.length){
case 1:
gameplayMessage.text = "Player " + (theWinner[0] + 1) + " has reached the objective\nand won this battle with\nthe most Victory Points!";
break;
case 2:
gameplayMessage.text = "The battle has ended in a tie\nbetween Player " + (theWinner[0] + 1) + " and Player " + (theWinner[1] + 1) + ".";
break;
case 3:
gameplayMessage.text = "The battle has ended in a tie\nbetween Player " + (theWinner[0] + 1) + ", Player " + (theWinner[1] + 1) + ",\nand Player " + (theWinner[2] + 1) + ".";
break;
default :
gameplayMessage.text = "The battle has ended in a four way tie!\nHeadquarters is proud of your\nextraordinarily valiant efforts, soldiers!";
}
yield WaitForSeconds (5);
gameplayMessage.text = "";
messageBackground.active = false;
Application.LoadLevel ("missions");
}
The complete Gameplay.js if anyone needs more than this, is found here. OK, It is messy (I still haven’t removed comments I don’t need etc) and a little brute force, but all there. And yes, I now realize I should have made each player an object with attributes to easier track score, position, graphics etc (function Player (icon,score) {}) but I’m not a real programmer and people (Jedd, Rob D, Rob H, and Tim) helped and contributed as they could given my crude explanations.
I will make sure to credit, in game, anyone who can offer a solution to this little bug of mine.
Many many thanks.