; *** Endodoi 
; *** (c) 2001 W. D. Troyka
; *** dtroyka@justice.com


;	WARI COMMON MACROS


;	ADD BEAN(S) TO HOLE WHILE SOWING

; 	add bean to hole while sowing first lap

(define AB if (piece? $1) (change-type $2) else)

(define ABC
	((AB $1 $2)   ((AB $3 $4)   ((AB $5 $6)   ((AB $7 $8)
	((AB $9 $10)  ((AB $11 $12) ((AB $13 $14) ((AB $15 $16)
	((AB $17 $18) ((AB $19 $20) ((AB $21 $22) ((AB $23 $24)
	((AB $25 $26) ((AB $27 $28) ((AB $29 $30) ((AB $31 $32)
	((AB $33 $34) ((AB $35 $36) ((AB $37 $38) ((AB $39 $40) 
	((AB $41 $42) ((AB $43 $44) ((AB $45 $46) ((AB $47 $48)
	((AB $49 $50) ((AB $51 $52) ((AB $53 $54) ((AB $55 $56)
	((AB $57 $58) ((AB $59 $60) ((AB $61 $62) ((AB $63 $64)
	((AB $65 $66) ((AB $67 $68) ((AB $69 $70) ((AB $71 $72)
	(set-flag emu true)))))))))))))))))))))))))))))))))))))
)

(define add-bean
	(ABC 	 0B  1B  1B  2B  2B  3B  3B  4B  4B  5B  5B  6B  
		 6B  7B  7B  8B  8B  9B  9B 10B 10B 11B 11B 12B 
		12B 13B 13B 14B 14B 15B 15B 16B 16B 17B 17B 18B 
		18B 19B 19B 20B 20B 21B 21B 22B 22B 23B 23B 24B
		24B 25B 25B 26B 26B 27B 27B 28B 28B 29B 29B 30B
		30B 31B 31B 32B 32B 33B 33B 34B 34B 35B 35B 36B)
)

; 	add two beans to hole when sowing two laps

(define add-bean2 
	(ABC 	 0B  2B  1B  3B  2B  4B  3B  5B  4B  6B  5B  7B  
		 6B  8B  7B  9B  8B 10B  9B 11B 10B 12B 11B 13B 
		12B 14B 13B 15B 14B 16B 15B 17B 16B 18B 17B 19B 
		18B 20B 19B 21B 20B 22B 21B 23B 22B 24B 23B 24B
		24B 26B 25B 27B 26B 28B 27B 29B 28B 30B 29B 31B
		30B 32B 31B 33B 32B 34B 33B 35B 34B 36B 35B 36B)
)

; 	add three beans to hole when sowing three laps

(define add-bean3 
	(ABC 	 0B  3B  1B  4B  2B  5B  3B  6B  4B  7B  5B  8B  
		 6B  9B  7B 10B  8B 11B  9B 12B 10B 13B 11B 14B 
		12B 15B 13B 16B 14B 17B 15B 18B 16B 19B 17B 20B 
		18B 21B 19B 22B 20B 23B 21B 24B 22B 24B 23B 24B
		24B 27B 25B 28B 26B 29B 27B 30B 28B 31B 29B 32B
		30B 33B 31B 34B 32B 35B 33B 36B 34B 36B 35B 36B)
)

; 	add four beans to hole when sowing four laps

(define add-bean4 
	(ABC 	 0B  4B  1B  5B  2B  6B  3B  7B  4B  8B  5B  9B  
		 6B 10B  7B 11B  8B 12B  9B 13B 10B 14B 11B 15B 
		12B 16B 13B 17B 14B 18B 15B 19B 16B 20B 17B 21B 
		18B 22B 19B 23B 20B 24B 21B 25B 22B 26B 23B 27B
		24B 28B 25B 29B 26B 30B 27B 31B 28B 32B 29B 33B
		30B 34B 31B 35B 32B 36B 33B 36B 34B 36B 35B 36B)
)


;	CAPTURE BEANS

(define OC (set-position-flag occ true))

(define FL (set-flag first-lap true))

(define SL (set-flag second-lap true))

;	if move would capture all opponent beans, no captures result

(define no-capture
    (if enemy? mark 
	(if (flag? first-lap)
	(while (and (or (piece? 1B) (piece? 2B)) enemy?) cw) 
	    (if (in-zone? wrap) back cc
	    (while (and (piece? 0B) enemy?) cc) 
	    	(if friend? add (verify false) else back)
	    else back) else 
	(if (in-zone? wrap)
    	(if (flag? second-lap)
	(while (and (or (piece? 0B) (piece? 1B)) enemy?) cw) 
	    (if friend? add (verify false) else back) else
	        (while (and (piece? 0B) enemy?) cw) 
	    (if friend? add (verify false) else back))
    	else back)
    ))
)

