Jump to content

Problem with my game player [COM]


Zisly
 Share

Recommended Posts

I made a ticTacToe game and decided to make a computer, simple heh?

Well the code does what it should but sometimes it doesn't set the data to the button (X).

Hope somebody could help me :mellow:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $player = 1
Global $b

$Form1 = GUICreate("Tic Tac Toe", 312, 122, -1, -1)
GUISetBkColor(0x000000)
$b1 = GUICtrlCreateButton("", 0, 0, 51, 41, 0)
$b2 = GUICtrlCreateButton("", 52, 0, 51, 41, 0)
$b3 = GUICtrlCreateButton("", 104, 0, 51, 41, 0)
$b4 = GUICtrlCreateButton("", 0, 40, 51, 41, 0)
$b5 = GUICtrlCreateButton("", 52, 40, 51, 41, 0)
$b6 = GUICtrlCreateButton("", 104, 40, 51, 41, 0)
$b7 = GUICtrlCreateButton("", 0, 80, 51, 41, 0)
$b8 = GUICtrlCreateButton("", 52, 80, 51, 41, 0)
$b9 = GUICtrlCreateButton("", 104, 80, 51, 41, 0)
GUISetState(@SW_SHOW)

Global $table[9] = ["", "", "", _; 0 1 2
        "", "", "", _; 3 4 5
        "", "", ""]; 6 7 8

main()

Func main()
    While 1
        Sleep(30)
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $b1
                If $player = 1 Or $player = 2 Then playermove($b1, 0)
            Case $b2
                If $player = 1 Or $player = 2 Then playermove($b2, 1)
            Case $b3
                If $player = 1 Or $player = 2 Then playermove($b3, 2)
            Case $b4
                If $player = 1 Or $player = 2 Then playermove($b4, 3)
            Case $b5
                If $player = 1 Or $player = 2 Then playermove($b5, 4)
            Case $b6
                If $player = 1 Or $player = 2 Then playermove($b6, 5)
            Case $b7
                If $player = 1 Or $player = 2 Then playermove($b7, 6)
            Case $b8
                If $player = 1 Or $player = 2 Then playermove($b8, 7)
            Case $b9
                If $player = 1 Or $player = 2 Then playermove($b9, 8)
        EndSwitch
        If $player = 3 Then bot()
    WEnd
EndFunc  ;==>main

Func check($Iplayer, $au)
    If $table[0] = $au And $table[1] = $au And $table[2] = $au Then winmsg($Iplayer); -
    If $table[3] = $au And $table[4] = $au And $table[5] = $au Then winmsg($Iplayer); -
    If $table[6] = $au And $table[7] = $au And $table[8] = $au Then winmsg($Iplayer); -
    If $table[0] = $au And $table[4] = $au And $table[8] = $au Then winmsg($Iplayer); \
    If $table[6] = $au And $table[4] = $au And $table[2] = $au Then winmsg($Iplayer); \
    If $table[0] = $au And $table[3] = $au And $table[6] = $au Then winmsg($Iplayer); V
    If $table[1] = $au And $table[4] = $au And $table[7] = $au Then winmsg($Iplayer); V
    If $table[2] = $au And $table[5] = $au And $table[8] = $au Then winmsg($Iplayer); V
    $checked = 0
    For $i = 0 To 8
        If Not $table[$i] = "" Then $checked += 1
    Next
    If $checked = 9 Then winmsg(0)
    
EndFunc  ;==>check

Func playermove($move, $val)
    If $table[$val] = "" Then
        $p = '0'
        GUICtrlSetFont($move, 26, 800, 0, "MS Sans Serif")
        GUICtrlSetColor($move, 0xFF0000)
        GUICtrlSetData($move, $p)
        $table[$val] = $p
        check($player, $p)
        Global $player = 3
    EndIf
EndFunc  ;==>playermove


Func winmsg($Iplayer)
EndFunc  ;==>winmsg

Func bot()
    While 1
        Local $comMove = Random(0, 8, 0)
        If $table[$comMove] = "" Then ExitLoop
    WEnd
    GUICtrlSetFont($b & $comMove + 1, 26, 800, 0, "MS Sans Serif")
    GUICtrlSetColor($b & $comMove + 1, 0x3B98D3)
    GUICtrlSetData($b & $comMove + 1, 'X')
    $table[$comMove] = 'X'
    check(3, 'X')
    Global $player = 1
EndFunc  ;==>bot

Zisly,

Link to comment
Share on other sites

1 major problem is you have "Global $player = 3" in your Playermove() function

