Jump to content

Cracker Barrel IQ tests


zfisherdrums
 Share

Recommended Posts

My wife and I occasionally dine at Cracker Barrel. No joke.

For the uninitiated, they have these "IQ" tests on the tables that - after playing them for awhile - I began to concoct a way to have the computer do all the heavy lifting. The script below is the fruit of my labor.

IQTest.au3

Today I tested it and found it to work as intended. I also was informed that I could have simply purchased it because it includes the solutions. Guess that makes me an idiot. Oh well. It was still fun to figure out.

Zach...

EDIT: See Ziggyny's post ( #6 ) for an excellent explaination of how the game is played.

Edited by zfisherdrums
Link to comment
Share on other sites

Hello gesller,

When you say it doesn't work, what specifically doesn't work? Are you receiving error messages or is it just taking a long time to run?

If it is the later, that is to be expected. The current script generates a solution for all 15 possible configurations and the recursive algorithm takes quite some time depending on the system. If you drop a small Sleep command in line 122 that will reduce the CPU usage but greatly increase the solve time.

One other thing to try is running the script to obtain the solution for only 1 configuration. To do that, replace the following:

For $X = 1 to 15
    Debug( $header )
    $GameBoard = CreateNewBoard( $x )
    Debug( GetBoardState( $GameBoard ) )
    PlayGame( $GameBoard )
    Debug( ) 
Next

...with this:

Debug( $header )
$GameBoard = CreateNewBoard( 4 )
Debug( GetBoardState( $GameBoard ) )
PlayGame( $GameBoard )
Debug( )

Notice that CreateNewBoard receives a 4 indicating that the game begins with the fourth hole empty. If it works, you should see the following:

1  2  3  4  5  6  7  8  9  A  B  C  D  E  F 
(X)(X)(X)( )(X)(X)(X)(X)(X)(X)(X)(X)(X)(X)(X)
Move peg 1 over peg 2 into hole 4
Move peg 6 over peg 3 into hole 1
Move peg 4 over peg 5 into hole 6
Move peg A over peg 6 into hole 3
Move peg 1 over peg 3 into hole 6
Move peg D over peg 8 into hole 4
Move peg 7 over peg 4 into hole 2
Move peg F over peg E into hole D
Move peg C over peg D into hole E
Move peg E over peg 9 into hole 5
Move peg 6 over peg 5 into hole 4
Move peg 2 over peg 4 into hole 7
Move peg B over peg 7 into hole 4
Edited by zfisherdrums
Link to comment
Share on other sites

@zfisherdrums.

I can't see what the rules are.

Why are 13|14|15 and 15|14|13 possible, but 1|2|3 and 3|2|1 not?

Some moves seem to be peg[n] jumps over peg[n+k] to peg[n + k +k + 1] where k can be either 1 or 2, but sometimes k can be 2 or 3, and sometimes k can be negative and sometimes it can't.

But there isn't a rule I can see for all moves. Peg 5 has fewer options than peg 4 etc. The pegs at the high end do not mirror the pegs at the low end.

Perhaps it's obvious to other people but I need help.

The aim of the game seems to be to convert the empty position at 1 to an empty position at 15, so my solution would be

peg 15 jumps over the rest and goes in hole 1.

I haven't quite got it have I?

My IQ must be down there with the crumbs in the bottom of the cracker barrel. :)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

The 'IQ test' involved here is a peg game. There's only one type of move you can make in the game...

Take a peg, move it to an empty hole two spaces away, and remove the peg in the intervening space. You can only move a peg in this manner if there is a peg in the middle hole.

Basically, you jump over pegs to empty holes, removing the peg you jumped over.

As for why 1-2-3 isn't a legal move, those three holes aren't in a row on that particular board. The sample board layout on the website he linked is a triangle, so hole 1 would be at the very top with holes 2 and 3 making up the next line. You can't go from 1 to 3 because they aren't two spaces away from each other on the board. 1-2-4 and 1-3-6 should be legal moves on the triangle, though. As is 4-5-6.

1
_2 3
4 5 6
Link to comment
Share on other sites

The 'IQ test' involved here is a peg game. There's only one type of move you can make in the game...

Take a peg, move it to an empty hole two spaces away, and remove the peg in the intervening space. You can only move a peg in this manner if there is a peg in the middle hole.

Basically, you jump over pegs to empty holes, removing the peg you jumped over.

As for why 1-2-3 isn't a legal move, those three holes aren't in a row on that particular board. The sample board layout on the website he linked is a triangle, so hole 1 would be at the very top with holes 2 and 3 making up the next line. You can't go from 1 to 3 because they aren't two spaces away from each other on the board. 1-2-4 and 1-3-6 should be legal moves on the triangle, though. As is 4-5-6.

1
_2 3
4 5 6
Thanks for taking the time to clarify. I should have done that in the first place! I will update the original to include your snyopsis ( or a reasonable facsimile ). :)

Zach...

Link to comment
Share on other sites

The 'IQ test' involved here is a peg game. There's only one type of move you can make in the game...

Take a peg, move it to an empty hole two spaces away, and remove the peg in the intervening space. You can only move a peg in this manner if there is a peg in the middle hole.

Basically, you jump over pegs to empty holes, removing the peg you jumped over.

As for why 1-2-3 isn't a legal move, those three holes aren't in a row on that particular board. The sample board layout on the website he linked is a triangle, so hole 1 would be at the very top with holes 2 and 3 making up the next line. You can't go from 1 to 3 because they aren't two spaces away from each other on the board. 1-2-4 and 1-3-6 should be legal moves on the triangle, though. As is 4-5-6.

1
_2 3
4 5 6
Thanks Ziggyny, it makes sense now. I thought the pegs were all in a line as the script output shows. Had I seen the link then I would have understood sooner. Especially since I have one of these games, except instead of pegs it has marbles in indentations. This makes it very difficult when you're drunk.

@zfisherdrums. Now that I understand the game, that's a really neat script. Very good.

It's interesting to see that PlayGame gets called as little as 41 times for one position, and more than 32,000 for another. I was expecting even more than that but then I suppose there are a lot of different ways to solve each starting position.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

My IQ must be down there with the crumbs in the bottom of the cracker barrel. :)

Quite the contrary; it's obvious that your IQ is elevated given your rationale. Your "problem" is my inability to fully describe the problem domain. Ziggyny's explaination is perfect. Think of the peg game as a marriage between Solataire and Checkers.

Zach...

Link to comment
Share on other sites

Hello gesller,

When you say it doesn't work, what specifically doesn't work? Are you receiving error messages or is it just taking a long time to run?

If it is the later, that is to be expected. The current script generates a solution for all 15 possible configurations and the recursive algorithm takes quite some time depending on the system. If you drop a small Sleep command in line 122 that will reduce the CPU usage but greatly increase the solve time.

One other thing to try is running the script to obtain the solution for only 1 configuration. To do that, replace the following:

For $X = 1 to 15
    Debug( $header )
    $GameBoard = CreateNewBoard( $x )
    Debug( GetBoardState( $GameBoard ) )
    PlayGame( $GameBoard )
    Debug( ) 
Next

...with this:

Debug( $header )
$GameBoard = CreateNewBoard( 4 )
Debug( GetBoardState( $GameBoard ) )
PlayGame( $GameBoard )
Debug( )

Notice that CreateNewBoard receives a 4 indicating that the game begins with the fourth hole empty. If it works, you should see the following:

1  2  3  4  5  6  7  8  9  A  B  C  D  E  F 
(X)(X)(X)( )(X)(X)(X)(X)(X)(X)(X)(X)(X)(X)(X)
Move peg 1 over peg 2 into hole 4
Move peg 6 over peg 3 into hole 1
Move peg 4 over peg 5 into hole 6
Move peg A over peg 6 into hole 3
Move peg 1 over peg 3 into hole 6
Move peg D over peg 8 into hole 4
Move peg 7 over peg 4 into hole 2
Move peg F over peg E into hole D
Move peg C over peg D into hole E
Move peg E over peg 9 into hole 5
Move peg 6 over peg 5 into hole 4
Move peg 2 over peg 4 into hole 7
Move peg B over peg 7 into hole 4

Oh, ok.. Sorry, It just didn't come up. I may not have given it enough time to load. I will recheck at home when my power comes back on. We are in the middle of an ice storm and power outages in the tens of thousands in my area. I am checking at email at work right now so no playing.. LOL Thanks!

Link to comment
Share on other sites

You can be genius in AJump, too.

AJump.au3:

(button can move up, down, left, right and up-left, down-right, and so on.)

;==========================================
; AJump (c) Valery Ivanov, 10 December 2007
; Initiated by
; http://www.autoitscript.com/forum/index.php?showtopic=59067
;
; Square Button Game Instructions
; 
; Jump buttons by clicking and dragging an adjoining button to the open hole on the other side. 
;  Each time a button is jumped, it is automatically removed. You can only remove buttons by jumping them.
;
; See if you can become a AJump genius by leaving only one button on the board!
;
; Size of AJump's desk
;  $DButton < 10!
;
Global $DButton = 6
$DButton = Mod($DButton, 10)

#include <GuiConstants.au3>
Global $Width, $Height, $h, $A = 40
Global $hWnd
Global $Current_Id, $Current_I, $Current_J
Global $Del_I, $Del_J
Global $Count

Global $Width = $DButton*$A, $Height = $Width
;Global $ColorPass[4] = [0xAAFFFF,0xFFAAAA,0xAAFFAA,0xAAAAFF]
Global $ColorPassive = 0xAABBFF
Global $ColorActive = 0xFFAAAA


$Style = $WS_THICKFRAME + $WS_POPUP
$StyleEx = $WS_EX_OVERLAPPEDWINDOW

$hWnd = GuiCreate("", $Width, $Height, (@DesktopWidth-$Width)/2, (@DesktopHeight-$Height)/2, $Style, $StyleEx)