; 	capture beans in opponent's hole and place in store

(define capture-beans 
    (if enemy? mark 
	(if (piece? $1) (change-type $2) (OC)
	    cascade x 
	    (while not-empty? e) 
	    (while (position-flag? occ) e) (OC) 
	    cascade e (OC) 
	    cascade e to (OC) 
	    back 
	else (if (piece? $3) (change-type $2) (OC)  
	    cascade x 
	    (while not-empty? e) 
	    (while (position-flag? occ) e) (OC) 
	    cascade e to (OC) 
	    back else add (verify false))) 
    else add (verify false))
)

;	capture beans after one, two, or three laps of sowing

(define CB (capture-beans 2B 0B 1B))

(define CB2 (capture-beans 1B 0B 0B))

(define CB3 (capture-beans 0B 0B 0B))

; 	capture beans clockwise in successive opponent holes

(define X  (FL) (no-capture) (while enemy? (CB) cw) add (verify false))

(define X2 (SL) (no-capture) (while enemy? (CB2) cw) add (verify false))

(define X3 (no-capture) (while enemy? (CB3) cw) add (verify false))


;	DETERMINE LEGAL MOVES

;	test to see which variant is being played

(define game-over?
	y1 
	(if 3-bean (verify (and (empty? $1) (empty? $2))) else
	(if 4-bean (verify (and (empty? $3) (empty? $4))) else
	(if 5-bean (verify (and (empty? $5) (empty? $6))) else
	(if 6-bean (verify (and (empty? $7) (empty? $8)))))))
	back
)

; 	limit move to friendly hole with beans in it

(define preconditions
	(verify (and friend? (not-piece? 0B)))
	(game-over? $1 $2 $3 $4 $5 $6 $7 $8) to
	(set-position-flag lift-hole true)
	(set-flag first-lap false)
	(set-flag second-lap false)
	(change-type 0B) 
)

;	give beans to opponent who has none

(define CG (set-position-flag give-hole true) (set-flag can-give true))

(define give-beans

	(set-flag no-beans false)
	(set-flag can-give false)

; 	are all six opponent holes empty?

	(while friend? cw)
	(while (and (piece? 0B) enemy?) cw)
	(if friend? (set-flag no-beans true)

; 	if so, is there a move that will give opponent beans?

	(if 	 (not-piece? 0B)  (CG)) cw
	(if (and (not-piece? 0B) 
		 (not-piece? 1B)) (CG)) cw
	(if (and (not-piece? 0B) 
		 (not-piece? 1B)
		 (not-piece? 2B)) (CG)) cw
	(if (and (not-piece? 0B) 
		 (not-piece? 1B) 
		 (not-piece? 2B) 
		 (not-piece? 3B)) (CG)) cw
	(if (and (not-piece? 0B) 
		 (not-piece? 1B) 
		 (not-piece? 2B) 
		 (not-piece? 3B)
		 (not-piece? 4B)) (CG)) cw
	(if (and (not-piece? 0B) 
		 (not-piece? 1B) 
		 (not-piece? 2B) 
		 (not-piece? 3B)
		 (not-piece? 4B)
		 (not-piece? 5B)) (CG)))
	(go to)

;	give beans to depleted opponent if possible

	(if (and (flag? no-beans) (flag? can-give))
		(verify (position-flag? give-hole)))
)

;	 award all remaining beans to opponent if side is empty

(define next y (while not-empty? e) (while (position-flag? occ) e))

(define ST (set-position-flag stop true))
	 
(define set-stop
	(if (piece? 1B) (next) (ST) else
	  (if (piece? 2B) (next) e (ST) else
	    (if (piece? 3B) (next) e e (ST) else
	      (if (piece? 4B) (next) e e e (ST) else
		(next) e e e e (ST))))) 
)

(define clean				; empty hole
	(if (not-piece? 0B) (change-type 0B)
	mark (set-stop) back (next) cascade
	(while (not-position-flag? stop) to flip (OC) e cascade)
	flip (OC) (set-position-flag stop false) to back)
)

(define clean-up			; empty all holes 
	((verify friend?) to capture
	(while (or (friend? cw) (empty? cw)) cw) 
	(while (and (piece? 0B) friend?) cc)
	(if enemy? capture (while enemy? (clean) cc) add)
))


;	SOW BEANS

; 	determine drop hole

(define DH (set-position-flag drop-hole true) back else)

