Sign in to follow this  
Followers 0
CodyBarrett

Snakes & Ladders!

7 posts in this topic

#1 ·  Posted (edited)

Here is my implementation of the childhood board game, please forgive the Programmers-Art, i'm not very good at graphics, just place the Jpg in the same Dir as the script and have fun!

Opt ('GUIONEVENTMODE',1)
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <Math.au3>
#include <Array.au3>
#include <WINAPI.au3>

Dim $Player[5]
Dim $Grid[101]
Dim $PlayerPeicePlacement[5]

Dim $SpecialSquare[15] = [1,4,9,17,21,28,51,53,62,64,71,87,93,95,98]
Dim $SpecialSquareReturn[15] =  [1,14,31,7,42,84,67,33,19,60,91,24,73,75,79]

$l_size = 80
$PlayerPeice = -1
$CurrentPlayerTurn = 0
$PlayerCount = 4

$MainWindow = GUICreate ('Snakes & Ladders',(10*$l_size)+(4*$l_size),(10*$l_size))
GUISetBkColor (0xABCDEF,$MainWindow)
GUISetFont (10,999,'','Tahoma',$MainWindow)
$count = 100


GUICtrlSetState (GUICtrlCreatePic (@ScriptDir & '\Background.jpg',0,0,10*$l_size,10*$l_size),128)

$Grid[0] = $MainWindow
For $y = 0 To 9
    If $y > 0  And  _MathCheckDiv($y,2) = 1 Then
        $Step = -1
        $b = 9
        $e = 0
    Else
        $Step = 1
        $b = 0
        $e = 9
    EndIf
    For $x = $b To $e Step $Step
        $Grid[$count] = GUICtrlCreateLabel ($count,$x*$l_size,$y*$l_size,$l_size,$l_size,BitOR ($SS_CENTERIMAGE,$SS_CENTER))
        GUICtrlSetBkColor ($Grid[$count],-2)
        GUICtrlSetColor ($Grid[$count],0x962929)
        $count -= 1
    Next
Next

$x = (10.5*$l_size)
$y = $l_size/2
$w = $l_size/2
$h = $l_size/2
$BlackPeice = GUICtrlCreateLabel ('',$x + ($l_size/2) _
                                                        ,$y _
                                                        ,$w _
                                                        ,$h _
                                                        ,BitOR ($SS_CENTERIMAGE,$SS_CENTER),$WS_EX_DLGMODALFRAME)
    GUICtrlSetBkColor (Default,0x000000)
    GUICtrlSetColor (Default,0xFFFFFF)
    GUICtrlSetOnEvent (Default,'ChoosePeice')
$RedPeice = GUICtrlCreateLabel ('',$x+( ($l_size/2)*2) _
                                                        ,$y _
                                                        ,$w _
                                                        ,$h _
                                                        ,BitOR ($SS_CENTERIMAGE,$SS_CENTER),$WS_EX_DLGMODALFRAME)
    GUICtrlSetBkColor (Default,0xFF0000)
    GUICtrlSetColor (Default,0xFFFFFF)
    GUICtrlSetOnEvent (Default,'ChoosePeice')
$BluePeice = GUICtrlCreateLabel ('',$x+( ($l_size/2)*3) _
                                                        ,$y _
                                                        ,$l_size/2 _
                                                        ,$l_size/2 _
                                                        ,BitOR ($SS_CENTERIMAGE,$SS_CENTER),$WS_EX_DLGMODALFRAME)
    GUICtrlSetBkColor (Default,0x0000FF)
    GUICtrlSetColor (Default,0xFFFFFF)
    GUICtrlSetOnEvent (Default,'ChoosePeice')
