File: tictactoe.cc

package info (click to toggle)
gtkmm-documentation 3.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 21,572 kB
  • sloc: cpp: 14,508; sh: 1,249; makefile: 1,042; xml: 104; perl: 64
file content (99 lines) | stat: -rw-r--r-- 2,580 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* example-start tictactoe tictactoe.c */

/* GTK - The GIMP Toolkit
 * Copyright(C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or(at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <gtkmm.h>
#include <gtkmm/grid.h>
#include <gtkmm/togglebutton.h>
#include "tictactoe.h"

TicTacToe::TicTacToe()
: Gtk::Box(Gtk::ORIENTATION_VERTICAL)
{
  auto grid = Gtk::make_managed<Gtk::Grid>();
  grid->set_row_homogeneous(true);
  grid->set_column_homogeneous(true);
  add( *grid);

  for(int i=0;i<3; i++)
  {
    for(int j=0;j<3; j++)
    {
      Gtk::ToggleButton* button = nullptr;
      buttons[i][j] = button = Gtk::make_managed<Gtk::ToggleButton>();
      grid->attach(*button, i, j, 1, 1);
      button->signal_toggled().connect( sigc::bind(
                                     sigc::mem_fun(*this, &TicTacToe::toggle),
                                     button));
      button->set_size_request(20, 20);
    }
  }

  grid->show_all();
}

TicTacToe::~TicTacToe()
{}

void
TicTacToe::clear()
{
  freeze();

  for(int i=0; i<3; i++)
    for(int j=0; j<3; j++)
      buttons[i][j]->set_active(false);

  thaw();
}

void
TicTacToe::toggle(Gtk::ToggleButton *button)
{
  if(frozen)
    return;

  static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
                 { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
                 { 0, 1, 2 }, { 0, 1, 2 } };
  static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
                 { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
                 { 0, 1, 2 }, { 2, 1, 0 } };

  bool success, found;

  for(int k=0; k<8; k++)
  {
    success = true;
    found = false;

    for(int i=0; i<3; i++)
    {
      success &= buttons[rwins[k][i]][cwins[k][i]]->get_active();
      found |= buttons[rwins[k][i]][cwins[k][i]] == button;
    }

    if(success && found)
    {
      tictactoe();
      break;
    }
  }
}