| 1 |
/* GTK - The GIMP Toolkit |
| 2 |
* Copyright(C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Library General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or(at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Library General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Library General Public |
| 15 |
* License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
#include <gtkmm.h> |
| 18 |
#include <gtkmm/grid.h> |
| 19 |
#include <gtkmm/togglebutton.h> |
| 20 |
#include "tictactoe.h" |
| 21 |
|
| 22 |
TicTacToe::TicTacToe() |
| 23 |
: Gtk::Box(Gtk::Orientation::VERTICAL) |
| 24 |
{ |
| 25 |
auto grid = Gtk::make_managed<Gtk::Grid>(); |
| 26 |
grid->set_row_homogeneous(true); |
| 27 |
grid->set_column_homogeneous(true); |
| 28 |
append(*grid); |
| 29 |
|
| 30 |
for(int i=0;i<3; i++) |
| 31 |
{ |
| 32 |
for(int j=0;j<3; j++) |
| 33 |
{ |
| 34 |
Gtk::ToggleButton* button = nullptr; |
| 35 |
buttons[i][j] = button = Gtk::make_managed<Gtk::ToggleButton>(); |
| 36 |
grid->attach(*button, i, j); |
| 37 |
button->signal_toggled().connect( sigc::bind( |
| 38 |
sigc::mem_fun(*this, &TicTacToe::toggle), |
| 39 |
button)); |
| 40 |
button->set_size_request(20, 20); |
| 41 |
} |
| 42 |
} |
| 43 |
clear(); |
| 44 |
} |
| 45 |
|
| 46 |
TicTacToe::~TicTacToe() |
| 47 |
{} |
| 48 |
|
| 49 |
void |
| 50 |
TicTacToe::clear() |
| 51 |
{ |
| 52 |
freeze(); |
| 53 |
|
| 54 |
for(int i=0; i<3; i++) |
| 55 |
for(int j=0; j<3; j++) |
| 56 |
buttons[i][j]->set_active(false); |
| 57 |
|
| 58 |
thaw(); |
| 59 |
} |
| 60 |
|
| 61 |
void |
| 62 |
TicTacToe::toggle(Gtk::ToggleButton *button) |
| 63 |
{ |
| 64 |
if(frozen) |
| 65 |
return; |
| 66 |
|
| 67 |
static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 }, |
| 68 |
{ 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 }, |
| 69 |
{ 0, 1, 2 }, { 0, 1, 2 } }; |
| 70 |
static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 }, |
| 71 |
{ 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 }, |
| 72 |
{ 0, 1, 2 }, { 2, 1, 0 } }; |
| 73 |
|
| 74 |
bool success, found; |
| 75 |
|
| 76 |
for(int k=0; k<8; k++) |
| 77 |
{ |
| 78 |
success = true; |
| 79 |
found = false; |
| 80 |
|
| 81 |
for(int i=0; i<3; i++) |
| 82 |
{ |
| 83 |
success &= buttons[rwins[k][i]][cwins[k][i]]->get_active(); |
| 84 |
found |= buttons[rwins[k][i]][cwins[k][i]] == button; |
| 85 |
} |
| 86 |
|
| 87 |
if(success && found) |
| 88 |
{ |
| 89 |
tictactoe(); |
| 90 |
break; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|