diff options
Diffstat (limited to 'examples/declarative/tic-tac-toe/tic-tac-toe.qml')
-rw-r--r-- | examples/declarative/tic-tac-toe/tic-tac-toe.qml | 123 |
1 files changed, 116 insertions, 7 deletions
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 |