24 game: Difference between revisions

Content added Content deleted
Line 5,836: Line 5,836:


</pre>
</pre>

=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
// 24 Game
// May 2024 Rich Love

#build CompilerOptions @"-Wno-unused-variable"

_mEdit = 2
editmenu _mEdit

void local fn EraseErrorText
CGRect r
r = fn CGRectMake(10, 200, 300, 15)
rect fill r,fn ColorBlack
end fn


local fn EvaluateMath( equation as CFStringRef ) as CFStringRef
equation = fn StringByReplacingOccurrencesOfString( lcase(equation), @"x", @"*" )
equation = fn StringByReplacingOccurrencesOfString( equation, @"%", @"*0.01" )
equation = fn StringByReplacingOccurrencesOfString( equation, @"^", @"**" )
CFStringRef result = NULL
RegularExpressionRef regex = fn RegularExpressionWithPattern( @"[0-9.]+", NSRegularExpressionCaseInsensitive, NULL )
CFArrayRef matches = fn RegularExpressionMatches( regex, equation, 0, fn CFRangeMake( 0, len(equation) ) )
NSInteger intConversions = 0
TextCheckingResultRef match
CFRange originalRange
CFRange adjustedRange
CFStringRef value
for match in matches
originalRange = fn TextCheckingResultRange( match )
adjustedRange = fn CFRangeMake( ( originalRange.location + ( intConversions * len( @".0") ) ), originalRange.length )
value = fn StringSubstringWithRange( equation, adjustedRange )
if fn StringContainsString( value, @"." )
continue
else
equation = fn StringByReplacingCharactersInRange( equation, adjustedRange, fn StringWithFormat( @"%@.0", value ) )
intConversions++
end if
next
ExceptionRef e
try
ExpressionRef expression = fn ExpressionWithFormat( equation )
CFNumberRef number = fn ExpressionValueWithObject( expression, NULL, NULL )
result = fn StringWithFormat( @"%.3f", dblval( number ) )
end try
catch (e)
result = fn StringWithFormat( @"%@", e ) : exit fn
end catch
// Test if result is an integer and, if so, return result as an integer
if( fn StringDoubleValue( result ) == fn floorf( fn StringDoubleValue( result ) ) )
result = fn ArrayFirstObject( fn StringComponentsSeparatedByString( result, @"." ) )
end if
end fn = result


local fn WasDuplicateCharacter(TheString as str255) as boolean
bool NumberUsedTwice
NumberUsedTwice = _false
short i
short dc(50) // should not need 50 but just in case
for i = 1 to 50
dc(i) = 0
next i
short counter
counter = 0
short j
for i = 1 to len$(TheString)
if asc(mid$(TheString,i,1)) > 48 && asc(mid$(TheString,i,1)) < 58
counter ++
dc(counter) = val(mid$(TheString,i,1))
end if
next i
short jCounter
jCounter = 0
for i = 1 to 4
for j = 1 to len$(TheString)
if val(str$(dc(i))) = int(val(mid$(TheString,j,1))) then jCounter ++
next j
next i
if jCounter > 4 then NumberUsedTwice = _true
if counter < 4 then NumberUsedTwice = _false
end fn = NumberUsedTwice



local fn QuitOrPlayAlert(GameResult as CFStringRef)
alert -2,,GameResult,@"Game Over",@"Quit;Play Again"
AlertButtonSetKeyEquivalent( 2, 2, @"\e" )
short result
result = alert 2
if ( result != NSAlertSecondButtonReturn ) then appterminate
end fn


local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 580, 250)
window 1, @"24 Game", r
windowcenter(1)
WindowSetBackgroundColor(1,fn ColorBlack)
end fn


///////// Start //////////

fn BuildWindow


"Main"

cls
text ,,fn colorWhite

print

print %(10,15),"Given four numbers and using just the +, -, *, and / operators; and the"
print %(10,30),"possible use of parenthesis (), enter an expression that equates to 24."
print %(10,45),"You can only use the numbers given and cannot use any number more than once."
print %(10,60),"Examples: 9618 Solution 9 + 6 + 1 + 8 or 6538 Solution 6 / ( 5 - 3 ) * 8"
print
print %(10,85),"Enter X or Q to quit"


"GetRandomNumbers"
short i
short d(4)

for i = 1 to 4
d(i)= rnd(9) // digets 1..9
next

//d(1) = 9:d(2) = 6:d(3) = 1:d(4) = 8 // Uncomment to test with 9618 numbers. Solution 9 + 6 + 1 + 8
//d(1) = 6:d(2) = 5:d(3) = 3:d(4) = 8 // Uncomment to test with 6538 numbers. Solution 6 / ( 5 - 3 ) * 8

bool NumberUsedTwice
NumberUsedTwice = _false
if d(1) = d(2) || d(1) = d(3) || d(1) = d(4) then NumberUsedTwice = _true
if d(2) = d(3) || d(2) = d(4) then NumberUsedTwice = _true
if d(3) = d(4) then NumberUsedTwice = _true

if NumberUsedTwice then "GetRandomNumbers"

print

text ,,fn colorGreen
print %(10,110),"These are your numbers: "
print %(10,125)
text ,18,fn colorGreen
for i = 1 to 4
print d(i); " ";
next

print

text ,12,fn colorWhite

print:print:print:print

str255 expr
bool TryAgain
TryAgain = _false
str255 MessageText
CFStringRef MessageTextRef

CFStringRef UserInput = 0
"InputExpression"

if TryAgain
MessageText = "Enter math expression: [ " + expr + " was incorrect ]"
MessageTextRef = fn StringWithPascalString(MessageText)
UserInput = input % (10, 190),MessageTextRef
else
UserInput = input % (10, 190),@"Enter math expression:"
end if

if ( UserInput == NULL ) then "InputExpression"
fn CFStringGetPascalString (UserInput, @expr, 256, _kCFStringEncodingMacRoman)
if expr = "" then "InputExpression"
if ucase$(expr) = "Q" then appterminate
if ucase$(expr) = "X" then appterminate


//check expr for validity

short MatchNumber
MatchNumber = 0

short Number
str255 NumberString

for i = 1 to 4
Number = d(i)
NumberString = right$(str$(Number),1)
if instr$(1,expr,NumberString ) > 0 then MatchNumber ++
next i

if MatchNumber < 4
fn EraseErrorText
text ,,fn colorRed
print %(10,200);"ERROR! Must use all numbers": goto "InputExpression"
text ,,fn colorWhite
end if

if fn WasDuplicateCharacter(expr)
fn EraseErrorText
text ,,fn colorRed
print %(10,200);"Error! Cannot use the same number twice": goto "InputExpression"
text ,,fn colorWhite
end if

fn EraseErrorText

if fn EvaluateMath( fn StringWithPascalString (expr)) = _false
text ,,fn colorRed
Print %(10,200);"Error! Incorrect math sequence."
goto "InputExpression"
text ,,fn colorWhite
end if


CFStringRef AnswerRef
AnswerRef = fn EvaluateMath( fn StringWithPascalString (expr))
CFStringRef GameResult
str255 AnswerString
AnswerString = fn StringPascalString(AnswerRef)

if int(val(AnswerString)) = 24 then GameResult = @"Correct" else GameResult = @"Incorrect"


if GameResult = @"Incorrect"
TryAgain = _true
goto "InputExpression"
end if

fn QuitOrPlayAlert(GameResult)
goto "Main"


handleevents
</syntaxhighlight>



=={{header|GAP}}==
=={{header|GAP}}==