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