24 game: Difference between revisions
→{{header|FutureBasic}}
Line 5,862:
local fn ArcRandom( a as long, b as long ) as long
long i
cln i = (arc4random()%(b-a+1))+a;
Line 5,868 ⟶ 5,867:
local fn GetRandomNumbers as CFStringRef
CFArrayRef combos = @[¬
@"1 3 2 6", @"1 7 2 1", @"1 5 2 2", @"1 9 2 1", @"3 7 6 2",¬
Line 5,875 ⟶ 5,873:
@"2 8 2 1", @"3 2 5 2", @"6 5 4 4", @"2 6 2 1", @"2 4 3 1",¬
@"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
@"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",¬
Line 5,882 ⟶ 5,880:
@"7 1 2 2", @"7 2 1 1", @"7 4 1 1", @"8 3 1 1", @"8 4 2 1",¬
@"8 1 3 1", @"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", @"
@"6 1 6 1", @"1 6 2 2", @"1 8 3 1", @"5 7 1 3", @"6 2 3
@"5 5 6 1", @"2 6 1 2", @"
@"4 7 3 2", @"5 5 4 9", @"
@"
@"1 4 7 1", @"1 5 6 2", @"1 5 8 1", @"
@"2 3 6 1", @"2 4 5 1", @"3 4 5
@"2 2 3 4"]
Line 5,896 ⟶ 5,894:
local fn EvaluateMath( equation as CFStringRef ) as CFStringRef
equation = fn StringByReplacingOccurrencesOfString( lcase(equation), @"x", @"*" )
Line 5,937 ⟶ 5,934:
local fn QuitOrPlayAlert(GameResult as CFStringRef)
alert -2,,GameResult,@"You won!",@"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
Line 5,955 ⟶ 5,949:
end fn
///////// Start //////////
fn BuildWindow
short d(4), i
CFStringRef CheckForDuplicates(97)
for i = 1 to 96
CheckForDuplicates(i) = @""
next i
short DuplicatesCounter
DuplicatesCounter = 0
Line 5,968 ⟶ 5,971:
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."
Line 5,974 ⟶ 5,976:
print %(10,60),"Examples: 9618 Solution 9 + 6 + 1 + 8 or 3173 Solution ((3 * 1) * 7) + 3"
print
print %(10,85),"Enter Q to quit or S to skip to the next number."
"GetFourNumbers"
CFArrayRef randomNumbers : randomNumbers= fn StringComponentsSeparatedByString( fn GetRandomNumbers, @" " )
CFStringRef RandomNumberblock : RandomNumberblock = @""
CFStringRef RandomNumberblockAdd : RandomNumberblockAdd = @""
for i = 0 to 3
// create a string from the 4 numbers
RandomNumberblockAdd = randomNumbers[i]
RandomNumberblock = fn StringByAppendingString(RandomNumberblock,RandomNumberblockAdd)
RandomNumberblock = fn StringByAppendingString(RandomNumberblock,@" ")
next i
if DuplicatesCounter = > 96
// reset counter when last number is retrieved and start from the first number block
DuplicatesCounter = 0
for i = 1 to 96
CheckForDuplicates(i) = @""
next i
end if
for i = 1 to 96
// check the current numbers with the numbers already used
if fn StringIsEqual(RandomNumberblock,CheckForDuplicates(i))
RandomNumberblock = fn StringWithString(CheckForDuplicates(DuplicatesCounter))
goto "GetFourNumbers"
end if
next i
DuplicatesCounter ++
CheckForDuplicates(DuplicatesCounter) = fn StringWithString(RandomNumberblock)
d(1) = fn StringIntegerValue( randomNumbers[0] )
d(2) = fn StringIntegerValue( randomNumbers[1] )
d(3) = fn StringIntegerValue( randomNumbers[2] )
d(4) = fn StringIntegerValue( randomNumbers[3] )
//d(1) = 9:d(2) = 6:d(3) = 1:d(4) = 8 // Uncomment to test with 9618 numbers. Solution 9 + 6 + 1 + 8
Line 5,994 ⟶ 6,023:
print
text ,,fn colorGreen
print %(10,110),"These are your numbers: "
Line 6,002 ⟶ 6,030:
print d(i); " ";
next
print
text ,12,fn colorWhite
printf @"\n\n\n"
CFStringRef expr
bool TryAgain : TryAgain = _false
CFStringRef MessageText
CFStringRef UserInput = NULL
"InputExpression"
if TryAgain
MessageText = fn StringWithFormat( @"Enter math expression: [
UserInput = input %(10, 190), MessageText, @"123456789+-*/()qs", YES,, 0
else
UserInput = input %
end if
if ( UserInput == NULL ) then "InputExpression"
expr = UserInput
if expr = @"" then "InputExpression"
if fn StringIsEqual(ucase
if fn StringIsEqual(ucase(expr) , @"S") then "Main"
//check expr for validity
short j
bool GotAllNumbers : GotAllNumbers = _false
short ThisNumberPosition : ThisNumberPosition = 0
short GotaNumber : GotaNumber = 0
short TotalNumbers : TotalNumbers = 0
for i = 1 to 4
GotaNumber = 0
for j =
ThisNumberPosition = instr
if ThisNumberPosition then GotaNumber = _true
next j
if GotaNumber then TotalNumbers ++
next i
if TotalNumbers => 4 then GotAllNumbers = _true
if GotAllNumbers = _false
Line 6,062 ⟶ 6,079:
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
fn EraseErrorText
if fn EvaluateMath(
text ,,fn colorRed
TryAgain = _true
Line 6,077 ⟶ 6,094:
end if
CFStringRef GameResult
if fn StringIntegerValue( fn EvaluateMath( expr ) ) == 24 then GameResult = @"Correct" else GameResult = @"Incorrect"
if GameResult = @"Incorrect"
Line 6,094 ⟶ 6,104:
fn QuitOrPlayAlert(GameResult)
goto "Main"
handleevents
|