(define drop-hole?
	(if (piece? $1)  cc (DH) 
	(if (piece? $2)  cc cc (DH) 
	(if (piece? $3)  cc cc cc (DH) 
	(if (piece? $4)  cc cc cc cc (DH) 
	(if (piece? $5)  cc cc cc cc cc (DH) 
	(if (piece? $6)  cc cc cc cc cc cc (DH) 
	(if (piece? $7)  cw cw cw cw cw (DH) 
	(if (piece? $8)  cw cw cw cw (DH) 
	(if (piece? $9)  cw cw cw (DH) 
	(if (piece? $10) cw cw (DH) 
	(if (piece? $11) cw (DH) 
	(verify false))))))))))))
)

(define drop-hole4?
	(if (piece? $1)  cc (DH) 
	(if (piece? $2)  cc cc (DH) 
	(if (piece? $3)  cc cc cc (DH) 
	(if (piece? $4)  cc cc cc cc (DH) 
	(if (piece? $5)  cw cw cw (DH) 
	(if (piece? $6)  cw cw (DH) 
	(if (piece? $7)  cw (DH) 
	(verify false))))))))
)

(define drop-hole-L4?
	(if (piece? $1)  cc (DH) 
	(if (piece? $2)  cc cc (DH) 
	(if (piece? $3)  cc cc cc (DH) 
	(verify false))))
)

; 	macros for counting holes

(define L1				; sow partial first lap
	(while (not-position-flag? drop-hole) 
	cc (add-bean))) 

(define FL1 cc				; finish first lap
	(while (not-position-flag? lift-hole) 
	(add-bean) cc)) 

(define L2 				; sow partial second lap
	(while (not-position-flag? drop-hole) 
	cc (add-bean2))) 

(define FL2 cc	 			; finish second lap
	(while (not-position-flag? lift-hole) 
	(add-bean2) cc))

(define L3 				; sow partial third lap
	(while (not-position-flag? drop-hole) 
	cc (add-bean3))) 

(define FL3 cc	 			; finish third lap
	(while (not-position-flag? lift-hole) 
	(add-bean3) cc))

(define L4 				; sow partial fourth lap
	(while (not-position-flag? drop-hole) 
	cc (add-bean4)))


;	PIECE DEFINITIONS

