A simple implementation of a TicTacToe game in DCI

43 views
Skip to first unread message

Samuel Abiassi

unread,
May 25, 2023, 6:33:05 AMMay 25
to object-composition
Hi there!

Trailing on the path formed by Andreas through his posts on DCI for Typescript, I tried to implement a little pet project to explore the DCI space using Typescript.

I tried to implement a TicTacToe game, using my own mental model of it (which is worth what it is) and I feel like I might have done a decent enough job to present it here.


This implementation follows an interesting conversation I had with Andreas (thanks a lot) which made me switch from a model where the players move where aknowledge by events emitted from the board to "request" made through the context public API


 I tried to address both the role unbinding and the name collision:
- by using symbols scoped to the Context (which I also used to document the behaviour)
- by implementing a role-binder function (https://gitlab.com/SamuelAbiassi/tictactoe-with-dci/-/blob/master/src/role-binder/role.ts?ref_type=heads) which lets me unbind at will

This is still an exploration work in progress but I'd definetly appreciate some feedback and guidance on the matter.

James O Coplien

unread,
May 25, 2023, 7:20:45 AMMay 25
to object-co...@googlegroups.com
I had a look at this and had a hard time getting into it deeply, mainly because I lack a development environment for the technology. I also couldn’t easily sort what was what in terms of Contexts, classes, objects, Roles, and so forth.

I do have one strategic question to start with.

Old-fashioned OO implements polymorphism at method-time binding with message-selector-to-method mapping in Smalltallk and virtual functions in C++.

DCI implements polymorphism with slightly earlier binding: the time of binding an object to its referent identifier, represented as a Role. DCI seems really to make sense if multiple different classes of actors can interchangeably play the same role (a rough definition of polymorphism).

I have a tough time figuring out what the roles would be for Tic-Tac-Toe, such that we can find objects of two (or hopefully more) classes than can stand in for a given role/object binding in the program. So, to start with: what are the Contexts (I can envision just one) and the Roles (I have a hard time envisioning more than three)?

--
You received this message because you are subscribed to the Google Groups "object-composition" group.
To unsubscribe from this group and stop receiving emails from it, send an email to object-composit...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/3014b669-249a-4cbd-b0ee-1cd79e507323n%40googlegroups.com.

Samuel Abiassi

unread,
May 25, 2023, 8:14:07 AMMay 25
to object-composition
In term of Context, there is basically only one "true" use-case, which is "playing a game". The other ones I have implemented ("sorting out if there is a winner" and "checking for a draw") are only some exploring thing to try my hand on "sub-context", but they don't really make much sense in practice.

In term of Roles, I've got a player1 and player2, the board, the game (as in game-state which is a mental model players commonly talk about), and an info space to display whatever needs to be displayed, such as who's turn is it, who won, and errors like invalid moves.

I've then used the context in 3 exemples, one basic with a displayed board and two humans players possibly playing. The second is the same board, but one of the player is an AI. The third one is kind of an abstraction of the UI as if the two player were playing remotely (I used a form to simulate the possible http requests)

Samuel Abiassi

unread,
May 25, 2023, 8:26:32 AMMay 25
to object-composition
Thinking about it, the "game" role feels like it does not make much sense and is maybe just a state of the context. I'm not sure how to tackle this.

Matthew Browne

unread,
May 25, 2023, 11:01:05 PMMay 25
to object-co...@googlegroups.com, Samuel Abiassi

Would it maybe make sense to invent a role in this case that doesn't necessarily exist in real life? e.g. GameOfficiant or GameMaster. I suppose there could actually be such a person in real life if you wanted someone to keep an eye on both players and make sure they're playing by the rules, haha. I think that in some games (just speaking generally here and not about DCI in particular), that role has been referred to as just "the computer".

I'm not so sure about the RestartButton role; it seems that the only role-specific behavior it has is announcing the game start, which could be done by just calling the announceGameStart method of the InfoSpace role that you already have.

You mentioned that player1 and player2 are roles, but I don't see them as roles in the code...?

Cheers,
Matt

James O Coplien

unread,
May 26, 2023, 5:53:41 AMMay 26
to object-co...@googlegroups.com
This seems exceptionally reasonable.

Samuel Abiassi

unread,
May 27, 2023, 7:00:18 AMMay 27
to object-composition
Funnilly enough, that's what I did in my 'subcontexts', introducing a Judge role. It only exists on those subcontexts and do not exist elsewhere on the system.

The point you made about the restart button is absolutely fair.

As for the player1 and player2 role, this was my biggest problem: right now, the two have no methods bounded to their role, and still, they are quite obviously present in my mental model of the context. So my question would be: should a Role always come with methods attached to it, or can a role be also a contextualization of the object in the particular context it is used on?

Matthew Browne

unread,
May 27, 2023, 7:17:42 AMMay 27
to object-co...@googlegroups.com
Regarding the Game role, that might be a fine role, I was just suggesting an alternative idea to consider. If we consider 'Game' to be a sort of orchestrator of the game as a whole, and that makes sense as a role in your mental model, then I think it works. Often it makes sense for objects that might be passive in real life to become more active in DCI (it's like the game itself is becoming sentient haha - we see this even in many of the money transfer examples where the source account role transfers money out of itself).

As for Player1 and Player2, in theory it's allowed to have roles with no role methods (in some of the money transfer examples, there's an Amount role with no methods). But I wonder if some of the behavior currently being played by one of the other roles would be more naturally played by Player1 or Player2. I'd have to think more about it but it's something to consider.


James O Coplien

unread,
May 27, 2023, 9:24:23 AMMay 27
to object-co...@googlegroups.com


On 27 May 2023, at 06.00, Samuel Abiassi <samuel....@gmail.com> wrote:

As for the player1 and player2 role, this was my biggest problem: right now, the two have no methods bounded to their role, and still, they are quite obviously present in my mental model of the context. So my question would be: should a Role always come with methods attached to it, or can a role be also a contextualization of the object in the particular context it is used on?

Now we’re coming to the interesting questions.

My mental model could well be formulated so that a Player role has a method called “evaluate threats” and another “select best current square.” I don’t know where you have this logic now. The game doesn’t decide, and the Xs and Os don’t individually decide.

Nonetheless, if your mental model ends up with these being methodless, I think that may be O.K. In my formulation I call the Stage Props..

Samuel Abiassi

unread,
May 28, 2023, 4:27:01 AMMay 28
to object-composition
"My mental model could well be formulated so that a Player role has a method called “evaluate threats” and another “select best current square.” I don’t know where you have this logic now. The game doesn’t decide, and the Xs and Os don’t individually decide."

I get what you're saying but I've got a question regarding this aspect. What you evoke is something I would (and actually did) implement for an AI player. However, I find it un-instinctive to "code" human behaviour into the system. In one hand they are obviously part of the use case, but in another hand, what you suggested in your exemples  (“evaluate threats” and another “select best current square.”) are expressing a strong intent to "win' the game, which may not be what the players are actually going for. You might purposefully want to play the worst move possible (for eduational purpose or just for the """fun""" of it). How would you approach such concern?

Reply all
Reply to author
Forward
0 new messages