GUISetFont(32)
;GUISetBkColor (0xAAAAFF)
AddButtons()

While Not IsNeighbors() 
 ResetButtons()
wend


GuiSetState()

While 1
 $Msg = GUIGetMsg(1)
 If $Msg[0] = $GUI_EVENT_CLOSE then exit 
 if $Msg[0] = $GUI_EVENT_MOUSEMOVE then MouseMoveHandler($Msg)
WEnd

EXIT

;--------------------------------
Func AddButtons()
local $i, $j, $X, $Y, $rI, $rJ, $Color

$rI = Random(1,$DButton,1)
$rJ = Random(1,$DButton,1)

For $i = 1 to $DButton
 $X = ($i-1)*$A
 For $j = 1 to $DButton
  $Y = ($j-1)*$A
  Assign('Ctrl_' & $i & $j, GuiCtrlCreateButton("", $X, $Y, $A, $A, $BS_CENTER + $BS_VCENTER), 2)
  GUICtrlSetBkColor(-1, $ColorPassive)
  Assign('Cell_' & $i & $j, 1, 2)
  if $i = $rI and $j = $rJ then 
    GUICtrlSetState(Eval('Ctrl_' & $i & $j), $GUI_HIDE)
    Assign('Cell_' & $i & $j, 0, 2)
  endif
 Next
Next
EndFunc

;=====================
func MouseMoveHandler($Msg)
local $X, $Y, $CursorInfo
local $Drag = 0

if Not IsButton($Msg[3], $Msg[4]) then return
$CursorInfo = GUIGetCursorInfo ($hWnd)
GUICtrlSetBkColor($Current_Id,$ColorActive)
While $CursorInfo[2] = 1 
 $Drag = 1
 $CursorInfo = GUIGetCursorInfo ($hWnd)
 $X = $CursorInfo[0]
 $Y = $CursorInfo[1]
 GUICtrlSetPos($Current_Id, $X, $Y, $A, $A)
WEnd
if $Drag = 1 then 
  $res = PutButton($Current_Id, $X+$A/2, $Y+$A/2)
  if $res then
   if Not IsNeighbors() then GameOver()
  else
   MsgBox(0,'','Invalid Move!')
  endif
endif
GUICtrlSetBkColor($Current_Id, $ColorPassive)
endfunc

;=====================
func IsButton($X, $Y)
local $i, $j
$i = Int($X/$A)+1
$j = Int($Y/$A)+1
if $i > 0 and $i <= $DButton and $j > 0 and $j <= $DButton then 
 if Eval('Cell_' & $i & $j) then 
  $Current_Id = Eval('Ctrl_' & $i & $j)
  $Current_I = $i
  $Current_J = $j
  return 1
 endif
endif
return 0
endfunc

;=====================
func PutButton($Current_Id, $X, $Y)
local $i, $j
$i = Int($X/$A)+1
$j = Int($Y/$A)+1
GUICtrlSetPos($Current_Id, ($Current_I-1)*$A, ($Current_J-1)*$A, $A, $A)
if IsMove($i,$j) then 
 Assign('Cell_' & $i & $j, 1, 2)
 GUICtrlSetState(Eval('Ctrl_' & $i & $j), $GUI_SHOW)
  
 Assign('Cell_' & $Current_I & $Current_J, 0, 2)
 GUICtrlSetState(Eval('Ctrl_' & $Current_I & $Current_J), $GUI_HIDE)

 Assign('Cell_' & $Del_I & $Del_J, 0, 2)
 GUICtrlSetState(Eval('Ctrl_' & $Del_I & $Del_J), $GUI_HIDE)
 return 1
endif
return 0
endfunc

;=====================
func IsMove($i,$j)
 $Del_I = 0
 $Del_J = 0
 if $Current_I = $i or Abs($Current_I-$i)=2 then $Del_I = Int(($Current_I+$i)/2)
 if $Current_J = $j or Abs($Current_J-$j)=2 then $Del_J = Int(($Current_J+$j)/2)
 if $Del_I and $Del_I then 
;  MsgBox(0,'', $Del_i & ' : ' & $Del_j)
   if Eval('Cell_' & $Del_I & $Del_J) <> 0 then return 1
 endif
 return 0
endfunc

;=====================
func GameOver()
local $c = 0
for $i = 1 to $DButton
 for $j = 1 to $DButton
  if Eval('Cell_' & $i & $j) <>  0 then $c += 1
 next
next

Select
case $c = 1 
  $Text = "         You are AJump genius!"
case $c < $DButton*$DButton/5
  $Text = "         You are AJump player!"
case $c < $DButton*$DButton/2
  $Text = '         You can be AJump player!'
case else
  $Text = "         You've got that..."
endselect
SplashTextOn("Game is over!", @CrLf & @CrLf & @CrLf & $Text, -1, -1, -1, -1, 4, "", 24)
Sleep(3000)
SplashOff()

While Not IsNeighbors() 
 ResetButtons()
wend

endfunc

