for(new i = 0; i < MAX_PLAYERS; i++) { if(LottoNumber[i] == LottoNum) { // Award player } else if(LottoNumber[i] != 0 && LottoNumber[i] != LottoNum) SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!"); else return 0; }
wrongNumber++; // check if the wrong number is equal to the number of participants in the lottery // if so, choose another number and repeat
for (new i = 0; i != MAX_PLAYERS; i ++) { if (LottoNumber[i] != LottoNum) { SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!"); continue; } // award player here break; // break here if you only want 1 lottery winner }
Code:
for (new i = 0; i != MAX_PLAYERS; i ++) { if (LottoNumber[i] != LottoNum) { SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!"); continue; } // award player here break; // break here if you only want 1 lottery winner } |
You didn't describe how the random number is being picked. Keep the number range small if you want players to guess it every time. Otherwise, pick a random one from all the numbers given by all participants. If you do it that way there will be always a chance to win for everyone.
Using foreach iterators may ease the job. |
new TotalWinners = 0;
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
{
if(!IsPlayerConnected(i)) continue; //make loop faster.
if(LottoNumber[i] != 0 && LottoNumber[i] != LottoNum) //check if they got a wrong number
{
SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!");
}
else
{
TotalWinners++; //so we got a one more winner
//Award
}
}
if(TotalWinners == 0) SendClientMessageToAll(-1, "nobody won");
Well, first, use GetPlayerPoolSize instead of MAX_PLAYERS, no need to loop through whole 1000 player if they're not connected.
Second, you're making it correct, but return is extra, you don't need that. remove return from code. so correct code looks like this PHP Code:
|
while(TotalWinners < 1) { LottoNum = 1 + random(59); for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++) { if(!IsPlayerConnected(i)) continue; if(LottoNumber[i] == LottoNum) { // Get the player's name GetPlayerName(i, Name, sizeof(Name)); format(Msg, sizeof(Msg), "{FFFF00}LOTTO: Congratulations, you have won $%i in the lottery!", LottoPrize); SendClientMessage(i, COLOR_WHITE, Msg); format(Msg, sizeof(Msg), "{FFFF00}LOTTO: %s has won $%i in the lottery. Congratulations!", Name, LottoPrize); SendClientMessageToAll(COLOR_WHITE, Msg); GivePlayerMoney(i, LottoPrize); TotalWinners++; break; } else if(LottoNumber[i] != 0 && LottoNumber[i] != LottoNum && TotalWinners > 0) { SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!"); } else return 0; } if(TotalWinners > 0) { for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++) { LottoNumber[i] = 0; LottoNum = 0; LottoPrize = 0; break; } }
wait, you want a winner even if they don't have a correct LottoNumber?
|
LottoNum = 1 + random(59);
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
{
if(!IsPlayerConnected(i)) continue;
if(LottoNumber[i] == LottoNum)
{
// Get the player's name
GetPlayerName(i, Name, sizeof(Name));
format(Msg, sizeof(Msg), "{FFFF00}LOTTO: Congratulations, you have won $%i in the lottery!", LottoPrize);
SendClientMessage(i, COLOR_WHITE, Msg);
format(Msg, sizeof(Msg), "{FFFF00}LOTTO: %s has won $%i in the lottery. Congratulations!", Name, LottoPrize);
SendClientMessageToAll(COLOR_WHITE, Msg);
GivePlayerMoney(i, LottoPrize);
TotalWinners++;
LottoNumber[i] = 0;
//break; NO NEED, WILL CAUSE ISSUES
}
else if(LottoNumber[i] != 0 && LottoNumber[i] != LottoNum && TotalWinners > 0)
{
SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!");
}
//else return 0; NO NEED, THIS WILL STOP WHOLE FUNCTION
LottoNumber[i] = 0;
}
LottoNum = 0;
if(TotalWinners == 0) //So if there is not a winner, start a second lottery.
{
new winnerID = random(GetPlayerPoolSize());
while(IsPlayerConnected(winnerID))
{
winnerID = random(GetPlayerPoolSize());
}
GetPlayerName(winnerID, Name, sizeof(Name));
format(Msg, sizeof(Msg), "{FFFF00}LOTTO: Congratulations, you have won $%i in the lottery!", LottoPrize);
SendClientMessage(winnerID, COLOR_WHITE, Msg);
format(Msg, sizeof(Msg), "{FFFF00}LOTTO: %s has won $%i in the lottery. Congratulations!", Name, LottoPrize);
SendClientMessageToAll(COLOR_WHITE, Msg);
GivePlayerMoney(winnerID, LottoPrize);
}
LottoPrize = 0;