pacmanrb 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +46 -0
  4. data/app/components/board_component.rb +100 -0
  5. data/app/components/hud_component.rb +15 -0
  6. data/app/controllers/application_controller.rb +24 -0
  7. data/app/controllers/game_controller.rb +68 -0
  8. data/app/controllers/game_over_controller.rb +26 -0
  9. data/app/controllers/home_controller.rb +22 -0
  10. data/app/models/application_record.rb +7 -0
  11. data/app/models/high_score.rb +7 -0
  12. data/app/state/application_state.rb +6 -0
  13. data/app/state/game_over_state.rb +9 -0
  14. data/app/state/game_state.rb +9 -0
  15. data/app/state/home_state.rb +7 -0
  16. data/app/views/game/show_view.rb +32 -0
  17. data/app/views/game_over/show_view.rb +40 -0
  18. data/app/views/home/show_view.rb +42 -0
  19. data/app/views/layouts/application_layout.rb +34 -0
  20. data/config/database.rb +14 -0
  21. data/config/routes.rb +7 -0
  22. data/db/development.sqlite3 +0 -0
  23. data/db/migrate/20260719010011_create_high_scores.rb +11 -0
  24. data/db/schema.rb +20 -0
  25. data/db/seeds.rb +1 -0
  26. data/db/test.sqlite3 +0 -0
  27. data/exe/pacmanrb +7 -0
  28. data/lib/pacman/application.rb +13 -0
  29. data/lib/pacman/arcade/brains.rb +46 -0
  30. data/lib/pacman/arcade/direction.rb +19 -0
  31. data/lib/pacman/arcade/ghost.rb +72 -0
  32. data/lib/pacman/arcade/layouts.rb +28 -0
  33. data/lib/pacman/arcade/maze.rb +67 -0
  34. data/lib/pacman/arcade/mode_schedule.rb +52 -0
  35. data/lib/pacman/arcade/navigator.rb +36 -0
  36. data/lib/pacman/arcade/pellet_field.rb +39 -0
  37. data/lib/pacman/arcade/player.rb +42 -0
  38. data/lib/pacman/arcade/position.rb +15 -0
  39. data/lib/pacman/arcade/scoreboard.rb +43 -0
  40. data/lib/pacman/arcade/spawn.rb +8 -0
  41. data/lib/pacman/arcade/world.rb +169 -0
  42. data/lib/pacman/board_scale.rb +42 -0
  43. data/lib/pacman/sprites.rb +145 -0
  44. data/lib/pacman/version.rb +5 -0
  45. data/lib/pacman.rb +22 -0
  46. metadata +127 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0e685b8744fb557883d2cef49049c68e4b3763a423e5cf91914291943a038d8a