;=====================
func IsNeighbors()
local $r, $c,  $tr, $tc
local $fr = 0, $fc = 0, $ffr = 0, $ffc = 0

; look for neighbors along rows and columns
for $i = 1 to $DButton
 $tr = 0
 $tc = 0
 $fr = 0
 $fc = 0
 $ffr = 1
 $ffc = 1
 for $j = 1 to $DButton
    $r = Eval('Cell_' & $i & $j)
    if not $r then $ffr = 0
    if $r and $tr then $fr = 1
    $tr = $r

    $c = Eval('Cell_' & $j & $i)
    if not $c then $ffc = 0
    if $c and $tc then $fc = 1
    $tc = $c
  next
  if $fr and Not $ffr then return 1
  if $fc and Not $ffc then return 1
next

;look for neighbors along diagonals
for $k = 4 to 2*$DButton - 2
  $tr = 0
  $tc = 0
  $fr = 0
  $fc = 0
  $ffr = 1
  $ffc = 1
  for $i = 1 to $k-1
    $j = $k -$i
    $r = Eval('Cell_' & $i & $j)
    if not $r then $ffr = 0
    if $r and $tr then $fr = 1
    $tr = $r

    $c = Eval('Cell_' & $j & $i)
    if not $c then $ffc = 0
    if $c and $tc then $fc = 1
    $tc = $c
  next
  if $fr and Not $ffr then return 1
  if $fc and Not $ffc then return 1
next

return 0
endfunc

;--------------------------------
Func ResetButtons()
local $i, $j
local $rI, $rj

$rI = Random(1,$DButton,1)
$rJ = Random(1,$DButton,1)
;MsgBox(0,'', $ri & ' : ' & $rj)

For $i = 1 to $DButton
 For $j = 1 to $DButton
  GUICtrlSetState(Eval('Ctrl_' & $i & $j), $GUI_SHOW)
  Assign('Cell_' & $i & $j, 1, 2)
  if $i = $rI and $j = $rJ then 
    GUICtrlSetState(Eval('Ctrl_' & $i & $j), $GUI_HIDE)
    Assign('Cell_' & $i & $j, 0, 2)
  endif
 Next
Next
EndFunc

The simplest case is to set here:

Global $DButton = 3

The point of world view

Link to comment
Share on other sites

This one has superdesk and color support, too

Needs a change

;=====================
func IsMove($i,$j)
$Del_I = 0
$Del_J = 0

if $Current_I = $i and $Current_J = $j then return 0;<--otherwise dragging and leaving in same place hides the button

if $Current_I = $i or Abs($Current_I-$i)=2 then $Del_I = Int(($Current_I+$i)/2)
if $Current_J = $j or Abs($Current_J-$j)=2 then $Del_J = Int(($Current_J+$j)/2)

if $Del_I and $Del_I then
;  MsgBox(0,'', $Del_i & ' : ' & $Del_j)
   if Eval('Cell_' & $Del_I & $Del_J) <> 0 then return 1
endif
return 0
endfunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Thank you.

This is updated version with color support.

;==========================================
; AJump © Valery Ivanov, 10 December 2007
; Initiated by
; [url="http://www.autoitscript.com/forum/index.php?showtopic=59067"]http://www.autoitscript.com/forum/index.php?showtopic=59067[/url]
;
; Square Button Game Instructions
; 
; Jump buttons by clicking and dragging an adjoining button to the open hole on the other side. 
;  Each time a button is jumped, it is automatically removed. You can only remove buttons by jumping them.
;
; See if you can become a AJump genius by leaving only one button on the board!
;
; This is a number of AJump's desk
;  $DButton < 10!
; History:
;  11 December 2007 - v.1.1
;  Added:
;   - color support
;   Fixed:
;   - bugs in IsNeighbors()
;

Global $DButton = 6
$DButton = Mod($DButton, 10)

$GameTip =  @CrLf & "Jump buttons by clicking and dragging an adjoining button " 
$GameTip &= "to the open hole on the other side. " & @CrLf & @CrLf
$GameTip &= "Each time a button is jumped, it is automatically removed. "
$GameTip &= "You can only remove buttons by jumping them." & @CrLf & @CrLf
$GameTip &= "See if you can become a AJump genius "
$GameTip &= "by leaving only one button on the board!"  & @CrLf & @CrLf
$GameTip &= "Esc to exit game!"  & @CrLf & @CrLf

#include <GuiConstants.au3>
Global $Width, $Height, $h, $A = 40
Global $hWnd
Global $Current_Id, $Current_I, $Current_J
Global $Del_I, $Del_J
Global $Count

Global $Width = $DButton*$A, $Height = $Width
Global $ColorPass[4] = [0xAAFFFF,0xFFAAAA,0xAAFFAA,0xAAAAFF]
Global $ColorPassive = 0xAAFFBB
Global $ColorActive = 0xFFFFFF

SplashTextOn("AJump game", $GameTip, -1, -1, -1, -1, 1, "Comic Sans MS", 16, 400)
Sleep(3000)
SplashOff()


