If I have an array with 14 elements, Is there a way to loop through the array so that when I get to array[13], it automatically goes back to array[0] up to array [13] and loop back to array[0] etc etc?
I was thinking modulo, but I am not sure where I would place that. Then I was thinking maybe I can make the array 15 elements, and when it reaches [15], set that array[15] = array[0] and somehow get it to loop again.
This array will be in a ‘for loop’ where I will be using array[i] and saying array[i] ++; I want it to continuously add +1 until I reach the “max”, or whatever i < max.
I am very new to processing, any help would be greatly appreciated! I don’t want to paste my code as it is not written well and is quite lengthy!
right, that might even stop readers from helping,
but what you should do is to make a extra small program
( complete / run-able for the problem only )
where we can discuss your question with.
Okay, sorry, I put together a really simple code. So in my code, int startingPoint will change depending on some other things so I want to be sure that when startingPoint is greater than 9, that it will loop the array back to [0] and add 1 to that. Right now, my program just crashes when I have a starting number of 10 or greater.
Here is the code:
int[] myArray = new int [14];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index
void initialNumbers()
{
for (int i = 0; i < myArray.length; i ++)
{
myArray[i] = startingAmount; //sets all to starting amount
}
}
void addNumber()
{
for (int i = 0; i < startingAmount+1; i ++)
{
myArray[startingPoint+i] ++;
}
myArray[startingPoint] = 0;
}
void setup()
{
initialNumbers();
addNumber();
}
yes, array what is long 14
has a possible index from 0 … 13
so a
for (int i = 0; i < startingAmount; i++) {
works, still the result is questionable
WHY YOU NOT PRINT ANYTHING
int[] myArray = new int [14];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index
void initialNumbers() {
for (int i = 0; i < myArray.length; i ++) myArray[i] = startingAmount; //sets all to starting amount
}
void addNumber() {
// for (int i = 0; i < startingAmount+1; i++) {
for (int i = 0; i < startingAmount; i++) {
println("add "+i);
myArray[startingPoint+i]++;
}
myArray[startingPoint] = 0;
}
void setup() {
initialNumbers();
addNumber();
println(myArray);
}
int along = 14;
int[] myArray = new int [along];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index
boolean diagp = true;
void initialNumbers() {
for (int i = 0; i < myArray.length; i ++) myArray[i] = startingAmount; //sets all to starting amount
}
void addNumber() {
for (int i = 0; i < startingAmount+1; i++) {
int j = startingPoint+i;
if ( j >= myArray.length ) j -= myArray.length;
myArray[j]++;
if ( diagp ) println("add i: "+i+ " j: "+j+ " myArray["+j+"]="+myArray[j]);
}
myArray[startingPoint] = 0;
}
void setup() {
initialNumbers();
addNumber();
if ( diagp ) for ( int i = 0; i < myArray.length; i++ ) print(myArray[i]+" , ");
}
Thank you so much! I think I understand now. It works great when I call that function in setup, but is there a way to get this function to work in draw so I can change the startingAmount value based on other functions? When I move the function into draw, no matter what my startingAmount is, I get an error saying out of bounds
I am playing around with @kll’s above code, except I moved “addNumber();” to void draw();. I guess because draw is continuously running, it keeps adding and adding, but I thought the for loop conditions would stop this!
int along = 14;
int[] myArray = new int [along];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index
boolean diagp = true;
// -----------------------------------------------------------------------------------
void setup() {
size(300, 300);
initialNumbers();
}
void draw() {
addNumber();
if ( diagp ) {
for ( int i = 0; i < myArray.length; i++ )
print(myArray[i]+" , ");
}//if
}
// -----------------------------------------------------------------------------------
void initialNumbers() {
//sets all to starting amount
for (int i = 0; i < myArray.length; i ++)
myArray[i] = startingAmount;
}
void addNumber() {
//
for (int i = 0; i < startingAmount+1; i++) {
int j = startingPoint+i;
if ( j >= myArray.length )
j -= myArray.length;
myArray[j]++;
if ( diagp )
println("add i: "
+i
+ " j: "
+j
+ " myArray["+j+"]="
+myArray[j]);
}//for
myArray[startingPoint] = 0;
}
//
I have never played the game but it suddenly dawned on me that the algorithm seemed familiar.
A quick google showed that although you start with the same game configuration there are several ways or sets of rules to play the game. One of them prevents you placing stones in your opponents home cup but the current algorithm does not allow for that.
Hi
Could you just enclose your for loop in an outer for loop or other looping construct? Initialise your inner loop variable to a start value computed inthe outer loop.
I’d write a very small class that does the looping interation. It only offers a constructor, taking the length of the loop, a method “iterate” and a method int value() to get the current index it is pointing on.
Normally I would say there isn’t much more to it than using modulo – so no need, your “class” is int idx, its “next” method is idx=(idx+1)%arr.length, and that’s it.
However, Java’s % is actually the remainder, which causes a gotcha on negatives, so IF you ever wanted to step negative numbers then it might be useful to wrap a loop counter in a method or class.
/**
* LoopCounter
* https://discourse.processing.org/t/looping-through-an-array/13078/19
**/
void setup() {
LoopCount lc = new LoopCount(4);
println(lc.val);
println(lc.inc(1), lc.inc(1), lc.inc(1), lc.inc(1));
println(lc.inc(2), lc.inc(2));
println(lc.inc(7));
println(lc.inc());
println(lc.dec(), lc.dec(), lc.dec(), lc.dec());
println(lc.dec(7));
}
class LoopCount {
int max;
int val;
LoopCount(int max) {
this.max = max;
}
LoopCount(int max, int val) {
this.max = max;
this.val = val;
}
int dec() {
return inc(-1);
}
int dec(int count) {
return inc(-count);
}
int inc() {
return inc(1);
}
int inc(int count) {
this.val = ((val+count)%max + max)%max;
return this.val;
}
}