(define common-piece
	(name $1)
	(notation "$2")
	(help "$2")
      	(description "$2:  Beans in a hole can be sown one at a time
		in successive holes counterclockwise around the board.  The
		lift hole is skipped on subsequent laps.  If two or three 
		beans are in an opponent hole at the conclusion of sowing,
		they are captured along with beans in any unbroken sequence 
		of preceding opponent holes containing two or three beans.  
		Captured beans are removed from play and placed in the 
		store.")
)

(define Piece
	(common-piece)
	(image 	Bottom "Mancala\$2.bmp"
		Top "Mancala\$2.bmp")
)



(define Test
   (piece
	(name test)
	(dummy)
	(attribute $1 true)
	(attribute $2 false) 
	(attribute $3 false)
	(attribute $4 false))
)


;	GAME BLOCK

(define common
   (description "Wari is among the most widely played of the Mancala 
	family of boardgames.  It is known by a variety of regional names, 
	including Awari, Oware, Awale, Woro, and Ayo, and it is played 
	throughout Africa and the West Indies.  The rules are as follows:
	\\
	The board is divided into twelve holes and two stores.  One player 
	plays from the bottom six holes and the other plays from the top 
	six.  The stores are used for storing captured pieces -- called 
	beans -- and do not play a role in the game.  The right store is 
	for the bottom player and the left store for the top player.  
	\\
	The game begins with four beans in each of the twelve holes.  The 
	bottom player goes first.  A move is made by picking up the beans 
	in a player's hole and sowing them one at a time counterclockwise 
	around the board. Sowing begins with the hole immediately after 
	the lift hole and continues until all the beans have been dropped.  
	If twelve or more beans are sown, the lift hole is skipped over 
	and remains empty at the end of the turn. 
	\\
	If the last piece dropped by a player lands in an opponent hole 
	that contains two or three beans (including any dropped beans), 
	those beans are captured and placed in the player's store. If the 
	captured hole is preceded by an unbroken chain of one or more 
	enemy holes containing two or three beans, those beans are also 
	captured.
	\\
	If all of the opponent's holes are empty, you must make a move 
	that gives at least one bean to the opponent, unless no other move 
	is available.  If you make a move that would normally capture all
	opponent beans, no captures result.
	\\
	The game ends when a player captures twenty-five beans or when a 
	player has no move because all of that player's holes are empty.  
	In the latter case, all remaining pieces are awarded to the 
	opponent.  If it is your turn to move and you have no beans, 
	you must click on an empty hole to award the remaining beans 
	to the opponent.  The winner is the player with the most  
	captured beans at the end of the game. 
	\\
	The holes hold up to thirty-six beans.  Any additional beans  
	added to a hole are skimmed off the top and removed from play.    
	In the event of 3-move repetition, the game is terminated and   
	the player with the most captured beans is the winner.  Beans 
	remaining in play are not counted.  In the variants, the players  
	start with three through six beans in each hole on boards with 
	dimensions 2x4, 2x5, 2x6, 2x7, and 2x8.  A player wins by
	capturing more than half of all available beans.
	\\
	These rules are based on descriptions provided by:
	\\ 
	R. C. Bell, Board and Table Games From Many Civilizations\
	David Parlett, The Oxford History of Boardgames\
	Larry Russ, The Complete Mancala Games Book\
	Mind Sports Organisation, Oware tournament rules, 
	www.msoworld.com/rules.html\
	University of Alberta Computer Awari Group, 
	www.cs.ualberta.ca/~awari\
	Didier and Olivier Guillion, Awale Computer Wari program,
	www.myriad-online.com/awale.htm (shareware)
	\\
	These sources differ on whether a move capturing all opponent 
	pieces is permitted, and how to award the remaining beans when 
	one player has no move.  The rules I have adopted are commonly 
	used.
	\\
	Please send any comments or bug reports to dtroyka@justice.com.  
	For other Mancala games available on Zillions, check out Congklak, 
	Cross-Wari, and Cross-Kalah.")
   (history "Mancala v. 2.0  (c) 2001  W. D. Troyka.
	\\
	Mancala games have been around for well over a thousand years.  In 
	Board And Table Games From Many Civilisations, R. C. Bell suggests 
	that holes cut into the `roofing slabs` of a temple in southern 
	Egypt dating from 1400 B.C. resemble a Mancala board.  Bell also 
	notes that patterns resembling Mancala boards were cut into 
	pyramids at Karnak and Luxor around the second century A.D.  
	Evidence of actual Mancala play during antiquity is limited.  
	There is little doubt, however, that by the sixth century A.D. 
	Mancala in a form similar to the modern game was widely played in 
	Africa and Arabia.  Paul Smith, in an article entitled `Mancala 
	Games` available at www.msoworld.com/mindzine/news/classic/mancala.
	html, comments that sixth-century boards have been found in Zaire, 
	Angola, and Ghana.
	\\
	Mancala games today are played throughout the world, most notably 
	in Africa, Indonesia, the Phillipines, the West Indies, and certain 
	parts of Central Asia.  In recent decades the game has also developed 
	a following in Europe and North America.
	\\
	Wari is among the best known and most widely played versions of 
	Mancala.  In The Complete Mancala Games Book, Larry Russ notes that 
	Wari is played in Africa in Senegal, Gambia, Guinea, Sierra Leone, 
	Liberia, Ivory Choast, Togo, Nigeria, Cameroon, and Gabon, and in the 
	Western Hemisphere in Surinam, Guyana, Grenada, Barbados, St. Lucia, 
	Martinique, Dominica, Antigua, and St. Kitts.  Paul Smith observes 
	that the strongest Wari players in the world today are from Antigua.")
   (strategy "A player threatens capture when the player has a hole containing 
	seeds that, when sown, will terminate in an opponent hole.  A player's 
	position generally becomes stronger as more opponent holes are 
	threatened.
	\\
	If a bean is threatened with capture, the player can protect the bean 
	by moving it or by adding a bean to the threatening hole so that it 
	will overshoot the threatened bean.  If two beans in a hole are 
	threatened, they may be protected by adding a third bean to the hole.  
	\\
	A hole with twelve or more beans can threaten an empty opponent hole 
	because a second bean will be dropped during the second lap of sowing, 
	resulting in capture of both beans.  For this reason, it may be 
	advantageous to collect enough beans in a hole to sow twice on the 
	opponent's side.  Conversely, you may wish to `overload` an opponent 
	hole that threatens a double sowing by adding enough beans so that it 
	sows back to the opponent's side.
	\\
	Hoarding beans in as many holes as possible can be a good strategy.  
	If the opponent runs low on beans, you can create capturing 
	opportunities by sowing into empty holes.")  
   (animate-drops false)
   (animate-captures false)
   (players Bottom Top)
   (turn-order Bottom Top)
   (pass-turn false)
   (count-condition (Bottom Top) stalemated)
   (count-condition (Bottom Top) repetition)
)

(define image-audio
   (music "..\Audio\Mancala\Nyayito.mid")		; song from Ghana
   (opening-sound "..\Audio\Mancala\drop.wav")
   (drop-sound "..\Audio\Mancala\sow.wav")
   (change-sound "..\Audio\Mancala\sow.wav")
   (win-sound "..\Audio\Mancala\elephant.wav")
   (loss-sound "..\Audio\Mancala\elephant.wav")
   (capture-sound "")
   (piece (Piece 36B 36-beans))
   (piece (Piece 35B 35-beans))
   (piece (Piece 34B 34-beans))
   (piece (Piece 33B 33-beans))
   (piece (Piece 32B 32-beans))
   (piece (Piece 31B 31-beans))
   (piece (Piece 30B 30-beans))
   (piece (Piece 29B 29-beans))
   (piece (Piece 28B 28-beans))
   (piece (Piece 27B 27-beans))
   (piece (Piece 26B 26-beans))
   (piece (Piece 25B 25-beans))
   (piece (Piece 24B 24-beans))
   (piece (Piece 23B 23-beans))
   (piece (Piece 22B 22-beans))
   (piece (Piece 21B 21-beans))
   (piece (Piece 20B 20-beans))
   (piece (Piece 19B 19-beans))
   (piece (Piece 18B 18-beans))
   (piece (Piece 17B 17-beans))
   (piece (Piece 16B 16-beans))
   (piece (Piece 15B 15-beans))
   (piece (Piece 14B 14-beans))
   (piece (Piece 13B 13-beans))
   (piece (Piece 12B 12-beans))
   (piece (Piece 11B 11-beans))
   (piece (Piece 10B 10-beans))
   (piece (Piece 9B 9-beans))
   (piece (Piece 8B 8-beans))
   (piece (Piece 7B 7-beans))
   (piece (Piece 6B 6-beans))
   (piece (Piece 5B 5-beans))
   (piece (Piece 4B 4-beans))
   (piece (Piece 3B 3-beans))
   (piece (Piece 2B 2-beans))	
   (piece (Piece 1B 1-bean))
   (piece (Piece 0B 0-beans))
)


;	SELECT SCREEN

(define select-board
    (board 
      	(image "Mancala\select.bmp")
  	(grid
     		(start-rectangle 20 20 170 55)
		(dimensions 
			("A/B/C" (192 0))  ; files
     		 	("6/5/4/3/2/1/0" (0 38))) ; ranks
)))

(define Select
	(name $1)
	(notation "$2")
	(help "Click here to select the $2 variants")
      	(description "$2:  Click here to select the $2 variants")
	(image 	Background "Mancala\$3.bmp")
	(open "$2.zrf")
)

(define Mancala-Select
   (title "Mancala Select")
   (description "Select the Mancala variant you want to play.")
   (history "Mancala v. 2.0  (c) 2001  W. D. Troyka")
   (strategy "")
   (music "..\Audio\Mancala\jeopardy.mid")
   (change-sound "..\Audio\Mancala\midiprompt.wav")
   (players Background Top Bottom)
   (turn-order Background)
   (select-board)
   (board-setup 
	(Background  
		(Wari A6) 	(Kalah B6) 	(Endodoi C6)
		(W4 A5)		(K4 B5)		(E4 C5)
		(W5 A4)		(K5 B4)		(E5 C4)
		(W6 A3)		(K6 B3)		(E6 C3)
		(W7 A2)		(K7 B2)		(E7 C2)
		(W8 A1)		(K8 B1)		(E8 C1)
		(Music A0)	(Music B0)	(Music C0)))
   (piece (Select Wari Wari2x6 header-W))
   (piece (Select W4 Wari2x4 S4))
   (piece (Select W5 Wari2x5 S5))
   (piece (Select W6 Wari2x6 S6))
   (piece (Select W7 Wari2x7 S7))
   (piece (Select W8 Wari2x8 S8))
   (piece (Select Kalah Kalah2x6 header-K))
   (piece (Select K4 Kalah2x4 S4))
   (piece (Select K5 Kalah2x5 S5))
   (piece (Select K6 Kalah2x6 S6))
   (piece (Select K7 Kalah2x7 S7))
   (piece (Select K8 Kalah2x8 S8))
   (piece (Select Endodoi Endodoi2x6 header-E))
   (piece (Select E4 Endodoi2x4 S4))
   (piece (Select E5 Endodoi2x5 S5))
   (piece (Select E6 Endodoi2x6 S6))
   (piece (Select E7 Endodoi2x7 S7))
   (piece (Select E8 Endodoi2x8 S8))
   (piece (name Music) (dummy) (moves (flip flip flip add)))
   (piece (name bean) (dummy))
)
