% operator seems not working in google apps script - google-apps-script

I want to count the number of 3 consecutive wins of team A and B. It seems to be always getting into the final ifs. Not when consecutivas equals 3,6,9...
var consecutivasA = 0;
var consecutivasB = 0;
var plusA = 0;
var plusB = 0;
for (var i = 2; i <= 30; i++) {
var row = values[i];
if (row[3] > row[4]) {
consecutivasA++;
consecutivasB = 0;
}
else if (row[3] < row[4]) {
consecutivasB++;
consecutivasA = 0;
}
else {
consecutivasA = 0;
consecutivasB = 0;
}
if (consecutivasB % 3 == 0) { plusB++; }
if (consecutivasA % 3 == 0) { plusA++; }

The way you do it you also increment when consecutivas is 0, because 0 % 3 is also 0. This example writes the outcome of the example data to the log.
function consecutivasMod () {
var consecutivasA = 0;
var consecutivasB = 0;
var plusA = 0;
var plusB = 0;
values=[
[0,0,0,1,0], [0,0,0,1,0], [0,0,0,1,0], [0,0,0,0,1], [0,0,0,0,1],
[0,0,0,0,1], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,2],
[0,0,0,0,3], [0,0,0,0,4], [0,0,0,0,5], [0,0,0,0,6], [0,0,0,0,7],
]
for (var i = 0; i < 15; i++) {
var row = values[i];
if (row[3] > row[4]) {
consecutivasA++;
consecutivasB = 0;
}
else if (row[3] < row[4]) {
consecutivasB++;
consecutivasA = 0;
}
else {
consecutivasA = 0;
consecutivasB = 0;
}
if (consecutivasB>0&&(consecutivasB % 3) == 0) { plusB++; }
if (consecutivasA>0&&(consecutivasA % 3) == 0) { plusA++; }
}
Logger.log ("consecutivasA "+plusA);
Logger.log ("consecutivasB "+plusB);
}

Log the values of consecutivasA and B right before the final ifs... I suspect strongly that your values are always zero due to an earlier logic error.

I would go about doing this another way--instead of using the logic you provide, which I think just checks if the total number of wins by Team A or Team B are divisible by 3 (which does not necessarily say anything about whether the wins are consecutive), I would try the following:
Run through your array and write an if statement checking if Team A won in a particular place. If yes, check if it won in n+1 and n+2 as well. If so, increment plusA.
Run through your array again and write an if statement checking if Team B won in a particular place. If yes, check if it won in n+1 and n+2 as well. If so, increment plusB.
NOTE: Check only until two entries before the final entry in your array, as going beyond that would return an array size error.

Related

AS3 hittest keeps hitting

I have the following problem:
I want to keep a score when i "hittest". I use the following code:
private function fnMoveMap():void
{
for (var i:int = 0; i < vPipeMax; i++)
{
var tmpPipe = _conMap.getChildAt(i);
//trace (tmpPipe.name);
if (tmpPipe._HIT.hitTestPoint(_P.x, _P.y, true))
{
tmpPipe.visible = false;
//stage.removeEventListener(Event.ENTER_FRAME, setScore);
vScores++;
txtScores.text = vScores.toString();
//break;
}
//reset pos
if (tmpPipe.x < 0)
{
//stage.addEventListener(Event.ENTER_FRAME, setScore);
tmpPipe.visible = true;
tmpPipe.x = 1050 - vXSpeed;
tmpPipe.y = randomRangeMC(minPipeY, maxPipeY);
//set score
//vScores++;
//txtScores.text = vScores.toString();
}
else
{
tmpPipe.x -= vXSpeed;
}
}
}
the var vScores keeps counts for 4 to 8 times.
How can i just count one?
The reason your vScores variable is incrementing by 4-8 is because you're looping multiple times with the for loop through vPipeMax.
You either need to restructure your code so that doesn't happen, or break out of the loop as soon as you increment the score.
Adobe's documentation on break: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#break

Issue with for loop in an Array