$Style = $WS_THICKFRAME + $WS_POPUP
$StyleEx = $WS_EX_OVERLAPPEDWINDOW

$hWnd = GuiCreate("AJump © Valery Ivanov, 10 December 2007", $Width, $Height, (@DesktopWidth-$Width)/2, (@DesktopHeight-$Height)/2, $Style, $StyleEx)

GUISetFont(32)
GUISetBkColor (0xFFFFFF)
AddButtons()

While Not IsNeighbors() 
 ResetButtons()
wend


GuiSetState()

While 1
 $Msg = GUIGetMsg(1)
 If $Msg[0] = $GUI_EVENT_CLOSE then exit 
 if $Msg[0] = $GUI_EVENT_MOUSEMOVE then MouseMoveHandler($Msg)
WEnd

EXIT

;--------------------------------
Func AddButtons()
local $i, $j, $X, $Y, $rI, $rJ, $Color

$rI = Random(1,$DButton,1)
$rJ = Random(1,$DButton,1)


For $i = 1 to $DButton
 $X = ($i-1)*$A
 For $j = 1 to $DButton
  $Y = ($j-1)*$A
  Assign('Ctrl_' & $i & $j, GuiCtrlCreateButton("", $X, $Y, $A, $A, $BS_CENTER + $BS_VCENTER), 2)
  $Color  = $ColorPass[Random(0,3,1)]
  GUICtrlSetBkColor(-1, $Color)
  Assign('Color_' & $i & $j, $Color, 2)
  Assign('Cell_' & $i & $j, 1, 2)
  if $i = $rI and $j = $rJ then 
    GUICtrlSetState(Eval('Ctrl_' & $i & $j), $GUI_HIDE)
    Assign('Cell_' & $i & $j, 0, 2)
  endif
 Next
Next

EndFunc

;=====================
func MouseMoveHandler($Msg)
local $X, $Y, $CursorInfo
local $Drag = 0

if Not IsButton($Msg[3], $Msg[4]) then return
$CursorInfo = GUIGetCursorInfo ($hWnd)
if $CursorInfo[2] then GUICtrlSetBkColor($Current_Id,$ColorActive)
While $CursorInfo[2] = 1 
 $Drag = 1
 $CursorInfo = GUIGetCursorInfo ($hWnd)
 $X = $CursorInfo[0]
 $Y = $CursorInfo[1]
 GUICtrlSetPos($Current_Id, $X, $Y, $A, $A)
WEnd
if $Drag = 1 then 
  $res = PutButton($Current_Id, $X+$A/2, $Y+$A/2)
  if $res then
   if Not IsNeighbors() then GameOver()
  else
   MsgBox(0,'','Invalid Move!')
  endif
endif
;Assign('Color_' & $i & $j, $Color, 2)
;GUICtrlSetBkColor($Current_Id, $ColorPassive)
endfunc

;=====================
func IsButton($X, $Y)
local $i, $j
$i = Int($X/$A)+1
$j = Int($Y/$A)+1
if $i > 0 and $i <= $DButton and $j > 0 and $j <= $DButton then 
 if Eval('Cell_' & $i & $j) then 
  $Current_Id = Eval('Ctrl_' & $i & $j)
  $Current_I = $i
  $Current_J = $j
  return 1
 endif
endif
return 0
endfunc

;=====================
func PutButton($Current_Id, $X, $Y)
local $i, $j, $Color
$i = Int($X/$A)+1
$j = Int($Y/$A)+1
GUICtrlSetPos($Current_Id, ($Current_I-1)*$A, ($Current_J-1)*$A, $A, $A)
$Color = Eval('Color_' & $Current_I & $Current_J)
GUICtrlSetBkColor($Current_Id, $Color)
if IsMove($i,$j) then 
 Assign('Cell_' & $i & $j, 1, 2)
 GUICtrlSetState(Eval('Ctrl_' & $i & $j), $GUI_SHOW)
  Assign('Color_' & $i & $j, $Color, 2)
  GUICtrlSetBkColor(Eval('Ctrl_' & $i & $j), $Color)
  
 Assign('Cell_' & $Current_I & $Current_J, 0, 2)
 GUICtrlSetState(Eval('Ctrl_' & $Current_I & $Current_J), $GUI_HIDE)

 Assign('Cell_' & $Del_I & $Del_J, 0, 2)
 GUICtrlSetState(Eval('Ctrl_' & $Del_I & $Del_J), $GUI_HIDE)
 return 1
endif
return 0
endfunc

;=====================
func IsMove($i,$j)
 $Del_I = 0
 $Del_J = 0
 if $Current_I = $i and $Current_J = $j then return 0;
 if $Current_I = $i or Abs($Current_I-$i)=2 then $Del_I = Int(($Current_I+$i)/2)
 if $Current_J = $j or Abs($Current_J-$j)=2 then $Del_J = Int(($Current_J+$j)/2)
 if $Del_I and $Del_J then 
