\ VP-game
\ High-level game logic
\ 
\ $Id: VP-game.txt,v 1.4 2000/10/25 00:17:43 kris_johnson Exp $

\ Copyright 2000
\ Kristopher D. Johnson
\ 
\ See LICENSE-JacksOrBetter for the
\ conditions under which you may
\ use, redistribute, or modify this 
\ code, or create derived works.

docneeds VP-score-hand
docneeds VP-stats
docneeds VP-chips
docneeds VP-payouts

.( VP-game... )

0 constant NewGame
1 constant BetweenHands
2 constant Dealt
3 constant GameOver

g >gameState constant HandState

: NewGame? ( -- f )
  HandState @ NewGame = ;

: Dealt? ( -- f )
  HandState @ Dealt = ;

: BetweenHands? ( -- f )
  HandState @ BetweenHands = ;

: GameOver? ( -- f )
  HandState @ GameOver = ;

\ Score of last completed hand
g >lastScore constant LastScore

\ Payout of last completed hand
g >lastPayout constant LastPayout

100 constant InitChips
5 constant InitBet

: new-game ( -- )
  reset-game-stats
  gamesPlayed 2incr
  NewGame HandState !
  InitChips Chips !
  InitBet Bet !
  Loss LastScore !
  0 LastPayout !
  init-hand
;

\ Deal new hand
: deal ( -- )
  totalHands 2incr
  gameHands 2incr
  deduct-bet
  shuffle-deck
  init-hand
  deal-hand
  Dealt HandState !
;

: state-after-draw ( -- )
  Chips @ 0= if
    GameOver HandState ! exit
  then
  BetweenHands HandState !
;

\ Draw cards and figure winnings
: draw ( -- )
  deal-hand
  score-hand  ( score )
  dup LastScore !
  score>payout ( payout )
  dup LastPayout !
  add-payout
  state-after-draw
;

\ Reset statistics and initialize
\ according to game state
: init-stats ( -- )
  reset-stats   \ set to zero
  GameOver? not if
    1. gamesPlayed 2!
  then
  Dealt? if
    1. totalHands 2!
    1. gameHands 2!
    Bet @ s>d
    2dup totalBet 2!
    gameBet 2!
  then
  Chips @ highChips !
;

