Bulls and cows: Difference between revisions
Content added Content deleted
(FutureBasic solution added) |
(→{{header|FutureBasic}}: Replaced with simpler code using same design) |
||
Line 3,251: | Line 3,251: | ||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
<syntaxhighlight lang="futurebasic"> |
<syntaxhighlight lang="futurebasic"> |
||
#build MinDeployment 10.14 |
|||
include "Tlbx GamePlayKit.incl" |
|||
include "NSLog.incl" |
|||
_window = 1 |
|||
str15 guess, goal |
|||
short x, y |
|||
cgRect wndrect |
|||
begin enum 1 |
begin enum 1 |
||
_window |
|||
_infoLabel |
|||
_num1Fld |
|||
_num2Fld |
|||
_num3Fld |
|||
_num4Fld |
|||
_bullLabel |
_bullLabel |
||
_cowLabel |
_cowLabel |
||
Line 3,266: | Line 3,264: | ||
_vertLine |
_vertLine |
||
_newGameBtn |
_newGameBtn |
||
_alert = 101 |
|||
end enum |
end enum |
||
void local fn showStr( string as str15 ) |
|||
_alert = 101 |
|||
short r |
|||
x = 20 |
|||
local fn SetWindowFrame( row as long ) as BOOL |
|||
for r = 1 to string[0] |
|||
print %(x,y)chr$( string[r] ); |
|||
r.origin.y = system( _scrnHeight ) |
|||
x += 39 |
|||
next |
|||
WindowSetContentSize( _window, r.size ) |
|||
end fn |
|||
WindowSetFrameTopLeftPoint( _window, r.origin ) |
|||
if ( r.size.height + 114 > r.origin.y ) then exit fn = NO |
|||
end fn = YES |
|||
void local fn NewGame |
void local fn NewGame |
||
str15 ch |
|||
long i, j, row = 0, bulls, cows, x, y = 67 |
|||
goal = "" : guess = "" :y = 20 |
|||
CFArrayRef c, g |
|||
window _window,,wndRect |
|||
CFStringRef guess |
|||
text ,,fn colorRed |
|||
cls |
cls |
||
fn |
fn showStr( "????" ) |
||
do |
|||
for i = _num1Fld to _num4Fld |
|||
ch = chr$(rnd(9) or _"0") |
|||
if instr$(0, goal, ch) == 0 then goal += ch |
|||
next |
|||
until goal[0] == 4 |
|||
nslog(@"%u",val&(goal)) //unrem for testing |
|||
c = fn ArraySubarrayWithRange( fn ArrayShuffledArray( @[@1,@2,@3,@4,@5,@6,@7,@8,@9] ), fn CFRangeMake(0,4) ) |
|||
y += 48 |
|||
textlabel _infoLabel |
|||
end fn |
|||
button _newGameBtn, NO |
|||
local fn SetWindowFrame |
|||
CGRect r = fn WindowContentRect( _window ) |
|||
r.size.height += 32 |
|||
r.origin.y -= 32 |
|||
window _window,,r |
|||
if ( r.origin.y < 150 ) |
|||
alert _alert,, @"Too many guesses!",, @"Give up", YES |
|||
fn newGame |
|||
end if |
|||
end fn |
|||
local fn play( ch as str15 ) |
|||
short r, bulls = 0, cows = 0 |
|||
if instr$(0, guess, ch) then exit fn |
|||
guess += ch |
|||
text,,fn colorDarkGray |
|||
fn showStr( guess ) |
|||
if guess[0] < 4 then exit fn |
|||
for r = 1 to 4 |
|||
if goal[r] == guess[r] then bulls++ : continue |
|||
guess = input %(-120,y+32)@"Guess the number:", @"123456789", YES, 4 |
|||
if ( |
if instr$(0, goal, chr$(guess[r]) ) then cows++ |
||
if ( len(guess) < 4 ) then beep : continue |
|||
g = @[mid(guess,0,1),mid(guess,1,1),mid(guess,2,1),mid(guess,3,1)] |
|||
mda(0) = {c[0],c[1],c[2],c[3]} |
|||
text , 24,,,, 57 |
|||
for i = 0 to 3 |
|||
print %(x,y)g[i]; |
|||
x += 39 |
|||
next |
|||
bulls = 0 : cows = 0 |
|||
for i = 0 to 3 |
|||
if ( intval(g[i]) == mda_integer(i) ) then bulls++ : mda(i) = 0 |
|||
next |
|||
for i = 0 to 3 |
|||
for j = 0 to 3 |
|||
if ( mda_integer(j) == intval(g[i]) ) then cows++ : mda(j) = 0 : break |
|||
next |
|||
next |
|||
x += 30 |
|||
print %(x,y)bulls, cows |
|||
if ( bulls == 4 ) |
|||
row++ : fn SetWindowFrame(row) |
|||
alert _alert,, @"You guessed the number!", guess,, YES |
|||
break |
|||
end if |
|||
row++ |
|||
if ( fn SetWindowFrame(row) == NO ) |
|||
alert _alert,, @"Too many guesses!",, @"Give up", YES |
|||
break |
|||
end if |
|||
x = 28 |
|||
y += 32 |
|||
wend |
|||
long fld = _num1Fld |
|||
for i = 0 to 3 |
|||
textlabel fld + i, c[i] |
|||
next |
next |
||
select |
|||
textlabel -_infoLabel |
|||
case bulls == 4 |
|||
button _newGameBtn, YES |
|||
text ,,fn colorRed |
|||
print %(x + 31, y)("W I N!") |
|||
y = 20 : fn showStr( goal ) |
|||
case else : print %(x + 35, y)bulls;" "; cows |
|||
y += 32 : guess = "" |
|||
end select |
|||
fn SetWindowFrame |
|||
end fn |
end fn |
||
void local fn BuildWindow |
void local fn BuildWindow |
||
subclass window _window, @"Bulls and cows", (0,0,311,114), NSWindowStyleMaskTitled + NSWindowStyleMaskClosable |
|||
long tag |
|||
wndrect = = fn WindowContentRect( _window ) |
|||
window _window, @"Bulls and cows", (0,0,311,114), NSWindowStyleMaskTitled + NSWindowStyleMaskClosable |
|||
textlabel _bullLabel, @"🐂", (198,59,38,40) |
|||
fn SetWindowFrame( 0 ) |
|||
textlabel _cowLabel, @"🐄", (255,59,38,40) |
|||
textlabel -_infoLabel, @"Press 'esc' key to give up", (18,98,283,14) |
|||
ControlSetSize( _infoLabel, NSControlSizeSmall ) |
|||
ControlSetAlignment( _infoLabel, NSTextAlignmentCenter ) |
|||
textlabel _num1Fld, @"-", (20,62,31,26) |
|||
textlabel _num2Fld, @"-", (59,62,31,26) |
|||
textlabel _num3Fld, @"-", (98,62,31,26) |
|||
textlabel _num4Fld, @"-", (137,62,31,26) |
|||
textlabel _bullLabel, @"🐂", (198,59,38,35) |
|||
textlabel _cowLabel, @"🐄", (255,59,38,35) |
|||
for tag = _num1Fld to _num4Fld |
|||
TextFieldSetTextColor( tag, fn ColorRed ) |
|||
ControlSetFontWithName( tag, @"Menlo", 24 ) |
|||
ControlSetAlignment( tag, NSTextAlignmentCenter ) |
|||
next |
|||
ControlSetFontWithName( _bullLabel, NULL, 30 ) |
ControlSetFontWithName( _bullLabel, NULL, 30 ) |
||
ControlSetAlignment( _bullLabel, NSTextAlignmentCenter ) |
|||
ControlSetFontWithName( _cowLabel, NULL, 30 ) |
ControlSetFontWithName( _cowLabel, NULL, 30 ) |
||
ControlSetAlignment( _cowLabel, NSTextAlignmentCenter ) |
|||
box _horzLine,, (12,50,287,5), NSBoxSeparator |
box _horzLine,, (12,50,287,5), NSBoxSeparator |
||
box _vertLine,, (180,12,5, |
box _vertLine,, (180,12,5,90), NSBoxSeparator |
||
ViewSetAutoresizingMask( _vertLine, NSViewHeightSizable ) |
ViewSetAutoresizingMask( _vertLine, NSViewHeightSizable ) |
||
button _newGameBtn,,, @"New Game", (198,13,100,32) |
button _newGameBtn,,, @"New Game", (198,13,100,32) |
||
ViewSetAutoresizingMask( _newGameBtn, NSViewMaxYMargin ) |
ViewSetAutoresizingMask( _newGameBtn, NSViewMaxYMargin ) |
||
text @"menlo bold",24,,fn ColorWindowBackground |
|||
end fn |
end fn |
||
void local fn |
void local fn DoDialog( evt as long, tag as long ) |
||
select ( |
select ( evt ) |
||
case |
case _windowKeyDown //: stop |
||
short ch = intval( fn EventCharacters ) |
|||
case _appShouldTerminateAfterLastWindowClosed : AppEventSetBool(YES) |
|||
if ch then fn play( chr$( ch or _"0" ) ):DialogEventSetBool(YES) |
|||
end select |
|||
end fn |
|||
void local fn DoDialog( ev as long, tag as long ) |
|||
select ( ev ) |
|||
case _btnClick : fn NewGame |
case _btnClick : fn NewGame |
||
case _windowWillClose : end |
|||
end select |
end select |
||
end fn |
end fn |
||
on appevent fn DoAppEvent |
|||
on dialog fn DoDialog |
on dialog fn DoDialog |
||
fn buildWindow |
|||
fn newGame |
|||
HandleEvents |
HandleEvents |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{out}} |
|||
[[file:FutureBasic - Bulls and cows.png]] |
|||
[[File:Bulls and Cows in FutureBasic.png]] |
|||
=={{header|Go}}== |
=={{header|Go}}== |