summaryrefslogtreecommitdiff
path: root/Userland/Games/Conway
diff options
context:
space:
mode:
authorAndres Crucitti <dasc495@gmail.com>2021-04-11 18:46:58 -0700
committerLinus Groh <mail@linusgroh.de>2021-05-15 17:44:21 +0100
commitd99991e39c30e44a4a1200107782a1e4b0ea8cb6 (patch)
tree9e7c14311c1879c21aa03a2d1aad6fbcf6765871 /Userland/Games/Conway
parente4f61c6f284d78e442d2e63b6fa83aa3f741072e (diff)
downloadserenity-d99991e39c30e44a4a1200107782a1e4b0ea8cb6.zip
Games: Add GameOfLife
This patch introduces a new game based on Conway's Game of Life.
Diffstat (limited to 'Userland/Games/Conway')
-rw-r--r--Userland/Games/Conway/CMakeLists.txt7
-rw-r--r--Userland/Games/Conway/Game.cpp163
-rw-r--r--Userland/Games/Conway/Game.h41
-rw-r--r--Userland/Games/Conway/main.cpp72
4 files changed, 0 insertions, 283 deletions
diff --git a/Userland/Games/Conway/CMakeLists.txt b/Userland/Games/Conway/CMakeLists.txt
deleted file mode 100644
index 18312c363a..0000000000
--- a/Userland/Games/Conway/CMakeLists.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-set(SOURCES
- main.cpp
- Game.cpp
-)
-
-serenity_app(Conway ICON app-conway)
-target_link_libraries(Conway LibGUI)
diff --git a/Userland/Games/Conway/Game.cpp b/Userland/Games/Conway/Game.cpp
deleted file mode 100644
index 054bbde4e6..0000000000
--- a/Userland/Games/Conway/Game.cpp
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
- * Copyright (c) 2021, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include "Game.h"
-#include <AK/Random.h>
-#include <LibGUI/Painter.h>
-#include <stdlib.h>
-#include <time.h>
-
-Game::Game()
-{
- reset();
-}
-
-Game::~Game()
-{
-}
-
-void Game::reset()
-{
- stop_timer();
- seed_universe();
- start_timer(m_sleep);
- update();
-}
-
-void Game::seed_universe()
-{
- for (int y = 0; y < m_rows; y++) {
- for (int x = 0; x < m_columns; x++) {
- m_universe[y][x] = (get_random<u32>() % 2) ? 1 : 0;
- }
- }
-}
-
-void Game::update_universe()
-{
- bool new_universe[m_rows][m_columns];
-
- for (int y = 0; y < m_rows; y++) {
- for (int x = 0; x < m_columns; x++) {
- int n = 0;
- auto cell = m_universe[y][x];
-
- for (int y1 = y - 1; y1 <= y + 1; y1++) {
- for (int x1 = x - 1; x1 <= x + 1; x1++) {
- if (m_universe[(y1 + m_rows) % m_rows][(x1 + m_columns) % m_columns]) {
- n++;
- }
- }
- }
-
- if (cell)
- n--;
-
- if (n == 3 || (n == 2 && cell))
- new_universe[y][x] = true;
- else
- new_universe[y][x] = false;
- }
- }
-
- for (int y = 0; y < m_rows; y++) {
- for (int x = 0; x < m_columns; x++) {
- m_universe[y][x] = new_universe[y][x];
- }
- }
-}
-
-void Game::timer_event(Core::TimerEvent&)
-{
- update_universe();
- update();
-}
-
-Gfx::IntRect Game::first_cell_rect() const
-{
- auto game_rect = rect();
- auto cell_size = Gfx::IntSize(game_rect.width() / m_columns, game_rect.height() / m_rows);
- auto x_margin = (game_rect.width() - (cell_size.width() * m_columns)) / 2;
- auto y_margin = (game_rect.height() - (cell_size.height() * m_rows)) / 2;
- return { x_margin, y_margin, cell_size.width(), cell_size.height() };
-}
-
-void Game::paint_event(GUI::PaintEvent& event)
-{
- GUI::Painter painter(*this);
- painter.add_clip_rect(event.rect());
- painter.fill_rect(event.rect(), m_dead_color);
- auto first_rect = first_cell_rect();
-
- for (int y = 0; y < m_rows; y++) {
- for (int x = 0; x < m_columns; x++) {
- Gfx::IntRect rect {
- x * first_rect.width() + first_rect.left(),
- y * first_rect.height() + first_rect.top(),
- first_rect.width(),
- first_rect.height()
- };
- painter.fill_rect(rect, m_universe[y][x] ? m_alive_color : m_dead_color);
- }
- }
-}
-
-void Game::mousedown_event(GUI::MouseEvent& event)
-{
- switch (event.button()) {
- case GUI::MouseButton::Left:
- case GUI::MouseButton::Right:
- m_last_button = event.button();
- break;
- default:
- return;
- }
- interact_at(event.position());
-}
-
-void Game::mouseup_event(GUI::MouseEvent& event)
-{
- if (event.button() == m_last_button)
- m_last_button = GUI::MouseButton::None;
-}
-
-void Game::mousemove_event(GUI::MouseEvent& event)
-{
- interact_at(event.position());
-}
-
-void Game::interact_at(const Gfx::IntPoint& point)
-{
- if (m_last_button == GUI::MouseButton::None)
- return;
-
- auto first_rect = first_cell_rect();
- // Too tiny window, we don't actually display anything.
- if (first_rect.width() == 0 || first_rect.height() == 0)
- return;
-
- // Too far left/up.
- if (point.x() < first_rect.left() || point.y() < first_rect.top())
- return;
-
- int cell_x = (point.x() - first_rect.left()) / first_rect.width();
- int cell_y = (point.y() - first_rect.top()) / first_rect.height();
-
- // Too far right/down.
- if (cell_x >= m_columns || cell_y >= m_rows)
- return;
-
- switch (m_last_button) {
- case GUI::MouseButton::Left:
- m_universe[cell_y][cell_x] = true;
- break;
- case GUI::MouseButton::Right:
- m_universe[cell_y][cell_x] = false;
- break;
- default:
- VERIFY_NOT_REACHED();
- }
-}
diff --git a/Userland/Games/Conway/Game.h b/Userland/Games/Conway/Game.h
deleted file mode 100644
index 634f92cbf7..0000000000
--- a/Userland/Games/Conway/Game.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2021, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibGUI/Widget.h>
-
-class Game : public GUI::Widget {
- C_OBJECT(Game)
-public:
- virtual ~Game() override;
- void reset();
-
- int rows() const { return m_rows; };
- int columns() const { return m_columns; };
-
-private:
- Game();
- virtual void paint_event(GUI::PaintEvent&) override;
- virtual void timer_event(Core::TimerEvent&) override;
- virtual void mousedown_event(GUI::MouseEvent&) override;
- virtual void mouseup_event(GUI::MouseEvent&) override;
- virtual void mousemove_event(GUI::MouseEvent&) override;
-
- Gfx::IntRect first_cell_rect() const;
- void seed_universe();
- void update_universe();
- void interact_at(const Gfx::IntPoint&);
-
- const Gfx::Color m_alive_color { Color::Green };
- const Gfx::Color m_dead_color { Color::Black };
- const int m_rows { 200 };
- const int m_columns { 200 };
- const int m_sleep { 100 };
- GUI::MouseButton m_last_button { GUI::MouseButton::None };
-
- bool m_universe[200][200];
-};
diff --git a/Userland/Games/Conway/main.cpp b/Userland/Games/Conway/main.cpp
deleted file mode 100644
index dc4325dfc7..0000000000
--- a/Userland/Games/Conway/main.cpp
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright (c) 2021, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include "Game.h"
-#include <LibGUI/Application.h>
-#include <LibGUI/Icon.h>
-#include <LibGUI/Menu.h>
-#include <LibGUI/Menubar.h>
-#include <LibGUI/Window.h>
-#include <stdio.h>
-#include <unistd.h>
-
-int main(int argc, char** argv)
-{
- if (pledge("stdio rpath wpath cpath recvfd sendfd cpath unix", nullptr) < 0) {
- perror("pledge");
- return 1;
- }
-
- auto app = GUI::Application::construct(argc, argv);
-
- if (pledge("stdio rpath recvfd sendfd", nullptr) < 0) {
- perror("pledge");
- return 1;
- }
-
- if (unveil("/res", "r") < 0) {
- perror("unveil");
- return 1;
- }
-
- if (unveil(nullptr, nullptr) < 0) {
- perror("unveil");
- return 1;
- }
-
- auto app_icon = GUI::Icon::default_icon("app-conway");
-
- auto window = GUI::Window::construct();
-
- window->set_title("Conway");
- window->resize(400, 400);
- window->set_double_buffering_enabled(true);
- window->set_icon(app_icon.bitmap_for_size(16));
-
- auto& game = window->set_main_widget<Game>();
- window->set_minimum_size(game.columns(), game.rows());
-
- auto menubar = GUI::Menubar::construct();
-
- auto& game_menu = menubar->add_menu("&Game");
-
- game_menu.add_action(GUI::Action::create("&Reset", { Mod_None, Key_F2 }, [&](auto&) {
- game.reset();
- }));
- game_menu.add_separator();
- game_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
- GUI::Application::the()->quit();
- }));
-
- auto& help_menu = menubar->add_menu("&Help");
- help_menu.add_action(GUI::CommonActions::make_about_action("Conway", app_icon, window));
-
- window->set_menubar(move(menubar));
-
- window->show();
-
- return app->exec();
-}