summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authormae <qt-info@nokia.com>2010-03-16 14:24:03 (GMT)
committermae <qt-info@nokia.com>2010-03-16 15:18:03 (GMT)
commit55a55426f24685400b7ab062b221f248ac159821 (patch)
tree122f6d63e1ab06f6dea2171ba3854e94e1242e0e /examples
parentdbcca9a809c9a5eb47f5c9236eb84c5baba7c560 (diff)
downloadQt-55a55426f24685400b7ab062b221f248ac159821.zip
Qt-55a55426f24685400b7ab062b221f248ac159821.tar.gz
Qt-55a55426f24685400b7ab062b221f248ac159821.tar.bz2
add alan's tic-tac-toe AI
Diffstat (limited to 'examples')
-rw-r--r--examples/declarative/tic-tac-toe/content/Button.qml35
-rw-r--r--examples/declarative/tic-tac-toe/content/pics/board.pngbin5524 -> 11208 bytes
-rw-r--r--examples/declarative/tic-tac-toe/tic-tac-toe.qml123
3 files changed, 151 insertions, 7 deletions
diff --git a/examples/declarative/tic-tac-toe/content/Button.qml b/examples/declarative/tic-tac-toe/content/Button.qml
new file mode 100644
index 0000000..cfc2f04
--- /dev/null
+++ b/examples/declarative/tic-tac-toe/content/Button.qml
@@ -0,0 +1,35 @@
+import Qt 4.6
+
+Rectangle {
+ id: container
+
+ signal clicked
+ property string text: "Button"
+ property bool down: false
+ property string mainCol: "lightgray"
+ property string darkCol: "darkgray"
+ property string lightCol: "white"
+
+ color: mainCol; smooth: true
+ width: txtItem.width + 20; height: txtItem.height + 6
+ border.width: 1; border.color: Qt.darker(mainCol); radius: 8;
+
+ gradient: Gradient {
+ GradientStop {
+ id: topGrad; position: 0.0
+ color: if (container.down) { darkCol } else { lightCol } }
+ GradientStop { position: 1.0; color: mainCol }
+ }
+
+ MouseArea { id: mr; anchors.fill: parent; onClicked: container.clicked() }
+
+ Text {
+ id: txtItem; text: container.text;
+ anchors.centerIn: container
+ color: "blue"
+ styleColor: "white"
+ style: Text.Outline
+ font.pixelSize: 14
+ font.bold: true
+ }
+}
diff --git a/examples/declarative/tic-tac-toe/content/pics/board.png b/examples/declarative/tic-tac-toe/content/pics/board.png
index cd85971..e7a7324 100644
--- a/examples/declarative/tic-tac-toe/content/pics/board.png
+++ b/examples/declarative/tic-tac-toe/content/pics/board.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
index 63ee483..ae187d2 100644
--- a/examples/declarative/tic-tac-toe/tic-tac-toe.qml
+++ b/examples/declarative/tic-tac-toe/tic-tac-toe.qml
@@ -2,11 +2,17 @@ import Qt 4.6
import "content"
Item {
- width: boardimage.width
- height: boardimage.height
+ 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"
}
@@ -31,7 +37,7 @@ Item {
}
Script {
- function winner()
+ function winner(board)
{
for (var i=0; i<3; ++i) {
if (board.children[i].state!=""
@@ -68,7 +74,7 @@ Item {
function makeMove(pos,player)
{
board.children[pos].state = player
- if (winner()) {
+ if (winner(board)) {
win(player + " wins")
return true
} else {
@@ -78,13 +84,95 @@ Item {
function computerTurn()
{
- // world's dumbest player
- for (var i=0; i<9; ++i)
+ 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()
+ }
+ 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)
@@ -102,6 +190,27 @@ Item {
}
}
+ 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