Initially the array holds the default values as 100. Once if the enemy looses its health then i want to set the default values from 100 to 0. When all the array elements gets a value of 0 then the message will trace as game win.
var enemyHealth:Array = new Array(100,100,100,100,0);
for (var i = 0; i< enemyHealth.length; i++)
{
if (enemyHealth[i] == 0)
{
trace ("game win");
}
}
Actually the issue is if any one of the array element holds a value of 0 then it trace a massage as game win.
So can any one help me in this regard.
You need to check all elements, not just one:
var allZero:Boolean = true;
var enemyHealth:Array = new Array(100,100,100,100,0);
for (var i = 0; i< enemyHealth.length; i++)
{
if (enemyHealth[i] != 0)
{
allZero = false;
break; // We know it's not a win, so we can stop the loop early
}
}
if (allZero) {
trace ("game win");
}
I would do something like this
var enemyHealth:Array = new Array(100,100,100,100,0);
var isDead:Boolean = true;
for (var i = 0; i< enemyHealth.length; i++)
{
if (enemyHealth[i] != 0)
{
isDead = false;
break;
}
}
if(isDead)
{
trace ("Game Win");
}
You can do what the other answerers have said or something like this which might help you more to get the exact amount of enemies dead.
var enemyHealth:Array = new Array(100,100,100,100,0);
var enemiesDead:int=0;
for (var i = 0; i< enemyHealth.length; i++)
{
if (enemyHealth[i] == 0)
{
enemiesDead++;
}
}
if(enemiesDead==enemyHealth.length)
{
trace("Game Over");
}
You can use the every method to check that every element of your array (or vector) meet a criterion.
const enemyHealth:Vector.<uint> = new <uint>[100,100,100,100,0];
const zeroHealth:Function = function(item:uint, index:int, vector:Vector.<uint>):Boolean {
return item === 0;
}
if (enemyHealth.every(zeroHealth)) {
trace("Game win")
}
I have changed the array to a Vector because they are more efficient, and you can specify the type of the elements, but it also works fine with array.

Adding indexes in array

I am adding two matrices (or possibly many) in ActionScript 3.0.
Now my problem is how can I add indexes in array that is something like this?
array1[1,2,3,4] + array2[2,4,5,6] = answer[3,6,8,10]
This function adds up all the arrays that are passed to it:
function sumOfArrays(...args):Array
{
var sum:Array = [];
var arrays:Array = [];
var longestArrayLength:uint = 0;
for (var i:int = 0, n:int = args.length; i < n; i++)
{
if (args[i] is Array)
{
arrays.push(args[i]);
longestArrayLength = args[i].length > longestArrayLength ? args[i].length : longestArrayLength;
}
}
for (var j:int = 0; j < longestArrayLength; j++)
{
sum[j] = 0;
for (i = 0; i < n; i++)
{
sum[j] += isNaN(arrays[i][j]) ? 0 : arrays[i][j];
}
}
return sum;
}
It can be used like this:
var sum:Array = sumOfArrays(array1, array2);
That's not possible. Arrays only allows access via one index. You'd have to write a method on your own for this. But be aware of the fact, that null would be referenced on answer at, 0, 1, 2, 4, 5 and so on.

Conditionally change the direction of a "for loop"?

Is it possible to conditionally change the direction of a for loop in ActionScript?
Example:
for(if(condition){var x = 0; x<number; x++}else{var x=number; x>0; x--}){
//do something
}
Interesting requirement. One way to keep the for is:
var start, loop_cond, inc;
if(condition)
{
start = 0;
inc = 1;
loop_cond = function(){return x < number};
}
else
{
start = number - 1;
inc = -1;
loop_cond = function(){return x >= 0};
}
for(var x = start; loop_cond(); x += inc)
{
// do something
}
We setup the start value, a function for the termination condition, and either a positive or negative increment. Then, we just call the function and use += to the do the increment or decrement.
ActionScript has the ternary operator, so you could do something like:
for (var x = cond ? 0 : number; cond ? x < number : x > 0; cond ? x++ : x--) {
}
But this is pretty ugly. :-)
You might also need/want to put some parens around pieces of that. I'm not sure about the operator precedence.
You might also consider using a higher order function. Imagine you have:
function forward (count, func) {
for (var x = 0; x < count; x++) {
func(x);
}
}
function backward (count, func) {
for (var x = count - 1; x >= 0; x--) {
func(x);
}
}
Then you could do:
(condition ? forward : backward) (number, function (x) {
// Your loop code goes here
})
You probably want a while loop instead:
var continueLooping, x;
if(condition)
{
x = 0
continueLooping = (x < number);
}
else
{
x = number;
continueLooping = (x > 0);
}
while (continueLooping)
{
// do something
if(condition)
{
x++;
continueLooping = (x < number);
}
else
{
x--;
continueLooping = (x > 0);
}
}
If you really want a for loop, you should use two of them:
function doSomething()
{
//doSomething
}
if(condition)
{
for(var x = 0; x<number; x++)
{
doSomething(x);
}
}
else
{
for(var x=number; x>0; x--})
{
doSomething(x);
}
}