So.... When it returns to the While loop it will ONLY execute the line that states "If $player = 3 Then bot()"

got it?

8)

Nope, I don't get it :mellow:

Do you want me to remove the "global $player = 3"?

I did so but it didn't work.

What should I do?

Link to comment
Share on other sites

This works...

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $player = 1
Global $b

$Form1 = GUICreate("Tic Tac Toe", 312, 122, -1, -1)
GUISetBkColor(0x000000)
$b1 = GUICtrlCreateButton("", 0, 0, 51, 41, 0)
$b2 = GUICtrlCreateButton("", 52, 0, 51, 41, 0)
$b3 = GUICtrlCreateButton("", 104, 0, 51, 41, 0)
$b4 = GUICtrlCreateButton("", 0, 40, 51, 41, 0)
$b5 = GUICtrlCreateButton("", 52, 40, 51, 41, 0)
$b6 = GUICtrlCreateButton("", 104, 40, 51, 41, 0)
$b7 = GUICtrlCreateButton("", 0, 80, 51, 41, 0)
$b8 = GUICtrlCreateButton("", 52, 80, 51, 41, 0)
$b9 = GUICtrlCreateButton("", 104, 80, 51, 41, 0)
GUISetState(@SW_SHOW)

Global $table[9] = ["", "", "", _; 0 1 2
        "", "", "", _; 3 4 5
        "", "", ""]; 6 7 8


While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $b1
            If $player = 1 Or $player = 2 Then playermove($b1, 0)
        Case $b2
            If $player = 1 Or $player = 2 Then playermove($b2, 1)
        Case $b3
            If $player = 1 Or $player = 2 Then playermove($b3, 2)
        Case $b4
            If $player = 1 Or $player = 2 Then playermove($b4, 3)
        Case $b5
            If $player = 1 Or $player = 2 Then playermove($b5, 4)
        Case $b6
            If $player = 1 Or $player = 2 Then playermove($b6, 5)
        Case $b7
            If $player = 1 Or $player = 2 Then playermove($b7, 6)
        Case $b8
            If $player = 1 Or $player = 2 Then playermove($b8, 7)
        Case $b9
            If $player = 1 Or $player = 2 Then playermove($b9, 8)
    EndSwitch
    If $player = 3 Then bot()
WEnd


Func check($Iplayer, $au)
    If $table[0] = $au And $table[1] = $au And $table[2] = $au Then winmsg($Iplayer); -
    If $table[3] = $au And $table[4] = $au And $table[5] = $au Then winmsg($Iplayer); -
    If $table[6] = $au And $table[7] = $au And $table[8] = $au Then winmsg($Iplayer); -
    If $table[0] = $au And $table[4] = $au And $table[8] = $au Then winmsg($Iplayer); \
    If $table[6] = $au And $table[4] = $au And $table[2] = $au Then winmsg($Iplayer); \
    If $table[0] = $au And $table[3] = $au And $table[6] = $au Then winmsg($Iplayer); V
    If $table[1] = $au And $table[4] = $au And $table[7] = $au Then winmsg($Iplayer); V
    If $table[2] = $au And $table[5] = $au And $table[8] = $au Then winmsg($Iplayer); V
    $checked = 0
    For $i = 0 To 8
        If Not $table[$i] = "" Then $checked += 1
    Next
    If $checked = 9 Then winmsg(0)

EndFunc   ;==>check

Func playermove($move, $val)
    If $table[$val] = "" Then
        $p = '0'
        GUICtrlSetFont($move, 26, 800, 0, "MS Sans Serif")
        GUICtrlSetColor($move, 0xFF0000)
        GUICtrlSetData($move, $p)
        $table[$val] = $p
        check($player, $p)
        $player = 3
    EndIf
EndFunc   ;==>playermove


Func winmsg($Iplayer)
    MsgBox(0x0, "YEA!!!", "WINNER!!!!.....      ")
    Exit
EndFunc   ;==>winmsg

Func bot()
    While 1
        Local $comMove = Random(0, 8, 1)
        If $table[$comMove] = "" Then ExitLoop
    WEnd
    $set = Eval("b" & $comMove + 1)
    GUICtrlSetFont($set, 26, 800, 0, "MS Sans Serif")
    GUICtrlSetColor($set, 0x3B98D3)
    GUICtrlSetData($set, 'X')
    $table[$comMove] = 'X'
    check(3, 'X')
    $player = 1
EndFunc   ;==>bot

8)

NEWHeader1.png

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...