June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,25 @@
v > > >> v
2 2 1234
4 ^1?3^4
>8*00p10p> >? ?5> 68*+00g10gpv
v9?7v6 0
8 0
> > >> ^ g
^p00 _v# `\*49:+1 <
_>"rorrE",,,,,$ >~:67*-!#v_:167*+-!#v_:95*-!#v_:295*+-!#v_:586*+\`#v_:97*2--!#v
$ $ $ $ : $
* + - / 1 :
^ < < < < 8 .
6 6
* 4
+ *
\ -
` > v_v
"
^ < _v e
^ _^#+*28:p2\*84\-*86g2:-+*441< s
o
L
> 1 |-*49"#<
| -*84gg01g00<p00*84<v <
>00g:1+00p66*`#^_ "niW">:#,_@

View file

@ -9,40 +9,43 @@ class ExpressionTree
constructor new : aLiteral
[
var aLevel := Integer new:0.
auto aLevel := Integer new:0.
aLiteral forEach(:ch)
[
var node := DynamicStruct new.
ch =>
$43 [ node set level(aLevel + 1); set operation:%add ]; // +
$45 [ node set level(aLevel + 1); set operation:%subtract ]; // -
$42 [ node set level(aLevel + 2); set operation:%multiply ]; // *
$47 [ node set level(aLevel + 2); set operation:%divide ]; // /
$40 [ aLevel append int:10. ^ $self ]; // (
$41 [ aLevel reduce int:10. ^ $self ]; // )
$43 [ node level := aLevel + 1. node operation := %add ]; // +
$45 [ node level := aLevel + 1. node operation := %subtract ]; // -
$42 [ node level := aLevel + 2. node operation := %multiply ]; // *
$47 [ node level := aLevel + 2. node operation := %divide ]; // /
$40 [ aLevel append(10). ^ self ]; // (
$41 [ aLevel reduce(10). ^ self ]; // )
! [
node set leaf(ch literal; toReal); set level(aLevel + 3).
node leaf := ch literal; toReal.
node level := aLevel + 3.
].
if ($nil == theTree)
if (nil == theTree)
[ theTree := node ];
[
if (theTree level >= node level)
[
node set left:theTree; set right:$nil.
node left := theTree.
node right := nilValue.
theTree := node
];
[
var aTop := theTree.
while (($nil != aTop right)and:$(aTop right; level < node level))
while ((nilValue != aTop right)&&(aTop right; level < node level))
[ aTop := aTop right ].
node set left(aTop right); set right:$nil.
node left := aTop right.
node right := nilValue.
aTop set right:node
aTop right := node
]
]
]
@ -53,8 +56,8 @@ class ExpressionTree
if (aNode containsProperty:%leaf)
[ ^ aNode leaf ];
[
var aLeft := $self eval:(aNode left).
var aRight := $self eval:(aNode right).
var aLeft := self eval:(aNode left).
var aRight := self eval:(aNode right).
^ aLeft~(aNode operation) eval:aRight
]
@ -65,14 +68,14 @@ class ExpressionTree
readLeaves : aList at:aNode
[
if ($nil == aNode)
if (nil == aNode)
[ InvalidArgumentException new; raise ].
if (aNode containsProperty:%leaf)
[ aList append(aNode leaf) ];
[
$self readLeaves:aList at(aNode left).
$self readLeaves:aList at(aNode right).
self readLeaves:aList at(aNode left).
self readLeaves:aList at(aNode right).
].
]
@ -86,7 +89,7 @@ class TwentyFourGame
constructor new
[
$self newPuzzle.
self newPuzzle.
]
newPuzzle
@ -129,14 +132,14 @@ class TwentyFourGame
exp readLeaves:Leaves.
ifnot (Leaves ascendant; sequenceEqual(theNumbers ascendant))
[ console printLine:"Invalid input. Enter an equation using all of those four digits. Try again.". ^ $self ].
[ console printLine:"Invalid input. Enter an equation using all of those four digits. Try again.". ^ self ].
var aResult := exp value.
if (aResult == 24)
[
console printLine("Good work. ",aLine,"=",aResult).
$self newPuzzle.
self newPuzzle.
];
[ console printLine("Incorrect. ",aLine,"=",aResult) ]
]
@ -159,13 +162,12 @@ extension gameOp
]
}
].
^ true
].
]
}
program =
public program =
[
var aGame := TwentyFourGame new; help.

View file

@ -1,19 +1,22 @@
: 24game
import: mapping
: game
| l expr w n i |
ListBuffer init(4, #[ 9 rand ]) ->l
4 #[ 9 rand ] Array init ->l
System.Out "Digits : " << l << " --> RPN Expression for 24 : " << drop
System.Console askln ->expr
System.Console accept ->expr
expr words forEach: w [
w "+" == ifTrue: [ + continue ]
w "-" == ifTrue: [ - continue ]
w "*" == ifTrue: [ * continue ]
w "/" == ifTrue: [ asFloat / continue ]
w "/" == ifTrue: [ >float / continue ]
w asInteger dup ->n ifNull: [ System.Out "Word " << w << " not allowed " << cr break ]
l indexOf(n) dup ->i ifNull: [ System.Out "Integer " << n << " is wrong " << cr break ]
w >integer dup ->n ifNull: [ System.Out "Word " << w << " not allowed " << cr break ]
n l indexOf dup ->i ifNull: [ System.Out "Integer " << n << " is wrong " << cr break ]
n l put(i, null)
]
l conform(#isNull) ifFalse: [ "Sorry, all numbers must be used..." println return ]
24 == ifTrue: [ "You won !" ] else: [ "You loose..." ] println ;
#null? l conform? ifFalse: [ "Sorry, all numbers must be used..." . return ]
24 if=: [ "You won !" ] else: [ "You loose..." ] .
;

View file

@ -0,0 +1,63 @@
# Project : 24 game
# Date : 2018/02/21
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
load "stdlib.ring"
digits = list(4)
check = list(4)
for choice = 1 to 4
digits[choice] = random(9)
next
see "enter an equation (using all of, and only, the single digits " + nl
for index = 1 to 4
see digits[index]
if index != 4
see " "
ok
next
see ")"
see " which evaluates to exactly 24. only multiplication (*), division (/)," + nl
see "addition (+) & subtraction (-) operations and parentheses are allowed:" + nl
see "24 = "
give equation
see "equation = " + equation + nl
while true
for char = 1 to len(equation)
digit = substr("0123456789", equation[char]) - 1
if digit >= 0
for index = 1 to 4
if digit = digits[index]
if not check[index]
check[index] = 1
exit
ok
ok
next
if index > 4
see "sorry, you used the illegal digit " + digit + nl
exit 2
ok
ok
next
for index = 1 to 4
if check[index] = 0
see "sorry, you failed to use the digit " + digits[index] + nl
exit 2
ok
next
for pair = 11 to 99
if substr(equation, string(pair))
see "sorry, you may not use a pair of digits " + pair + nl
ok
next
eval("result = " + equation)
if result = 24
see "congratulations, you succeeded in the task!" + nl
exit
else
see "sorry, your equation evaluated to " + result + " rather than 24!" + nl
ok
end