summaryrefslogtreecommitdiffstats
path: root/examples/declarative/toys/tic-tac-toe
diff options
context:
space:
mode:
Diffstat (limited to 'examples/declarative/toys/tic-tac-toe')
-rw-r--r--examples/declarative/toys/tic-tac-toe/content/Button.qml37
-rw-r--r--examples/declarative/toys/tic-tac-toe/content/TicTac.qml20
-rw-r--r--examples/declarative/toys/tic-tac-toe/content/pics/board.pngbin0 -> 12258 bytes
-rw-r--r--examples/declarative/toys/tic-tac-toe/content/pics/o.pngbin0 -> 1470 bytes
-rw-r--r--examples/declarative/toys/tic-tac-toe/content/pics/x.pngbin0 -> 1331 bytes
-rw-r--r--examples/declarative/toys/tic-tac-toe/content/tic-tac-toe.js145
-rw-r--r--examples/declarative/toys/tic-tac-toe/tic-tac-toe.qml77
-rw-r--r--examples/declarative/toys/tic-tac-toe/tic-tac-toe.qmlproject16
8 files changed, 295 insertions, 0 deletions
diff --git a/examples/declarative/toys/tic-tac-toe/content/Button.qml b/examples/declarative/toys/tic-tac-toe/content/Button.qml
new file mode 100644
index 0000000..ecf18cd
--- /dev/null
+++ b/examples/declarative/toys/tic-tac-toe/content/Button.qml
@@ -0,0 +1,37 @@
+import Qt 4.7
+
+Rectangle {
+ id: container
+
+ property string text: "Button"
+ property bool down: false
+ property string mainCol: "lightgray"
+ property string darkCol: "darkgray"
+ property string lightCol: "white"
+
+ width: buttonLabel.width + 20; height: buttonLabel.height + 6
+ border { width: 1; color: Qt.darker(mainCol) }
+ radius: 8;
+ color: mainCol
+ smooth: true
+
+ gradient: Gradient {
+ GradientStop {
+ id: topGrad; position: 0.0
+ color: if (container.down) { darkCol } else { lightCol }
+ }
+ GradientStop { position: 1.0; color: mainCol }
+ }
+
+ signal clicked
+
+ MouseArea { id: mr; anchors.fill: parent; onClicked: container.clicked() }
+
+ Text {
+ id: buttonLabel
+
+ anchors.centerIn: container
+ text: container.text;
+ font.pixelSize: 14
+ }
+}
diff --git a/examples/declarative/toys/tic-tac-toe/content/TicTac.qml b/examples/declarative/toys/tic-tac-toe/content/TicTac.qml
new file mode 100644
index 0000000..d247943
--- /dev/null
+++ b/examples/declarative/toys/tic-tac-toe/content/TicTac.qml
@@ -0,0 +1,20 @@
+import Qt 4.7
+
+Item {
+ signal clicked
+
+ states: [
+ State { name: "X"; PropertyChanges { target: image; source: "pics/x.png" } },
+ State { name: "O"; PropertyChanges { target: image; source: "pics/o.png" } }
+ ]
+
+ Image {
+ id: image
+ anchors.centerIn: parent
+ }
+
+ MouseArea {
+ anchors.fill: parent
+ onClicked: parent.clicked()
+ }
+}
diff --git a/examples/declarative/toys/tic-tac-toe/content/pics/board.png b/examples/declarative/toys/tic-tac-toe/content/pics/board.png
new file mode 100644
index 0000000..7e5b7ba
--- /dev/null
+++ b/examples/declarative/toys/tic-tac-toe/content/pics/board.png
Binary files differ
diff --git a/examples/declarative/toys/tic-tac-toe/content/pics/o.png b/examples/declarative/toys/tic-tac-toe/content/pics/o.png
new file mode 100644
index 0000000..abc7ee0
--- /dev/null
+++ b/examples/declarative/toys/tic-tac-toe/content/pics/o.png
Binary files differ
diff --git a/examples/declarative/toys/tic-tac-toe/content/pics/x.png b/examples/declarative/toys/tic-tac-toe/content/pics/x.png
new file mode 100644
index 0000000..ddc65c8
--- /dev/null
+++ b/examples/declarative/toys/tic-tac-toe/content/pics/x.png
Binary files differ
diff --git a/examples/declarative/toys/tic-tac-toe/content/tic-tac-toe.js b/examples/declarative/toys/tic-tac-toe/content/tic-tac-toe.js
new file mode 100644
index 0000000..f8d6d9f
--- /dev/null
+++ b/examples/declarative/toys/tic-tac-toe/content/tic-tac-toe.js
@@ -0,0 +1,145 @@
+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
+}
+
diff --git a/examples/declarative/toys/tic-tac-toe/tic-tac-toe.qml b/examples/declarative/toys/tic-tac-toe/tic-tac-toe.qml
new file mode 100644
index 0000000..dd13052
--- /dev/null
+++ b/examples/declarative/toys/tic-tac-toe/tic-tac-toe.qml
@@ -0,0 +1,77 @@
+import Qt 4.7
+import "content"
+import "content/tic-tac-toe.js" as Logic
+
+Item {
+ id: game
+
+ property bool show: false
+ property real difficulty: 1.0 //chance it will actually think
+
+ width: 440
+ height: 480
+ anchors.fill: parent
+
+ Image {
+ id: boardimage
+ anchors { verticalCenter: parent.verticalCenter; 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 (!Logic.makeMove(index,"X"))
+ Logic.computerTurn()
+ }
+ }
+ }
+ }
+
+ Timer {
+ id: endtimer
+ interval: 1600
+ onTriggered: { msg.opacity = 0; Logic.restart() }
+ }
+ }
+
+ Row {
+ spacing: 4
+ anchors { top: board.bottom; 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
+
+ anchors.centerIn: parent
+ opacity: 0
+ color: "blue"
+ style: Text.Outline; styleColor: "white"
+ font.pixelSize: 50; font.bold: true
+ }
+}
diff --git a/examples/declarative/toys/tic-tac-toe/tic-tac-toe.qmlproject b/examples/declarative/toys/tic-tac-toe/tic-tac-toe.qmlproject
new file mode 100644
index 0000000..d4909f8
--- /dev/null
+++ b/examples/declarative/toys/tic-tac-toe/tic-tac-toe.qmlproject
@@ -0,0 +1,16 @@
+import QmlProject 1.0
+
+Project {
+ /* Include .qml, .js, and image files from current directory and subdirectories */
+ QmlFiles {
+ directory: "."
+ }
+ JavaScriptFiles {
+ directory: "."
+ }
+ ImageFiles {
+ directory: "."
+ }
+ /* List of plugin directories passed to QML runtime */
+ // importPaths: [ " ../exampleplugin " ]
+}