Tutorial |
||
|
||
|
||
|
#ifndef
TICTACTOE_H #define TICTACTOE_H . . . #endif // TICTACTOE_H |
#ifndef
TICTACTOE_H #define TICTACTOE_H #ifndef INTI_GTK_BOX_H #include <inti/gtk/box.h> #endif #ifndef INTI_SIGNALS_H #include <inti/signals.h> #endif namespace Inti { namespace Gtk { class ToggleButton; } } // namespace Inti /* Tictactoe */ class Tictactoe : public Inti::Gtk::VBox { Tictactoe(const Tictactoe&); Tictactoe& operator=(const Tictactoe&); Inti::Gtk::ToggleButton *buttons[3][3]; void on_toggle(Inti::Gtk::ToggleButton *button); public: // Constructors Tictactoe(); virtual ~Tictactoe(); // Methods void clear(); // Signals Inti::Signal0<void> tictactoe_signal; }; #endif // TICTACTOE_H |
tictactoe_signal.emit(); // or tictactoe_signal(); |
#include"tictactoe.h" #include <inti/gtk/table.h> #include <inti/gtk/togglebutton.h> #include <inti/bind.h> using namespace Inti; /* Tictactoe */ Tictactoe::Tictactoe() { Gtk::Table *table = new Gtk::Table(3, 3, true); add(*table); table->show(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { buttons[i][j] = new Gtk::ToggleButton; table->attach(*buttons[i][j], i, i + 1, j, j + 1); buttons[i][j]->sig_toggled().connect(bind(slot(this, &Tictactoe::on_toggle), buttons[i][j])); buttons[i][j]->set_size_request(20, 20); buttons[i][j]->show(); } } } Tictactoe::~Tictactoe() { } void Tictactoe::clear() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { buttons[i][j]->set_active(false); } } } void Tictactoe::on_toggle(Gtk::ToggleButton *button) { 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_signal.emit(); break; } } } |
#include<inti/main.h> #include <inti/core.h> #include <iostream> #include "tictactoe.h" using namespace Inti; class TictactoeTest : public Gtk::Window { Tictactoe *ttt; protected: void on_win(); public: TictactoeTest(); virtual ~TictactoeTest(); }; TictactoeTest::TictactoeTest() { set_title("Tictactoe"); set_border_width(10); ttt = new Tictactoe; add(*ttt); ttt->tictactoe_signal.connect(slot(this, &TictactoeTest::on_win)); ttt->show(); } TictactoeTest::~TictactoeTest() { } void TictactoeTest::on_win() { using namespace std; cout << "Yay, you won!" << endl; ttt->clear(); } INTI_MAIN(TictactoeTest) |
|
|||
|
|||