summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorWarwick Allison <warwick.allison@nokia.com>2009-11-24 02:08:16 (GMT)
committerWarwick Allison <warwick.allison@nokia.com>2009-11-24 02:08:16 (GMT)
commitef7e4d6b0b1e8ab65201b0464e3fa473b505f833 (patch)
tree7f73f5feb4b40406286ecabb106b014dc35475c7 /examples
parent8d263b7cbf6b0bd17deeb8b56c90e0232057b832 (diff)
downloadQt-ef7e4d6b0b1e8ab65201b0464e3fa473b505f833.zip
Qt-ef7e4d6b0b1e8ab65201b0464e3fa473b505f833.tar.gz
Qt-ef7e4d6b0b1e8ab65201b0464e3fa473b505f833.tar.bz2
Tic-tac-toe.
Diffstat (limited to 'examples')
-rw-r--r--examples/declarative/tic-tac-toe/content/pics/board.pngbin0 -> 5524 bytes
-rw-r--r--examples/declarative/tic-tac-toe/content/pics/o.pngbin0 -> 1470 bytes
-rw-r--r--examples/declarative/tic-tac-toe/content/pics/x.pngbin0 -> 1331 bytes
-rw-r--r--examples/declarative/tic-tac-toe/tic-tac-toe.qml115
4 files changed, 115 insertions, 0 deletions
diff --git a/examples/declarative/tic-tac-toe/content/pics/board.png b/examples/declarative/tic-tac-toe/content/pics/board.png
new file mode 100644
index 0000000..cd85971
--- /dev/null
+++ b/examples/declarative/tic-tac-toe/content/pics/board.png
Binary files differ
diff --git a/examples/declarative/tic-tac-toe/content/pics/o.png b/examples/declarative/tic-tac-toe/content/pics/o.png
new file mode 100644
index 0000000..abc7ee0
--- /dev/null
+++ b/examples/declarative/tic-tac-toe/content/pics/o.png
Binary files differ
diff --git a/examples/declarative/tic-tac-toe/content/pics/x.png b/examples/declarative/tic-tac-toe/content/pics/x.png
new file mode 100644
index 0000000..ddc65c8
--- /dev/null
+++ b/examples/declarative/tic-tac-toe/content/pics/x.png
Binary files differ
diff --git a/examples/declarative/tic-tac-toe/tic-tac-toe.qml b/examples/declarative/tic-tac-toe/tic-tac-toe.qml
new file mode 100644
index 0000000..63ee483
--- /dev/null
+++ b/examples/declarative/tic-tac-toe/tic-tac-toe.qml
@@ -0,0 +1,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
+ }
+}