Skip to content

Commit

Permalink
Update README with new example code
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnAD committed Apr 25, 2018
1 parent c196197 commit 3b2129d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ And, when running from a terminal,
#
# Game of Thai 21 example
#
# decription: Twenty one flags are placed on a beach. Each player takes a turn
# Description: Twenty one flags are placed on a beach. Each player takes a turn
# removing between 1 and 3 flags. The player that removes the last remaining flag wins.
#
# this game was introduced by an episode of Survivor: http://survivor-org.wikia.com/wiki/21_Flags
# This game was introduced by an episode of Survivor: http://survivor-org.wikia.com/wiki/21_Flags
#
import strutils
import turn_based_game
import tables
#
# 1. define our game object
Expand All @@ -57,20 +58,20 @@ type
# 2. add our rules (methods)
#
method setup*(self: GameOfThai21, players: seq[GenericPlayer]) =
method setup*(self: GameOfThai21, players: seq[Player]) =
self.default_setup(players)
self.pile = 21
method possible_moves(self: GameOfThai21): seq[string] =
var limit = 2
if self.pile < 3:
limit = self.pile - 1
@["1", "2", "3"][0..limit]
method possible_moves(self: GameOfThai21): OrderedTable[string, string] =
if self.pile==1:
return {"1": "Take One"}.toOrderedTable
elif self.pile==2:
return {"1": "Take One", "2": "Take Two"}.toOrderedTable
return {"1": "Take One", "2": "Take Two", "3": "Take Three"}.toOrderedTable
method make_move(self: GameOfThai21, move: string): string =
var count = move.parseInt()
self.pile -= count # remove bones.
self.determine_winner()
return "$# flags removed.".format([count])
method determine_winner(self: GameOfThai21) =
Expand All @@ -80,6 +81,10 @@ method determine_winner(self: GameOfThai21) =
self.winner_player_number = self.current_player_number
return
# the following method is not _required_, but makes it nicer to read
method status(self: GameOfThai21): string =
"$# flags available.".format([self.pile])
#
# 3. invoke the new game object
#
Expand Down
12 changes: 3 additions & 9 deletions examples/thai21.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#
# Game of Thai 21 example
#
# description: Twenty one flags are placed on a beach. Each player takes a turn
# Description: Twenty one flags are placed on a beach. Each player takes a turn
# removing between 1 and 3 flags. The player that removes the last remaining flag wins.
#
# this game was introduced by an episode of Survivor: http://survivor-org.wikia.com/wiki/21_Flags
# This game was introduced by an episode of Survivor: http://survivor-org.wikia.com/wiki/21_Flags
#

import strutils
Expand All @@ -27,13 +27,6 @@ method setup*(self: GameOfThai21, players: seq[Player]) =
self.default_setup(players)
self.pile = 21

# method possible_moves(self: GameOfThai21): seq[string] =
# if self.pile==1:
# return @["1"]
# elif self.pile==2:
# return @["1", "2"]
# return @["1", "2", "3"]

method possible_moves(self: GameOfThai21): OrderedTable[string, string] =
if self.pile==1:
return {"1": "Take One"}.toOrderedTable
Expand All @@ -53,6 +46,7 @@ method determine_winner(self: GameOfThai21) =
self.winner_player_number = self.current_player_number
return

# the following method is not _required_, but makes it nicer to read
method status(self: GameOfThai21): string =
"$# flags available.".format([self.pile])

Expand Down

0 comments on commit 3b2129d

Please sign in to comment.