$GreenPeice = GUICtrlCreateLabel ('',$x+( ($l_size/2)*4) _
                                                        ,$y _
                                                        ,$l_size/2 _
                                                        ,$l_size/2 _
                                                        ,BitOR ($SS_CENTERIMAGE,$SS_CENTER),$WS_EX_DLGMODALFRAME)
    GUICtrlSetBkColor (Default,0x00FF00)
    GUICtrlSetColor (Default,0xFFFFFF)
    GUICtrlSetOnEvent (Default,'ChoosePeice')

$y = ($l_size/2)*3
$Die = GUICtrlCreateButton ('Roll Die',(10*$l_size)+ ($l_size*1.5) _
                                                            ,$y _
                                                            ,$l_size _
                                                            ,$l_size)
    GUICtrlSetOnEvent (Default,'RollDie')

$y = ($l_size/2)*6
$w = ($l_size/2)*6
$h = ($l_size/2)*11

$Console = GUICtrlCreateEdit ('',$x _
                                                ,$y _
                                                ,$w _
                                                ,$h _
                                                ,$WS_VSCROLL)
    GUICtrlSetBkColor (Default,0xABCDEF)
    GUICtrlSetFont (Default,8,999,'','Tahoma')

$h = $l_size/2
$QuitButton = GUICtrlCreateButton ('QUIT Game',(10*$l_size) + ($l_size/2) _
                                                                            ,(10*$l_size) - $l_size _
                                                                            , $w _
                                                                            , $h)
    GUICtrlSetOnEvent (Default,'QUIT')


GUISetOnEvent (-3,'QUIT',$MainWindow)
GUISetState (@SW_SHOW,$MainWindow)


While True
    Sleep (250)
WEnd


Func RollDie ()
    Local $n, $random, $x, $y, $number, $end, $lastsquare, $data = ''
    If $CurrentPlayerTurn = 0 Then Return
    $end = Random (1,6,1)
    For $n = 1 To $end
        $random = Random (1,6,1)
        GUICtrlSetData ($Die,$random)
        Sleep (100)
    Next
    $number = GUICtrlRead ($Die)
    Console_Write ('Player' & $CurrentPlayerTurn & ' Has Rolled a ' & $number)
    If $PlayerPeicePlacement[$CurrentPlayerTurn]+$number < 100 Then
        $lastsquare = $PlayerPeicePlacement[$CurrentPlayerTurn]
        $PlayerPeicePlacement[$CurrentPlayerTurn] += $number
        $SquareType = GetSquareType ($PlayerPeicePlacement[$CurrentPlayerTurn])
        $pos = ControlGetPos ($MainWindow,'',$Grid[$PlayerPeicePlacement[$CurrentPlayerTurn]])
        If $SquareType <> -1 Then
            $PlayerPeicePlacement[$CurrentPlayerTurn] = $SquareType
            $pos = ControlGetPos ($MainWindow,'',$Grid[$SquareType])
            If $lastsquare < $SquareType Then $data = ' Up A Ladder'
            If $lastsquare > $SquareType Then $data = ' Down A Snake'
        EndIf
        Console_Write ('Player' & $CurrentPlayerTurn & ' Moves From ' & $lastsquare & $data & ' To ' & $PlayerPeicePlacement[$CurrentPlayerTurn] )
        $x = $pos[0] + ($l_size/16) + ($CurrentPlayerTurn*($l_size/12))
        $y = $pos[1] + ($l_size/4)
        GUICtrlSetPos ($Player[$CurrentPlayerTurn],$x,$y)
    ElseIf $PlayerPeicePlacement[$CurrentPlayerTurn]+$number = 100 Then
        MsgBox (48,'WINNER!!','Player' & $CurrentPlayerTurn & ' Has Won The Game!!!','',WinGetHandle ('[active]'))
        Console_Write ('!!!!!!!!!!Player' & $CurrentPlayerTurn & ' Has Won The Game!!!!!!!!!!')
        $CurrentPlayerTurn = 0
    EndIf
    NewTurn ()
EndFunc

