jhylands

n tic tac toe

Apr 16th, 2015
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //create global variable to hold the game board
  3. var board = createBoard(4);
  4.  
  5. //function to create a new n dimentional board
  6. function createBoard(dimentions){
  7.     if(dimentions ==0){
  8.         return "-";
  9.     }else{
  10.         board = new Array();
  11.         for(i=0;i<3;i++){
  12.             board[i] = createBoard(dimentions-1);
  13.         }
  14.         return board;
  15.     }
  16. }
  17.  
  18. //function to check the board for a winner
  19. function checkBoard(board){
  20.     //base case: dimentions of board liniar
  21.     if(typeof(board[0])!="array"){
  22.         if(board[0] == board[1] && board[1]==board[2] && board[0]!="-"){
  23.             return true;
  24.         }else{
  25.             return false;
  26.         }
  27.     }else{
  28.         //not liniar use the recusrsive case
  29.         var three = false;
  30.         //rows
  31.         for(i=0;i<3;i++){
  32.             three = three || checkBoard([board[0][i],board[1][i],board[2][i]]);
  33.         }
  34.         //collumns
  35.         for(i=0;i<3;i++){
  36.             three = three || checkBoard([board[i][0],board[i][1],board[i][2]]);
  37.         }
  38.         //top left->bottom right
  39.         three = three || checkBoard([board[0][0],board[1][1],board[2][2]]);
  40.         //bottom left -> top right
  41.         three = three || checkBoard([board[0][2],[1][1],[2][0]]);
  42.         return three;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment