Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var chessBoard = function () {
  2.     var chessBoard = [];
  3.     var alph = "abcdefgh".toUpperCase().split("");
  4.     var evenAlph = 'aceg';
  5.     //create properties in object chessBoard that are letters A-H (i.e, chessBoard.A, chessBoard.B or similarly chessBoard['A'] or chessBoard['B'] etc.
  6.     for (var i = 0; i < alph.length; i++) {
  7.         chessBoard[alph[i]] = [];
  8.     }
  9.     //loop through properties of chessBoard with for ... in. in each loop, "v in chessBoard" means like how i in for(var i = 0; ... i++) changes i each time starting with 0 and ending with whatever is specified (i.e. i < 5), v will become the properties of chessBoard each loop, starting with v = 'A' and ending with v = 'H'
  10.     for (v in chessBoard) {
  11.         // "if this property name is an even letter (evenAlph)"
  12.         if (evenAlph.indexOf(v.toLowerCase()) !== -1) {
  13.             //start with 0, end with 8 (make 8 objects: A1-A8, B1-B8, etc.
  14.             for (var i = 0; i < 8; i++) {
  15.                 //i is even (if the remainder of i divided by two is; 0, i.e. 2 divides i evenly)
  16.                 if (i % 2 == 0) {
  17.                     //push with cell object, which is black
  18.                     chessBoard[v].push({
  19.                         black: true,
  20.                         white: false
  21.                     });
  22.                 } else {
  23.                     //push with cell object, which is white
  24.                     chessBoard[v].push({
  25.                         black: false,
  26.                         white: true
  27.                     });
  28.                 }
  29.             }
  30.             //otherwise (this property is not an evenAlph) we start with white, end black
  31.         } else {
  32.             for (var i = 0; i < 8; i++) {
  33.                 if (i % 2 == 0) {
  34.                     chessBoard[v].push({
  35.                         black: false,
  36.                         white: true
  37.                     });
  38.                 } else {
  39.                     chessBoard[v].push({
  40.                         black: true,
  41.                         white: false
  42.                     });
  43.                 }
  44.             }
  45.         }
  46.     }
  47.     return chessBoard;
  48. }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement