source: trunk/examples/declarative/toys/tic-tac-toe/content/tic-tac-toe.js@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 3.5 KB
Line 
1function winner(board)
2{
3 for (var i=0; i<3; ++i) {
4 if (board.children[i].state != ""
5 && board.children[i].state == board.children[i+3].state
6 && board.children[i].state == board.children[i+6].state)
7 return true
8
9 if (board.children[i*3].state != ""
10 && board.children[i*3].state == board.children[i*3+1].state
11 && board.children[i*3].state == board.children[i*3+2].state)
12 return true
13 }
14
15 if (board.children[0].state != ""
16 && board.children[0].state == board.children[4].state != ""
17 && board.children[0].state == board.children[8].state != "")
18 return true
19
20 if (board.children[2].state != ""
21 && board.children[2].state == board.children[4].state != ""
22 && board.children[2].state == board.children[6].state != "")
23 return true
24
25 return false
26}
27
28function restartGame()
29{
30 game.running = true
31
32 for (var i=0; i<9; ++i)
33 board.children[i].state = ""
34}
35
36function makeMove(pos, player)
37{
38 board.children[pos].state = player
39 if (winner(board)) {
40 gameFinished(player + " wins")
41 return true
42 } else {
43 return false
44 }
45}
46
47function canPlayAtPos(pos)
48{
49 return board.children[pos].state == ""
50}
51
52function computerTurn()
53{
54 var r = Math.random();
55 if (r < game.difficulty)
56 smartAI();
57 else
58 randomAI();
59}
60
61function smartAI()
62{
63 function boardCopy(a) {
64 var ret = new Object;
65 ret.children = new Array(9);
66 for (var i = 0; i<9; i++) {
67 ret.children[i] = new Object;
68 ret.children[i].state = a.children[i].state;
69 }
70 return ret;
71 }
72
73 for (var i=0; i<9; i++) {
74 var simpleBoard = boardCopy(board);
75 if (canPlayAtPos(i)) {
76 simpleBoard.children[i].state = "O";
77 if (winner(simpleBoard)) {
78 makeMove(i, "O")
79 return
80 }
81 }
82 }
83 for (var i=0; i<9; i++) {
84 var simpleBoard = boardCopy(board);
85 if (canPlayAtPos(i)) {
86 simpleBoard.children[i].state = "X";
87 if (winner(simpleBoard)) {
88 makeMove(i, "O")
89 return
90 }
91 }
92 }
93
94 function thwart(a,b,c) { //If they are at a, try b or c
95 if (board.children[a].state == "X") {
96 if (canPlayAtPos(b)) {
97 makeMove(b, "O")
98 return true
99 } else if (canPlayAtPos(c)) {
100 makeMove(c, "O")
101 return true
102 }
103 }
104 return false;
105 }
106
107 if (thwart(4,0,2)) return;
108 if (thwart(0,4,3)) return;
109 if (thwart(2,4,1)) return;
110 if (thwart(6,4,7)) return;
111 if (thwart(8,4,5)) return;
112 if (thwart(1,4,2)) return;
113 if (thwart(3,4,0)) return;
114 if (thwart(5,4,8)) return;
115 if (thwart(7,4,6)) return;
116
117 for (var i =0; i<9; i++) {
118 if (canPlayAtPos(i)) {
119 makeMove(i, "O")
120 return
121 }
122 }
123 restartGame();
124}
125
126function randomAI()
127{
128 var unfilledPosns = new Array();
129
130 for (var i=0; i<9; ++i) {
131 if (canPlayAtPos(i))
132 unfilledPosns.push(i);
133 }
134
135 if (unfilledPosns.length == 0) {
136 restartGame();
137 } else {
138 var choice = unfilledPosns[Math.floor(Math.random() * unfilledPosns.length)];
139 makeMove(choice, "O");
140 }
141}
142
143function gameFinished(message)
144{
145 messageDisplay.text = message
146 messageDisplay.visible = true
147 game.running = false
148}
149
Note: See TracBrowser for help on using the repository browser.