How to write this snippet more clean [closed] - iphone

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In a turn-based game, I'd like to find the previous player.
To find the next player, I can just type:
int lastPlayer = match.currentPlayer - 1;
The problem is when the currentPlayer is player 1. Then lastPlayer becomes 0, which is wrong. It should be player6.
To fix this, I can do:
int lastPlayer = match.currentPlayer - 1;
if (lastPlayer == 0)
lastPlayer = match.numberOfPlayers;
My question is how to write this in a cleaner way. I know game center, turn based code do something like:
(currentIndex + 1) % match.participants.count];
How can I rewrite my code to do the same?
Thanks in advance

How about that?
int lastPlayer = match.currentPlayer > 1 ? match.currentPlayer - 1 : match.numberOfPlayers;

If you have a turn counter like this:
for(int i=0;i< maxTurns;i++){//do something}
and player1 plays the 1st turn, player2 the second and so on,
you can get the current players number exactly as its done in gamecenter.
Maybe you don't know the modulo "%" operator,
it returns the rest of a division, for example
3%2 = 1
your turncounter keeps counting up so the next player is
(turncounter + 1)%match.numberOfPlayer
in this case the modulo operator kind of "resets" your counter when it reaches the max player count (its round 6 and there are 5 players 6%5 = 1).
To sum it up, currentPlayer is turncounter%match.numberOfPlayer
nextPlayer is ++turncounter%match.numberOfPlayer

Related

Print repetition in Swift while statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In my while statement, I cannot understand why my output is printed twice ?
I would like to print i only one time, where is my error ?
func fetch2(){
var i: Int = 0
while i <= (self.returned-1) {
let itemLookUp = "https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemLookup?appid=\(self.appId)&itemcode=\(self.arrayCodeProduct[i])&responsegroup=large"
print(i)
i = i+1
}
}
Here is the output that I obtain :
0
1
2
3
0
1
2
3
Thank you in advance.
It looks like fetch2() is called twice.
Add a print(#function) before you var i and check that fetch2() is not called several times.

How to while loops work in Scala? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Would you please explain this code step by step, as I don't understand why there is a inner = 1 in the while loop. A general overview of this program and how it works would be deeply appreciated as I am currently learning loops in Scala!
import scala.io.StdIn._
object loops4 {
def main(args: Array[String]): Unit = {
var outer = 1;
var inner = 1;
print("How many units for the base of the triangle? ");
var base: Int = readInt();
while (outer <= base) {
inner = 1
while (inner <= outer) {
print ('*');
inner += 1;
}
println("\n");
outer += 1;
}
}
}
Don't "learn loops in scala", it's a waste of time. Learn scala.
This does the same thing (except for redundant empty lines between stars) as your snippet, except, I bet you don't have to ask people on internet how it works :)
(1 to base).foreach { n => println("*" * n) }
It might be helpful to debug the code as Carcigenicate suggested. Inner is set to 1 in the outer loop because it's changed to match the outer loop on the previous iteration of the outer loop. Debug and observe inner at the end of the outer loop. As far as what this code is doing, it's constructing a triangle in standard output and the outer loop is counting lines, and the inner loop is constructing the lines with '*' symbols. As Dima said there are more idiomatic ways to do this in Scala.

Modify variable in for loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
The code below would print abc 5 times and then print 1024. As far as I understand, in any for, the "iterator " is automatically declared (the equivalent of a C(++)/Java for(int i=1; i<=5; i++) ). Is it possible to actually not automatically create that variable and use the one declared before the for so that it would print abc 5 times and then print 5, thus modifying it?
var i = 1024
for i in 1...5 {
print("abc")
}
print(i)
#DrummerB's answer works, but if you want a for...in loop, this will also work. It's the same principle - declare your variable outside the loop and increment it inside it:
var i:Int = 0
for _ in 0...5 {
print("abc")
i += 1
}
print(i)
Since you aren't referencing a loop variable, Swift syntax recommends an underscore.
You could just rewrite the for loop as a while loop like this:
var i = 1024
i = 1
while i <= 5 {
print("abc")
i = i+1
}
print(i)
If you really want to change the value of the i using the loop in that way, you can do:
var i=1024
for j in 1...5 {
print("abc")
i = j
}
print(i)
The j would be used strictly in the loop, so once finished, it's value is dumped. But a variable declared before (i in your case), could take it's value and maintain it.

What are my errors in the following code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is the function:
for (i = 0; i <= array.Length; i++) {
if (array[i].transform.position = 0)
array.RemoveAt(i);
print(“Removed element: “ + array[i].name);
else if (array[i].transform.position > 0)
array[i].transform.forward = Vector3(1,0,0);
}
I'm not sure if this is a valid question but there is for sure some logic errors :
First of all i'm not sure there is a RemoveAt(int index) for arrays
(but i'm not a big unityscript user) (there is for List though)
You should absolutely never (even if some weird languages maybe allow it to you) try to access an object you just deleted... which is what you try to do here :
array.RemoveAt(i);
print(“Removed element: “ + array[i].name);
Position is a Vector3 NOT a int or float so you cannot do : array[i].transform.position = 0
You should never use the = (assignment operator) in an if() you should use the == (comparison operator) (because = returns always true when assignment is possible)
That line is wrong for the same reason as before array[i].transform.position > 0
array[i].transform.forward = Vector3(1,0,0); Leaves me wondering because if it was C# i'd try the new keyword before Vector3() and i'd prefer floats that way :
array[i].transform.forward = new Vector3(1.0F,0,0); But even there Unity will throw you an error stating that you cannot modify components of Transform without making a copy first i believe...
But nice try :D
you can't compare a vector to 0 it needs to be like this
if(myObject.transform.position==Vector3.zero)
and for removing things I'd suggest to include System.Collections.generic lib
then use Listarray then you can use array.RemoveAt(index);

How to express the performance of loop & inner loop in big o notation? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
For below loop and inner loop to express the performance in big o notation is :
O(N squared) as its performance is proportional to the square of the size of the input data set.
var counter = 0
var counterval = 0;
for ((key, value) <- m2.par){
for ((key2, value2) <- m2.par){
counter = counter + 1;
println(counter)
}
println(counterval)
}
Is this correct ?
Yes, if you consider the size of m2 to be the input size and that increasing counter and printing it are both O(1) (which is a very reasonable assumption).