2.1
2.2
3.1
3.2
3.3
9.0
top
← prev up next →

Turn-Based GamesπŸ”— i

1Basic turn-based game interfacesπŸ”— i

generic interface

gen:turn-based-game

A TBG is an instance of gen:turn-based-game . This is meant to be a dictionary of game-related methods. It is NOT meant to be the state of the game.

Each instance defines its own types for Side , GameState , and MoveChoice , which are used by the methods to keep track of players and turns, what state the game is in, and possible choices a player could make on a given turn.

procedure

( sides tbggame-state)(listof Side )

tbg:TBG
game-state:GameState
Returns the set of sides that are currently in the game. Order in the result list does not matter.

procedure

( next-side tbggame-stateside)Side

tbg:TBG
game-state:GameState
side:Side
Returns the side to play once side’s turn is over. This should be called after side’s turn is over and game-state has been updated with side’s move choice.

procedure

game-state
side
move-choice)Boolean
tbg:TBG
game-state:GameState
side:Side
move-choice:MoveChoice
Determines whether the given move-choice is a valid legal move according to the rules of the game. If it is legal, valid-move-choice? returns true, otherwise it returns false.

procedure

( valid-move-choices tbggame-stateside)

tbg:TBG
game-state:GameState
side:Side
Returns a sequence which will enumerate all possible legal moves within the rules of the game.

NOTE: This sequence may or may not be finite, so if you’re relying on using it to iterate through all the elements, check with known-finite? first.

procedure

game-state
side
move-choice)GameState
tbg:TBG
game-state:GameState
side:Side
move-choice:MoveChoice
Returns the game state after side plays the given move-choice for their turn. This should not mutate the original game state, but return a new one.

procedure

( winning-state? tbggame-stateside)Boolean

tbg:TBG
game-state:GameState
side:Side
Determines whether the given side has won in the given game-state according to the rules of the game.

This interface includes two more methods:

procedure

( standard-initial-state tbg)GameState

tbg:TBGI
Produces the standard initial game state for the given turn-based game.

procedure

( standard-initial-side tbg)Side

tbg:TBGI
Produces the standard starting side for the given turn-based game, that is, the side that starts the game with their turn.

2User interfaces for playing turn-based gamesπŸ”— i

generic interface

gen:turn-based-game/gui

In order to be used for a user interface like the start function, turn-based games must implement the gen:turn-based-game/gui interface in addition to gen:turn-based-game . A TBGG is an instance of both of these interfaces. A TBGGI is a TBGG that also specifies initial states with gen:turn-based-game/standard-initial-state . Again, instances of these are meant to be dictionaries of game-related methods, not the state of the game.

Each instance should define its own type for TurnState , in addition to the types Side, GameState, and MoveChoice. The TurnState type keeps track of any state that a player could build up before making a final MoveChoice for their turn.
The key and mouse handlers should return HandlerResults according to the data definition below.

procedure

game-state
side
turn-state)Image
tbg:TBGG
game-state:GameState
side:Side
turn-state:TurnState

procedure

game-state
side
winner)Image
tbg:TBGG
game-state:GameState
side:Side
winner:(or/c #fSide )

procedure

( start-turn tbg)TurnState

tbg:TBGG
Determines the initial state that every player starts their turn with. The first mouse event or key event of a turn will use the result of this method as the starting turn-state.

procedure

( handle-mouse tbg
game-state
side
turn-state
mouse-posn
mouse-event)HandlerResult
tbg:TBGG
game-state:GameState
side:Side
turn-state:TurnState
mouse-posn:Posn
mouse-event:MouseEvent
This method handles mouse events, including "move" for mouse movements, "button-down" and "button-up" for mouse clicks, "drag" for dragging movements, and "enter" and "leave" for when the mouse enters and leaves the game canvas.

Mouse scrolling motions are actually not handled as mouse events; they are handled as key events using handle-key .

When a mouse event happens, this method can either continue the turn with an updated turn-state using continue-turn , or finish the turn with a final MoveChoice, using finish-turn . If the mouse event is not relevant, it should continue the turn with the same turn-state as before.

For example, some games might require moving a piece by clicking on it and then clicking on the space to move it to. On the first click, the handle-mouse method should return (continue-turn piece-selection) where piece-selection is a TurnState representing which piece was clicked on to be moved. Then on the second click, the handle-mouse method should return (finish-turn move-choice) where move-choice is a MoveChoice representing which piece should be moved and where it should go. A rough sketch of the definition would look like this:

;;ASpacerepresentsthepositionofaspaceontheboard.
 
;;AMoveChoiceisa(moveSpaceSpace)
(struct move[fromto])
;;representingmovingapiecefromthe`from`spacetothe`to`space.
 
;;ATurnStateisoneof:
;;-#false;beginningofturn
;;-Space;thepieceonthisspacehasbeenselectedtobemoved
 
;;handle-mouse:
;;TBGGGameStateSideTurnStatePosnMouseEvent->HandlerResult
(define (handle-mouse selfgamesideturn-stateposnmouse)
(cond
[(mouse=? "button-down"mouse)
(cond [(false? turn-state)
;;thisisthefirstclick
(if (piece-exists-at-space?gameside(posn->spaceposn))
;;selectthepieceatthisspacetobemoved
(continue-turn (posn->spaceposn))
;;otherwisejustastrayclickonanemptyspace
(continue-turn turn-state))]
[else
;;thereisalreadyapieceselected;thisisthesecondclick
(if (piece-can-move-here?gameturn-state(posn->spaceposn))
;;thisisafinishedmovechoice
(finish-turn (moveturn-state(posn->spaceposn)))
;;otherwisethiswouldbeaninvalidmove
....)])]
[else
;;themouseeventisnotrelevant
(continue-turn turn-state)]))

procedure

( handle-key tbg
game-state
side
turn-state
key-event)HandlerResult
tbg:TBGG
game-state:GameState
side:Side
turn-state:TurnState
key-event:KeyEvent
This method handles key events. One-character strings like "a", "b", and "c" represent the "regular" keys like A, B, and C. This includes strings like " " for the Spacebar, "\r" for the Enter key, and "\t" for the Tab key.

Other keys are represented with multi-character strings, such as "left", "right", "up", and "down" for the arrow keys, and "shift" and "rshift" for the two shift keys.

Mouse scrolling motions are also handled as key events, with "wheel-up" representing scrolling up, "wheel-down" representing scrolling down, and "wheel-left" and "wheel-right" representing scrolling left and right.

Just like with handle-mouse , when a key event happens, this method can either continue the turn with an updated turn-state using continue-turn , or finish the turn with a final MoveChoice using finish-turn .

procedure

( start game-desc)Void

game-desc:TBGGI
Starts the turn-based game with its standard initial state.

3Playing against the computerπŸ”— i

A ComputerPlayer is an object that describes a strategy the computer will use to play a game. Some will be specific to a particular game, but some can be generic across all TBG instances, although for some they might be increbibly slow.

An automated turn-based-game player that only looks n moves ahead.

An automated turn-based-game player that looks n moves ahead, and for each game state after that it randomly explores p different paths of k more moves ahead, scoring each game state according to the percentage of those paths that produce winning results.

procedure

( start/computer game-desccomputer-players)Void

game-desc:TBGGI
computer-players:(hash/c Side ComputerPlayer )
Starts the turn-based game with its standard initial state, with some of the sides played by the computer.

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /