Spielstein bewegen

  • Hi Community,

    mein neustes Problem liegt darin, dass ich es einfach nicht gebacken bekomme, die Spielsteine (aus dem folgenden Script) richtig zu verschieben. Es geht dabei um Spielsteine auf einem Damebrett.

    Die Steine können folgendermaßen bewegt werden: Ansehen

    Spoiler anzeigen
    [autoit]


    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #include <GDIplus.au3>
    #include <GUICtrlSetOnHover_UDF.au3>
    #include <_ArrayDisplayTree.au3>
    #include <Misc.au3>
    ;#include <Draughts_Ki.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt("GUIOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    coinnumbers:
    2 - RP normal coin , 4 - RP king coin
    3 - KI normal coin , 6 - KI king coin

    [/autoit] [autoit][/autoit] [autoit]

    -> Insert: acknowledge oneself beaten
    #ce
    Global $aBoard[8][4] = _
    [ _
    [3, 3, 3, 3], _
    [3, 3, 3, 3], _
    [3, 3, 3, 3], _
    [0, 0, 0, 0], _
    [0, 0, 0, 0], _
    [2, 2, 2, 2], _
    [2, 2, 2, 2], _
    [2, 2, 2, 2] _
    ]
    Global $pPlayerID[2][2] = [[2, 4],[3, 6]] ; 0 = RP,1 = KI
    Global $pPlayerNames[2] = ["<RealPlayer>", "Computer"]
    Global $pActPlayer = -1
    Global $iTCoords[2]
    Global $bFSelc = False
    Global $iCFSelc = -1
    Global $aLabelDummy[8][4]
    Global $aCoords[8][4][2]
    Global $sIconPath = @ScriptDir & "\"

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()
    Global $hImage_Background = _GDIPlus_BitmapCreateFromFile($sIconPath & "game_b.jpg")
    Global $hImage_Coin_Blue = _GDIPlus_BitmapCreateFromFile($sIconPath & "coin_b.png")
    Global $hImage_Coin_Red = _GDIPlus_BitmapCreateFromFile($sIconPath & "coin_r.png")
    Global $hImage_Selection = _GDIPlus_BitmapCreateFromFile($sIconPath & "back_selc.png")

    [/autoit] [autoit][/autoit] [autoit]

    Global $iWidth = _GDIPlus_ImageGetWidth($hImage_Background)
    Global $iHeight = _GDIPlus_ImageGetHeight($hImage_Background)
    Global $dx = _GDIPlus_ImageGetWidth($hImage_Coin_Blue)
    Global $dy = _GDIPlus_ImageGetHeight($hImage_Coin_Blue)

    [/autoit] [autoit][/autoit] [autoit]

    Global Const $pi_div_380 = 4 * ATan(3) / 380
    Global $radius = 60
    Global $i, $x = $iWidth / 2 - $dx / 2, $y = $iHeight / 2 - $dy / 2
    Global $hwnd = GUICreate("Draughts (c) by Jautois", 600, $iHeight)
    GUISetOnEvent(-3, "_Exit")
    $pLabel_Player = GUICtrlCreateLabel("Spieler", 408, 8, 180, 20)
    GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    Global $graphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
    Global $bitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $graphics)
    Global $hBack_Buffer = _GDIPlus_ImageGetGraphicsContext($bitmap)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Background, 0, 0) ;draw background

    [/autoit] [autoit][/autoit] [autoit]

    For $Row = 0 To 7
    For $Col = 0 To 3
    $iCurY = $Row * 50
    $iCurX = $Col * 100 + 50
    If Mod($Row, 2) = 0 Then $iCurX -= 50
    If $Row < 3 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Blue, $iCurX, $iCurY)
    If $Row > 4 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Red, $iCurX, $iCurY)
    $aLabelDummy[$Row][$Col] = GUICtrlCreateLabel('', $iCurX, $iCurY, 50, 50, $SS_NOTIFY)
    GUICtrlSetOnEvent(-1, "_LabelClick")
    _GUICtrl_SetOnHover(-1, "_Hover_Proc", "_Leave_Hover_Func")
    $aCoords[$Row][$Col][0] = $iCurX
    $aCoords[$Row][$Col][1] = $iCurY
    Next
    Next

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)

    [/autoit] [autoit][/autoit] [autoit]

    _InitGame()

    [/autoit] [autoit][/autoit] [autoit]

    While Sleep(100)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    ; If any label is getting hovered this function will be call
    Func _Hover_Proc($iCtrlID, $iParam)
    If _IsCoinActPlayer(_GetCoords($iCtrlID)) Then
    _GetCoords($iCtrlID)
    _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Selection, $aCoords[$iTCoords[1]][$iTCoords[0]][0], $aCoords[$iTCoords[1]][$iTCoords[0]][1])
    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)
    EndIf
    EndFunc ;==>_Hover_Proc

    [/autoit] [autoit][/autoit] [autoit]

    ; If any label will be left after it was on hover, this function will be call
    Func _Leave_Hover_Func($iCtrlID)
    _DrawBoardAndCoins()
    EndFunc ;==>_Leave_Hover_Func

    [/autoit] [autoit][/autoit] [autoit]

    ; Checks if the position is free
    Func _IsFreePos($iFunc_ID)
    _GetCoords($iFunc_ID)
    ;MsgBox(1,"",$iTCoords[0] & " - " & $iTCoords[1])
    If Not $aBoard[$iTCoords[1]][$iTCoords[0]] = 2 Or Not $aBoard[$iTCoords[1]][$iTCoords[0]] = 4 Or Not $aBoard[$iTCoords[1]][$iTCoords[0]] = 3 Or Not $aBoard[$iTCoords[1]][$iTCoords[0]] = 6 Then
    Return True
    Else
    Return False
    EndIf
    EndFunc ;==>_IsFreePos

    [/autoit] [autoit][/autoit] [autoit]

    Func _IsCoinActPlayer($Coords)
    If $aBoard[$Coords[1]][$Coords[0]] = $pPlayerID[$pActPlayer][0] Or $aBoard[$Coords[1]][$Coords[0]] = $pPlayerID[$pActPlayer][1] Then
    Return True
    Else
    Return False
    EndIf
    EndFunc ;==>_IsCoinActPlayer

    [/autoit] [autoit][/autoit] [autoit]

    ; If a label is clicked
    Func _LabelClick()
    If $bFSelc Then
    If _IsFreePos(@GUI_CtrlId) Then
    ; Turn possible
    _DrawMoveCoin($iCFSelc, @GUI_CtrlId)
    $bFSelc = False
    Else
    MsgBox(1, "", "This turn isn't possible")
    $bFSelc = False
    EndIf
    Else
    If _IsCoinActPlayer(_GetCoords(@GUI_CtrlId)) Then
    _GetCoords(@GUI_CtrlId)
    _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Selection, $aCoords[$iTCoords[1]][$iTCoords[0]][0], $aCoords[$iTCoords[1]][$iTCoords[0]][1])
    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)
    $bFSelc = True
    $iCFSelc = @GUI_CtrlId
    EndIf
    EndIf
    EndFunc ;==>_LabelClick

    [/autoit] [autoit][/autoit] [autoit]

    Func _Exit()
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_GraphicsDispose($hBack_Buffer)
    _GDIPlus_Shutdown()
    Exit
    EndFunc ;==>_Exit

    [/autoit] [autoit][/autoit] [autoit]

    ; Returns the coordinates from a GUI_CtrlID
    Func _GetCoords($iFunc_ID)
    $iTCoords[0] = Mod($iFunc_ID - 4, 4) ; X
    $iTCoords[1] = Int(($iFunc_ID - 4) / 4) ; Y
    Return $iTCoords
    EndFunc ;==>_GetCoords

    [/autoit] [autoit][/autoit] [autoit]

    ; Draws the board and the coins
    Func _DrawBoardAndCoins()
    _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Background, 0, 0) ;draw background
    For $Row = 0 To 7
    For $Col = 0 To 3
    $iCurY = $Row * 50
    $iCurX = $Col * 100 + 50
    If Mod($Row, 2) = 0 Then $iCurX -= 50
    If $aBoard[$Row][$Col] = 3 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Blue, $iCurX, $iCurY)
    If $aBoard[$Row][$Col] = 2 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Red, $iCurX, $iCurY)
    Next
    Next
    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)
    EndFunc ;==>_DrawBoardAndCoins

    [/autoit] [autoit][/autoit] [autoit]

    Func _InitGame()
    $pActPlayer = 1;Random(0, 1, 1) ; Random Player begins
    GUICtrlSetData($pLabel_Player, "It's " & $pPlayerNames[$pActPlayer] & "'s turn!")
    EndFunc ;==>_InitGame

    [/autoit] [autoit][/autoit] [autoit]

    Func _DrawMoveCoin($iFunc_F = -1, $iFunc_S = -1)
    Local $aCoordsF, $aCoordsS
    $aCoordsF = _GetCoords($iFunc_F)
    $aCoordsS = _GetCoords($iFunc_S)
    $aBoard[$aCoordsF[1]][$aCoordsF[0]] = 0
    For $i = 0 To 9
    Sleep(100)
    _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Background, 0, 0)
    $iCou = 0
    For $Row = 0 To 7
    For $Col = 0 To 3
    If $iCou = $iFunc_F Then
    If $aCoordsF[1] < $aCoordsS[1] Then
    $iNewX = $aCoordsF[0] * 50 + ($i + 1) * 5 + $aCoordsF[0] * 50
    $iNewY = $aCoordsF[1] * 50 + ($i + 1) * 5
    Else

    [/autoit] [autoit][/autoit] [autoit]

    $iNewX = ($aCoordsF[0] + 1) * 50 - ($i + 1) * 5 + $aCoordsF[0] * 50
    $iNewY = $aCoordsF[1] * 50 - ($i + 1) * 5
    EndIf
    ToolTip($iNewX & " - " & $iNewY)
    If $pActPlayer = 0 Then
    _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Red, $iNewX, $iNewY)
    Else
    _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Blue, $iNewX, $iNewY)
    EndIf
    Else
    $iCurY = $Row * 50
    $iCurX = $Col * 100 + 50
    If Mod($Row, 2) = 0 Then $iCurX -= 50
    If $aBoard[$Row][$Col] = 3 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Blue, $iCurX, $iCurY)
    If $aBoard[$Row][$Col] = 2 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Red, $iCurX, $iCurY)
    EndIf
    $iCou += 1
    Next
    Next
    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)
    Next
    $aBoard[$aCoordsS[1]][$aCoordsS[0]] = $pPlayerID[$pActPlayer][0]
    EndFunc ;==>_DrawMoveCoin

    [/autoit] [autoit][/autoit] [autoit][/autoit]

    Genauer gesagt geht es um die letzte Funktion _DrawMoveCoin(). Dort werden in den Variablen $aCoordsF und $aCoordsS die Koordinaten der beiden Felder berechnet. $aCoordsF[0] - X Koordinate des ersten geklickten Feldes und $aCoordsF[1] - Y Koordinate des ersten geklickten Feldes. Das gleiche bei $aCoordsS nur die Koordinaten des zweiten Feldes.

    Folgendes berechnet immer die neuen X und Y Koordinaten (für die Bewegung).

    [autoit]


    If $aCoordsF[1] < $aCoordsS[1] Then
    $iNewX = $aCoordsF[0] * 50 + ($i + 1) * 5 + $aCoordsF[0] * 50
    $iNewY = $aCoordsF[1] * 50 + ($i + 1) * 5
    Else
    $iNewX = ($aCoordsF[0] + 1) * 50 - ($i + 1) * 5 + $aCoordsF[0] * 50
    $iNewY = $aCoordsF[1] * 50 - ($i + 1) * 5
    EndIf

    [/autoit]

    Das richtige Berechnen dieser Werte ist das eigentliche Problem. Da die Spielsteine in fast alle diagonalen Richtungen springen können.

    Vielen Dank das du dir bis hierher schonmal die Zeit genommen hast! Ich hoffe du kannst mir auch helfen :D

    Einmal editiert, zuletzt von Jautois (21. Dezember 2009 um 14:07)

  • Wieso Bots? Wer spricht denn von Bots?

    Er will selber ein Dame-Spiel schreiben, nicht eins steuern.

  • latemail: Du bringst mich gerade auf eine Idee, denn ich habe eigentlich schon alle möglichen Koordinaten abgespeichert. In $aCoords[8][4][2] stehen jeweils die X und Y Koordinaten für jedes "mögliche" Feld.

    @Detroit49: Wie kann man darin einen Bot sehen? Ich habe das Spiel doch selbst geschrieben. :wacko:

    @Arkaneus: Danke :D

  • Okej hab das Problem gelöst. Habe es über das Array, mit den bereits gespeicherten Koordinaten, gelöst.

  • Kannst du die Funktion denn dann mal hier reinstellen, und die Bilder zum Spiel? Würd das nämlich gern mal ausprobieren :)