Why does my Actionscript 3 program randomly get stuck in an infinite loop?

mBlocks is a 2-dimensional array of Block objects. Every time my application runs, it runs the InitGridNumbers function. Sometimes, it will get stuck in an infinite loop. Other times, it builds and runs without issues.
public function InitGridNumbers():void
{
var tempRow:Array;
var tempColumn:Array;
var tempNum:int;
for (var i:int = 0; i < mNumRows; i++)
{
tempRow = GetRow(i);
for (var j:int = 0; j < mNumColumns; j++)
{
// if number is unassigned
if (tempRow[j] == 0)
{
var cantMoveOn:Boolean = true;
while (cantMoveOn)
{
tempNum = Math.random() * mNumColumns + 1;
if (!CheckRow(i, tempNum) && !CheckColumn(j, tempNum))
cantMoveOn = false;
}
mBlocks[i][j].SetNumber(tempNum);
}
}
}
}
public function CheckRow(rowNum:int, checkNum:int):Boolean
{
var tempRow:Array = GetRow(rowNum);
for (var i:int = 0; i < mNumColumns; i++)
{
if (checkNum == tempRow[i])
return true;
}
return false;
}
public function CheckColumn(columnNum:int, checkNum:int):Boolean
{
var tempColumn:Array = GetColumn(columnNum);
for (var i:int = 0; i < mNumColumns; i++)
{
if (checkNum == tempColumn[i])
return true;
}
return false;
}
public function GetRow(rowNum:int):Array
{
var rowArray:Array = new Array(mNumRows);
for (var i:int = 0; i < mNumRows; i++)
rowArray[i] = mBlocks[rowNum][i].mNumber;
return rowArray;
}
public function GetColumn(columnNum:int):Array
{
var columnArray:Array = new Array(mNumColumns);
for (var i:int = 0; i < mNumColumns; i++)
columnArray[i] = mBlocks[i][columnNum].mNumber;
return columnArray;
}
To begin with, checkColumn, getColumn and getRow methods are wrong. To get a row, you should copy numColumns items and to get a column, you should copy numRows items. In other words, if there are r rows and c columns, there would be c items per each row and r items per each column.
public function checkColumn(columnNum:int, checkNum:int):Boolean
{
var tempColumn:Array = getColumn(columnNum);
for (var i:int = 0; i < mNumRows; i++)
{
if (checkNum == tempColumn[i])
return true;
}
return false;
}
public function getRow(rowNum:int):Array
{
var rowArray:Array = new Array();//needn't specify length in advance.
for (var i:int = 0; i < mNumColumns; i++)
rowArray[i] = mBlocks[rowNum][i].mNumber;
return rowArray;
}
public function getColumn(columnNum:int):Array
{
var columnArray:Array = new Array();
for (var i:int = 0; i < mNumRows; i++)
columnArray[i] = mBlocks[i][columnNum].mNumber;
return columnArray;
}
while (cantMoveOn)
{
//call Math.floor
tempNum = Math.floor(Math.random() * mNumColumns) + 1;
if (!checkRow(i, tempNum) && !checkColumn(j, tempNum))
cantMoveOn = false;
}
It looks like you're checking for a number that is not present in the current row and column. It's hard to say without knowing more details, but can you think of a scenario where this would be impossible?
For example, if there are four columns and five rows, the tempNum would always be between one and four. Now if the number of rows is five and the corresponding column already has all numbers up to four, the if statement would never evaluate to true and hence you'd end up in an infinite loop
0 1 2 3
1
2
3
4
in case grid is a square, how about this:
0 1 2 3
4
0
0