;  MsgBox(0,'', $Del_i & ' : ' & $Del_j)
   if Eval('Cell_' & $Del_I & $Del_J) <> 0 then return 1
 endif
 return 0
endfunc

;=====================
func GameOver()
local $c = 0
for $i = 1 to $DButton
 for $j = 1 to $DButton
  if Eval('Cell_' & $i & $j) <>  0 then $c += 1
 next
next

Select
case $c = 1 
  $Text = "         You are AJump genius!"
case $c < $DButton*$DButton/5
  $Text = "         You are AJump player!"
case $c < $DButton*$DButton/2
  $Text = '         You can be AJump player!'
case else
  $Text = "         You've got that..."
endselect
SplashTextOn("Game is over!", @CrLf & @CrLf & @CrLf & $Text, -1, -1, -1, -1, 4, "", 24)
Sleep(3000)
SplashOff()

While Not IsNeighbors() 
 ResetButtons()
wend

endfunc

;=====================
func IsNeighbors()
local $r, $c,  $tr, $tc
local $fr = 0, $fc = 0, $ffr = 0, $ffc = 0

; look for neighbors along rows and columns
for $i = 1 to $DButton
 $tr = 0
 $tc = 0
 $fr = 0
 $fc = 0
 $ffr = 1
 $ffc = 1
 for $j = 1 to $DButton
    $r = Eval('Cell_' & $i & $j)
    if not $r then $ffr = 0
    if $r and $tr then $fr = 1
    $tr = $r

    $c = Eval('Cell_' & $j & $i)
    if not $c then $ffc = 0
    if $c and $tc then $fc = 1
    $tc = $c
  next
  if $fr and Not $ffr then return 1
  if $fc and Not $ffc then return 1
next

;look for neighbors along diagonals
for $k = 2 to $DButton + 1
  $tr = 0
  $tc = 0
  $fr = 0
  $fc = 0
  $ffr = 1
  $ffc = 1
  for $i = 1 to $k-1
    $j = $k - $i
    $r = Eval('Cell_' & $i & $j)
    if not $r then $ffr = 0
    if $r and $tr then $fr = 1
    $tr = $r

    $c = Eval('Cell_' & ($DButton - $i + 1) & ($DButton - $j + 1))
    if not $c then $ffc = 0
    if $c and $tc then $fc = 1
    $tc = $c
  next
   if $fr and Not $ffr then return 1
  if $fc and Not $ffc then return 1

next

for $k = 4 to $DButton + 1
  $tr = 0
  $tc = 0
  $fr = 0
  $fc = 0
  $ffr = 1
  $ffc = 1
  for $i = 1 to $k-1
    $j = $k - $i
    $r = Eval('Cell_' & ($DButton - $i + 1) & $j)
    if not $r then $ffr = 0
    if $r and $tr then $fr = 1
    $tr = $r

    $c = Eval('Cell_' & $j & ($DButton - $i + 1))
    if not $c then $ffc = 0
    if $c and $tc then $fc = 1
    $tc = $c
  next
  if $fr and Not $ffr then return 1
  if $fc and Not $ffc then return 1

next


return 0
endfunc

;--------------------------------
Func ResetButtons()
local $i, $j
local $rI, $rj, $Color

$rI = Random(1,$DButton,1)
$rJ = Random(1,$DButton,1)
;MsgBox(0,'', $ri & ' : ' & $rj)

For $i = 1 to $DButton
 For $j = 1 to $DButton
  GUICtrlSetState(Eval('Ctrl_' & $i & $j), $GUI_SHOW)
  $Color  = $ColorPass[Random(0,3,1)]
  GUICtrlSetBkColor(-1, $Color)
  Assign('Color_' & $i & $j, $Color, 2)
  Assign('Cell_' & $i & $j, 1, 2)
  if $i = $rI and $j = $rJ then 
    GUICtrlSetState(Eval('Ctrl_' & $i & $j), $GUI_HIDE)
    Assign('Cell_' & $i & $j, 0, 2)
  endif
 Next
Next
EndFunc
Edited by Valery

The point of world view

Link to comment
Share on other sites

This is triangle desk version AJump3Ex.au3.

$DButton < 100

;==========================================
; AJump (c) Valery Ivanov, 10 December 2007
; Initiated by
; http://www.autoitscript.com/forum/index.php?showtopic=59067
;
; Triangle Button Game Instructions
; 
; Jump buttons by clicking and dragging an adjoining button to the open hole on the other side. 
;  Each time a button is jumped, it is automatically removed. You can only remove buttons by jumping them.
;
; See if you can become a AJump genius by leaving only one button on the board!
;
; History:
;  11 December 2007 - v.1.1
;  Added:
;   - color support
;   -  0 < $DButton < 100 (super desk!) 
;   Fixed:
;   - bugs in IsNeighbors()
;  12 December 2007 - v.1.2
;  Added:
;   - triangle desk ( file AJump3Ex.au3 )
;

Global $DButton = 15
;$DButton = Mod($DButton, 10)

