-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotocols.rkt
162 lines (141 loc) · 7.57 KB
/
protocols.rkt
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#lang racket
;; ---------------------------------------------------------------------------------------------------
;; interface specification for the interaction between the game administrator and
(require "basics.rkt" "board.rkt" "state.rkt" "Lib/contract.rkt")
(provide
(contract-out
[turn-player/c
;; the proxy object that encapsulates all needed information
contract?]
[board-well-formed
(-> board? any)]
[complementary-hotels
;; do the hotels on the board and the available hotels add up
(-> board? (-> (listof hotel?) any))]
;; [player] refers to an automated player who presumably has cash and tiles
[good-placement
;; can the [player] place the tile?
(-> (instanceof/c turn-player/c) (-> (maybe/c tile?) any))]
[good-hotel-for-placement
;; can the [player] use the specified hotel for placing the tile?
(-> (instanceof/c turn-player/c) (maybe/c tile?) (-> (maybe/c hotel?) any))]
[good-shares
;; can the [player] buy the shares and are they available?
(-> (instanceof/c turn-player/c) (maybe/c tile?) (maybe/c hotel?) (-> shares-order/c any))]))
#| the turn is a proxy power object that
enables the player to execute actions
during its turn
see admin-intf for player signatures
Admin Player1 Player2 Player3
| | | |
internal | | | |
state | create(s = internal state) | | | create turn from
|------------------------------> t = Turn | | | internal state
| | | | |
| take turn(t) | | | |
|-------------------------------------------->| | | initiate turn
| | | | |
| | | | | initiate place:
| | place(p,h) | | | place callback for
| |<-----------| | | a merger placement
| | | | | p = tile, h = hotel
| | | | |
| | keep(hs) | | | do you wish to keep
| |----------->| | | any shares in the
| | keep(hs) | | | acquired hotels hs?
| |-------------------->| |
| | keep(hs) | | |
| |----------------------------->|
| | | | |
| | | | | end place:
| | place(hd) | | | here are everyone's
| |===========>| | | 'keep' decisions hd
| | | | |
| | | | |
| take turn(p,h,o) | | | | end turn:
|<============================================| | | tile p, hotel h,
| | | | | buy o shares
| | | | |
| | | | |
| check protocol() | | | | check whether player
| | | | | overstep its powers
| --- | | |
| ----- | | | if okay, commit
| | | | requested actions
| | | | | otherwise, throw
| | | | | out player
|#
(define turn-player/c
(class/c
(init-field
(current-state state?))
(field
;; (board-well-formed b)
(board board?)
(current player?)
(shares shares?)
;; (complementary-hotels board)
(hotels (listof hotel?)))
(reconcile-shares
;; update this turn's understanding of how many shares are available
(->m shares? any))
(place
;; this method is called once, and exactly once, when a merger occurs
(->dm ([t tile?][h hotel?]) #:pre (eq? (what-kind-of-spot (get-field board this) t) MERGING)
(players (listof player?))))))
;; --- turn input
(define (board-well-formed b)
;; if I had someone else implement the player and/or the strategy, he
;; would have to refine this function to require minimal coherence
#t)
(define (complementary-hotels b)
(define founded-hotels (filter (lambda (h) (size-of-hotel b h)) ALL-HOTELS))
(define ALL-HOTELS-set (apply seteq ALL-HOTELS))
(lambda (lh)
(set=? (apply seteq (append founded-hotels lh)) ALL-HOTELS-set)))
;; --- turn output
(define (good-placement turn)
(define board (get-field board turn))
(define players-tiles (player-tiles (get-field current turn)))
(lambda (t)
(or (boolean? t)
(and (member t players-tiles)
(not (eq? (what-kind-of-spot board t) IMPOSSIBLE))))))
(define (good-hotel-for-placement turn placement)
(define board (get-field board turn))
(define available-h (get-field hotels turn))
(lambda (h)
(define t placement)
(cond
[(boolean? t) (not h)]
[(boolean? h)
(define s (what-kind-of-spot board t))
(and (memq s (list FOUNDING SINGLETON GROWING)) (==> (eq? s FOUNDING) (empty? available-h)))]
[else
(define s (what-kind-of-spot board t))
(cond
;; if founding action, make sure hotel is in provided list
[(eq? FOUNDING s) (==> (cons? available-h) (member h available-h))]
;; if merging, make sure the hotel is the majority owner
[(eq? MERGING s) (let-values ([(w _) (merging-which board t)]) (member h w))]
;; if placement and hotel aren't #f, it should not be a different kind of spot
[else #f])])))
(define (good-shares turn tile hotel)
(cond
[(boolean? tile) (lambda (x) #t)]
[else
(define state
(let ((state (get-field current-state turn)))
(cond
;; did turn use a side-effect to update the state?, if not:
[(free-spot? (state-board state) tile)
(if hotel
(state-place-tile state tile hotel)
(state-place-tile state tile))]
[else state])))
(define board (state-board state))
(define player-s-cash (player-money (state-current-player state)))
(define available-s (state-shares state))
(define (l-affordable? hotels) (affordable? board hotels player-s-cash))
(define (l-available? hotels) (shares-available? available-s hotels))
(and/c l-available? l-affordable?)]))