2008-07-22 04:29:48 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! Copyright © 2008 Reginald Keith Ford II
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! 24, the Factor game!
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								USING: kernel random namespaces shuffle sequences
							 | 
						
					
						
							
								
									
										
										
										
											2008-07-24 16:34:25 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								parser io math prettyprint combinators continuations
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-07 02:58:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								arrays words quotations accessors math.parser backtrack assocs ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-07-22 04:29:48 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-07-22 04:43:27 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								IN: 24-game
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-07 02:58:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								SYMBOL: commands
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-23 22:40:17 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: nop ( -- ) ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-07-22 04:29:48 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: do-something ( a b -- c ) { + - * } amb-execute ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: maybe-swap ( a b -- a b ) { nop swap } amb-execute ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: some-rots ( a b c -- a b c )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    #! Try each permutation of 3 elements.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    { nop rot -rot swap spin swapd } amb-execute ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-07 02:58:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: makes-24? ( a b c d -- ? )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-06 17:53:41 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            some-rots do-something
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            some-rots do-something
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-07 02:58:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            maybe-swap do-something
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            24 =
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ 4drop ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if-amb ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-07-22 04:29:48 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: q ( -- obj ) "quit" ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-07 02:58:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: show-commands ( -- ) "Commands: " write commands get unparse print ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-07-22 04:29:48 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: report ( vector -- ) unparse print show-commands ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: give-help ( -- ) "Command not found..." print show-commands ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: find-word ( string choices -- word ) [ name>> = ] with find nip ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-07 02:58:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: obtain-word ( -- word )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    readln commands get find-word dup
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ drop give-help obtain-word ] unless ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-07-22 04:29:48 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: done? ( vector -- t/f ) 1 swap length = ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-07 02:58:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: victory? ( vector -- t/f ) { 24 } = ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: apply-word ( vector word -- array ) 1quotation with-datastack >array ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: update-commands ( vector -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    length 3 <
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ commands [ \ rot swap remove ] change ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-07-22 04:29:48 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								DEFER: check-status
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: quit-game ( vector -- ) drop "you're a quitter" print ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: quit? ( vector -- t/f ) peek "quit" = ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-07 02:58:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: end-game ( vector -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    dup victory? 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ drop "You WON!" ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ pop number>string " is not 24... You lose." append ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if print ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! The following two words are mutually recursive,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! providing the repl loop of the game
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: repeat ( vector -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    dup report obtain-word apply-word dup update-commands check-status  ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: check-status ( object -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    dup done?
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ end-game ] 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ dup quit? [ quit-game ] [ repeat ] if ]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: build-quad ( -- array ) 4 [ 10 random ] replicate >array ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-23 18:12:41 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: 24-able? ( quad -- t/f ) [ makes-24? ] with-datastack first ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-07 02:58:50 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: 24-able ( -- vector ) build-quad dup 24-able? [ drop build-quad ] unless ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: set-commands ( -- ) { + - * / rot swap q } commands set ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-08-12 02:42:23 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: play-game ( -- ) set-commands 24-able repeat ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-06 17:53:41 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								MAIN: play-game
							 |