24 game: Difference between revisions

259 bytes removed ,  19 days ago
Line 5,839:
=={{header|FutureBasic}}==
24 Game
 
May 12, 2024 Rich Love
May 13, 2024
 
Rich Love
 
Modified to include 96 blocks of numbers for random selection.
 
Thanks to Ken and Bernie
<syntaxhighlight lang="futurebasic">
Line 5,849 ⟶ 5,854:
 
void local fn EraseErrorText
 
CGRect r
r = fn CGRectMake(10, 200, 400, 15)
rect fill r,fn ColorBlack
 
end fn
 
local fn ArcRandom( a as long, b as long ) as long
'~'1
long i
long i
cln i = (arc4random()%(b-a+1))+a;
cln i = (arc4random()%(b-a+1))+a;
end fn = fn floor(i)
 
local fn GetRandomNumbers as CFStringRef
'~'1
CFArrayRef combos = @[¬
CFArrayRef combos = @[¬
@"1 3 2 6", @"1 7 2 1", @"1 5 2 2", @"1 9 2 1", @"1 8 3 1",¬
@"1 2 3 92 6", @"1 67 32 1", @"1 45 32 32", @"1 49 52 61", @"1 3 47 46 2",¬
@"1 12 53 49", @"1 96 3 1", @"21 24 3 23", @"21 64 35 16", @"2 41 3 14 4",¬
@"21 81 25 14", @"31 29 53 21", @"62 52 43 42", @"2 6 23 1", @"26 4 39 18",¬
@"2 8 12 1", @"3 2 95 1 12", @"36 15 24 4", @"32 56 2 1", @"32 4 3 2 21",¬
@"32 78 1 1", @"3 2 49 1 1", @"23 59 76 82", @"3 85 12 1", @"4 23 3 12 2",¬
@"43 47 21 1", @"43 82 14 1", @"82 85 97 68", @"43 68 1 1", @"5 34 2 3 1",¬
@"54 74 12 1", @"54 8 1 2 21", @"18 58 19 46", @"54 26 21 21", @"5 43 12 1",¬
@"5 87 1 1", @"35 41 32 32", @"61 6 25 1 4", @"65 2 2 12", @"6 15 4 1 1",¬
@"65 28 21 1", @"93 54 53 93", @"76 16 32 1", @"76 52 12 1", @"86 1 84 61",¬
@"79 16 2 23", @"79 25 15 19", @"7 4 1 3 1", @"87 35 1 1", @"8 41 28 16",¬
@"87 1 32 12", @"87 2 1 1", @"97 34 21 1", @"28 3 51 31", @"98 14 2 1",¬
@"98 1 8 3 1", @"98 2 41 31", @"9 43 12 31", @"2 13 25 23", @"9 1 3 2 41",¬
@"69 1 68 13", @"1 69 2 24 3", @"9 4 1 8 3 1", @"5 72 1 32 2", @"21 3 12 4",¬
@"56 51 6 1", @"21 6 12 2", @"1 8 3 1 2 4", @"45 97 61 13", @"2 3 4 1 24",¬
@"45 15 26 31", @"52 56 41 92", @"4 3 1 2 4", @"4 9 6 1 2 2", @"63 24 1 2",¬
@"14 27 3 42", @"1 25 5 24 9", @"14 3 41 2", @"6 1 32 8 12", @"16 42 51 2",¬
@"1 42 73 14", @"1 52 65 2", @"1 53 84 12", @"1 63 78 1", @"2 31 4 5 2",¬
@"21 34 67 1", @"2 41 5 16 2", @"31 45 58 1", @"4 1 26 7 31", @"2 1 3 54 2",¬
@"2 3 6 1", @"2 4 5 1", @"3 4 5 1", @"4 1 2 3", @"2 1 3 5",¬
@"2 2 3 4"]
@"2 2 3 4"]
 
long i = fn ArcRandom( 0, len(combos) - 1 )
long i = fn ArcRandom( 0, len(combos) - 1 )
end fn = combos[i]
 
 
local fn EvaluateMath( equation as CFStringRef ) as CFStringRef
'~'1
equation = fn StringByReplacingOccurrencesOfString( lcase(equation), @"x", @"*" )
equation = fn StringByReplacingOccurrencesOfString( lcase(equation), @"x", @"*" )
 
CFStringRef result = NULL
CFStringRef result = NULL
RegularExpressionRef regex = fn RegularExpressionWithPattern( @"[0-9.]+", NSRegularExpressionCaseInsensitive, NULL )
RegularExpressionRef regex = fn RegularExpressionWithPattern( @"[0-9.]+", NSRegularExpressionCaseInsensitive, NULL )
CFArrayRef matches = fn RegularExpressionMatches( regex, equation, 0, fn CFRangeMake( 0, len(equation) ) )
CFArrayRef matches = fn RegularExpressionMatches( regex, equation, 0, fn CFRangeMake( 0, len(equation) ) )
NSInteger intConversions = 0
NSInteger intConversions = 0
TextCheckingResultRef match
TextCheckingResultRef match
CFRange originalRange
CFRange adjustedRangeoriginalRange
CFRange adjustedRange
CFStringRef value
CFStringRef value
 
