Decide different outcomes of team position by score - ios

I don't know if this is the right place to ask such a question but I hope so! I'm developing a game for fun where you have different teams who get points for each kick they can do in volley with a football. When the game is over I'm counting the score of each team and sort them by the best score.
So say:
team 1 = 9 points
team 2 = 7 points
team 3 = 20 points
Then I've sorted it by the best one, so the positions are:
team 3 = 20 points
team 1 = 9 points
team 2 = 7 points
Then I print it on the screen and say like "Team 3 comes on first place with 20 points". "Team 1...second", "team 2 .....third".
The problem is when some of the teams get the same score. So say it ends up like this:
team 1 = 9 points
team 2 = 9 points
team 3 = 20 points
In such case I want it to print like: "Team 3 comes on first place with 20 points", "Team 1 and Team 2 comes on second place with 9 points each".
Here is the problem, I've been thinking and thinking but I can't come up with a smart solution of how to make the code consider a shared first/second/third place.
Here is my current code that doesn't take account of that:
for var i = 0; i < teamScoreArray.count; ++i { //sorted array by best score first
var tmpTeam:Team = teamScoreArray[i]
if i == 0{
firstPlacelbl.text = "On first place comes \(tmpTeam.teamName) with \(tmpTeam.teamScore) points"
}
if i == 1{
secondPlacelbl.text = "On second place comes \(tmpTeam.teamName) with \(tmpTeam.teamScore) points"
}
if i == 2{
thirdPlacelbl.text = "On third place comes \(tmpTeam.teamName) with \(tmpTeam.teamScore) points"
}
if i == 3{
fourthPlacelbl.text = "On fourth place comes \(tmpTeam.teamName) with \(tmpTeam.teamScore) points"
}
prevScore = tmpTeam.teamScore
}
Does anyone have any idea of how to make this?
Thank you in advance!

Increase the rank only when the score in the current iteration differs from the previous one:
var rank = 0
var previousScore = 0
for team in teamScoreArray {
if team.teamScore != previousScore {
rank += 1
}
println("Rank \(rank): \(team.teamName)")
previousScore = team.teamScore
}

Related

Why my OrderSend function returns a price higher than the candle high price?

