examples/declarative/toys/tic-tac-toe/content/tic-tac-toe.js
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 function winner(board)
       
     2 {
       
     3     for (var i=0; i<3; ++i) {
       
     4         if (board.children[i].state!=""
       
     5             && board.children[i].state==board.children[i+3].state
       
     6             && board.children[i].state==board.children[i+6].state)
       
     7             return true
       
     8 
       
     9         if (board.children[i*3].state!=""
       
    10             && board.children[i*3].state==board.children[i*3+1].state
       
    11             && board.children[i*3].state==board.children[i*3+2].state)
       
    12             return true
       
    13     }
       
    14 
       
    15     if (board.children[0].state!=""
       
    16         && board.children[0].state==board.children[4].state!=""
       
    17         && board.children[0].state==board.children[8].state!="")
       
    18         return true
       
    19 
       
    20     if (board.children[2].state!=""
       
    21         && board.children[2].state==board.children[4].state!=""
       
    22         && board.children[2].state==board.children[6].state!="")
       
    23         return true
       
    24 
       
    25     return false
       
    26 }
       
    27 
       
    28 function restart()
       
    29 {
       
    30     // No moves left - start again
       
    31     for (var i=0; i<9; ++i)
       
    32         board.children[i].state = ""
       
    33 }
       
    34 
       
    35 function makeMove(pos,player)
       
    36 {
       
    37     board.children[pos].state = player
       
    38     if (winner(board)) {
       
    39         win(player + " wins")
       
    40         return true
       
    41     } else {
       
    42         return false
       
    43     }
       
    44 }
       
    45 
       
    46 function computerTurn()
       
    47 {
       
    48     var r = Math.random();
       
    49     if(r < game.difficulty){
       
    50         smartAI();
       
    51     }else{
       
    52         randAI();
       
    53     }
       
    54 }
       
    55 
       
    56 function smartAI()
       
    57 {
       
    58     function boardCopy(a){
       
    59         var ret = new Object;
       
    60         ret.children = new Array(9);
       
    61         for(var i = 0; i<9; i++){
       
    62             ret.children[i] = new Object;
       
    63             ret.children[i].state = a.children[i].state;
       
    64         }
       
    65         return ret;
       
    66     }
       
    67     for(var i=0; i<9; i++){
       
    68         var simpleBoard = boardCopy(board);
       
    69         if (board.children[i].state == "") {
       
    70             simpleBoard.children[i].state = "O";
       
    71             if(winner(simpleBoard)){
       
    72                 makeMove(i,"O")
       
    73                 return
       
    74             }
       
    75         }
       
    76     }
       
    77     for(var i=0; i<9; i++){
       
    78         var simpleBoard = boardCopy(board);
       
    79         if (board.children[i].state == "") {
       
    80             simpleBoard.children[i].state = "X";
       
    81             if(winner(simpleBoard)){
       
    82                 makeMove(i,"O")
       
    83                 return
       
    84             }
       
    85         }
       
    86     }
       
    87     function thwart(a,b,c){//If they are at a, try b or c
       
    88         if (board.children[a].state == "X") {
       
    89             if (board.children[b].state == "") {
       
    90                 makeMove(b,"O")
       
    91                 return true
       
    92             }else if (board.children[c].state == "") {
       
    93                 makeMove(c,"O")
       
    94                 return true
       
    95             }
       
    96         }
       
    97         return false;
       
    98     }
       
    99     if(thwart(4,0,2)) return;
       
   100     if(thwart(0,4,3)) return;
       
   101     if(thwart(2,4,1)) return;
       
   102     if(thwart(6,4,7)) return;
       
   103     if(thwart(8,4,5)) return;
       
   104     if(thwart(1,4,2)) return;
       
   105     if(thwart(3,4,0)) return;
       
   106     if(thwart(5,4,8)) return;
       
   107     if(thwart(7,4,6)) return;
       
   108     for(var i =0; i<9; i++){//Backup
       
   109         if (board.children[i].state == "") {
       
   110             makeMove(i,"O")
       
   111             return
       
   112         }
       
   113     }
       
   114     restart();
       
   115 }
       
   116 
       
   117 function randAI()
       
   118 {
       
   119     var open = 0;
       
   120     for (var i=0; i<9; ++i)
       
   121         if (board.children[i].state == "") {
       
   122             open += 1;
       
   123         }
       
   124     if(open == 0){
       
   125         restart();
       
   126         return;
       
   127     }
       
   128     var openA = new Array(open);//JS doesn't have lists I can append to (i think)
       
   129     var acc = 0;
       
   130     for (var i=0; i<9; ++i)
       
   131         if (board.children[i].state == "") {
       
   132             openA[acc] = i;
       
   133             acc += 1;
       
   134         }
       
   135     var choice = openA[Math.floor(Math.random() * open)];
       
   136     makeMove(choice, "O");
       
   137 }
       
   138 
       
   139 function win(s)
       
   140 {
       
   141     msg.text = s
       
   142     msg.opacity = 1
       
   143     endtimer.running = true
       
   144 }
       
   145