for match in matches
for match in matches
originalRange = fn TextCheckingResultRange( match )
originalRange = fn TextCheckingResultRange( match )
adjustedRange = fn CFRangeMake( ( originalRange.location + ( intConversions * len( @".0") ) ), originalRange.length )
adjustedRange = fn CFRangeMake( ( originalRange.location + ( intConversions * len( @".0") ) ), originalRange.length )
value = fn StringSubstringWithRange( equation, adjustedRange )
value = fn StringSubstringWithRange( equation, adjustedRange )
if fn StringContainsString( value, @"." )
if fn StringContainsString( value, @"." )
continue
continue
else
else
equation = fn StringByReplacingCharactersInRange( equation, adjustedRange, fn StringWithFormat( @"%@.0", value ) )
equation = fn StringByReplacingCharactersInRange( equation, adjustedRange, fn StringWithFormat( @"%@.0", value ) )
intConversions++
intConversions++
end if
end if
next
next
ExceptionRef e
ExceptionRef e
try
try
ExpressionRef expression = fn ExpressionWithFormat( equation )
ExpressionRef expression = fn ExpressionWithFormat( equation )
CFNumberRef number = fn ExpressionValueWithObject( expression, NULL, NULL )
CFNumberRef number = fn ExpressionValueWithObject( expression, NULL, NULL )
result = fn StringWithFormat( @"%.3f", dblval( number ) )
result = fn StringWithFormat( @"%.3f", dblval( number ) )
end try
end try
 
catch (e)
catch (e)
result = fn StringWithFormat( @"%@", e ) : exit fn
result = fn StringWithFormat( @"%@", e ) : exit fn
end catch
end catch
// Test if result is an integer and, if so, return result as an integer
// Test if( fn StringDoubleValue( result )is ==an fninteger floorf(and, if fnso, StringDoubleValue(return result )as )an )integer
if( fn StringDoubleValue( result ) == fn ArrayFirstObjectfloorf( fn StringComponentsSeparatedByStringStringDoubleValue( result, @".") ) )
result = fn ArrayFirstObject( fn StringComponentsSeparatedByString( result, @"." ) )
end if
end fn = result
Line 5,929 ⟶ 5,937:
 
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
 
Line 5,964 ⟶ 5,972:
print %(10,30),"possible use of parenthesis (), enter an expression that equates to 24."
print %(10,45),"You must use all your numbers and only those numbers."
print %(10,60),"Examples: 9618 Solution 9 + 6 + 1 + 8 or 37733173 Solution ((3 * 1) * 7) + 3"
print
print %(10,85),"Enter Q to quit"
Line 5,992 ⟶ 6,000:
text ,18,fn colorGreen
for i = 1 to 4
print d(i); " ";
next
 
Line 6,011 ⟶ 6,019:
 
if TryAgain
MessageText = "Enter math expression: [ " + expr + " was incorrect ]"
MessageTextRef = fn StringWithPascalString(MessageText)
UserInput = input % (10, 190),MessageTextRef, @"123456789+-*/()q", YES,, 0
else
UserInput = input % (10, 190),@"Enter math expression:", @"123456789+-*/()q", YES,, 0
end if
 
Line 6,022 ⟶ 6,030:
if expr = "" then "InputExpression"
if ucase$(expr) = "Q" then appterminate
if ucase$(expr) = "X" then appterminate
 
 
Line 6,039 ⟶ 6,046:
 
for i = 1 to 4
GotaNumber = 0
for j = 1 to len$(expr)
ThisNumberPosition = instr$(j,expr,right$(str$(d(i)),1))
if ThisNumberPosition then GotaNumber = 1
next j
if GotaNumber then TotalNumbers ++
next i
 
Line 6,052 ⟶ 6,059:
 
if GotAllNumbers = _false
fn EraseErrorText
text ,,fn colorRed
TryAgain = _true
print %(10,200);"ERROR! Must use all your numbers and only those numbers.":goto "InputExpression"
text ,,fn colorWhite
end if
 
Line 6,063 ⟶ 6,070:
 
if fn EvaluateMath( fn StringWithPascalString (expr)) = _false
text ,,fn colorRed
TryAgain = _true
Print %(10,200);"Error! Incorrect math sequence."
goto "InputExpression"
text ,,fn colorWhite
end if
 
Line 6,081 ⟶ 6,088:
 
if GameResult = @"Incorrect"
TryAgain = _true
goto "InputExpression"
end if
 
44

edits