author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
||
44 |
#include "tictactoe.h" |
|
45 |
||
46 |
TicTacToe::TicTacToe(QWidget *parent) |
|
47 |
: QWidget(parent) |
|
48 |
{ |
|
49 |
} |
|
50 |
||
51 |
QSize TicTacToe::minimumSizeHint() const |
|
52 |
{ |
|
53 |
return QSize(200, 200); |
|
54 |
} |
|
55 |
||
56 |
QSize TicTacToe::sizeHint() const |
|
57 |
{ |
|
58 |
return QSize(200, 200); |
|
59 |
} |
|
60 |
||
61 |
void TicTacToe::setState(const QString &newState) |
|
62 |
{ |
|
63 |
turnNumber = 0; |
|
64 |
myState = "---------"; |
|
65 |
int position = 0; |
|
66 |
while (position < 9 && position < newState.length()) { |
|
67 |
QChar mark = newState.at(position); |
|
68 |
if (mark == Cross || mark == Nought) { |
|
69 |
++turnNumber; |
|
70 |
myState.replace(position, 1, mark); |
|
71 |
} |
|
72 |
position++; |
|
73 |
} |
|
74 |
update(); |
|
75 |
} |
|
76 |
||
77 |
QString TicTacToe::state() const |
|
78 |
{ |
|
79 |
return myState; |
|
80 |
} |
|
81 |
||
82 |
void TicTacToe::clearBoard() |
|
83 |
{ |
|
84 |
myState = "---------"; |
|
85 |
turnNumber = 0; |
|
86 |
update(); |
|
87 |
} |
|
88 |
||
89 |
void TicTacToe::mousePressEvent(QMouseEvent *event) |
|
90 |
{ |
|
91 |
if (turnNumber == 9) { |
|
92 |
clearBoard(); |
|
93 |
update(); |
|
94 |
} else { |
|
95 |
for (int position = 0; position < 9; ++position) { |
|
96 |
QRect cell = cellRect(position / 3, position % 3); |
|
97 |
if (cell.contains(event->pos())) { |
|
98 |
if (myState.at(position) == Empty) { |
|
99 |
if (turnNumber % 2 == 0) |
|
100 |
myState.replace(position, 1, Cross); |
|
101 |
else |
|
102 |
myState.replace(position, 1, Nought); |
|
103 |
++turnNumber; |
|
104 |
update(); |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
void TicTacToe::paintEvent(QPaintEvent * /* event */) |
|
112 |
{ |
|
113 |
QPainter painter(this); |
|
114 |
painter.setRenderHint(QPainter::Antialiasing); |
|
115 |
||
116 |
painter.setPen(QPen(Qt::darkGreen, 1)); |
|
117 |
painter.drawLine(cellWidth(), 0, cellWidth(), height()); |
|
118 |
painter.drawLine(2 * cellWidth(), 0, 2 * cellWidth(), height()); |
|
119 |
painter.drawLine(0, cellHeight(), width(), cellHeight()); |
|
120 |
painter.drawLine(0, 2 * cellHeight(), width(), 2 * cellHeight()); |
|
121 |
||
122 |
painter.setPen(QPen(Qt::darkBlue, 2)); |
|
123 |
||
124 |
for (int position = 0; position < 9; ++position) { |
|
125 |
QRect cell = cellRect(position / 3, position % 3); |
|
126 |
||
127 |
if (myState.at(position) == Cross) { |
|
128 |
painter.drawLine(cell.topLeft(), cell.bottomRight()); |
|
129 |
painter.drawLine(cell.topRight(), cell.bottomLeft()); |
|
130 |
} else if (myState.at(position) == Nought) { |
|
131 |
painter.drawEllipse(cell); |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
painter.setPen(QPen(Qt::yellow, 3)); |
|
136 |
||
137 |
for (int position = 0; position < 9; position = position + 3) { |
|
138 |
if (myState.at(position) != Empty |
|
139 |
&& myState.at(position + 1) == myState.at(position) |
|
140 |
&& myState.at(position + 2) == myState.at(position)) { |
|
141 |
int y = cellRect((position / 3), 0).center().y(); |
|
142 |
painter.drawLine(0, y, width(), y); |
|
143 |
turnNumber = 9; |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
for (int position = 0; position < 3; ++position) { |
|
148 |
if (myState.at(position) != Empty |
|
149 |
&& myState.at(position + 3) == myState.at(position) |
|
150 |
&& myState.at(position + 6) == myState.at(position)) { |
|
151 |
int x = cellRect(0, position).center().x(); |
|
152 |
painter.drawLine(x, 0, x, height()); |
|
153 |
turnNumber = 9; |
|
154 |
} |
|
155 |
} |
|
156 |
if (myState.at(0) != Empty && myState.at(4) == myState.at(0) |
|
157 |
&& myState.at(8) == myState.at(0)) { |
|
158 |
painter.drawLine(0, 0, width(), height()); |
|
159 |
turnNumber = 9; |
|
160 |
} |
|
161 |
if (myState.at(2) != Empty && myState.at(4) == myState.at(2) |
|
162 |
&& myState.at(6) == myState.at(2)) { |
|
163 |
painter.drawLine(0, height(), width(), 0); |
|
164 |
turnNumber = 9; |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
QRect TicTacToe::cellRect(int row, int column) const |
|
169 |
{ |
|
170 |
const int HMargin = width() / 30; |
|
171 |
const int VMargin = height() / 30; |
|
172 |
return QRect(column * cellWidth() + HMargin, |
|
173 |
row * cellHeight() + VMargin, |
|
174 |
cellWidth() - 2 * HMargin, |
|
175 |
cellHeight() - 2 * VMargin); |
|
176 |
} |