Jump to content

Code for Connect four games (Four stars)


PunProgram
 Share

Recommended Posts

HotKeySet("{Esc}","esc")
Global $board[8][8]
For $x=1  To 7   Step +1
 for $y=1  To 6   Step + 1
  $board[$x][$y]=0
  Next
 Next
Global $turn = 1
Global $a1 = 250
Global $a2 = 250
Global $a3 = 250
Global $a4 = 250
Global $a5 = 250
Global $a6 = 250
Global $a7 = 250
Global $play = 1
Global $color = 0xff0000
Func esc()
 Exit
EndFunc
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Four Stars", 350, 350, -1,-1)
$b1 = GUICtrlCreateButton("1",0,300,50,50)
$b2 = GUICtrlCreateButton("2",50,300,50,50)
$b3 = GUICtrlCreateButton("3",100,300,50,50)
$b4 = GUICtrlCreateButton("4",150,300,50,50)
$b5 = GUICtrlCreateButton("5",200,300,50,50)
$b6 = GUICtrlCreateButton("6",250,300,50,50)
$b7 = GUICtrlCreateButton("7",300,300,50,50)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
 if mod($turn,2) = 0 Then
  $play = 2
  $color=0x0000ff
 Else
  $play = 1
  $color=0xff0000
 EndIf
 if $a1 < 0 Then
  GUICtrlSetState($b1,$GUI_HIDE)
 EndIf
 if $a2 < 0 Then
  GUICtrlSetState($b2,$GUI_HIDE)
 EndIf
 if $a3 < 0 Then
  GUICtrlSetState($b3,$GUI_HIDE)
 EndIf
 if $a4 < 0 Then
  GUICtrlSetState($b4,$GUI_HIDE)
 EndIf
 if $a5 < 0 Then
  GUICtrlSetState($b5,$GUI_HIDE)
 EndIf
 if $a6 < 0 Then
  GUICtrlSetState($b6,$GUI_HIDE)
 EndIf
 if $a7 < 0 Then
  GUICtrlSetState($b7,$GUI_HIDE)
 EndIf
 $m = GUIGetMsg()
 Switch $m
  Case $GUI_EVENT_CLOSE
   Exit
  Case $b1
   GUICtrlCreateButton($play,0,$a1,50,50)
   GUICtrlSetBkColor(-1,$color)
   $turn = $turn + 1
   $yi= $a1/50+1
   $board[1][$yi]=$play
  $a1 = $a1 - 50
  check()
  Case $b2
   GUICtrlCreateButton($play,50,$a2,50,50)
   GUICtrlSetBkColor(-1,$color)
   $turn = $turn + 1
   $yi= $a2/50+1
   $board[2][$yi]=$play
   $a2 = $a2 - 50
   check()
  Case $b3
   GUICtrlCreateButton($play,100,$a3,50,50)
   GUICtrlSetBkColor(-1,$color)
   $turn = $turn + 1
   $yi= $a3/50+1
   $board[3][$yi]=$play
   $a3 = $a3 - 50
   check()
  Case $b4
   GUICtrlCreateButton($play,150,$a4,50,50)
   GUICtrlSetBkColor(-1,$color)
   $turn = $turn + 1
   $yi= $a4/50+1
   $board[4][$yi]=$play
   $a4 = $a4 - 50
   check()
  Case $b5
   GUICtrlCreateButton($play,200,$a5,50,50)
   GUICtrlSetBkColor(-1,$color)
   $turn = $turn + 1
   $yi= $a5/50+1
   $board[5][$yi]=$play
   $a5 = $a5 - 50
   check()
  Case $b6
   GUICtrlCreateButton($play,250,$a6,50,50)
   GUICtrlSetBkColor(-1,$color)
   $turn = $turn + 1
   $yi= $a6/50+1
   $board[6][$yi]=$play
   $a6 = $a6 - 50
   check()
  Case $b7
   GUICtrlCreateButton($play,300,$a7,50,50)
   GUICtrlSetBkColor(-1,$color)
   $turn = $turn + 1
   $yi= $a7/50+1
   $board[7][$yi]=$play
   $a7 = $a7 - 50
   check()
 EndSwitch
WEnd
Func check()
For $t=1 to 2  Step +1
For $x=1  to 4 step +1
     For $y = 6   to 4  step -1
    if $board[$x][$y]=$t  And $board[$x+1][$y-1]=$t  And $board[$x+2][$y-2]=$t And $board[$x+3][$y-3]=$t Then
   MsgBox(0,"The winner is","Player" & $t)
   Exit
  EndIf
  Next
 Next
For $x=1  To 4  Step +1
 for $y=1  To 6  Step + 1
  if $board[$x][$y]=$t  And $board[$x][$y+1]=$t  And $board[$x][$y+2]=$t And $board[$x][$y+3]=$t Then
   MsgBox(0,"The winner is","Player" & $t)
   Exit
  EndIf
  if $board[$x][$y]=$t  And $board[$x+1][$y]=$t  And $board[$x+2][$y]=$t And $board[$x+3][$y]=$t Then
   MsgBox(0,"The winner is","Player" & $t)
   Exit
  EndIf
  if $board[$x][$y]=$t  And $board[$x+1][$y+1]=$t  And $board[$x+2][$y+2]=$t And $board[$x+3][$y+3]=$t Then
   MsgBox(0,"The winner is","Player" & $t)
   Exit
  EndIf
  Next
 Next
Next
EndFunc

Edited by Melba23
Added code tags
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...