1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "tictactoe.h"
52 
53 #include <QMouseEvent>
54 #include <QPainter>
55 
defaultState()56 static inline QString defaultState() { return QStringLiteral("---------"); }
57 
TicTacToe(QWidget * parent)58 TicTacToe::TicTacToe(QWidget *parent)
59     : QWidget(parent)
60 {
61 }
62 
minimumSizeHint() const63 QSize TicTacToe::minimumSizeHint() const
64 {
65     return QSize(200, 200);
66 }
67 
sizeHint() const68 QSize TicTacToe::sizeHint() const
69 {
70     return QSize(200, 200);
71 }
72 
setState(const QString & newState)73 void TicTacToe::setState(const QString &newState)
74 {
75     turnNumber = 0;
76     myState = defaultState();
77     int position = 0;
78     while (position < 9 && position < newState.length()) {
79         QChar mark = newState.at(position);
80         if (mark == Cross || mark == Nought) {
81             ++turnNumber;
82             myState.replace(position, 1, mark);
83         }
84         position++;
85     }
86     update();
87 }
88 
state() const89 QString TicTacToe::state() const
90 {
91     return myState;
92 }
93 
clearBoard()94 void TicTacToe::clearBoard()
95 {
96     myState = defaultState();
97     turnNumber = 0;
98     update();
99 }
100 
mousePressEvent(QMouseEvent * event)101 void TicTacToe::mousePressEvent(QMouseEvent *event)
102 {
103     if (turnNumber == 9) {
104         clearBoard();
105         update();
106     } else {
107         for (int position = 0; position < 9; ++position) {
108             QRect cell = cellRect(position / 3, position % 3);
109             if (cell.contains(event->pos())) {
110                 if (myState.at(position) == Empty) {
111                     if (turnNumber % 2 == 0)
112                         myState.replace(position, 1, Cross);
113                     else
114                         myState.replace(position, 1, Nought);
115                     ++turnNumber;
116                     update();
117                 }
118             }
119         }
120     }
121 }
122 
paintEvent(QPaintEvent *)123 void TicTacToe::paintEvent(QPaintEvent * /* event */)
124 {
125     QPainter painter(this);
126     painter.setRenderHint(QPainter::Antialiasing);
127 
128     painter.setPen(QPen(Qt::darkGreen, 1));
129     painter.drawLine(cellWidth(), 0, cellWidth(), height());
130     painter.drawLine(2 * cellWidth(), 0, 2 * cellWidth(), height());
131     painter.drawLine(0, cellHeight(), width(), cellHeight());
132     painter.drawLine(0, 2 * cellHeight(), width(), 2 * cellHeight());
133 
134     painter.setPen(QPen(Qt::darkBlue, 2));
135 
136     for (int position = 0; position < 9; ++position) {
137         QRect cell = cellRect(position / 3, position % 3);
138 
139         if (myState.at(position) == Cross) {
140             painter.drawLine(cell.topLeft(), cell.bottomRight());
141             painter.drawLine(cell.topRight(), cell.bottomLeft());
142         } else if (myState.at(position) == Nought) {
143             painter.drawEllipse(cell);
144         }
145     }
146 
147     painter.setPen(QPen(Qt::yellow, 3));
148 
149     for (int position = 0; position < 9; position = position + 3) {
150         if (myState.at(position) != Empty
151                 && myState.at(position + 1) == myState.at(position)
152                 && myState.at(position + 2) == myState.at(position)) {
153             int y = cellRect((position / 3), 0).center().y();
154             painter.drawLine(0, y, width(), y);
155             turnNumber = 9;
156         }
157     }
158 
159     for (int position = 0; position < 3; ++position) {
160         if (myState.at(position) != Empty
161                 && myState.at(position + 3) == myState.at(position)
162                 && myState.at(position + 6) == myState.at(position)) {
163             int x = cellRect(0, position).center().x();
164             painter.drawLine(x, 0, x, height());
165             turnNumber = 9;
166         }
167     }
168     if (myState.at(0) != Empty && myState.at(4) == myState.at(0)
169             && myState.at(8) == myState.at(0)) {
170         painter.drawLine(0, 0, width(), height());
171         turnNumber = 9;
172     }
173     if (myState.at(2) != Empty && myState.at(4) == myState.at(2)
174             && myState.at(6) == myState.at(2)) {
175         painter.drawLine(0, height(), width(), 0);
176         turnNumber = 9;
177     }
178 }
179 
cellRect(int row,int column) const180 QRect TicTacToe::cellRect(int row, int column) const
181 {
182     const int HMargin = width() / 30;
183     const int VMargin = height() / 30;
184     return QRect(column * cellWidth() + HMargin,
185                  row * cellHeight() + VMargin,
186                  cellWidth() - 2 * HMargin,
187                  cellHeight() - 2 * VMargin);
188 }
189