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 8125673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:53:38+00:00 2026-06-06T06:53:38+00:00

Could you provide an example of how one would represent a player class with

  • 0

Could you provide an example of how one would represent a “player” class with mutable properties such as HP and position (3d vector), functions such as init, setters and getters?

  • 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-06-06T06:53:41+00:00Added an answer on June 6, 2026 at 6:53 am

    I agree with mikera, that you should try to do it immutably, but you ask specifically for mutable properties, so the way I might suggest is very similar to mikera’s answer, but to use atoms inside the map where you will have mutable properties you may want to change.

    (def player1
      {:type       :player
       :team       :red
       :hit-points (atom 10)
       :location   (atom [17 9 6])})
    

    Notice that only the things you may want to change are wrapped in atoms. In order to access the mutable data you would have to dereference it, like so:

    @(player1 :hit-points)
    10
    

    In order to set the value you could use swap! or reset!, like so:

    (swap! (player1 :hit-points) dec)
    9
    @(player1 :hit-points)
    9
    (reset! (player1 :hit-points) 2)
    2
    @(player1 :hit-points)
    2
    

    This would be an example for you making one player, although you asked for something like an init, a getter, and a setter. I should say at this point that I have almost no programming experience outside of Clojure, so I may not have a complete grasp of what those would be, but here’s how I would set it up.

    (defn new-player
      [hit-points location]
      {:type       :player
       :team       :red
       :hit-points (atom hit-points)
       :location   (atom location)})
    

    Then when I wanted to make a new player, I would do:

    (def my-player
      (new-player 20 [0 0 0]))
    {:type       :player
     :team       :red
     :hit-points #<Atom@1959415: 20>
     :location   #<Atom@12d0e49: [0 0 0]>}
    

    I think it would unnecessary to make explicit “getters” and/or “setters”, because you could simply get any mutable data by dereferencing it, and set any mutable data with swap! or reset! in exactly the same way I showed above. Having said that, if you wanted to, you could do something like this:

    (defn get-hp
      [player]
      @(player :hit-points))
    (defn get-loc
      [player]
      @(player :location))
    (defn set-hp
      [player new-hp]
      (reset! (player :hit-points) new-hp))
    (defn set-loc
      [player new-loc]
      (reset! (player :location) new-loc))
    

    Now with these you could do:

    (get-hp my-player)
    20
    (get-loc my-player)
    [0 0 0]
    (set-hp my-player 17)
    17
    (get-hp my-player)
    17
    (set-loc my-player [0 1 1])
    [0 1 1]
    (get-loc my-player)
    [0 1 1]
    

    Since this answer isn’t already long enough, I think it might be nice to include default values when making new players. I can think of an easy, but not necessarily elegant way to do this:

    (defn new-player
      ([]
       (new-player 20 [0 0 0]))
      ([hp-or-loc]
        (cond
          (integer? hp-or-loc) (new-player hp-or-loc [0 0 0])
          (vector? hp-or-loc)  (new-player 20 hp-or-loc)
          :else                (throw (Exception. "Value must be either integer for hp or vector for location."))))
      ([hp loc]
       {:type       :player
        :team       :red
        :hit-points (atom hp)
        :location   (atom loc)}))
    

    Now by default, new players will have 20 hp and be at location [0 0 0]. If passed either an integer or a vector, it will assume that should be the value for hp or location (respectively), or else it will throw an exception.

    Again, I think that mutable data would probably be unnecessary in most cases, and the simplest solution may be to conceive of the problem not as “how can a make this mutable data structure” but rather “how can I create new, updated versions of immutable data, then pass these data back to the beginning of a loop, wherein afterwards I may update and recur again”.

    Hopefully some of this is helpful.

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

Sidebar

Related Questions

I would like to know if someone could provide example or a link on
Could some one provide an example use of fastcall for use with gcc? If
I was wondering if anyone could provide an example of how to take a
Could someone provide an example for drawing graphics without using Windows Forms? I have
Could someone provide some example on implementing SEH in VB6? Everything I've seen so
Could someone provide an example of the usefulness of the jal instruction can how
Could anyone provide working example in C++ with event handling for Skype4COM, i.e. incoming
Could someone provide a simple example using getopt.pas with short and long command line
I could really use an example of this. Can anyone provide me with a
http://docs.oracle.com/javase/1.5.0/docs/guide/jpda/trace.html Could anyone please provide me couple of examples of how to use Java

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.