firex
AutoIT Гуру
- Сообщения
- 943
- Репутация
- 208
AutoIt: 3.3.8.1
Версия: 1.0
Категория: Графика, GDI+, Элементы GUI, Разное
Описание: Захотелось чем то поделиться :3 Примерно с год назад была задана курсовая (На Delphi), но так как с ним я не особо дружу, то разработал первоначально на AutoIt, а затем перенес в Delphi.
Крестики-нолики 3x3, стандартные правила. Еще при создании учел вариант быстрого добавления других алгоритмов. Создание своих профилей, скинов и тому подобное.
Код/Пример:
Файл: Rar
Снимок:

Источник: autoit-script.ru
Автор(ы): Firex
P.S. Можете предложить свой алгоритм, посоревнуетесь с моим. (default alg)
Версия: 1.0
Категория: Графика, GDI+, Элементы GUI, Разное
Описание: Захотелось чем то поделиться :3 Примерно с год назад была задана курсовая (На Delphi), но так как с ним я не особо дружу, то разработал первоначально на AutoIt, а затем перенес в Delphi.
Крестики-нолики 3x3, стандартные правила. Еще при создании учел вариант быстрого добавления других алгоритмов. Создание своих профилей, скинов и тому подобное.
Код/Пример:
players.cfg
skin_default.cfg
Код:
1,1,default,Бот #1 "Простой алгоритм";
2,1,default,Бот #2 "Простой алгоритм";
3,2,playercontrol,Человек #1;
4,2,playercontrol,Человек #2;
5,1,bullshit,Бот #3 "Рандом алгоритм";
6,1,bullshit,Бот #4 "Рандом алгоритм";
skin_default.cfg
Код:
1,images\bg_blue.png;
2,images\title_main.png;
3,images\grid_main.png;
4,images\x_1.png;
5,images\o_1.png;
6,images\line_h.png;
7,images\line_v.png;
8,images\line_m.png;
9,images\line_s.png;
Код:
#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=Tictactoe.exe
#AutoIt3Wrapper_UseUpx=N
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; #HEAD# ========================================================================================================================
; Title .........: Tictactoe
; Version .......: 1.0.0
; Language ......: Russian
; Description ...:
; Author(s) .....: Firex
; ===============================================================================================================================
#Region Initialization
#NoAutoIt3Execute
#Include <GDIPlus.au3>
#Include <Array.au3>
#Include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("TrayMenuMode",1)
Opt("WinWaitDelay",5)
;Basic constants
Global Const $STM_SETIMAGE = 0x0172
Global Const $ALG_MOVEMENT[ 4 ] = [ 'last|new|player_num', 0, 0, 0 ]
Global Const $ALG_FIELDSTATE[ 10 ] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
Global Const $ALG_REVERSEDFIELD[ 10 ] = [ 0, 1, 2, 3, 8, 0, 4, 7, 6, 5 ]
Global Const $ALG_PRIORITYCELLS[ 5 ] = [ 4, 2, 4, 6, 8 ] ;For default algorithm
; ==
Global Const $GUI_GRIDCELLS[ 10 ][ 2 ] = [ [''], [ 5, 10], [99, 10], [198, 10], [5, 125], [99, 125], [198, 125], [5, 238], [99, 238], [198, 238] ]
Global Const $GUI_BUTTONGRID[ 10 ] [ 2 ] = [ [''], [ 5, 10], [100, 10], [203, 10], [5, 124], [100, 124], [203, 124], [5, 238], [100, 238], [203, 238] ]
Global Const $GUI_LINES[ 9 ][ 5 ] = [ [''], [ 6,0,45,290,30], [6,0,160,290,30], [6,0,275,290,30], [7,35,0,30,350], [7,128,0,30,350], [7,225,0,30,350], [8,0,0,290,350], [9,0,0,290,350] ]
;Basic variables
Global $hSkin = _Tictactoe_SkinLoad('skin_default.cfg')
Global $hPlayer = _Tictactoe_PlayersLoad('players.cfg')
Global $hCtrls[13]
Global $hGui = _GUI_Main( 'Tictactoe - v.0.1.a' )
#EndRegion Initialization
#Region Body
While 1
$hMsg = GUIGetMsg()
Switch $hMsg
Case $GUI_EVENT_CLOSE, $hCtrls[5]
Exit
Case $hCtrls[3]
_GUI_Manual( 'Tictactoe - Manual Set' )
Case $hCtrls[4]
_GUI_About( 'Tictactoe - About' )
Case $hCtrls[0]
_GUI_Game( 1, 2 )
Case $hCtrls[1]
_GUI_Game( 3, 2 )
Case $hCtrls[2]
_GUI_Game( 3, 4 )
EndSwitch
WEnd
#EndRegion Body
Func _Tictactoe_SkinLoad( $cfg )
_GDIPlus_Startup()
Local $temp, $index, $hskin[10] = ['hImages']
Local $data = FileRead( @ScriptDir & '\' & $cfg )
If $data = -1 Then _
_Tictactoe_Quit( 1488, 'Can not load skin!' )
$data = StringSplit(StringReplace(StringStripCR($data),@LF,''), ';' )
For $index = 1 To $data[0] Step 1
$temp = StringSplit($data[$index],',')
If IsArray($temp) And UBound($temp) = 3 Then _
$hskin[$temp[1]] = _GDIPlus_ImageLoadFromFile(@ScriptDir & '\' & $temp[2])
Next
Return $hskin
EndFunc
Func _Tictactoe_PlayersLoad( $cfg )
Local $temp, $index, $size, $hplayer[1][3] = [['player','alg','name']]
Local $data = FileRead( @ScriptDir & '\' & $cfg )
If $data = -1 Then _
_Tictactoe_Quit( 1488, 'Can not load player profiles!' )
Local $CPoint = 1
$data = StringSplit(StringReplace(StringStripCR($data),@LF,''), ';' )
For $index = 1 To $data[0] Step 1
$temp = StringSplit($data[$index],',')
If IsArray($temp) And UBound($temp) = 5 Then
If $CPoint = $temp[1] Then
$size = UBound($hplayer)
ReDim $hplayer[$size+1][3]
$hplayer[$size][0] = $temp[2]
$hplayer[$size][1] = $temp[3]
$hplayer[$size][2] = $temp[4]
EndIf
$CPoint += 1
EndIf
Next
Return $hplayer
EndFunc
Func _Tictactoe_CheckLines( $stt, $player, $count, $ret = 0 )
Local $IndexA, $IndexB, $Null, $Occupied, $LineNum = 0
Local $Lines[8][3] = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]
For $IndexA = 0 To UBound($Lines) - 1 Step 1
Local $Null = 1, $Occupied = 0
For $IndexB = 0 To 2 Step 1
If Not $stt[$Lines[$IndexA][$IndexB]] Then
$Null = $Lines[$IndexA][$IndexB]
ElseIf $stt[$Lines[$IndexA][$IndexB]] = $player Then
$Occupied += 1
Else
$Occupied = 0
ExitLoop
EndIf
Next
If $Occupied = $count And $Occupied Then
If $ret Then
Return $IndexA + 1
Else
Return $Null
EndIf
EndIf
Next
Return 0
EndFunc
Func _Tictactoe_GetRandomCell( $stt )
Local $Array = _ArrayCreate(0), $Index
For $Index = 1 To 9 Step 1
If Not $stt[$Index] Then _
_ArrayAdd($Array,$Index)
Next
If UBound($Array) = 1 Then
Return 0
ElseIf UBound($Array) = 2 Then
Return $Array[1]
EndIf
Return $Array[Random(1,UBound($Array)-1,1)]
EndFunc
Func _Tictactoe_Quit( $exitcode, $errmsg = -1, $errcode = -1 )
If $errmsg <> -1 Then _
MsgBox( 16, 'Error - ' & $errcode, $errmsg )
Exit $exitcode
EndFunc
#Region GUIs
Func _GUI_Main( $guiname )
Local $hGui = GUICreate($guiname, 290, 350, -1, -1, 0x94CA0000, 0x00000100)
GUISetBkColor($GUI_BKCOLOR_TRANSPARENT)
_GUICtrlPic_Create($hSkin[1], 0, 0, 290, 350 )
$hCtrls[0] = GUICtrlCreateButton("newfag - algorithm vs algorithm", 20, 146, 250, 26)
$hCtrls[1] = GUICtrlCreateButton("foreveralone - player vs algorithm ", 20, 175, 250, 26)
$hCtrls[2] = GUICtrlCreateButton("alphamale - player vs player", 20, 204, 250, 26)
$hCtrls[3] = GUICtrlCreateButton("manualset - undefined vs undefined", 20, 233, 250, 26)
$hCtrls[4] = GUICtrlCreateButton("About", 20, 284, 250, 26)
$hCtrls[5] = GUICtrlCreateButton("Exit - :C", 20, 313, 250, 26)
$hCtrls[6] = _GUICtrlPic_Create($hSkin[2], 0, -10, 290, 160 )
GUISetState(@SW_SHOW, $hGui)
Return $hGui
EndFunc
Func _GUI_Game( $playerid_1 = 0, $playerid_2 = 0 )
If Not $playerid_1 Or Not $playerid_2 Then _
Return ;Не все игроки определены
Local $iPos = WinGetPos($hGui, "")
GUISetState(@SW_DISABLE, $iPos)
Local $guiname = 'Game[' & $hPlayer[$playerid_1][2] & ' vs ' & $hPlayer[$playerid_2][2] & ']'
$hGuiGame = GUICreate( $guiname, 290, 350, $iPos[0], $iPos[1] + 100, -1, -1, $hGui)
GUISetBkColor($GUI_BKCOLOR_TRANSPARENT)
_GUICtrlPic_Create($hSkin[1], 0, 0, 290, 350 )
_GUICtrlPic_Create($hSkin[3], 0, 0, 290, 350 )
Local $hButtons[10] = [ 9 ], $Index
#CS
For $Index = 1 To 9 Step 1
Local $shifts = 0
Local $shiftx = Int(($Index/4)+1)+3
Local $shifty = Int(($Index/3)+0.67)
If Mod($Index + 1, 3) = -1 Then _
$shifts = 8
$hButtons[$Index] = GUICtrlCreateLabel('', $GUI_GRIDCELLS[$shiftx], $GUI_GRIDCELLS[$shifty], 80+$shifts, 102 )
;GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
Sleep(10)
Next
#CE
For $Index = 1 To 9 Step 1
Local $shifts = 0
If Not Mod($Index + 1, 3) Then _
$shifts = 8
$hButtons[$Index] = GUICtrlCreateLabel('', $GUI_BUTTONGRID[$Index][0], $GUI_BUTTONGRID[$Index][1], 80+$shifts, 102 )
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
Next
GUISetState( @SW_SHOW )
Local $Moves = $ALG_MOVEMENT, $State = $ALG_FIELDSTATE
Local $Major[3] = ['',$playerid_1,$playerid_2]
Local $Another[3] = ['',$playerid_2,$playerid_1]
Local $Cellf, $Celle, $Winner = -1 ;f - friendly, e - enemy, -1 - undefined
$Moves[3] = 1
While 1
Switch $hPlayer[$Major[$Moves[3]]][1] ;Алгоритм
Case 'default' ;Ходит компьютер с алгоритмом default
$Cellf = _Tictactoe_CheckLines( $State, $Major[$Moves[3]], 2 )
$Celle = _Tictactoe_CheckLines( $State, $Another[$Moves[3]], 2 )
If $Cellf Then ;Есть ряд, где две клетки наши и одна свободна
$Moves[2] = $Cellf
ElseIf $Celle Then ;Есть ряд, где две клетки противника и одна свободна
$Moves[2] = $Celle
ElseIf Not $State[5] And Random(1,3,1) <> 3 Then ;Центр свободен & Рандом для непредсказуемости
$Moves[2] = 5 ;Занимаем центр
ElseIf $State[5] = $Major[$Moves[3]] Then ;Центр наш
;Ставим фишку в центральную вертикаль/горизонталь. - $ALG_PRIORITYCELLS
For $Index = 1 To UBound($ALG_PRIORITYCELLS) - 1 Step 1
If Not $State[$ALG_PRIORITYCELLS[$Index]] And (Not $Moves[2] Or Random(0,1,1)) Then _
$Moves[2] = $ALG_PRIORITYCELLS[$index]
Next
EndIf
If Not $Moves[2] Then
$Cellf = _Tictactoe_CheckLines( $State, $Major[$Moves[3]], 1 )
If $Cellf Then ;Есть ряд, где одна клетка наша и две свободны
$Moves[2] = $Cellf
Else
$Cellf = _Tictactoe_GetRandomCell( $State )
If $Cellf Then _
$Moves[2] = $Cellf
EndIf
EndIf
Case "bullshit"
$Cellf = _Tictactoe_GetRandomCell( $State )
If $Cellf Then _
$Moves[2] = $Cellf
Case 'playercontrol' ;Ходит игрок (управление мышкой). - Для возможности игры по сети(в будущем)
While Not $Moves[2]
$hMsg = GUIGetMsg()
If $hMsg = $GUI_EVENT_CLOSE Then
$Winner = 0
ExitLoop 2
EndIf
For $Index = 1 To $hButtons[0] Step 1
If $hMsg = $hButtons[$Index] And Not $State[$Index] Then
$Moves[2] = $Index
ExitLoop
EndIf
Next
Sleep(10)
WEnd
EndSwitch
;ConsoleWrite('Игрок ' & $Major[$Moves[3]] & ' ходит в ' & $Moves[2] & ' клетку' & @LF) ;DEBUG
If $Moves[2] Then
$Moves[1] = $Moves[2]
$State[$Moves[2]] = $Major[$Moves[3]]
If $Moves[3] = 1 Then
$Moves[3] = 2
_GUICtrlPic_Create($hSkin[4], $GUI_GRIDCELLS[$Moves[2]][0], $GUI_GRIDCELLS[$Moves[2]][1], 90, 100 )
ElseIf $Moves[3] = 2 Then
$Moves[3] = 1
_GUICtrlPic_Create($hSkin[5], $GUI_GRIDCELLS[$Moves[2]][0], $GUI_GRIDCELLS[$Moves[2]][1], 90, 100 )
EndIf
$Moves[2] = 0
Else
$Winner = 0
EndIf
Sleep(500)
If $Winner = -1 Then
$Cellf = _Tictactoe_CheckLines( $State, $Another[$Moves[3]], 3, 1 )
If $Cellf Then ;Проверка для игрока
_GUICtrlPic_Create($hSkin[$GUI_LINES[$Cellf][0]], $GUI_LINES[$Cellf][1], $GUI_LINES[$Cellf][2], $GUI_LINES[$Cellf][3], $GUI_LINES[$Cellf][4] )
$Winner = $Another[$Moves[3]]
ExitLoop
ElseIf Not _Tictactoe_GetRandomCell( $State ) Then
$Winner = 0
ExitLoop
EndIf
ContinueLoop
EndIf
ExitLoop
WEnd
Sleep(500)
If $Winner Then
MsgBox( 64, 'EndGame', 'Победил: ' & $hPlayer[$Winner][2] )
Else
MsgBox( 64, 'EndGame', 'Ничья!' )
EndIf
GUISetState(@SW_ENABLE, $hGui)
GUIDelete($hGuiGame)
EndFunc
Func _GUI_Manual( $guiname )
Local $hGuiManual, $hMsg, $hCtrlsManual[13]
Local $iPos = WinGetPos($hGui, ""), $index
Local $size = UBound($hPlayer) - 1, $sdata = ''
GUISetState(@SW_DISABLE, $iPos)
$hGuiManual = GUICreate( $guiname, 290, 178, $iPos[0], $iPos[1] + 100, -1, -1, $hGui)
GUISetBkColor($GUI_BKCOLOR_TRANSPARENT)
_GUICtrlPic_Create($hSkin[1], 0, -50, 290, 350 )
$hCtrlsManual[0 ] = GUICtrlCreateList("", 5, 5, 280, 80)
$hCtrlsManual[1 ] = GUICtrlCreateList("", 5, 80, 280, 80)
$hCtrlsManual[2 ] = GUICtrlCreateButton("Set players and start", 5, 154, 280, 22)
GUICtrlSetState( $hCtrlsManual[2 ], $GUI_DISABLE )
For $index = 1 To $size Step 1
$sdata &= $hPlayer[$index][2]
If $size > $index Then _
$sdata &= '|'
Next
GUICtrlSetData($hCtrlsManual[0 ],$sdata,'')
GUISetState(@SW_SHOW, $hGuiManual)
Local $nsitem = '', $ositem = ''
Local $nitem = '', $oitem = '', $ids
While 1
$hMsg = GUIGetMsg()
Switch $hMsg
Case $GUI_EVENT_CLOSE
ExitLoop
Case $hCtrlsManual[2 ]
If $ids[0] And $ids[1] Then
_GUI_Game( $ids[0], $ids[1] )
ExitLoop
Else
_Tictactoe_Quit( 4, 'Shift fucking arrays!', 1337 )
EndIf
EndSwitch
$nitem = GUICtrlRead($hCtrlsManual[0 ])
$nsitem = GUICtrlRead($hCtrlsManual[1 ])
If $oitem <> $nitem Then
$sdata = ''
For $index = 1 To $size Step 1
If $hPlayer[$index][2] <> $nitem Then
$sdata &= $hPlayer[$index][2]
If $size > $index Then _
$sdata &= '|'
EndIf
Next
GUICtrlSetData($hCtrlsManual[1 ],'','')
GUICtrlSetData($hCtrlsManual[1 ],$sdata,'')
GUICtrlSetState($hCtrlsManual[2 ], $GUI_DISABLE )
$oitem = $nitem
ElseIf $nsitem <> $ositem Then
$ositem = $nsitem
Dim $ids[2] = [0,0]
For $index = 1 To $size Step 1
If $hPlayer[$index][2] = $nitem Then
$ids[0] = $index
ElseIf $hPlayer[$index][2] = $nsitem Then
$ids[1] = $index
EndIf
Next
GUICtrlSetState( $hCtrlsManual[2 ], $GUI_ENABLE )
EndIf
WEnd
GUISetState(@SW_ENABLE, $hGui)
GUIDelete($hGuiManual)
EndFunc
Func _GUI_About( $guiname )
MsgBox( 64, $guiname, 'Tictactoe - concept' & @LF & _
'Version:' & @TAB & '0.1.b' & @LF & _
'Thanks:' & @TAB & 'Hidan' & @LF & _
'Author:' & @TAB & 'Firex' & @LF & @LF & _
'Not specifically for codhacks.ru', 0, $hGui )
EndFunc
#EndRegion GUIs
Func _GUICtrlPic_Create($hImage, $iX, $iY, $iW, $iH )
Local $iCtrl, $hBitmap, $hObject
$iCtrl = GUICtrlCreatePic('', $iX, $iY, $iW, $iH)
GUICtrlSetState(-1, $GUI_DISABLE)
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
$hObject = _SendMessage(GUICtrlGetHandle($iCtrl), 0x0172, 0, $hBitmap )
_WinAPI_DeleteObject( $hObject )
Return $iCtrl
EndFunc
Func _GUICtrlPic_Show($iCtrl)
GUICtrlSetState($iCtrl, $GUI_SHOW)
GUICtrlSetState($iCtrl, $GUI_DISABLE)
EndFunc
Func _GUICtrlPic_Hide($iCtrl)
GUICtrlSetState($iCtrl, $GUI_HIDE)
EndFunc
Файл: Rar
Снимок:

Источник: autoit-script.ru
Автор(ы): Firex
P.S. Можете предложить свой алгоритм, посоревнуетесь с моим. (default alg)