first version: three types of field with randomness
authorSerge Ziryukin <ftrvxmtrx@gmail.com>
Wed, 31 Mar 2010 20:29:37 +0000 (23:29 +0300)
committerSerge Ziryukin <ftrvxmtrx@gmail.com>
Wed, 31 Mar 2010 20:29:37 +0000 (23:29 +0300)
colorflood/CMakeLists.txt [new file with mode: 0644]
colorflood/src/CMakeLists.txt [new file with mode: 0644]
colorflood/src/field.cpp [new file with mode: 0644]
colorflood/src/field.hpp [new file with mode: 0644]
colorflood/src/main.cpp [new file with mode: 0644]
colorflood/src/window.cpp [new file with mode: 0644]
colorflood/src/window.hpp [new file with mode: 0644]

diff --git a/colorflood/CMakeLists.txt b/colorflood/CMakeLists.txt
new file mode 100644 (file)
index 0000000..734a809
--- /dev/null
@@ -0,0 +1,14 @@
+# Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program 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 General Public License for more details.
+
+cmake_minimum_required(VERSION 2.6)
+project(colorflood)
+add_subdirectory(src)
diff --git a/colorflood/src/CMakeLists.txt b/colorflood/src/CMakeLists.txt
new file mode 100644 (file)
index 0000000..e3d2778
--- /dev/null
@@ -0,0 +1,30 @@
+# Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program 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 General Public License for more details.
+
+set(src
+  field.cpp
+  main.cpp
+  window.cpp
+  )
+
+set(moc
+  field.hpp
+  window.hpp
+  )
+
+include(FindQt4)
+find_package(Qt4 4.6.2 COMPONENTS QtCore QtGui REQUIRED)
+include(${QT_USE_FILE})
+
+QT4_WRAP_CPP(moc_src ${moc})
+
+add_executable(colorflood ${moc_src} ${src})
+target_link_libraries(colorflood ${QT_LIBRARIES})
diff --git a/colorflood/src/field.cpp b/colorflood/src/field.cpp
new file mode 100644 (file)
index 0000000..d47250a
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+  Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; version 2 of the License.
+
+  This program 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 General Public License for more details.
+*/
+
+#include <QtGui>
+#include "field.hpp"
+
+static const int fieldWidth = 420;
+
+const int Field::rects[Field::NUM_SIZES] = { 14, 21, 28 };
+const int Field::turns[Field::NUM_SIZES] = { 25, 35, 50 };
+
+Field::Field (QWidget *parent)
+    : QWidget (parent)
+{
+    // FIXME -- restore saved state
+}
+
+Field::Field (QWidget *parent, const QVector<QBrush> &brushes, FieldSize size)
+    : QWidget (parent)
+{
+    init(brushes, size);
+}
+
+Field::~Field ()
+{
+    // FIXME -- save state
+}
+
+Field::FieldSize Field::getSize () const
+{
+    return size;
+}
+
+/*
+=================
+randomize
+=================
+*/
+void Field::randomize ()
+{
+    Field::FieldRect rect;
+    rect.flood = false;
+
+    data.clear();
+    data = QVector<FieldRect> (rects[size] * rects[size], rect);
+
+    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
+
+    for (QVector<Field::FieldRect>::iterator rect = data.begin();
+         rect != data.end();
+         rect++)
+    {
+        (*rect).brush = qrand() % brushes.size();
+    }
+}
+
+int Field::getNumRectsOfSize (FieldSize size)
+{
+    return rects[size];
+}
+
+int Field::getNumTurnsOfSize (FieldSize size)
+{
+    return turns[size];
+}
+
+void Field::init (const QVector<QBrush> &brushes, FieldSize size)
+{
+    this->size = size;
+    this->brushes = brushes;
+
+    setFixedSize(fieldWidth, fieldWidth);
+    randomize();
+}
+
+int Field::getRectSize (FieldSize size)
+{
+    return fieldWidth / rects[size];
+}
+
+void Field::paintEvent (QPaintEvent *event)
+{
+    QPainter painter;
+    painter.begin(this);
+
+    QRect rect = QRect(0, 0, getRectSize(size), getRectSize(size));
+
+    for (int y = 0; y < rects[size] ;y++)
+    {
+        int n = y * rects[size];
+
+        for (int x = 0; x < rects[size] ;x++, n++)
+        {
+            rect.moveTo(x * rect.width(), y * rect.height());
+
+            if (rect.intersects(event->rect()))
+                painter.fillRect(rect, brushes.at(data[n].brush));
+        }
+    }
+
+    painter.end();
+}
diff --git a/colorflood/src/field.hpp b/colorflood/src/field.hpp
new file mode 100644 (file)
index 0000000..06ccf28
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+  Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; version 2 of the License.
+
+  This program 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 General Public License for more details.
+*/
+
+#ifndef _FIELD_HPP
+#define _FIELD_HPP
+
+#include <QWidget>
+#include <QVector>
+#include <QBrush>
+
+class QPaintEvent;
+
+class Field : public QWidget
+{
+    Q_OBJECT;
+
+public:
+    typedef enum
+    {
+        SIZE_SMALL = 0,
+        SIZE_NORMAL,
+        SIZE_LARGE,
+        NUM_SIZES
+    }FieldSize;
+
+    Field (QWidget *parent, const QVector<QBrush> &brushes, FieldSize size);
+    Field (QWidget *parent);
+    ~Field ();
+
+    FieldSize getSize () const;
+    void randomize ();
+
+    static int getNumRectsOfSize (FieldSize size);
+    static int getNumTurnsOfSize (FieldSize size);
+
+private:
+    typedef struct
+    {
+        char brush;
+        bool flood;
+    }FieldRect;
+
+    static const int rects[NUM_SIZES];
+    static const int turns[NUM_SIZES];
+
+    void init (const QVector<QBrush> &brushes, FieldSize size);
+    static int getRectSize (FieldSize size);
+
+    FieldSize          size;
+    QVector<QBrush>    brushes;
+    QVector<FieldRect> data;
+
+protected:
+    void paintEvent (QPaintEvent *event);
+};
+
+#endif // !_FIELD_HPP
diff --git a/colorflood/src/main.cpp b/colorflood/src/main.cpp
new file mode 100644 (file)
index 0000000..e2d7efd
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+  Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; version 2 of the License.
+
+  This program 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 General Public License for more details.
+*/
+
+#include <QApplication>
+#include "window.hpp"
+
+/*
+=================
+main
+=================
+*/
+int main (int argc, char **argv)
+{
+    QApplication app(argc, argv);
+
+    Window window;
+    window.show();
+
+    return app.exec();
+}
diff --git a/colorflood/src/window.cpp b/colorflood/src/window.cpp
new file mode 100644 (file)
index 0000000..0ee5554
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+  Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; version 2 of the License.
+
+  This program 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 General Public License for more details.
+*/
+
+#include <QtGui>
+#include "window.hpp"
+#include "field.hpp"
+
+Window::Window ()
+    : QWidget()
+{
+    setWindowTitle(tr("Color flood"));
+
+    setWindowState(windowState() | Qt::WindowFullScreen);
+
+    QVector<QBrush> brushes;
+
+#if 1
+    // standart color scheme
+    brushes << QBrush(QColor(0x00, 0x00, 0xff)); // blue
+    brushes << QBrush(QColor(0xff, 0x00, 0x00)); // red
+    brushes << QBrush(QColor(0x00, 0xff, 0x00)); // green
+    brushes << QBrush(QColor(0xff, 0xff, 0x00)); // yellow
+    brushes << QBrush(QColor(0xff, 0x00, 0xff)); // magenta
+    brushes << QBrush(QColor(0x80, 0x00, 0x80)); // purple
+#else
+    // color-blind color scheme
+    brushes << QBrush(QColor(0x00, 0x00, 0x00));
+    brushes << QBrush(QColor(0x31, 0x31, 0x31), Qt::Dense1Pattern);
+    brushes << QBrush(QColor(0x62, 0x62, 0x62), Qt::Dense3Pattern);
+    brushes << QBrush(QColor(0x93, 0x93, 0x93), Qt::CrossPattern);
+    brushes << QBrush(QColor(0xc4, 0xc4, 0xc4));
+    brushes << QBrush(QColor(0xff, 0xff, 0xff));
+#endif
+
+    field = new Field(this, brushes, Field::SIZE_LARGE);
+
+    QHBoxLayout *layout = new QHBoxLayout;
+    layout->addWidget(field);
+    layout->setAlignment(field, Qt::AlignRight);
+    setLayout(layout);
+}
diff --git a/colorflood/src/window.hpp b/colorflood/src/window.hpp
new file mode 100644 (file)
index 0000000..b76d9b1
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+  Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; version 2 of the License.
+
+  This program 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 General Public License for more details.
+*/
+
+#ifndef _WINDOW_HPP
+#define _WINDOW_HPP
+
+#include <QWidget>
+
+class Field;
+
+class Window : public QWidget
+{
+    Q_OBJECT;
+
+public:
+    Window ();
+
+private:
+    Field *field;
+};
+
+#endif // !_WINDOW_HPP