summaryrefslogtreecommitdiffstats
path: root/examples/declarative/tic-tac-toe/tic-tac-toe.qml
blob: 63ee4836a7fb938320ab19ad998202988832404d (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
import Qt 4.6
import "content"

Item {
    width: boardimage.width
    height: boardimage.height

    Image {
        id: boardimage
        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()
            {
                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()) {
                    win(player + " wins")
                    return true
                } else {
                    return false
                }
            }

            function computerTurn()
            {
                // world's dumbest player
                for (var i=0; i<9; ++i)
                    if (board.children[i].state == "") {
                        makeMove(i,"O")
                        return
                    }
                restart()
            }

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

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

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