$GameTip =  @CrLf & "Jump buttons by clicking and dragging an adjoining button " 
$GameTip &= "to the open hole on the other side. " & @CrLf & @CrLf
$GameTip &= "Each time a button is jumped, it is automatically removed. "
$GameTip &= "You can only remove buttons by jumping them." & @CrLf & @CrLf
$GameTip &= "See if you can become a AJump genius "
$GameTip &= "by leaving only one button on the board!"  & @CrLf & @CrLf
$GameTip &= "Esc to exit game!"  & @CrLf & @CrLf

#include <GuiConstants.au3>
Global $Width, $Height, $h, $A = 40
if $DButton > 9 then $A = 25

Global $hWnd
Global $Current_Id, $Current_I, $Current_J
Global $Del_I, $Del_J
Global $Count

Global $Width = $DButton*$A, $Height = $Width
Global $cWidth = Int($Width*1.2)
Global $ColorPass[4] = [0xAAFFFF,0xFFAAAA,0xAAFFAA,0xAAAAFF]
Global $ColorPassive = 0xAAFFBB
Global $ColorActive = 0xFFFFFF

SplashTextOn("AJump game", $GameTip, -1, -1, -1, -1, 1, "Comic Sans MS", 16, 400)
Sleep(5000)
SplashOff()


$Style = $WS_THICKFRAME + $WS_POPUP
$StyleEx = $WS_EX_OVERLAPPEDWINDOW

$hWnd = GuiCreate("AJump3 (c) Valery Ivanov, 10 December 2007", $Width, $Height, (@DesktopWidth-$Width)/2, (@DesktopHeight-$Height)/2, $Style, $StyleEx)

GUISetFont(32)
GUISetBkColor (0xFFFFFF)
AddButtons()

GuiSetState()

While IsNeighbors() = 0
;  MsgBox(0,'','New')
 ResetButtons()
Wend


While 1
 $Msg = GUIGetMsg(1)
 If $Msg[0] = $GUI_EVENT_CLOSE then exit 
 if $Msg[0] = $GUI_EVENT_MOUSEMOVE then MouseMoveHandler($Msg)
WEnd

EXIT

;--------------------------------
Func AddButtons()
local $i, $j, $X, $Y, $rI, $rJ, $Color

 $rJ = Random(1,$DButton,1)

 For $j = 1 to $DButton 
  $Y = ($j-1)*$A
  if $j = $rJ then $rI = Random(1,$j,1)
  For $i = 1 to $j
     $X = $A/2 * ($DButton - $j) + ($i-1)*$A
          GuiCtrlCreateLabel("", $X, $Y, $A, $A, $SS_CENTER+$SS_SUNKEN, 2)
     Assign('Ctrl_' & $i & '_' & $j, GuiCtrlCreateButton("", $X, $Y, $A, $A, $BS_CENTER + $BS_VCENTER), 2)
     $Color  = $ColorPass[Random(0,3,1)]
     GUICtrlSetBkColor(-1, $Color)
     Assign('Color_' & $i & '_' & $j, $Color, 2)
     Assign('Cell_' & $i & '_' & $j, 1, 2)
     if $i = $rI and $j = $rJ then 
       GUICtrlSetState(Eval('Ctrl_' & $i & '_' & $j), $GUI_HIDE)
       Assign('Cell_' & $i & '_' & $j, 0, 2)
     endif
   Next
 Next

EndFunc

;=====================
func MouseMoveHandler($Msg)
local $X, $Y, $CursorInfo
local $Drag = 0

if Not IsButton($Msg[3], $Msg[4]) then return
$CursorInfo = GUIGetCursorInfo ($hWnd)
if $CursorInfo[2] then GUICtrlSetBkColor($Current_Id,$ColorActive)
While $CursorInfo[2] = 1 
 $Drag = 1
 $CursorInfo = GUIGetCursorInfo ($hWnd)
 $X = $CursorInfo[0]
 $Y = $CursorInfo[1]
 GUICtrlSetPos($Current_Id, $X, $Y, $A, $A)
WEnd
if $Drag = 1 then 
  $res = PutButton($Current_Id, $X+$A/2, $Y+$A/2)
  if $res then
   if IsNeighbors() = 0 then GameOver()
  else
   MsgBox(0,'','Invalid Move!')
  endif
endif
endfunc

;=====================
func IsButton($X, $Y)
local $i, $j

$j = Int($Y/$A) + 1
$i = Int((2*$X/$A - $DButton + $j)/2) + 1

if $j > 0 and $j <= $DButton and $j > 0 and $i <= $j then 
 if Eval('Cell_' & $i & '_' & $j) then 
  $Current_Id = Eval('Ctrl_' & $i & '_' & $j)
  $Current_I = $i
  $Current_J = $j
  return 1
 endif
endif
return 0
endfunc