Func GetSquareType($number)
    For $n = 1 To UBound ($SpecialSquare)-1
        If $number = $SpecialSquare[$n] Then
            Return $SpecialSquareReturn[$n]
        EndIf
    Next
    Return -1
EndFunc

Func NewTurn ()
    If $CurrentPlayerTurn = 0 Then Return
    GUICtrlSetData ($Die, 'Roll Die')
    $CurrentPlayerTurn += 1
    If $CurrentPlayerTurn > $PlayerCount Then $CurrentPlayerTurn = 1
    Console_Write ('==>It Is Player' & $CurrentPlayerTurn & 's Turn!')
EndFunc

Func Console_Write ($data)
    ControlCommand ($MainWindow,'','Edit1','EditPaste',@CRLF & $data & @CRLF)
EndFunc

Func ChoosePeice ()
    Local $array[5], $n
    $array[1] = $BlackPeice
    $array[2] = $RedPeice
    $array[3] = $BluePeice
    $array[4] = $GreenPeice
    If @GUI_CtrlId = $BlackPeice Then $PlayerPeice = $BlackPeice
    If @GUI_CtrlId = $RedPeice Then $PlayerPeice = $RedPeice
    If @GUI_CtrlId = $BluePeice Then $PlayerPeice = $BluePeice
    If @GUI_CtrlId = $GreenPeice Then $PlayerPeice = $GreenPeice
    GUICtrlSetOnEvent ($BlackPeice,'')
    GUICtrlSetOnEvent ($RedPeice,'')
    GUICtrlSetOnEvent ($BluePeice,'')
    GUICtrlSetOnEvent ($GreenPeice,'')
    $CurrentPlayerTurn = 1
    For $n = 1 To 4
        If $array[$n] <> '' Then
            GUICtrlSetPos ($array[$n],($l_size/16)+($n*($l_size/12)),9.25*$l_size)
            $Player[$n] = $array[$n]
            $array[$n] = ''
            $PlayerPeicePlacement[$n] = 1
            If $Player[$n] = $PlayerPeice Then $CurrentPlayerTurn = $n
        EndIf
        GUICtrlSetData ($Player[$n],'P'&$n)
    Next
    Console_Write ('Player' & $CurrentPlayerTurn & ', Roll The Die To Start The Game Off!')
EndFunc

Func QUIT ()
    Exit
EndFunc
post-41162-0-00349800-1292517293_thumb.j Edited by CodyBarrett

Share this post


Link to post
Share on other sites



dangit... hmm. time to go back to MSPAINT. lol.

Share this post


Link to post
Share on other sites

This is really good. But, I found a glitch with this, when a player lands on square #4 it never goes up the ladder.

==>It Is Player4s Turn!

Player4 Has Rolled a 3

Player4 Moves From 1 To 4

==>It Is Player1s Turn!


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

odd...

This works

==>It Is Player4s Turn!

Player4 Has Rolled a 4

Player4 Moves From 5 Up A Ladder To 31

==>It Is Player1s Turn!

and yeah actually i've been able to replicate the glitch... but i don't know why, it works on 5 to 31

5+4=9

9 moves up to 31.

i'm going to keep looking into why 4 doesn't move up to 14. sorry guys.. :\

EDIT

well that was a quick glitch fix... haha i forgot that arrays start at 0 instead of 1 and thats what i did for the specialsquare arrays, fixed by adding a 1 for both as the first element.

Edited by CodyBarrett

Share this post


Link to post
Share on other sites

It seems that if you choose any color other than black, the black piece will never move although the game does keep track of it's position on the board in the text area.


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

It seems that if you choose any color other than black, the black piece will never move although the game does keep track of it's position on the board in the text area.

I think i fixed it, only problem is, you can still pick what color you are, but its defined to P1 thru P4 left to right, so if you pick anything but black you won't be player 1 BUT you will still go first. other than that odd occurrence i believe its fixed.

Share this post


Link to post
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
Sign in to follow this  
Followers 0