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
56static inline QString defaultState() { return QStringLiteral("---------"); }
57
58TicTacToe::TicTacToe(QWidget *parent)
59 : QWidget(parent)
60{
61}
62
63QSize TicTacToe::minimumSizeHint() const
64{
65 return QSize(200, 200);
66}
67
68QSize TicTacToe::sizeHint() const
69{
70 return QSize(200, 200);
71}
72
73void 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(i: position);
80 if (mark == Cross || mark == Nought) {
81 ++turnNumber;
82 myState.replace(i: position, len: 1, after: mark);
83 }
84 position++;
85 }
86 update();
87}
88
89QString TicTacToe::state() const
90{
91 return myState;
92}
93
94void TicTacToe::clearBoard()
95{
96 myState = defaultState();
97 turnNumber = 0;
98 update();
99}
100
101void 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(row: position / 3, col: position % 3);
109 if (cell.contains(p: event->pos())) {
110 if (myState.at(i: position) == Empty) {
111 if (turnNumber % 2 == 0)
112 myState.replace(i: position, len: 1, after: Cross);
113 else
114 myState.replace(i: position, len: 1, after: Nought);
115 ++turnNumber;
116 update();
117 }
118 }
119 }
120 }
121}
122
123void TicTacToe::paintEvent(QPaintEvent * /* event */)
124{
125 QPainter painter(this);
126 painter.setRenderHint(hint: QPainter::Antialiasing);
127
128 painter.setPen(QPen(Qt::darkGreen, 1));
129 painter.drawLine(x1: cellWidth(), y1: 0, x2: cellWidth(), y2: height());
130 painter.drawLine(x1: 2 * cellWidth(), y1: 0, x2: 2 * cellWidth(), y2: height());
131 painter.drawLine(x1: 0, y1: cellHeight(), x2: width(), y2: cellHeight());
132 painter.drawLine(x1: 0, y1: 2 * cellHeight(), x2: width(), y2: 2 * cellHeight());
133
134 painter.setPen(QPen(Qt::darkBlue, 2));
135
136 for (int position = 0; position < 9; ++position) {
137 QRect cell = cellRect(row: position / 3, col: position % 3);
138
139 if (myState.at(i: position) == Cross) {
140 painter.drawLine(p1: cell.topLeft(), p2: cell.bottomRight());
141 painter.drawLine(p1: cell.topRight(), p2: cell.bottomLeft());
142 } else if (myState.at(i: position) == Nought) {
143 painter.drawEllipse(r: 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(i: position) != Empty
151 && myState.at(i: position + 1) == myState.at(i: position)
152 && myState.at(i: position + 2) == myState.at(i: position)) {
153 int y = cellRect(row: (position / 3), col: 0).center().y();
154 painter.drawLine(x1: 0, y1: y, x2: width(), y2: y);
155 turnNumber = 9;
156 }
157 }
158
159 for (int position = 0; position < 3; ++position) {
160 if (myState.at(i: position) != Empty
161 && myState.at(i: position + 3) == myState.at(i: position)
162 && myState.at(i: position + 6) == myState.at(i: position)) {
163 int x = cellRect(row: 0, col: position).center().x();
164 painter.drawLine(x1: x, y1: 0, x2: x, y2: height());
165 turnNumber = 9;
166 }
167 }
168 if (myState.at(i: 0) != Empty && myState.at(i: 4) == myState.at(i: 0)
169 && myState.at(i: 8) == myState.at(i: 0)) {
170 painter.drawLine(x1: 0, y1: 0, x2: width(), y2: height());
171 turnNumber = 9;
172 }
173 if (myState.at(i: 2) != Empty && myState.at(i: 4) == myState.at(i: 2)
174 && myState.at(i: 6) == myState.at(i: 2)) {
175 painter.drawLine(x1: 0, y1: height(), x2: width(), y2: 0);
176 turnNumber = 9;
177 }
178}
179
180QRect 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

source code of qttools/examples/designer/taskmenuextension/tictactoe.cpp