Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4016876
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:47:36+00:00 2026-05-20T09:47:36+00:00

I’m trying to create a basic Pacman game in C++ (I’ll use Java syntax

  • 0

I’m trying to create a basic Pacman game in C++ (I’ll use Java syntax in this question as this is somewhat easier to demonstrate), but I can’t find a good design option.

So far I have 4 classes:
– Monster: Can be subclassed for monster-specific behaviour and contains all logic for the monsters
– Player: Contains player-logic
– Map: Contains a 2d array representing the map. This array specifies which positions are walls or Pacman food
– Game: contains a Player, a Map and a list of Monsters.

To keep it simple:

public class Game {
  Player player;
  Map map;
  ArrayList<Monster> monsters;

  public Game() {
    player = new Player();
    map = new Map();
    monsters = new ArrayList<Monster();
    monsters.add(new ScaryMonster());
    monsters.add(new DumpMonster());
  }

  public void update() {
    player.update();
    map.update();

    for (Monster monster: monsters) {
      monster.update();
    }

  public void draw() {
    map.draw();
    player.draw();

    for (Monster monster: monsters) {
      monster.draw();
  }
}

So all I have to do now is to create a Game object and call update() and draw() on it every time. Very simple. But it doesn’t work.

Assume I call update() on the player-object and the player (which is the Pacman ofcourse) hits food. In that case, the map-object should get notified of this (and the position) to remove the food from the 2d-array. Assume the player kills a monster, the position of the monster should get changed (the Monster class has a “position field”). And you can imagine a lot more of these situations.

An option would be to pass the map and monster object as parameters in the update() and draw() method of the player object. And to pass the player and monster objects as parameters in the method calls of map. But that surely doesn’t sound like a good OO design.

What’s a good OO way to solve this? I was thinking about using the Observer pattern (so Game is the subject, player, map and monsters are observers), but that doesn’t make any sense: that way the observers will have to let the subject know of any changes, which is obviously not the correct way of using this.

Any tips would be very welcome.

Thank you very much 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:47:36+00:00Added an answer on May 20, 2026 at 9:47 am

    Why don’t you try mapping actions?

    Every action has a reaction. So let’s say the pacman hits food. That’s an action, “hitting food”, which in turn has a reaction (notifying the map, or the food, or whatever you like) that the food is not there any longer.

    Now imagine the pacman hits a monster, that’s another action… what would be the reaction to that? Well it might cause the monster to get dead (a call to the BeDeath method :P) or it could cause the pacman to get death… whatever it is it allow you to chain actions to reactions in the game.

    That means the logic of the game, the rules would be in the game class, who in addition already knows all the elements needed and can communicate with each one.

    Edit: A simple example (very simple, as the game gets more complex you’ll need to think better about actions and reactions structure)

    public void IGameInfo
    {
      List<Monster> Monsters {get;}
      Pacman Pacman {get;}
      Map Map {get;}
    }
    
    public void ComputeReactions()
    {
      foreach (actionChecker in Actions)
      {
        actionChecker.Check(gameInfo);
      }
    }
    
    public void ComputeDotEaten(IGameInfo gameInfo)
    {
      foreach (dot in gameInfo.Map.Dots)
        if (pacman.location == dot.location)
          dot.MarkEaten();
    }
    
    public void ComputeMonsterEaten(IGameInfo gameInfo)
    {
      foreach (Monster in gameInfo.monsters)
        if (gameInfo.pacman.location == gameInfo.monster.location &&
            gameInfo.pacman.Invulnerable)
          monster.MarkDeath();
        else
          Game.EndGame();
    }
    

    Or if you like you could also map the reactions

    public void ComputeDotEaten(IGameInfo gameInfo)
    {
      foreach (dot in gameInfo.Map.Dots)
        if (pacman.location == dot.location)
          Reactions["DotEaten"].Execute(dot);
    }
    

    Note that for that to work all you reactions must share a common signature (i.e, taking an array of objects that are cast to the expected parameters)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.