Commits

Colin Copeland committed 5a27ff4

add alternating colors

Comments (0)

Files changed (1)

tic-tac-toe/media/game.js

 var line_size = 10;
 var box_size = 0;
+var board = [
+    [0, 0, 0],
+    [0, 0, 0],
+    [0, 0, 0],
+];
+var turn = 1;
+var colors = {
+    '-1': "#1435AD",
+    '1': "#A67300",
+}
 
 function setup_board(ctx) {
     box_size = ctx.canvas.width/3.0;
     ctx.fillRect(0, box_size * 2, ctx.canvas.width, line_size);
     // save?
     ctx.save();
-    window.localStorage['board'] = [
-        [0, 0, 0],
-        [0, 0, 0],
-        [0, 0, 0],
-    ]
-    window.localStorage['turn'] = 1;
 }
 
 function draw(ctx, x, y) {
     console.log('mark', row, col);
     var x = row * box_size + line_size;
     var y = col * box_size + line_size;
-    var turn = window.localStorage['turn'];
-    var board = new TicTacToe(window.localStorage['board']);
-    if (board.space_is_free(row, col)) {
-        console.log('free space');
-        if (turn == 1) {
-            ctx.fillStyle = "#1435AD";
-        } else {
-            ctx.fillStyle = "#A67300";
-        }
-        board.move(row, col, turn);
+    var game = new TicTacToe(board);
+    if (game.space_is_free(row, col)) {
+        game.move(row, col, turn);
+        ctx.fillStyle = colors[turn];
         ctx.fillRect(x, y, box_size - line_size, box_size - line_size);
     }
-    console.log(board.board);
-    window.localStorage['board'] = board.board;
-    window.localStorage['turn'] *= -1;
-    console.log(window.localStorage['board']);
-    // ctx.save();
+    board = game.board;
+    turn *= -1;
 }
 
 
         return this.board[row][col] == 0;
     }
     this.move = function(row, col, turn) {
-        console.log('board', row, col, turn);
-        console.log('before', this.board[row][col]);
         this.board[row][col] = turn;
-        console.log('after', this.board[row][col]);
-        console.log('board', this.board);
     }
 }
 
 
-
 $(document).ready(function() {
     var canvas = $('#board');
     var ctx = canvas.get(0).getContext("2d");