I'm trying to write an Ea with 2 moving averages, and I want it to send an order when they cross.
Sell orders seem fine, but for Buy orders in 1-min chart, the Ask price which I use to send the order is not in the candle price range. Actually I did Print("Ask=",Ask) right before my OrderEntry function and right after OrderSend(). They are equal to some price way above the candle-[High] and previous candle-[close]
But when I try higher time frames, it get closer to the candle and in daily time frame, the Ask price for orders would be in the candle range (High-Low). Does anybody know why this is happening?
void CheckForMaTrade(){
double PreviousFast = iMA(NULL,0,FastMaPeriod,FastMaShift,FastMaMethod,FastMaAppliedTo,2);
double CurrentFast = iMA(NULL,0,FastMaPeriod,FastMaShift,FastMaMethod,FastMaAppliedTo,1);
double PreviousSlow = iMA(NULL,0,SlowMaPeriod,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,2);
double CurrentSlow = iMA(NULL,0,SlowMaPeriod,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,1);
if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow)
OrderEntry(0);
if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow)
OrderEntry(1);
}
void OrderEntry(int direction){
if(direction == 0){
double bsl=0;
double btp=0;
if(StopLoss != 0)
bsl = Ask-(StopLoss*pips);
if(TakeProfit != 0)
btp = Ask+(TakeProfit*pips);
if(OpenOrdersThisPair(Symbol()) < 10 )
OrderSend(NULL,OP_BUY,LotSize,Ask,0,bsl,btp,NULL,MagicNumber,0,Gold);
}
enter image description here
In MT4, the candles are drawn by BID prices only. ASK prices are not taken into consideration. So it is possible that Ask > highest bid.

Polling a database with items of different priority

Given a list of items that have priorities ranging between 1 and 4 where one is the most important and 4 is the least important, how can I create a system that polls a central database for updates on the list of items, but polling the higher priority items more frequently than the lower priority items?
for example:
I have items a, b, and c that have priority values 1, 2, and three respectively.
In a perfectly functioning program, item a should be polling for updates more often than b and c, and b should be polling more frequently than c.
My main issue is designing a system in which all items are still polled regularly but the highest priority are polled more frequently.
Sorry I don't have more details but I don't really have any source code or anything to build off of, I'm kinda at a blank slate right now.
Also I'm working Lua, but I'm just looking for a rough pseudo-code version of ideas, I'd like to work out the exact implementation my self. For simplicity sake, polling the database can be represented by a method call: getDatabaseInfo(itemIndex);
All answers appreciated, thank you for your time!
local items = { -- all your items are here
{name = "item #1", update_period = 1 },
{name = "item #2", update_period = 1.5}, -- updates for item#2 will happen 1.5 times less frequent than updates for item#1
{name = "item #3", update_period = 2 }, -- updates for item#3 will happen 2 times less frequent than updates for item#1
{name = "item #4", update_period = 3 }, -- updates for item#4 will happen 3 times less frequent than updates for item#1
}
local function update_one_item()
local min_ctr, itemIndex = math.huge
for idx, item in ipairs(items) do
item.current_counter = item.current_counter or 0
if item.current_counter < min_ctr then
min_ctr = item.current_counter
itemIndex = idx
end
end
for idx, item in ipairs(items) do
item.current_counter = item.current_counter - min_ctr
end
items[itemIndex].current_counter = items[itemIndex].current_counter + items[itemIndex].update_period
getDatabaseInfo(itemIndex);
print(items[itemIndex].name.." updated")
end
for _ = 1, 20 do
update_one_item()
end
Output:
item #1 updated
item #2 updated
item #3 updated
item #4 updated
item #1 updated
item #2 updated
item #1 updated
item #3 updated
item #1 updated
item #2 updated
item #4 updated
item #1 updated
item #3 updated
item #2 updated
item #1 updated
item #1 updated
item #2 updated
item #3 updated
item #4 updated
item #1 updated
Depending on how often you wish for each value to be updated, a loop like this could work.
Things with primary priority are updated every time
Things with secondary priority are updated every 2 cycles
Things with tertiary priority are updated every 3 cycles
Things with quaternary priority are updated every 4 cycles
Code:
for i = 1,math.huge do
for ii = 1,#items do do
if getPriority(ii) == 1 then
items[ii] = getDatabaseInfo(ii)
elseif i%2 == 0 and getPriority(ii) == 2 then
items[ii] = getDatabaseInfo(ii)
elseif i%3 == 0 and getPriority(ii) == 3 then
items[ii] = getDatabaseInfo(ii)
elseif i%4 == 0 and GetPriority(ii) == 4 then
items[ii] = getDatabaseInfo(ii)
end
end
sleep(interval)
end
The way this works is it does a check for if the current iteration number is divisible by 1,2,3, or 4.
One could add a 3rd nested loop which loops over all priorities, so you can have limitless amounts of priorities, but I'll leave that as an exercise to the reader :)
As noted by Piglet in the comments, you can use this shortcut to make the code more efficient:
if i % getPriority(ii) == 0 then ...

Speed up game after every 50 points

I am making a 2D reaction game with sprite-kit and I am having the same problem again. I already asked this question once and had good answers and everything worked finde but now I am stuck again.
Like the title says I want to speed up my game every 50 points, but it just speeds up when I get the right point number like 50, 100, 150.. The problem is that I have combo points and its always some points more. For example from 48 to 51 points, so it never speeds up. How can I speed up the game even its some points more ? I mean from 50 to 100 one speed up and from 100 to 150 and so on. Here is my code so far:
if (points % 10 == 0) {
if (readyToSpeed) {
speed++;
NSLog(#"speed up");
readyToSpeed = NO;
}
}
Thanks for the help ! (code in objective-c please)
EDIT: Works perfectly using both answers combined.
Instead of incrementing your speed you could do it like so:
speed = baseSpeed + (points / 50);
Where baseSpeed is the speed at the start of the game.
Don't worry about the exact 50x multiple points, keep track of the current point "group" via modulo to derive your speed value
cur_speed = points - (points % 50);
e.g. if they're at 203 points, then cur_speed is 203-(203%50) -> 203-3 -> 200. If they suddenly jump to 308 pts because they hit an insane points combo, then the speed becomes 308-(308%50) -> 308-8 -> 300
if you want just between 50-100 and 100-150 do something like this:
If points > 50 && points < 100 {
//speedup code
}else if points > 100 && points < 150 {
//speedup code
} //etc
Update: if you want this to be endless do this:
var speednumber = points/50
speednumber = speednumber-decimal //some code to cut of the decimals
setSpeedTo(speednumber+1) //you got to make a function for this
setSpeedTo(1) will be normal
2 will be from 50-100
3 100-150
Etc.

How to add 1 to the score each time an object's y coordinate is equal to another object's y coordinate

i have a game where the user must move the ball in a vertical way between 2 objects , i want to have a scoring system that allows me to add 1 to the score each time the ball is equal to the 2 objects so i tried the code below but it is not working probably,it is not adding one each time the ball's y coordinates are equal to the object's y cooridnates.
if(imageview.center.y == imageview1.center.y){
int score = score + 1;
scorelabel.text = [NSString stringWithFormat:#"%i",score];
}
As I assume your imageview.centre.y will be different then imageView1.centre.y .Also you could use this method CGRectIntersection(<#CGRect r1#>, <#CGRect r2#>) and place your both frame in provided fields. So it will notify whenever there is an intersection of two frames. Code will be like -
if (CGRectIntersectsRect(self.pacman.frame, self.exit.frame))
{
//Wirte your score handling code here...
}
else
{
//Skip or do what you want...
}
Hope this might help you.
Your variable is recreated every time your if is performing.
Define it somewhere before #implementation
int score = 0;
And replace int score = score + 1; with score++
As comment you should be aware of comparison of double. See This Question for details

Action Script 2, I want to stop the counting down to 0

I need help with Action Script 2.0
I have this scoreboard system with 2 buttons one to count up to a certain number and the other to count down if i need to.
However, the problem with the count down button.
Count Down button code:
on(release) {
score.text = 0;
score.text = scorebk
_root.scorebk--;
if the score is 9 for example when i count down it adds up 1 and then it counts down normally, also when score is 0 it goes to -1...etc.
I do not want that. how can i stop this button from going to minus? and counts normally.
You just have to check that the value is greater than zero before you decrement:
on(release) {
if(scorebk > 0){
scorebk--;
}
score.text = scorebk;
}

Resources