Jump to content

Recommended Posts

Posted (edited)

I have a project from college to code an XO Game in C++, i didn't start coding that on C++ yet but i made one with AutoIt on less than 1 hour, i just like to share it here, simple and cute :D

;~ Options
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
;~ ----------------------------------------------------------------------------------------------------
;~ Variables
Const $Title = "XO Game!"
Local $GUI, $Button[9]
Global $Turn = True
;~ ----------------------------------------------------------------------------------------------------
;~ GUI
$GUI = GUICreate($Title, 300, 300)
GUISetFont(72)
$Button[0] = GUICtrlCreateButton("", 0, 0, 100, 100)
$Button[1] = GUICtrlCreateButton("", 100, 0, 100, 100)
$Button[2] = GUICtrlCreateButton("", 200, 0, 100, 100)
$Button[3] = GUICtrlCreateButton("", 0, 100, 100, 100)
$Button[4] = GUICtrlCreateButton("", 100, 100, 100, 100)
$Button[5] = GUICtrlCreateButton("", 200, 100, 100, 100)
$Button[6] = GUICtrlCreateButton("", 0, 200, 100, 100)
$Button[7] = GUICtrlCreateButton("", 100, 200, 100, 100)
$Button[8] = GUICtrlCreateButton("", 200, 200, 100, 100)
GUISetState()
;~ ----------------------------------------------------------------------------------------------------
;~ Events
For $i = 0 To 8
GUICtrlSetOnEvent($Button[$i], "_Clicked")
Next
GUISetOnEvent(-3, "_Exit")
;~ ----------------------------------------------------------------------------------------------------
;~ Functions
Func _Clicked()
If $Turn = True Then
  GUICtrlSetData(@GUI_CtrlId, "X")
  $Turn = False
  For $i = 0 To 8
   Call("_CheckButton" & $i, "X")
  Next
Else
  GUICtrlSetData(@GUI_CtrlId, "O")
  $Turn = True
  For $i = 0 To 8
   Call("_CheckButton" & $i, "O")
  Next
EndIf
GUICtrlSetState(@GUI_CtrlId, 128)
_GameOver()
EndFunc   ;==>_Clicked
Func _CheckButton0($Mark)
If GUICtrlRead($Button[0]) = $Mark And GUICtrlRead($Button[1]) = $Mark And GUICtrlRead($Button[2]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[0]) = $Mark And GUICtrlRead($Button[3]) = $Mark And GUICtrlRead($Button[6]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[0]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[8]) = $Mark Then _Winner($Mark)
EndFunc   ;==>_CheckButton0
Func _CheckButton1($Mark)
If GUICtrlRead($Button[0]) = $Mark And GUICtrlRead($Button[1]) = $Mark And GUICtrlRead($Button[2]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[1]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[7]) = $Mark Then _Winner($Mark)
EndFunc   ;==>_CheckButton1
Func _CheckButton2($Mark)
If GUICtrlRead($Button[0]) = $Mark And GUICtrlRead($Button[1]) = $Mark And GUICtrlRead($Button[2]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[2]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[6]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[2]) = $Mark And GUICtrlRead($Button[5]) = $Mark And GUICtrlRead($Button[8]) = $Mark Then _Winner($Mark)
EndFunc   ;==>_CheckButton2
Func _CheckButton3($Mark)
If GUICtrlRead($Button[0]) = $Mark And GUICtrlRead($Button[3]) = $Mark And GUICtrlRead($Button[6]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[3]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[5]) = $Mark Then _Winner($Mark)
EndFunc   ;==>_CheckButton3
Func _CheckButton4($Mark)
If GUICtrlRead($Button[0]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[8]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[2]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[6]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[3]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[5]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[1]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[7]) = $Mark Then _Winner($Mark)
EndFunc   ;==>_CheckButton4
Func _CheckButton5($Mark)
If GUICtrlRead($Button[2]) = $Mark And GUICtrlRead($Button[5]) = $Mark And GUICtrlRead($Button[8]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[3]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[5]) = $Mark Then _Winner($Mark)
EndFunc   ;==>_CheckButton5
Func _CheckButton6($Mark)
If GUICtrlRead($Button[0]) = $Mark And GUICtrlRead($Button[3]) = $Mark And GUICtrlRead($Button[6]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[2]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[6]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[6]) = $Mark And GUICtrlRead($Button[7]) = $Mark And GUICtrlRead($Button[8]) = $Mark Then _Winner($Mark)
EndFunc   ;==>_CheckButton6
Func _CheckButton7($Mark)
If GUICtrlRead($Button[1]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[7]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[6]) = $Mark And GUICtrlRead($Button[7]) = $Mark And GUICtrlRead($Button[8]) = $Mark Then _Winner($Mark)
EndFunc   ;==>_CheckButton7
Func _CheckButton8($Mark)
If GUICtrlRead($Button[0]) = $Mark And GUICtrlRead($Button[4]) = $Mark And GUICtrlRead($Button[8]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[2]) = $Mark And GUICtrlRead($Button[5]) = $Mark And GUICtrlRead($Button[8]) = $Mark Then _Winner($Mark)
If GUICtrlRead($Button[6]) = $Mark And GUICtrlRead($Button[7]) = $Mark And GUICtrlRead($Button[8]) = $Mark Then _Winner($Mark)
EndFunc   ;==>_CheckButton8
Func _Winner($Mark)
MsgBox(64, $Title, $Mark & " Wins!", 0, $GUI)
Exit
EndFunc   ;==>_Winner
Func _GameOver()
Local $CheckAll = 0
For $i = 0 To 8
  If GUICtrlGetState($Button[$i]) = 144 Then $CheckAll += 1
Next
If $CheckAll = 9 Then MsgBox(64, $Title, "Game Over!", 0, $GUI)
EndFunc   ;==>_GameOver
Func _Exit()
Exit
EndFunc   ;==>_Exit
;~ ----------------------------------------------------------------------------------------------------
;~ Idle Loop
While 1
Sleep(100)
WEnd

Sorry if there are similar threads, i didn't search for them! BTW it's the new one by me :oops:

Edited by D4RKON3
Posted

Few things:

- I'd like to see a computer opponent

- Make the first player able to choose X or O(not always X that starts)

But a good game for 1 hour work :D

Challenge accepted!

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
×
×
  • Create New...