24 game: Difference between revisions

Content added Content deleted
Line 5,852: Line 5,852:


void local fn EraseErrorText
void local fn EraseErrorText

CGRect r
CGRect r
r = fn CGRectMake(10, 200, 400, 15)
r = fn CGRectMake(10, 200, 400, 15)
rect fill r,fn ColorBlack
rect fill r,fn ColorBlack

end fn
end fn


local fn ArcRandom( a as long, b as long ) as long
local fn ArcRandom( a as long, b as long ) as long
long i
long i
cln i = (arc4random()%(b-a+1))+a;
cln i = (arc4random()%(b-a+1))+a;
end fn = fn floor(i)
end fn = fn floor(i)


local fn GetRandomNumbers as CFStringRef
local fn GetRandomNumbers as CFStringRef
CFArrayRef combos = @[¬
CFArrayRef combos = @[¬
@"1 3 2 6", @"1 7 2 1", @"1 5 2 2", @"1 9 2 1", @"3 7 6 2",¬
@"1 3 2 6", @"1 7 2 1", @"1 5 2 2", @"1 9 2 1", @"3 7 6 2",¬
@"1 2 3 9", @"1 6 3 1", @"1 4 3 3", @"1 4 5 6", @"1 3 4 4",¬
@"1 2 3 9", @"1 6 3 1", @"1 4 3 3", @"1 4 5 6", @"1 3 4 4",¬
@"1 1 5 4", @"1 9 3 1", @"2 2 3 2", @"2 6 3 1", @"6 4 9 8",¬
@"1 1 5 4", @"1 9 3 1", @"2 2 3 2", @"2 6 3 1", @"6 4 9 8",¬
@"2 8 2 1", @"3 2 5 2", @"6 5 4 4", @"2 6 2 1", @"2 4 3 1",¬
@"2 8 2 1", @"3 2 5 2", @"6 5 4 4", @"2 6 6 8", @"2 4 3 1",¬
@"2 8 1 1", @"2 9 1 1", @"3 9 6 2", @"3 5 2 1", @"3 3 2 2",¬
@"2 8 1 1", @"2 9 1 1", @"3 9 6 2", @"3 5 2 1", @"3 3 2 2",¬
@"3 7 1 1", @"3 2 4 1", @"2 5 7 8", @"3 8 1 6", @"4 2 9 1",¬
@"3 7 1 1", @"3 2 4 1", @"2 5 7 8", @"3 8 1 6", @"4 2 9 1",¬
@"4 4 2 1", @"4 8 1 1", @"8 8 9 6", @"4 6 1 1", @"5 3 2 1",¬
@"4 4 2 1", @"4 8 1 1", @"8 8 9 6", @"4 6 1 1", @"5 3 2 1",¬
@"5 7 1 1", @"5 1 2 2", @"1 5 1 4", @"5 2 2 2", @"5 4 1 1",¬
@"5 7 1 1", @"5 1 2 2", @"1 5 1 4", @"5 2 2 2", @"5 4 1 1",¬
@"5 8 1 1", @"3 4 3 3", @"6 6 2 1", @"6 2 2 1", @"6 1 4 1",¬
@"5 8 1 1", @"3 4 3 3", @"6 6 2 1", @"6 7 8 9", @"6 1 4 1",¬
@"9 6 2 3", @"9 5 5 9", @"7 1 3 1", @"7 5 1 1", @"8 1 8 6",¬
@"9 6 2 3", @"9 5 5 9", @"7 1 3 1", @"7 5 1 1", @"8 1 8 6",¬
@"7 1 2 2", @"7 2 1 1", @"7 4 1 1", @"8 3 1 1", @"8 4 2 1",¬
@"7 1 2 2", @"7 2 1 1", @"7 4 1 1", @"7 8 8 9", @"8 4 2 1",¬
@"8 1 3 1", @"8 2 1 1", @"9 3 2 1", @"2 3 5 3", @"9 1 2 1",¬
@"4 5 7 9", @"8 2 1 1", @"9 3 2 1", @"2 3 5 3", @"9 1 2 1",¬
@"9 1 8 3", @"9 2 4 3", @"9 4 1 3", @"4 7 8 2", @"1 8 6 2",¬
@"9 1 8 3", @"9 2 4 3", @"9 4 1 3", @"4 7 8 2", @"1 8 6 2",¬
@"6 1 6 1", @"1 6 2 2", @"1 8 3 1", @"5 7 1 3", @"6 2 3 4",¬
@"6 1 6 1", @"3 5 6 8", @"1 8 3 1", @"5 7 1 3", @"6 2 3 4",¬
@"5 5 6 1", @"2 6 1 2", @"8 1 2 4", @"4 9 6 1", @"6 4 1 2",¬
@"5 5 6 1", @"2 6 1 2", @"8 1 2 4", @"4 9 6 1", @"6 4 1 2",¬
@"4 7 3 2", @"5 5 4 9", @"7 3 6 2", @"6 1 2 2", @"6 2 1 2",¬
@"4 7 3 2", @"5 5 4 9", @"7 3 6 2", @"5 5 6 7", @"6 2 1 2",¬
@"9 2 3 7", @"1 2 5 2", @"5 3 4 7", @"1 3 8 1", @"1 4 5 2",¬
@"9 2 3 7", @"1 2 5 2", @"5 3 4 7", @"1 1 3 5", @"1 4 5 2",¬
@"1 4 7 1", @"1 5 6 2", @"1 5 8 1", @"2 6 7 1", @"2 3 4 2",¬
@"1 4 7 1", @"1 5 6 2", @"1 5 8 1", @"2 6 7 1", @"2 3 4 2",¬
@"2 3 6 1", @"2 4 5 1", @"3 4 5 9", @"4 1 2 9", @"2 1 3 5",¬
@"2 3 6 1", @"2 4 5 1", @"3 4 5 9", @"4 1 2 9", @"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]
end fn = combos[i]




local fn EvaluateMath( equation as CFStringRef ) as CFStringRef
local fn EvaluateMath( equation as CFStringRef ) as CFStringRef
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 originalRange
CFRange adjustedRange
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 result is an integer and, if so, return result as an integer
if( fn StringDoubleValue( result ) == fn floorf( fn StringDoubleValue( result ) ) )
if( fn StringDoubleValue( result ) == fn floorf( fn StringDoubleValue( result ) ) )
result = fn ArrayFirstObject( fn StringComponentsSeparatedByString( result, @"." ) )
result = fn ArrayFirstObject( fn StringComponentsSeparatedByString( result, @"." ) )
end if
end if
end fn = result
end fn = result
Line 5,932: Line 5,932:


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




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

end fn
end fn


Line 5,956: Line 5,956:
CFStringRef CheckForDuplicates(97)
CFStringRef CheckForDuplicates(97)
for i = 1 to 96
for i = 1 to 96
CheckForDuplicates(i) = @""
CheckForDuplicates(i) = @""
next i
next i


Line 5,983: Line 5,983:


for i = 0 to 3
for i = 0 to 3
// create a string from the 4 numbers
// create a string from the 4 numbers
RandomNumberblockAdd = randomNumbers[i]
RandomNumberblockAdd = randomNumbers[i]
RandomNumberblock = fn StringByAppendingString(RandomNumberblock,RandomNumberblockAdd)
RandomNumberblock = fn StringByAppendingString(RandomNumberblock,RandomNumberblockAdd)
RandomNumberblock = fn StringByAppendingString(RandomNumberblock,@" ")
RandomNumberblock = fn StringByAppendingString(RandomNumberblock,@" ")
next i
next i




if DuplicatesCounter = > 96
if DuplicatesCounter = > 96
// reset counter when last number is retrieved and start from the first number block
// reset counter when last number is retrieved and start from the first number block
DuplicatesCounter = 0
DuplicatesCounter = 0
for i = 1 to 96
for i = 1 to 96
CheckForDuplicates(i) = @""
CheckForDuplicates(i) = @""
next i
next i
end if
end if


for i = 1 to 96
for i = 1 to 96
// check the current numbers with the numbers already used
// check the current numbers with the numbers already used
if fn StringIsEqual(RandomNumberblock,CheckForDuplicates(i))
if fn StringIsEqual(RandomNumberblock,CheckForDuplicates(i))
RandomNumberblock = fn StringWithString(CheckForDuplicates(DuplicatesCounter))
RandomNumberblock = fn StringWithString(CheckForDuplicates(DuplicatesCounter))
goto "GetFourNumbers"
goto "GetFourNumbers"
end if
end if
next i
next i


Line 6,026: Line 6,026:
text ,18,fn colorGreen
text ,18,fn colorGreen
for i = 1 to 4
for i = 1 to 4
print d(i); " ";
print d(i); " ";
next
next
print
print
Line 6,041: Line 6,041:


if TryAgain
if TryAgain
MessageText = fn StringWithFormat( @"Enter math expression: [ '%@' was incorrect ]", expr )
MessageText = fn StringWithFormat( @"Enter math expression: [ '%@' was incorrect ]", expr )
UserInput = input %(10, 190), MessageText, @"123456789+-*/()qs", YES,, 0
UserInput = input %(10, 190), MessageText, @"123456789+-*/()qs", YES,, 0
else
else
UserInput = input %(10, 190), @"Enter math expression:", @"123456789+-*/()qs", YES,, 0
UserInput = input %(10, 190), @"Enter math expression:", @"123456789+-*/()qs", YES,, 0
end if
end if


Line 6,065: Line 6,065:


for i = 1 to len$(fn stringpascalstring(expr))
for i = 1 to len$(fn stringpascalstring(expr))
if asc(mid$(fn stringpascalstring(expr),i,1)) > 48 && asc(mid$(fn stringpascalstring(expr),i,1)) < 58
if asc(mid$(fn stringpascalstring(expr),i,1)) > 48 && asc(mid$(fn stringpascalstring(expr),i,1)) < 58
ExtraNumbers ++
ExtraNumbers ++
end if
end if
next i
next i


if ExtraNumbers > 4
if ExtraNumbers > 4
fn EraseErrorText
fn EraseErrorText
text ,,fn colorRed
text ,,fn colorRed
TryAgain = _true
TryAgain = _true
print %(10,200);"Error! Extra numbers not allowed": goto "InputExpression"
print %(10,200);"Error! Extra numbers not allowed": goto "InputExpression"
text ,,fn colorWhite
text ,,fn colorWhite
end if
end if




for i = 1 to 4
for i = 1 to 4
GotaNumber = 0
GotaNumber = 0
for j = 0 to len(expr) -1
for j = 0 to len(expr) -1
ThisNumberPosition = instr( j, expr, right(str( d(i)),1 ))
ThisNumberPosition = instr( j, expr, right(str( d(i)),1 ))
ThisNumberPosition ++
ThisNumberPosition ++
if ThisNumberPosition then GotaNumber = _true
if ThisNumberPosition then GotaNumber = _true
next j
next j
if GotaNumber then TotalNumbers ++
if GotaNumber then TotalNumbers ++
next i
next i


Line 6,092: Line 6,092:


if GotAllNumbers = _false
if GotAllNumbers = _false
fn EraseErrorText
fn EraseErrorText
text ,,fn colorRed
text ,,fn colorRed
TryAgain = _true
TryAgain = _true
print %(10,200);"ERROR! Must use all your numbers and only those numbers." : goto "InputExpression"
print %(10,200);"ERROR! Must use all your numbers and only those numbers." : goto "InputExpression"
text ,,fn colorWhite
text ,,fn colorWhite
end if
end if


Line 6,103: Line 6,103:


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


Line 6,114: Line 6,114:


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