4
+ data.tar.gz: d5b6e7fe11076ea9a64c2ce67787cfdc010bf60d26cb1683e1e9ede949e37396
5
+ SHA512:
6
+ metadata.gz: ebb661277be30360ba2d00e4276c1296e263c4b068bbe32199b72c6ec526dd4815f44bb3d17d324eb33c95719bf4d0e0272d5cc3b0e5885de5be5d6341bb7ad3
7
+ data.tar.gz: 777249d1f917118c266e73e1ea69b24e0fa9f092938cb469fa17c9c1c61a207dc8675352d6effcdda534aa3a7bf63ea566a66a7c6621c2511cb7568792180be5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pando
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Pacman
2
+
3
+ A Pacman arcade clone built with the [Charming](https://github.com/pandorocks/charming)
4
+ terminal UI framework.
5
+
6
+ ## Screenshots
7
+ <img width="1256" height="1065" alt="image" src="https://github.com/user-attachments/assets/72cc474b-f54d-40aa-b622-6e203251e0ca" />
8
+
9
+
10
+ Run it with:
11
+
12
+ ```sh
13
+ bundle exec pacmanrb
14
+ ```
15
+
16
+ ## How to play
17
+
18
+ - **Enter** on the title screen starts a game.
19
+ - Steer with the **arrow keys** or **WASD** — turns are buffered and taken at the
20
+ next opening, like the arcade.
21
+ - Eat every pellet to clear the level; levels get faster. Power pellets — drawn as cherries —
22
+ frighten the ghosts — they turn blue and dithered — so you can eat them for
23
+ combo bonuses (200/400/800/1600); eaten ghosts race home as floating eyes.
24
+ - The board scales to your terminal. At larger sizes the actors are drawn as
25
+ half-block sprites: Pac-Man is a circle whose mouth points where he's headed,
26
+ ghosts get their classic domed-and-scalloped shape.
27
+ - You have 3 lives. Game over shows the top-5 high score table (persisted in
28
+ SQLite via `db/development.sqlite3`).
29
+ - **r**/**Enter** restarts from the game over screen; **q** quits anywhere;
30
+ **ctrl+p** opens the command palette.
31
+
32
+ The four ghosts have distinct brains: one chases you, one ambushes four tiles
33
+ ahead, one wanders randomly, one patrols its corner. They alternate between
34
+ scatter and chase on a timer.
35
+
36
+ ## Development
37
+
38
+ ```sh
39
+ bundle exec rspec # full suite (game logic + controllers + views + integration)
40
+ CHARMING_ENV=test bundle exec charming db:migrate # prepare the test database
41
+ ```
42
+
43
+ Game rules live in plain Ruby under `lib/pacman/arcade/` (no TTY needed to test
44
+ them); the Charming app layer (controllers/views/components) is under `app/`.
45
+ Live game state is held in memory per run — it intentionally does not survive a
46
+ session's JSON serialization; only high scores persist.
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ # Draws the maze as a grid of blocks. Each maze cell becomes `scale` terminal
5
+ # rows by `scale * 2` columns, so the board can grow to fill large terminals.
6
+ class BoardComponent < Charming::Component
7
+ WALL_STYLE = Charming::UI.style.foreground("#2121de")
8
+ PELLET_STYLE = Charming::UI.style.foreground("#ffb8ae")
9
+ CHERRY_STYLE = Charming::UI.style.foreground("#ff2121").bold
10
+ STEM_STYLE = Charming::UI.style.foreground("#21c821")
11
+ PLAYER_STYLE = Charming::UI.style.foreground("#ffff00").bold
12
+ FRIGHTENED_STYLE = Charming::UI.style.foreground("#2121de").bold
13
+ EATEN_STYLE = Charming::UI.style.foreground("#ffffff")
14
+ GHOST_STYLES = [
15
+ Charming::UI.style.foreground("#ff0000").bold,
16
+ Charming::UI.style.foreground("#ffb8de").bold,
17
+ Charming::UI.style.foreground("#00ffde").bold,
18
+ Charming::UI.style.foreground("#ffb847").bold
19
+ ].freeze
20
+
21
+ def render
22
+ (0...maze.height).flat_map { |row| block_rows(row) }.join("\n")
23
+ end
24
+
25
+ private
26
+
27
+ def maze
28
+ world.maze
29
+ end
30
+
31
+ def scale
32
+ assigns.fetch(:scale, 1)
33
+ end
34
+
35
+ def cell_width
36
+ scale * 2
37
+ end
38
+
39
+ def block_rows(row)
40
+ (0...scale).map do |subrow|
41
+ (0...maze.width).map { |col| cell(Arcade::Position.new(row: row, col: col), subrow) }.join
42
+ end
43
+ end
44
+
45
+ def cell(position, subrow)
46
+ return ghost_cell(position, subrow) if ghost_at(position)
47
+ return PLAYER_STYLE.render(player_sprite[subrow]) if world.player.position == position
48
+ return WALL_STYLE.render("#" * cell_width) if maze.wall?(position)
49
+ return cherry_cell(subrow) if world.pellets.power?(position)
50
+ return pellet_cell(position, subrow) if middle?(subrow)
51
+
52
+ blank
53
+ end
54
+
55
+ def player_sprite
56
+ @player_sprite ||= Sprites.player(scale: scale, direction: world.player.direction)
57
+ end
58
+
59
+ def pellet_cell(position, _subrow)
60
+ return PELLET_STYLE.render(".".center(cell_width)) if world.pellets.include?(position)
61
+
62
+ blank
63
+ end
64
+
65
+ def cherry_cell(subrow)
66
+ Sprites.cherry(scale: scale)[subrow].chars.map { |char| cherry_char(char) }.join
67
+ end
68
+
69
+ def cherry_char(char)
70
+ return char if char == " "
71
+ return STEM_STYLE.render(char) if char == "│"
72
+
73
+ CHERRY_STYLE.render(char)
74
+ end
75
+
76
+ def middle?(subrow)
77
+ subrow == (scale - 1) / 2
78
+ end
79
+
80
+ def blank
81
+ " " * cell_width
82
+ end
83
+
84
+ def ghost_cell(position, subrow)
85
+ ghost = ghost_at(position)
86
+ return FRIGHTENED_STYLE.render(Sprites.frightened(scale: scale)[subrow]) if ghost.edible?
87
+ return EATEN_STYLE.render(Sprites.eaten(scale: scale)[subrow]) if ghost.eaten?
88
+
89
+ ghost_style(ghost).render(Sprites.ghost(scale: scale)[subrow])
90
+ end
91
+
92
+ def ghost_at(position)
93
+ world.ghosts.find { |ghost| ghost.position == position }
94
+ end
95
+
96
+ def ghost_style(ghost)
97
+ GHOST_STYLES.fetch(world.ghosts.index(ghost) % GHOST_STYLES.length)
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class HudComponent < Charming::Component
5
+ def render
6
+ row(
7
+ text("SCORE #{world.score}", style: theme.title),
8
+ text("LIVES #{world.lives}", style: theme.warn),
9
+ text("LEVEL #{world.level}", style: theme.info),
10
+ text("PELLETS #{world.pellets.remaining}", style: theme.muted),
11
+ gap: 4
12
+ )
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class ApplicationController < Charming::Controller
5
+ layout Layouts::ApplicationLayout
6
+
7
+ key "ctrl+p", :open_command_palette, scope: :global
8
+ key "q", :quit, scope: :global
9
+
10
+ command "Home" do
11
+ navigate_to "/"
12
+ end
13
+
14
+ command "Theme", :open_theme_palette
15
+ command "Close palette", :close_command_palette
16
+ command "Quit app", :quit
17
+ command "Game" do
18
+ navigate_to "/game"
19
+ end
20
+ command "GameOver" do
21
+ navigate_to "/game_over"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class GameController < ApplicationController
5
+ timer :tick, every: 0.12, action: :advance
6
+
7
+ key "up", :turn_up, scope: :global
8
+ key "down", :turn_down, scope: :global
9
+ key "left", :turn_left, scope: :global
10
+ key "right", :turn_right, scope: :global
11
+ key "w", :turn_up, scope: :global
12
+ key "s", :turn_down, scope: :global
13
+ key "a", :turn_left, scope: :global
14
+ key "d", :turn_right, scope: :global
15
+
16
+ def show
17
+ render :show, world: world, palette: command_palette
18
+ end
19
+
20
+ def advance
21
+ return unless movement_pulse?
22
+
23
+ world.tick
24
+ return finish_game if world.over?
25
+
26
+ show
27
+ end
28
+
29
+ def turn_up = turn(Arcade::Direction.up)
30
+
31
+ def turn_down = turn(Arcade::Direction.down)
32
+
33
+ def turn_left = turn(Arcade::Direction.left)
34
+
35
+ def turn_right = turn(Arcade::Direction.right)
36
+
37
+ private
38
+
39
+ # Skips timer pulses at high zoom so on-screen speed stays playable.
40
+ def movement_pulse?
41
+ game.pulse += 1
42
+ pace = BoardScale.new(screen: screen, maze: world.maze).pace
43
+ (game.pulse % pace).zero?
44
+ end
45
+
46
+ def finish_game
47
+ summary = state(:game_over, GameOverState)
48
+ summary.score = world.score
49
+ summary.level = world.level
50
+ HighScore.create!(name: "PLAYER", score: world.score)
51
+ game.world = nil
52
+ navigate_to "/game_over"
53
+ end
54
+
55
+ def turn(direction)
56
+ world.queue_turn(direction)
57
+ show
58
+ end
59
+
60
+ def world
61
+ game.world ||= Arcade::World.classic
62
+ end
63
+
64
+ def game
65
+ state(:game, GameState)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class GameOverController < ApplicationController
5
+ key "r", :restart, scope: :global
6
+ key "enter", :restart, scope: :global
7
+
8
+ def show
9
+ render :show,
10
+ game_over: game_over,
11
+ high_scores: HighScore.top(5).to_a,
12
+ palette: command_palette
13
+ end
14
+
15
+ def restart
16
+ state(:game, GameState).world = nil
17
+ navigate_to "/game"
18
+ end
19
+
20
+ private
21
+
22
+ def game_over
23
+ state(:game_over, GameOverState)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class HomeController < ApplicationController
5
+ key "enter", :start_game, scope: :global
6
+ key "space", :start_game, scope: :global
7
+
8
+ def show
9
+ render :show, home: home, palette: command_palette
10
+ end
11
+
12
+ def start_game
13
+ navigate_to "/game"
14
+ end
15
+
16
+ private
17
+
18
+ def home
19
+ state(:home, HomeState)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class ApplicationRecord < ActiveRecord::Base
5
+ self.abstract_class = true
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class HighScore < ApplicationRecord
5
+ scope :top, ->(count) { order(score: :desc).limit(count) }
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class ApplicationState < Charming::ApplicationState
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class GameOverState < ApplicationState
5
+ attribute :title, :string, default: "GameOver"
6
+ attribute :score, :integer, default: 0
7
+ attribute :level, :integer, default: 1
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class GameState < ApplicationState
5
+ attribute :title, :string, default: "Game"
6
+ attribute :world
7
+ attribute :pulse, :integer, default: 0
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class HomeState < ApplicationState
5
+ attribute :title, :string, default: "Pacman"
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ module Game
5
+ class ShowView < Charming::View
6
+ def render
7
+ return cramped_notice if board_scale.cramped?
8
+
9
+ Charming::UI.center(
10
+ column(
11
+ render_component(HudComponent.new(world: world)),
12
+ render_component(BoardComponent.new(world: world, scale: board_scale.factor)),
13
+ gap: 1
14
+ ),
15
+ width: layout_screen.width,
16
+ height: layout_screen.height
17
+ )
18
+ end
19
+
20
+ private
21
+
22
+ def board_scale
23
+ @board_scale ||= BoardScale.new(screen: layout_screen, maze: world.maze)
24
+ end
25
+
26
+ def cramped_notice
27
+ text "Terminal too small — need at least " \
28
+ "#{world.maze.width * 2}x#{world.maze.height + BoardScale::HUD_ROWS}.", style: theme.warn
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ module GameOver
5
+ class ShowView < Charming::View
6
+ def render
7
+ Charming::UI.center(
8
+ column(banner, summary, high_score_table, hint, gap: 1),
9
+ width: layout_screen.width,
10
+ height: layout_screen.height
11
+ )
12
+ end
13
+
14
+ private
15
+
16
+ def banner
17
+ text "GAME OVER", style: theme.title.bold
18
+ end
19
+
20
+ def high_score_table
21
+ return "" if high_scores.empty?
22
+
23
+ column(
24
+ text("HIGH SCORES", style: theme.title),
25
+ *high_scores.map.with_index(1) do |entry, rank|
26
+ text("#{rank}. #{entry.name} #{entry.score}", style: theme.info)
27
+ end
28
+ )
29
+ end
30
+
31
+ def summary
32
+ text "Final score #{game_over.score} · reached level #{game_over.level}", style: theme.info
33
+ end
34
+
35
+ def hint
36
+ text "Press r or Enter to play again · q to quit", style: theme.muted
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ module Home
5
+ class ShowView < Charming::View
6
+ LOGO = <<~ART
7
+ ██████╗ █████╗ ██████╗ ███╗ ███╗ █████╗ ███╗ ██╗
8
+ ██╔══██╗██╔══██╗██╔════╝█████╗████╗ ████║██╔══██╗████╗ ██║
9
+ ██████╔╝███████║██║ ╚════╝██╔████╔██║███████║██╔██╗ ██║
10
+ ██╔═══╝ ██╔══██║██║ ██║╚██╔╝██║██╔══██║██║╚██╗██║
11
+ ██║ ██║ ██║╚██████╗ ██║ ╚═╝ ██║██║ ██║██║ ╚████║
12
+ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝
13
+ ART
14
+
15
+ def render
16
+ Charming::UI.center(
17
+ column(logo_block, subtitle, start_hint, controls_hint, gap: 1),
18
+ width: layout_screen.width,
19
+ height: layout_screen.height
20
+ )
21
+ end
22
+
23
+ private
24
+
25
+ def logo_block
26
+ text LOGO.chomp, style: theme.title
27
+ end
28
+
29
+ def subtitle
30
+ text "PAC-MAN — a Charming arcade clone", style: theme.info
31
+ end
32
+
33
+ def start_hint
34
+ text "Press Enter to start", style: theme.title.bold
35
+ end
36
+
37
+ def controls_hint
38
+ text "Arrows or WASD to steer · q to quit", style: theme.muted
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ module Layouts
5
+ # Full-bleed layout: the game owns the whole terminal. No sidebar, no
6
+ # borders — just the screen content with the command palette overlaid.
7
+ class ApplicationLayout < Charming::View
8
+ def render
9
+ screen_layout(background: theme.background) do
10
+ pane(:content, grow: 1) do
11
+ yield_content
12
+ end
13
+
14
+ overlay command_palette_modal if command_palette_modal
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def palette_component
21
+ assigns.fetch(:palette, nil)
22
+ end
23
+
24
+ def command_palette_modal
25
+ return unless palette_component
26
+
27
+ render_component Charming::Components::CommandPaletteModal.new(
28
+ content: palette_component,
29
+ theme: theme
30
+ )
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "fileutils"
5
+
6
+ # The database file is selected by CHARMING_ENV (development, test, production).
7
+ environment = ENV["CHARMING_ENV"] || "development"
8
+ database_path = File.expand_path("../db/#{environment}.sqlite3", __dir__)
9
+ FileUtils.mkdir_p(File.dirname(database_path))
10
+
11
+ ActiveRecord::Base.establish_connection(
12
+ adapter: "sqlite3",
13
+ database: database_path
14
+ )
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ Pacman::Application.routes do
4
+ root "home#show"
5
+ screen "/game", to: "game#show", title: "Game"
6
+ screen "/game_over", to: "game_over#show", title: "GameOver"
7
+ end
Binary file
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateHighScores < ActiveRecord::Migration[8.1]
4
+ def change
5
+ create_table :high_scores do |t|
6
+ t.string :name
7
+ t.integer :score
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
data/db/schema.rb ADDED
@@ -0,0 +1,20 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # This file is the source Rails uses to define your schema when running `bin/rails
6
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
7
+ # be faster and is potentially less error prone than running all of your
8
+ # migrations from scratch. Old migrations may fail to apply correctly if those
9
+ # migrations use external dependencies or application code.
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema[8.1].define(version: 2026_07_19_010011) do
14
+ create_table "high_scores", force: :cascade do |t|
15
+ t.datetime "created_at", null: false
16
+ t.string "name"
17
+ t.integer "score"
18
+ t.datetime "updated_at", null: false
19
+ end
20
+ end
data/db/seeds.rb ADDED
@@ -0,0 +1 @@
1
+ # frozen_string_literal: true
data/db/test.sqlite3 ADDED
Binary file
data/exe/pacmanrb ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "pacman"
6
+
7
+ Charming.run(Pacman::Application.new)
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ class Application < Charming::Application
5
+ root File.expand_path("../..", __dir__)
6
+
7
+ Charming::UI::Theme.built_in_names.each do |theme_name|
8
+ theme theme_name.to_sym, built_in: theme_name
9
+ end
10
+
11
+ default_theme :phosphor
12
+ end
13
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pacman
4
+ module Arcade
5
+ # Each brain answers one question: where should this ghost head right now?
6
+ module Brains
7
+ class Chase
8
+ def target(world)
9
+ world.player.position
10
+ end
11
+ end
12
+
13
+ class Ambush
14
+ LOOKAHEAD = 4
15
+
16
+ def target(world)
17
+ player = world.player
18
+ LOOKAHEAD.times.reduce(player.position) { |spot, _| spot.step(player.direction) }
19
+ end
20
+ end
21
+
22
+ class Corner
23
+ def initialize(corner:)
24
+ @corner = corner
25
+ end
26
+
27
+ def target(_world)
28
+ @corner
29
+ end
30
+ end
31
+
32
+ class Wander
33
+ def initialize(rng:)
34
+ @rng = rng
35
+ end
36
+
37
+ def target(world)
38
+ Position.new(
39
+ row: @rng.rand(world.maze.height),
40
+ col: @rng.rand(world.maze.width)
41
+ )
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end