Trying to check possible combos in this sort of array:
[ ["X","X","X"], ["","",""], ["","",""] ]
Combos include:
tic tac toe logic
So, these are true:
[ ["X","",""], ["","X",""], ["","","X"] ]
[ ["","","X"], ["","X",""], ["X","",""] ]
[ ["","X",""], ["","X",""], ["","X",""] ]
So far, I have it working as long as my array[0][0] is the one of the selected choices, but I feel I'm moving towards a stack of code.
function testWin(){
var cs=b.length, ph=0, pv=0, pd=0;
for(var i=0;i<cs;i++){
if(b[0][i]==="X"){ ph++; }
if(b[i][0]==="X"){ pv++; }
if(b[i][i]==="X"){ pd++; }
}
if(ph===cs || pv===cs || pd===cs){ alert("YOU WIN!"); }
}
Fiddle here: http://jsfiddle.net/z4XLj/
Been trying another for loop to no success to identify 0
Tried a while loop, but it crashed and I don't think that is the most effective anyway.
Need to find if...
ANY array[i][this] contains nothing but X
ANY array[this][i] contains nothing but X
I now this looks like black magic, but here you have:
function testWin(){
var cs = b.length, I = [], J = [], d = [0, 0];
for(var i=0; i<cs; i++){
for(var j=0; j<cs; j++){
if(b[i][j] != 'X') continue;
I[i] = (I[i] || 0) + 1;
J[j] = (J[j] || 0) + 1;
if(i == j) d[0]++; //Diagonal \
if(i == cs-j-1) d[1]++; //Diagonal /
}
}
if(d.concat(I).concat(J).indexOf(cs) >=0) alert("You win");
}
jsfiddle: http://jsfiddle.net/edgarinvillegas/z4XLj/3/
It considers any horizontal, vertical or diagonal victory for board of any size
:O It works! :)
Cheers, from La Paz, Bolivia
function testWin() {
var xCount;
var rowsCount = b.length;
// First check rows
for (var row = 0; row < rowsCount; row++) {
xCount = 0;
colsCount = b[row].length;
for (var col = 0; col < colsCount; col++) {
if (b[row][col] == "X") {
xCount++;
} else {
break;
}
}
if (xCount == colsCount) {
alert ("YOU WIN!");
return true;
}
}
// Now check columns -- just invert the above nested loops
colsCount = b[0].length;
for (var col = 0; col < colsCount; col++) {
xCount = 0;
for (var row = 0; row < rowsCount; row++) {
if (b[row][col] == "X") {
xCount++;
} else {
break;
}
}
if (xCount == rowsCount) {
alert ("YOU WIN!");
return true;
}
}
// Check diagonals
if (b[1][1] == "X" &&
((b[0][0] == "X" && b[2][2] == "X") || (b[0][2] == "X" && b[2][0] == "X"))) {
alert ("YOU WIN!");
return true;
}
return false;
}
Related
When running this code I currently getting the error "TypeError: Invalid value for y-coordinate. Make sure you are passing finite numbers to setPosition(x, y)." all of my functions do work are are declared properly. When I use a println and print out letYPos it prints out "NaN" but when I call the function again it prints out the correct value. Does anyone know how I can fix this or if there even is a way to fix this?
var count = 0;
var letYPos = 0;
var y = 3;
function testing()
{
var letXPos = 25;
letYPos += 75;
if (count == 6)
{
println("You have ran out of guesses, the correct anwser was: " + secretWord);
return;
}
var input = readLine("Enter your word: ");
if (input == null)
{
println("You have to enter a word! ");
return;
}
if (input.length != 5)
{
println("That is not a five letter word, please try again.");
return;
}
var x = 3;
for (var i = 0; i < input.length; i++)
{
if (input.includes(secretWord.charAt(i)))
{
var index = input.indexOf(secretWord.charAt(i));
}
if (index == 0) { yellow(3, y ) }
if (index == 1) { yellow(83, y ) }
if (index == 2) { yellow(163, y) }
if (index == 3) { yellow(243, y) }
if (index == 4) { yellow(323, y) } index = null;
}
for (var i = 0; i < input.length; i++)
{
if (input.charAt(i) == secretWord.charAt(i))
{
green(x, y);
}
x += 80;
}
y += 80;
for (var i = 0; i < input.length; i++)
{
for (var a = 0; a <= 5; a++)
{
var txt = new Text(input.charAt(a), font);
txt.setPosition(letXPos, letYPos);
add(txt);
letXPos += 80;
}
}
if (input == secretWord)
{
println("That's Correct, Congratulations!");
return;
}
count++;
setTimeout(testing, 100);
}
I am during writing game. When condition (playerlife < 1) is meet, game should stop. Whole game is in singlePlayer function. The problem is, I don't know how to end this function. Simple placing condition inside singlePlayer function doesn't work because it is checked only once during starting a game.
if (playerlife < 1) {
return;
}
I also tried to put this condition in interval and check if condition is meet continously but I doesn't work and anyway it doesn't looks like a good idea.
Below is part of code where after moving player there are checked some conditions. Game is similar to old school "Frogger". When player jump in to the water then he lost 1 life. After loosing 3 lives game should be over.
$(function() {
function singlePlayer() {
checkPosition(x, y) {
for (var i = 0; i < raftsTab.length; i++) {
if (x == raftsTab[i].PosX && y == raftsTab[i].PosY) {
let thisRaft = raftsTab[i];
console.log(x, y);
console.log(thisRaft);
clearInterval(MoveToPlayer);
movePlayer(thisRaft);
return;
}
}
for (var i = 0; i < raftsTab.length; i++) {
if (x !== raftsTab[i].PosX && y !== raftsTab[i].PosY && y !== 0 && y !== 5 && y !== 10) {
player1.lifes = player1.lifes - 1;
//player dead after loosing 3 lives - it would be perfect if game could be ended from here
$('.lifes').text("Player lifes: " + player1.lifes);
for (var i = 0; i < trophiesTab.length; i++) {
if (player1.trophie - 1 == i) {
trophiesTab[i].show();
player1.trophie = -1;
}
}
player1.PosX = 5;
player1.PosY = 10;
clearInterval(MoveToPlayer);
changePosition();
return;
}
}
for (var i = 0; i < trophiesTab.length; i++) {
if (x == trophiesTab[i].PosX && y == trophiesTab[i].PosY && player1.trophie == -1) {
trophiesTab[i].hide();
}
}
if (x == 5 && y == 0 && player1.trophie !== -1) {
tresure1 = tresure1 + player1.trophie;
player1.items = player1.items + 1;
$('.items').text("Gathered items: " + player1.items + "/3");
player1.trophie = -1;
console.log(tresure1);
}
clearInterval(MoveToPlayer);
}
}
});
I am learning JavaScript by programming my first game (a simple laser-mirror-type game). The game operates in a grid and I want to determine if a cell holds an obstacle or not. So I call this function:
function updateGrid () {
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
for (let o = 0; o < obstacles.length; o++) {
if (grid[i][j].x === obstacles[o].x && grid[i][j].y === obstacles[o].y) {
grid[i][j].obstacle = true;
} else if (grid[i][j].x != obstacles[o].x && grid[i][j].y != obstacles[o].y) {
//grid[i][j].obstacle = false;
}
}
for (let m = mirrors.length - 1; m >= 0; m--) {
if (grid[i][j].x + cellOffset.x== mirrors[m].x && grid[i][j].y + cellOffset.y == mirrors[m].y) {
grid[i][j].mirror = true;
} else {
grid[i][j].mirror = false;
}
}
if (grid[i][j].x + cellOffset.x == target.x && grid[i][j].y + cellOffset.y == target.y) {
grid[i][j].target = true;
} else {
grid[i][j].target = false;
}
if (grid[i][j].x == laserPen.x && grid[i][j].y + (rowH / 2) - (cellOffset.y / 4) == laserPen.y) {
grid[i][j].pen = true;
} else {
grid[i][j].pen = false;
}
}
}
}
However the if-statement that determines if the cell contains an obstacle, seems to not work.
This works (sets grid[ i ][ j ].obstacle to true):
for (let o = 0; o < obstacles.length; o++) {
if (grid[i][j].x === obstacles[o].x && grid[i][j].y === obstacles[o].y) {
grid[i][j].obstacle = true;
} else if (grid[i][j].x != obstacles[o].x && grid[i][j].y != obstacles[o].y) {
//grid[i][j].obstacle = false;
}
}
This does not (sets grid[ i ][ j ].obstacle to false):
for (let o = 0; o < obstacles.length; o++) {
if (grid[i][j].x === obstacles[o].x && grid[i][j].y === obstacles[o].y) {
grid[i][j].obstacle = true;
} else if (grid[i][j].x != obstacles[o].x && grid[i][j].y != obstacles[o].y) {
grid[i][j].obstacle = false;
}
}
I actually added the else-if just for safety, but it failed to work with a simple else-statement as well.
I am using the p5.js library and any insight into what is happening here would be greatly appreciated. Thanks!
Found the bug after some testing. I was iterating through all the obstacles on the same grid cell (i.e. test obstacles[0] for grid[2][2], test obstacles[1] for grid[2][2]). This meant that if the first obstacle in the array proved true, but the others proved false, the overall grid[2][2].obstacle would become false. Or to map it visually:
grid[2][1] ...
grid[2][2].obstacle = true!
grid[2][2].obstacle = false.
grid[2][2].obstacle = false.
grid[2][3] ...
Output:
grid[2][2].obstacle is false :(
I am trying to do a pop-up warning before the sales order is saved if the exact same item is entered twice when the order is created/modified on Netsuite. However, there is no window popping up and I am not sure what is wrong with the script. Here is what I got:
function validateitem (type){
var flag = true;
var numLine = nlapiGetLineItemCount('item');
itemArr = [];
if (type == 'item' && numLine > 0) {
for(var i = 0; i < numLine; i++) {
var itemSO = {};
itemSO.id = nlapiGetLineValue('item','item',i);
if (itemSO.id != null && itemSO.id !=''){
for (var j = 0; j < numLine; j++){
if(itenArr.indexOf(itemSO[i].id) === -1) {
itemArr.push(itemSO[i].id);}
else{
if (!confirm('You have entered a duplicate item for this sales order. Continue?'))
{
flag = false;
}
}
}
}
}
}
return flag;
}
Can somebody help, please?
Here is a slightly edited version:
function validateitem (){
var flag = true;
var numLine = nlapiGetLineItemCount('item');
itemArr = [];
if (numLine > 0) {
for(var i = 1; i <= numLine; i++) {
var itemSO = nlapiGetLineItemValue('item','item',i);
if (itemSO != null && itemSO !=''){
for (var j = 1; j <= numLine; j++){
if(itemArr.indexOf(itemSO[i]) === -1) {
itemArr.push(itemSO[i]);}
else{
flag = false;
}
}
}
}
}
if (flag == false){
alert('You have entered the same item twice.Continue?');
}
return flag;
}
This is the complete after-edit code that works:
function validateitem (){
var flag = true;
var numLine = nlapiGetLineItemCount('item');
itemArr = [];
if (numLine > 0) {
for(var i = 1; i <= numLine; i++) {
var itemSO = nlapiGetLineItemValue('item','item',i);
if (itemSO != null && itemSO !=''){
for (var j = i+1; j <= numLine; j++){
var itemSOplus = nlapiGetLineItemValue('item','item',j);
if(itemSO === itemSOplus) {
flag = false;
}
}
}
}
}
if (flag == false){
alert('You have entered the same item twice.Continue?');
}
return flag;
}
Thanks to Krypton!!
As per SuiteAnswers ID 10579, there are no paramters passed to the saveRecord client event. Therefore when your code checks the following:
if (type == 'item' && numLine > 0)
it finds that type equals undefined, so the condition is not met and the code will jump straight down to return flag which has been set to true.
Also note that in SuiteScript 1.0, line indexes start from 1 - not 0 as your code seems to assume.
EDIT - adding comment to form part of this answer:
I'd like to understand your logic behind itemSO[i] - as itemSO is not an array. Why not just compare the item from the current line of the inner loop with the current line of the outer loop and set the flag false if they match? Also, the inner loop need only start from j = i + 1 as the previous lines would have already been compared.
I'm creating a simple tic-tac-toe game and I have a boolean called winAlert that if it is true it should alert the player that they have won. This works correctly for the most part, but there is one instance where it does not. If the game is won and all of the cells are filled, the console logs that winAlert's value is false, but it still alerts the player that they have won, as if it were true. Could someone look over this code and see why this is behaving in this way? http://jsfiddle.net/Z5c9P/3/
This function is where I think the problem lies, but I don't know for sure.
var determineWin = function (pMoves) {
for (var i = 0; i < winConditions.length; i++) {
if (winConditions[i].length > pMoves.length) {
continue;
}
for (var j = 0; j < winConditions[i].length; j++) {
winAlert = false;
for (var k = 0; k < pMoves.length; k++) {
if (pMoves[k] === winConditions[i][j]) {
winAlert = true;
break;
}
}
if (!winAlert) break;
}
if (winAlert) {
alert(currentPlayer + " wins!");
break;
}
}
};
Here's the code that calls this function:
$('td').one('click', function () {
turnCount += 1;
setCurrentPlayer();
$(this).text(currentPlayer);
cellTracker = $(this).attr('id');
storeMoves();
determineWin(xMoves);
determineWin(oMoves);
if(turnCount === 9 && winAlert === false) {
alert("Tie game!");
}
console.log(turnCount, xMoves, oMoves, winAlert);
});
This is happening because your code does the following:
storeMoves();
determineWin(xMoves);
determineWin(oMoves);
if(turnCount === 9 && winAlert === false) {
alert("Tie game!");
}
console.log(turnCount, xMoves, oMoves, winAlert);
So if X ever wins the game, determineWin(xMoves) will set the variable to true, and determinWin(oMoves) will set it back to false, all before the console.log()
One way to solve this would be to only check for a win for the current player's moves:
storeMoves();
determineWin(currentPlayer == 'X' ? xMoves : yMoves);
if(turnCount === 9 && winAlert === false) {
alert("Tie game!");
}
console.log(turnCount, xMoves, oMoves, winAlert);
You have called determineWin on each player. so if x wins, determineWin(oMoves); will make winAlert false. Is this the problem?
Maybe you should create a new determineWin which only called once to determine who is the winner.
this code will just skip another user(so winAlert is still true) when his cell is less than 3, so this problem doesn't need fill all cells but just each player has more than 3 cells.
if (winConditions[i].length > pMoves.length) {
continue;
}
i change a little your code Fiddle
var determineWin = function (pMoves) {
for (var i = 0; i < winConditions.length; i++) {
if (winConditions[i].length > pMoves.length) {
continue;
}
winAlert = false;
matches = 0;
for (var j = 0; j < winConditions[i].length; j++) {
for (var k = 0; k < pMoves.length; k++) {
if (pMoves[k] === winConditions[i][j]) {
matches++;
}
}
}
if (matches == 3) return true;
}
return false;
};
and then
$('td').one('click', function () {
turnCount += 1;
setCurrentPlayer();
$(this).text(currentPlayer);
cellTracker = $(this).attr('id');
storeMoves();
if (determineWin(xMoves)){ // this is changed
alert("X Win")
return;
};
if (determineWin(oMoves)){
alert("O Win")
return;
};
if(turnCount === 9 && winAlert === false) {
alert("Tie game!");
}
console.log(turnCount, xMoves, oMoves, winAlert);
});
** Updated to clarify