Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //create global variable to hold the game board
- var board = createBoard(4);
- //function to create a new n dimentional board
- function createBoard(dimentions){
- if(dimentions ==0){
- return "-";
- }else{
- board = new Array();
- for(i=0;i<3;i++){
- board[i] = createBoard(dimentions-1);
- }
- return board;
- }
- }
- //function to check the board for a winner
- function checkBoard(board){
- //base case: dimentions of board liniar
- if(typeof(board[0])!="array"){
- if(board[0] == board[1] && board[1]==board[2] && board[0]!="-"){
- return true;
- }else{
- return false;
- }
- }else{
- //not liniar use the recusrsive case
- var three = false;
- //rows
- for(i=0;i<3;i++){
- three = three || checkBoard([board[0][i],board[1][i],board[2][i]]);
- }
- //collumns
- for(i=0;i<3;i++){
- three = three || checkBoard([board[i][0],board[i][1],board[i][2]]);
- }
- //top left->bottom right
- three = three || checkBoard([board[0][0],board[1][1],board[2][2]]);
- //bottom left -> top right
- three = three || checkBoard([board[0][2],[1][1],[2][0]]);
- return three;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment