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