summaryrefslogtreecommitdiffstats
path: root/examples/declarative/tic-tac-toe/tic-tac-toe.qml
blob: ae187d259e542db989f916d47b7dcde0ff5b771e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import Qt 4.6
import "content"

Item {
    id: game
    property bool show: false;
    width: 440
    height: 480
    anchors.fill: parent
    property real difficulty: 1.0; //chance it will actually think

    Image {
        id: boardimage
        anchors.verticalCenter: parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter
        source: "content/pics/board.png"
    }

    Grid {
        id: board
        anchors.fill: boardimage

        columns: 3

        Repeater {
            model: 9
            TicTac {
                width: board.width/3
                height: board.height/3
                onClicked: {
                    if (!endtimer.running) {
                        if (!makeMove(index,"X"))
                            computerTurn()
                    }
                }
            }
        }

        Script {
            function winner(board)
            {
                for (var i=0; i<3; ++i) {
                    if (board.children[i].state!=""
                        && board.children[i].state==board.children[i+3].state
                        && board.children[i].state==board.children[i+6].state)
                        return true

                    if (board.children[i*3].state!=""
                        && board.children[i*3].state==board.children[i*3+1].state
                        && board.children[i*3].state==board.children[i*3+2].state)
                        return true
                }

                if (board.children[0].state!=""
                    && board.children[0].state==board.children[4].state!=""
                    && board.children[0].state==board.children[8].state!="")
                    return true

                if (board.children[2].state!=""
                    && board.children[2].state==board.children[4].state!=""
                    && board.children[2].state==board.children[6].state!="")
                    return true

                return false
            }

            function restart()
            {
                // No moves left - start again
                for (var i=0; i<9; ++i)
                    board.children[i].state = ""
            }

            function makeMove(pos,player)
            {
                board.children[pos].state = player
                if (winner(board)) {
                    win(player + " wins")
                    return true
                } else {
                    return false
                }
            }

            function computerTurn()
            {
                var r = Math.random();
                if(r < game.difficulty){
                    smartAI();
                }else{
                    randAI();
                }
            }

            function smartAI()
            {
                function boardCopy(a){
                    var ret = new Object;
                    ret.children = new Array(9);
                    for(var i = 0; i<9; i++){
                        ret.children[i] = new Object;
                        ret.children[i].state = a.children[i].state;
                    }
                    return ret;
                }
                for(var i=0; i<9; i++){
                    var simpleBoard = boardCopy(board);
                    if (board.children[i].state == "") {
                        simpleBoard.children[i].state = "O";
                        if(winner(simpleBoard)){
                            makeMove(i,"O")
                            return
                        }
                    }
                }
                for(var i=0; i<9; i++){
                    var simpleBoard = boardCopy(board);
                    if (board.children[i].state == "") {
                        simpleBoard.children[i].state = "X";
                        if(winner(simpleBoard)){
                            makeMove(i,"O")
                            return
                        }
                    }
                }
                function thwart(a,b,c){//If they are at a, try b or c
                    if (board.children[a].state == "X") {
                        if (board.children[b].state == "") {
                            makeMove(b,"O")
                            return true
                        }else if (board.children[c].state == "") {
                            makeMove(c,"O")
                            return true
                        }
                    }
                    return false;
                }
                if(thwart(4,0,2)) return;
                if(thwart(0,4,3)) return;
                if(thwart(2,4,1)) return;
                if(thwart(6,4,7)) return;
                if(thwart(8,4,5)) return;
                if(thwart(1,4,2)) return;
                if(thwart(3,4,0)) return;
                if(thwart(5,4,8)) return;
                if(thwart(7,4,6)) return;
                for(var i =0; i<9; i++){//Backup
                    if (board.children[i].state == "") {
                        makeMove(i,"O")
                        return
                    }
                }
                restart();
            }

            function randAI()
            {
                var open = 0;
                for (var i=0; i<9; ++i)
                    if (board.children[i].state == "") {
                        open += 1;
                    }
                if(open == 0){
                    restart();
                    return;
                }
                var openA = new Array(open);//JS doesn't have lists I can append to (i think)
                var acc = 0;
                for (var i=0; i<9; ++i)
                    if (board.children[i].state == "") {
                        openA[acc] = i;
                        acc += 1;
                    }
                var choice = openA[Math.floor(Math.random() * open)];
                makeMove(choice, "O");
            }

            function win(s)
            {
                msg.text = s
                msg.opacity = 1
                endtimer.running = true
            }
        }

        Timer {
            id: endtimer
            interval: 1600
            onTriggered: { msg.opacity = 0; restart() }
        }
    }

    Row {
        spacing: 4
        anchors.top: board.bottom
        anchors.horizontalCenter: board.horizontalCenter
        Button {
            text: "Hard"
            onClicked: game.difficulty=1.0;
            down: game.difficulty == 1.0
        }
        Button {
            text: "Moderate"
            onClicked: game.difficulty=0.8;
            down: game.difficulty == 0.8
        }
        Button {
            text: "Easy"
            onClicked: game.difficulty=0.2;
            down: game.difficulty == 0.2
        }
    }

    Text {
        id: msg
        opacity: 0
        anchors.centerIn: parent
        color: "blue"
        styleColor: "white"
        style: Text.Outline
        font.pixelSize: 50
        font.bold: true
    }
}