home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / IO Examples / Scrabble / state.icl < prev    next >
Encoding:
Modula Implementation  |  1997-05-14  |  3.8 KB  |  106 lines  |  [TEXT/3PRM]

  1. implementation module state
  2.  
  3.  
  4. import    StdBool, StdList
  5. import    board, language
  6. import    Random
  7.  
  8.  
  9. ::    *State
  10.     =    {    playmode        :: Playmode                //    The playing mode
  11.         ,    board            :: Board                //    The playing board
  12.         ,    player            :: Player                //    Who's move is it (Player1 or Player2)
  13.         ,    player1            :: Playerinfo            //    The information of player 1
  14.         ,    player2            :: Playerinfo            //    The information of player 2
  15.         ,    direction        :: Direction            //    Last direction chosen by a Person
  16.         ,    letterbox        :: [Char]                //    The current set of available letters
  17.         ,    lexicon            :: Tree                    //    The current list of valid words
  18.         ,    wordsadded        :: Bool                    //    Words have been added by the players
  19.         ,    dimensions        :: (Int,Int,Int,Int)    //    The smallest enclosing rectangle surrounding the layn words
  20.         ,    strength        :: Strength                //    Strength of the Computer player
  21.         ,    progress        :: Progress                //    The progress of a Computer player
  22.         ,    files            :: *Files                //    The file system
  23.         ,    random            :: [Int]                //    An infinite random Integer list
  24.         }
  25. ::    Playerinfo
  26.     =    {    kind            :: Playerkind            //    The kind of the player
  27.         ,    letters            :: [Char]                //    Letters of player
  28.         ,    points            :: Int                    //    Score of player
  29.         ,    placedword        :: Bool                    //    Player placed a word in last turn (initially True)
  30.         }
  31.  
  32.  
  33. /***************************************************************************************************************
  34.     The value initdimensions will cause the computer player to place its first word at the center position of 
  35.     the board (7,7).
  36. ****************************************************************************************************************/
  37. initdimensions :: (Int,Int,Int,Int)
  38. initdimensions = (8,6,8,6)
  39.  
  40.  
  41. /***************************************************************************************************************
  42.     The initstate fixes the starting players. 
  43. ****************************************************************************************************************/
  44. initstate :: Files -> State
  45. initstate fs
  46. #    (wordlist,fs)        = readtree fs
  47. =    {    playmode        = EndPlayer2
  48.     ,    board            = initboard
  49.     ,    player            = Player2
  50.     ,    player1            = {    kind        = Person
  51.                           ,    letters        = []
  52.                           ,    points        = 0
  53.                           ,    placedword    = True
  54.                           }
  55.     ,    player2            = {    kind        = Computer
  56.                           ,    letters        = []
  57.                           ,    points        = 0
  58.                           ,    placedword    = True
  59.                           }
  60.     ,    direction        = Hor
  61.     ,    letterbox        = letterbox
  62.     ,    lexicon            = wordlist
  63.     ,    wordsadded        = False
  64.     ,    dimensions        = initdimensions
  65.     ,    strength        = Maximum
  66.     ,    progress        = Letter 'a' initplacing
  67.     ,    files            = fs
  68.     ,    random            = []
  69.     }
  70.  
  71.  
  72. /***************************************************************************************************************
  73.     When starting a new game the word list should not be read in again because this takes to long. 
  74.     For this purpose the function initialisestate is used.
  75. ****************************************************************************************************************/
  76. initialisestate :: State (IOState *s) -> (State,IOState *s)
  77. initialisestate t=:{random,player1,player2} io
  78. #    (rs,io)                    = getRandomList random io
  79.     (letterbox,letters1,rs)    = grab letterbox 7 rs
  80.     (letterbox,letters2,rs)    = grab letterbox 7 rs
  81. =    (    {t    & playmode        = EndPlayer2
  82.             , player        = Player2
  83.             , player1        = {player1 & letters=letters1,points=0}
  84.             , player2        = {player2 & letters=letters2,points=0}
  85.             , board            = initboard
  86.             , letterbox        = letterbox
  87.             , dimensions    = initdimensions
  88.             , random        = rs
  89.         }
  90.      ,    io
  91.      )
  92. where
  93.     getRandomList :: ![Int] !(IOState *s) -> (![Int],!IOState *s)
  94.     getRandomList rs io
  95.     |    not (isEmpty rs)    = (rs,io)
  96.     #    (seed,io)            = GetNewRandomSeed io
  97.     |    otherwise            = (random_list seed,io)
  98.     where
  99.         random_list :: !RandomSeed -> [Int]
  100.         random_list seed
  101.         #    (random,seed)    = Random seed
  102.         =    [random : random_list seed]
  103.  
  104. getboardletters :: State -> ([Char],State)
  105. getboardletters t=:{board} = (getplacedletters board,t)
  106.