javascript - Loop through a game board represented by a matrix and analyze all possible 3 in a row winning positions -


i have 6 x 6 game board, represented following array:

gameboard= [         0 , 1, 2, 3, 4, 5,         6 , 7, 8, 9,10,11,         12,13,14,15,16,17,         18,19,20,21,22,23,         24,25,26,27,28,29,         30,31,32,33,34,35     ] 

i working on game of hybrid tic tac toe.

here first step (cpu's turn):

loop through each position of board

for(var i=0, len = gameboard.length; < len; i++){ } 

now, lets @ position 8.

my logic check surrounding positions (1,2,3,7,9,13,14,15) for:

  • if there "x" (opponent), score = 0;
  • if there blank space, sc0re = 1;
  • if there "0", score = 2

at end, store inside array.

the position biggest score array, position cpu play.

finally, question: how code above logic inside loop?, keeping in mind there positions 0,1,2,4... aren't surrounded other positions can't use logic above everywhere.

here have tried , thinking of building on:

function scorecalc(pos){         console.log("score: "+pos);         var score = 0,             pocket;         pocket = jquery(document).find('[data-kcpos="'+pos+'"]');         if(jquery(pocket).hasclass("blank")){             score += 1;         } else if(jquery(pocket).text() == "x"){             score += 0;         } else if(jquery(pocket).text() == "0"){             score += 2;         }         console.log("score: "+score);         return score;     }   function minimax(){          var pos,             testmargin,             totest,             pocket,             score,             totestscore,             totalscore = new array();          for(var i=0, len = gameboard.length; < len; i++){             score = 0;             pos = gameboard[i];             // check if pocket empty             pocket = jquery(document).find('[data-kcpos="'+pos+'"]');             if(jquery(pocket).hasclass("blank")){                 // check see if have margin                 testmargin = jquery.inarray(pos, margins);                 if(testmargin != -1){ // if have margin                  } else {                     // test pocket above                     totest = pos - 6;                     totestscore = scorecalc(totest);                     score += totestscore;                      // test pocket bellow                     totest = pos + 6;                     totestscore = scorecalc(totest);                     score += totestscore;                      // test pocket left                     totest = pos - 1;                     totestscore = scorecalc(totest);                     score += totestscore;                      // test pocket right                     totest = pos + 1;                     totestscore = scorecalc(totest);                     score += totestscore;                      // test top left diagonal                     totest = pos - 7;                     totestscore = scorecalc(totest);                     score += totestscore;                      // test bottom right diagonal                     totest = pos + 7;                     totestscore = scorecalc(totest);                     score += totestscore;                      // test top right diagonal                     totest = pos - 5;                     totestscore = scorecalc(totest);                     score += totestscore;                      // test bottom left diagonal                     totest = pos + 5;                     totestscore = scorecalc(totest);                     score += totestscore;                 }                 totalscore.push({key: pos, val: score});             }         }         // sort array         totalscore.sort(function descending(a,b){              return b.val - a.val;         });          pocket = jquery(document).find('[data-kcpos="'+totalscore[0].key+'"]');         jquery(pocket).find(".ui-btn-text").text("0");         jquery(pocket).removeclass("blank");          return totalscore;     } 

using 2 dimensional array might make easier check surrounding areas. way you'll know position row , column, , can check [row-1, col-1], [row-1,col],[row-1, col+1] etc... make checking corner cases 0 or top row cases easier instead of knowing in 36 element array first 6 first row. players position x, y coordinate, board array gameboard[x][y].

the 2d array make checking edge cases easier, , might make game logic little easier understand. plus it'll give flexibility changing board size if desire.

good luck!


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -