Tuesday, 7 January 2014

CHAIN REACTION FOR WINDOWS

1/07/2014 07:24:00 am

                                           
Edit: Its been years since I wrote this code. After taking a look at this code, I realize how dirty my programming used to be. Now, believing that I might have improved my programming skills, I tried to make it in java. You can find the code here.

                                              Hey!! Ever played a game called Chain Reaction on Android? Well its a wonderful mind game. When I played it for the first time I thought of implementing its logic on Autoit and here's the consequence... I just tried to implement the logic, so no animations and number of players two.
Chain Reaction
Following is the source code and a video of this in action...




Code follows....

;Chain Reaction game logic implementation for desktop
;libraries...
#include <GUIConstantsEx.au3>
#include <GuiButton.au3>
;Variables
Global $xc = 9, $yc = 7, $sleep = 200
Global $btn[63], $iMemo, $val[63], $col[63], $click = 1, $color, $red = 0, $green = 0
;initializing variables..
For $k = 0 To 62
   $col[$k] = 0
Next
;Creating gui
GUICreate("Buttons", 350, 350) 
$iMemo = GUICtrlCreateEdit("", 230, 10, 100, 150)
   $n = -1
For $yy = 0 To $yc-1
   Local $y = 10
    For $x = 0 To $xc-1
  $n = $n + 1
        $btn[$n] = GUICtrlCreateButton( $val[$n], 10+($yy*30), $y, 30, 30)
        $y += 30
Next
  Next
  $kk = 0
  $kk1 = -1
GUISetState()
;Logic beigins here
While(1)
;Reading input via click
$kl =  GUIGetMsg()
If $kl>2 Then
  $kk = $kl-3
  $kk1 = $kk - 1
  ;Setting number of possible dots per each box as 3 by default and then they change when required
  $poss = 3
  If ((mod($kk1,9)==0)Or(mod($kk1,9)==8)Or($kk1-mod($kk1,9)==0)Or(($kk1-mod($kk1,9))/9==$yc-1)) Then $poss = 2
  If (($kk1==0)Or($kk1==8)Or($kk1==54)Or($kk1==62)) Then $poss = 1
  GUICtrlSetData($iMemo, $kk)
  ;If there is a click on a box and if the number of dots in that block less than or equal
  ;to the max possible dots then incrementing the value..
  If $val[$kk1] < $poss Then
  $val[$kk1] = $val[$kk1]+1
  ;
  ;setting the color variable
  Switch $click
Case 1
                $click = 2
$col[$kk1] = $click
$color = 0xff0000
Case 2
                $click = 1
$col[$kk1] = $click
$color = 0x00ff00
        EndSwitch
;Enabling and disabling buttons
;Disabling the buttons filled by previous color and enabling the buttons to be filled by current color
For $k = 0 To 62
  If (($col[$k]<>$click)And($col[$k]<>0)) Then 
 GUICtrlSetState($btn[$k],$GUI_ENABLE)
  EndIf
  If (($col[$k]==$click)) Then 
 GUICtrlSetState($btn[$k],$GUI_DISABLE)
 EndIf
  Next
  ;
  ;setting the data and color
  GUICtrlSetData($btn[$kk1],$val[$kk1])
  GUICtrlSetBkColor($btn[$kk1],$color)
  ;If chain reaction triggers...
Else 
  ;Calculating and showing the reaction
  Global $jk = 1
  GUICtrlSetData($iMemo, "wait")
  switch $click
Case 1
                $click = 2
$col[$kk1] = $click
$color = 0xff0000
Case 2
                $click = 1
$col[$kk1] = $click
$color = 0x00ff00
        EndSwitch
;Calling a function to update the values after reaction
  $val = update($val,$kk1,0)
  While(1)
 Local $jk = 1
 ;Check if any of the element has exceeded and update if exceeded
  For $k = 0 To 62
 $yon = checkifexceed($val[$k],$k)
 If $yon == 0 Then
$val = update($val,$k,1)
$jk = 0
EndIf
 Next
 If $jk == 1 Then ExitLoop
  WEnd
  ;Enabling and disabling the corresponding boxes
For $k = 0 To 62
  If (($col[$k]<>$click)And($col[$k]<>0)) Then 
 GUICtrlSetState($btn[$k],$GUI_ENABLE)
  EndIf
  If (($col[$k]==$click)) Then 
 GUICtrlSetState($btn[$k],$GUI_DISABLE)
 EndIf
  Next
  $red = 0
$green = 0
For $cl = 0 To 62
  If $col[$cl] == 1 Then
 $red = 1
  ElseIf $col[$cl] == 2 Then
 $green = 1
  EndIf
Next
  If $red == 0 And $green == 1 Then 
 MsgBox(0,"Game Over","Green Wins")
 Exit
  EndIf
 If $green == 0 And $red == 1 Then 
MsgBox(0,"Game Over","Red wins")
Exit
 EndIf
EndIf
EndIf
        Switch $kl
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
GUICtrlSetData($iMemo, "play")
    WEnd
;Functions
;To update all the neighbouring boxes and current box when reaction occurs
Func update($val1,$kk1,$w)
  ;Sound indication that a reaction has occured
  Beep(1000,100)
  Local $nei[4] = [-1,-1,-1,-1]
  $nei = neigh($kk1)
  For $k = 0 To 3
 If $nei[$k]<>-1 Then
 $val1[$nei[$k]] += 1
 GUICtrlSetData($btn[$nei[$k]],$val1[$nei[$k]])
 GUICtrlSetBkColor($btn[$nei[$k]],$color)
 $col[$nei[$k]] = $click
 EndIf
  Next
  $poss = possval($kk1)
  If $w ==1 Then $poss += 1
    $val1[$kk1] -= $poss
GUICtrlSetData($btn[$kk1],$val1[$kk1])
GUICtrlSetBkColor($btn[$kk1],$color)
$col[$kk1] = $click
 If $val1[$kk1] == 0 Then
  GUICtrlSetBkColor($btn[$kk1],0xffffff)
  GUICtrlSetState($btn[$kk1],$GUI_ENABLE)
$col[$kk1] = 0
  EndIf
sleep($sleep)
Return $val1
 EndFunc 
 ;Function to find neighbours of a given box
 Func neigh($kk1)
Local $nei[4] = [-1,-1,-1,-1],$k = 0
  If Mod($kk1,9)<>0 Then
 $nei[$k] = $kk1-1
 $k += 1
 EndIf
  If Mod($kk1,9)<>$xc-1 Then
 $nei[$k] = $kk1+1
 $k += 1
 EndIf
  If ($kk1-Mod($kk1,9))<>0 Then
 $nei[$k] = $kk1-9
 $k += 1
 EndIf
  If ($kk1-mod($kk1,9))/9<>$yc-1 Then
 $nei[$k] = $kk1+9
 $k += 1
 EndIf
    
Return $nei
 EndFunc
 ;function to check if the value in a box has exceeded the limit
 Func checkifexceed($val,$kk1)
  local $yon = 1,$poss = 3
  If ((mod($kk1,9)==0)Or(mod($kk1,9)==8)Or($kk1-mod($kk1,9)==0)Or(($kk1-mod($kk1,9))/9==$yc-1)) Then $poss = 2
  If (($kk1==0)Or($kk1==8)Or($kk1==54)Or($kk1==62)) Then $poss = 1
 If $val > $poss Then $yon = 0
Return $yon
 EndFunc
 ;Max possible value in a box
Func possval($kk1)
  local $poss = 3
  If ((mod($kk1,9)==0)Or(mod($kk1,9)==8)Or($kk1-mod($kk1,9)==0)Or(($kk1-mod($kk1,9))/9==$yc-1)) Then $poss = 2
  If (($kk1==0)Or($kk1==8)Or($kk1==54)Or($kk1==62)) Then $poss = 1
Return $poss
 EndFunc 


You can also download it from here.

Written by

0 comments:

Post a Comment

 

© 2013 The Repository. All rights resevered. Designed by Templateism

Back To Top