Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 2307 times)
0 Members and 1 Guest are viewing this topic.
Offline DavidW

Junior Member


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.

Hello!
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.

BUT:

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 Member


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!

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
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
switch(state)
{
  STATE_INIT:  
                     ...initialise game...
                     state=STATE_START;
                     break;
   STATE_START:
                     ...choose player, setup something...
                     state=STATE_WAIT_FOR_PLAYERCOMMAND;
                     break;
   STATE_WAIT_FOR_PLAYERCOMMAND:
                     //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;}  
                     
                     ...play some animations on idle time...
                     
                     break;
   STATE_DO_MOVE:
                  ...keep in this state until moveanimations are finished
                  if(animation_frames>100) state=STATE_UPDATE_BOARD;
                  break;

...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


SphereCCG.com


« 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 Member


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.

Hello!
Offline ChexWithRaisins

Senior Newbie


Projects: 1


SphereCCG.com


« 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 Member


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....

Hello!
Offline ChexWithRaisins

Senior Newbie


Projects: 1


SphereCCG.com


« 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (15 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (33 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (30 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (43 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!