;=====================
func PutButton($Current_Id, $X, $Y)
local $i, $j, $Color
$j = Int($Y/$A) + 1
$i = Int((2*$X/$A - $DButton + $j)/2) + 1
GUICtrlSetPos($Current_Id, $A/2 * ($DButton - $Current_J) + ($Current_I-1)*$A, ($Current_J-1)*$A, $A, $A)
$Color = Eval('Color_' & $Current_I & '_' & $Current_J)
GUICtrlSetBkColor($Current_Id, $Color)
if IsMove($i,$j) then 

 Assign('Cell_' & $i & '_' & $j, 1, 2)
 GUICtrlSetState(Eval('Ctrl_' & $i & '_' & $j), $GUI_SHOW)
 Assign('Color_' & $i & '_' & $j, $Color, 2)
 GUICtrlSetBkColor(Eval('Ctrl_' & $i & '_' & $j), $Color)
  
 Assign('Cell_' & $Current_I & '_' & $Current_J, 0, 2)
 GUICtrlSetState(Eval('Ctrl_' & $Current_I & '_' & $Current_J), $GUI_HIDE)

 Assign('Cell_' & $Del_I & '_' & $Del_J, 0, 2)
 GUICtrlSetState(Eval('Ctrl_' & $Del_I & '_' & $Del_J), $GUI_HIDE)
 return 1
else
  return 0
endif
endfunc

;=====================
func IsMove($i,$j)
 $Del_I = 0
 $Del_J = 0
 if $i > $j then return 0
 if $i = $Current_I and $j = $Current_J then return 0
 if $i = $Current_I or Abs($Current_I-$i)=2 then $Del_I = Int(($Current_I+$i)/2)
 if $j = $Current_J or Abs($Current_J-$j)=2 then $Del_J = Int(($Current_J+$j)/2)
 if $Del_I and $Del_J then 
;  MsgBox(0,'', $Del_i & ' : ' & $Del_j)
   if Eval('Cell_' & $Del_I & '_' & $Del_J) <> 0 then return 1
 endif
 return 0
endfunc

;=====================
func GameOver()
local $c = 0
for $j = 1 to $DButton
 for $i = 1 to $j
  if Eval('Cell_' & $i & '_' & $j) <>  0 then $c += 1
 next
next

Select
case $c = 1 
  $Text = "         You are AJump genius!"
case $c < $DButton*$DButton/5
  $Text = "         You are AJump player!"
case $c < $DButton*$DButton/2
  $Text = '         You can be AJump player!'
case else
  $Text = "         You've got that..."
endselect
SplashTextOn("Game is over!", @CrLf & @CrLf & @CrLf & $Text, -1, -1, -1, -1, 4, "", 24)
Sleep(3000)
SplashOff()

While Not IsNeighbors() 
 ResetButtons()
wend

endfunc

;=====================
func IsNeighbors()
local $ni, $nj
local $r, $c,  $tr, $tc
local $fr = 0, $fc = 0, $ffr = 0, $ffc = 0

; look for neighbors along rows and columns
for $j = 2 to $DButton
 $tr = 0
 $fr = 0
 $ffr = 1
 for $i = 1 to $j
    $r = Int(Eval('Cell_' & $i & '_' & $j))
    if not $r then $ffr = 0
    if $r and $tr then $fr = 1
    $tr = $r
 next
 if $fr and Not $ffr then return 1
next

for $i = 1 to $DButton-2
 $tr = 0
 $fr = 0
 $ffr = 1
 for $j = $i to $DButton
    $r = Int(Eval('Cell_' & $i & '_' & $j))
    if not $r then $ffr = 0
    if $r and $tr then $fr = 1
    $tr = $r
 next
 if $fr and Not $ffr then return 1
next

;look for neighbors along diagonals
for $k = 6 to $DButton + 1
  $tr = 0
  $tc = 0
  $fr = 0
  $fc = 0
  $ffr = 1
  $ffc = 1
  for $i = 1 to $k-1
    $j = $k - $i
    if $i >= $j then
      $r = Int(Eval('Cell_' & $i & '_' & $j))
      if not $r then $ffr = 0
      if $r and $tr then $fr = 1
      $tr = $r
    endif
  next
  if $fr and Not $ffr then return 1
next

return 0
endfunc

;--------------------------------
Func ResetButtons()
local $i, $j
local $rI, $rj, $Color

 $rJ = Random(1,$DButton,1)

 For $j = 1 to $DButton 
  if $j = $rJ then $rI = Random(1,$j,1)
  For $i = 1 to $j
  GUICtrlSetState(Eval('Ctrl_' & $i & '_' & $j), $GUI_SHOW)
  $Color  = $ColorPass[Random(0,3,1)]
  GUICtrlSetBkColor(-1, $Color)
  Assign('Color_' & $i & '_' & $j, $Color, 2)
  Assign('Cell_' & $i & '_' & $j, 1, 2)
  if $i = $rI and $j = $rJ then 
    GUICtrlSetState(Eval('Ctrl_' & $i & '_' & $j), $GUI_HIDE)
    Assign('Cell_' & $i & '_' & $j, 0, 2)
  endif
 Next
Next
EndFunc

Previous code of AJump.au3 was fixed (see above).

The point of world view

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...