Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (686)
Games in Android Showcase (198)
games submitted by our members
Games in WIP (758)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Game Logic where?  (Read 2867 times)
0 Members and 1 Guest are viewing this topic.
Offline DavidW

Junior Devvie

Medals: 3
Exp: 7 years

« Posted 2012-03-21 05:58:45 »

As I said in my last thread about sorting, I am working on a card game.  I guess I need a little advice about how to organize the game logic.  In the game, players take turns making a move.  Only certain moves are available to you on your turn depending on the current state of the game.  Occasionally, things happen automatically.

I am not sure where to put all this game logic from an OO point of view.  My original plan was to have each player object have a askForMove() method, which will return a Move enum.  Depending on what the player decided to do, different things happen.  If the player hasn't decided on  a move yet, it just returns a "NONE" type, and the loop goes around again.  This requires a lot of error checking because the player may request a move which isn't actually available.  So I have a MoveEvaluater class which decides if a requested move is legal.  If it is legal then the MoveEvaluater gives the thumbs up to the Table class to do whatever move was requested.

My question is, is this actually the best way to organize all this?  Anyone else have any experience with turned based card games?

I'd like to eventually write an AI which will play the game.  So I am worried about implementing this later on.

Offline 65K
« Reply #1 - Posted 2012-03-21 08:24:23 »

If it's turn based, how could there be undecided players ?

A a multiplayer game is probably physically distributed, you will always need to check any input from the players.
So, some kind of rule engine could list possible moves for the current player (useful for the UI), but then always check if move requests are valid (and the client wasn't hacked).
I wouldn't put it into the player class, but in separate rule logic class.
Offline Damocles
« Reply #2 - Posted 2012-03-21 08:52:06 »

The core part of any card-game is the rule system. Wich determines what is allowed to happen.
Since its roundbased it can be represented as a simple satemachine.

state 1:
prepare game
set state=2

state 2:
round open, choose fist player
set state=3

state 3:
wait for players action
if action received: evaluate action if possible -> check against "rule" engine
if valid:  set state = 4;
if not valid: notice player that action is illegal,  

state 4:
do action,  

when gameround ends  (one player won), set state = 1 (to reset the game)
else choose next plaxer ,  
set state = 3;

(of course there can be many more special states)

-> anyhow, the round waits as long until the current player in the round gives a valid action.

-> you can also give out a list of possible actions to the player and draw  it on the gui.
But its not really needed.
Lets say you have a game like solitair. There is a rule that you can place cards only at specific position.
But the player can pick up any card and move it.
But when releasing the mouse-drag of the card, the Game checks the position against the rulebase.
If its not valid, the card will simply "slide back" to the startposition.
-> the round only advances when the action was valid against the rulebase.


Once you have an AI you need to provide the AI with some enumaration of the possible current actions.
(The AI will probably try to traverse though a tree of possible actions to choose the best one, for that you need some
filter for valid actions)

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DavidW

Junior Devvie

Medals: 3
Exp: 7 years

« Reply #3 - Posted 2012-03-22 02:33:25 »

If it's turn based, how could there be undecided players ?

The game updates 30 times a second.  An undecided player is just someone who is still thinking of a move.  While waiting, the screen is animating (like a bounding arrow over the current player's picture).  Once the player decides, then the game does the actual move.

Thanks for the suggestions.  I was trying to implement the state machine already, but it was a little confusing.  I guess I need to just try harder!

Offline Damocles
« Reply #4 - Posted 2012-03-22 07:00:34 »

The core points of a statemachine are
#1 a current state
#2 a (finite) number of states it can be in
#3 rules how to move from one state to the next

You can use switch-case or if-else if contruct to actually run the statemachine

int state=STATE_INIT;

//run every gameframe by the gameloop
                     ...initialise game...
                     ...choose player, setup something...
                     //the statemachine remains in this state until there is a valid move
                     if( ..some playercommand stored...) { ... check if valid ...  when yes, state=STATE_DO_MOVE; break;}  
            some animations on idle time...
                  ...keep in this state until moveanimations are finished
                  if(animation_frames>100) state=STATE_UPDATE_BOARD;

...more states....


-> you can first note down wich possible state the game can be in, and how to move from one to the next.
But its not hard to extend the statemachine later by just adding new special states or substates

Offline ChexWithRaisins

Senior Newbie

Projects: 1

« Reply #5 - Posted 2012-04-01 00:36:23 »

Why does it have to keep asking for move?  Why can't you just have it update once the move is received?

Help Beta test SphereCCG!
Offline DavidW

Junior Devvie

Medals: 3
Exp: 7 years

« Reply #6 - Posted 2012-04-01 01:01:18 »

Why does it have to keep asking for move?  Why can't you just have it update once the move is received?
I update the screen every frame (to do animation) so it seemed easier to poll for input every frame as well.

Offline ChexWithRaisins

Senior Newbie

Projects: 1

« Reply #7 - Posted 2012-04-01 01:07:00 »

Maybe make an object that saves all of the pertinent game info and poll that when you redraw.  Once a player makes a move you can update the game object.

Help Beta test SphereCCG!
Offline DavidW

Junior Devvie

Medals: 3
Exp: 7 years

« Reply #8 - Posted 2012-04-01 01:50:00 »

That's basically what I have been trying to do, but I haven't really had time to work on the project for a while.  If I have an afternoon and get something working I'll post what I ended up doing in this thread.  My main issue was that I wasn't sure if I should pull information from the player, or have the player push information toward the game.  Not sure if that makes sense at all....

Offline ChexWithRaisins

Senior Newbie

Projects: 1

« Reply #9 - Posted 2012-04-01 03:00:13 »

Yea I think I understand what you mean.  From where are the moves being sent?  Are they sent remotely (from a player on a different machine) or are they coming from the same machine?

I think either way, it will probably be more efficient for the player to push rather than have the game pull.

Help Beta test SphereCCG!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

roseslayer (532 views)
2016-08-06 11:43:29

roseslayer (488 views)
2016-08-06 09:43:11

xTheGamerCodes (562 views)
2016-08-04 15:40:59

xTheGamerCodes (557 views)
2016-08-04 15:40:24

orrenravid (902 views)
2016-07-16 03:57:23

theagentd (979 views)
2016-07-11 14:28:54

Hydroque (1076 views)
2016-07-06 05:56:57

Hydroque (1050 views)
2016-07-03 08:52:54

GrandCastle (866 views)
2016-07-01 09:13:47

GrandCastle (645 views)
2016-07-01 09:09:45
Rendering resources
by Roquen
2016-08-08 05:55:21

Rendering resources
by Roquen
2016-08-08 05:52:42

Rendering resources
by Roquen
2016-08-08 05:50:38

Rendering resources
by Roquen
2016-08-08 05:49:53

Rendering resources
by Roquen
2016-08-08 05:32:39

Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!