Post by tailoredreaction on Dec 1, 2020 22:10:09 GMT
If I in fact have jetted off in the wrong direction from almost day 1, then this little bit of programming will totally mess with your mind.
"Connect 4" was my first program I ever wrote using JustBasic and the GUI features.
====================================================================================
I will need to learn how to post code without doing this! :-)
Copy/paste and run this in version2. It works. Not 100% done with the buttons at the end of the game yet, but it still works fine for now.
===================================================================
"Connect 4" was my first program I ever wrote using JustBasic and the GUI features.
====================================================================================
I will need to learn how to post code without doing this! :-)
Copy/paste and run this in version2. It works. Not 100% done with the buttons at the end of the game yet, but it still works fine for now.
===================================================================
[Connect4Start]
nomainwin ' getting rid of the main window before starting
WindowWidth = 888 ' setting the graphic window dimensions
WindowHeight = 750
UpperLeftX=int((DisplayWidth-WindowWidth)/2) ' centering the graphic window
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
' Putting all of the graphic window elements in the window
graphicbox #main.graphicbox1, 26, 536, 100, 100
graphicbox #main.graphicbox2, 128, 536, 100, 100
graphicbox #main.graphicbox3, 230, 536, 100, 100
graphicbox #main.graphicbox4, 332, 536, 100, 100
graphicbox #main.graphicbox5, 434, 536, 100, 100
graphicbox #main.graphicbox6, 536, 536, 100, 100
graphicbox #main.graphicbox7, 638, 536, 100, 100
graphicbox #main.graphicbox8, 26, 434, 100, 100
graphicbox #main.graphicbox9, 128, 434, 100, 100
graphicbox #main.graphicbox10, 230, 434, 100, 100
graphicbox #main.graphicbox11, 332, 434, 100, 100
graphicbox #main.graphicbox12, 434, 434, 100, 100
graphicbox #main.graphicbox13, 536, 434, 100, 100
graphicbox #main.graphicbox14, 638, 434, 100, 100
graphicbox #main.graphicbox15, 26, 332, 100, 100
graphicbox #main.graphicbox16, 128, 332, 100, 100
graphicbox #main.graphicbox17, 230, 332, 100, 100
graphicbox #main.graphicbox18, 332, 332, 100, 100
graphicbox #main.graphicbox19, 434, 332, 100, 100
graphicbox #main.graphicbox20, 536, 332, 100, 100
graphicbox #main.graphicbox21, 638, 332, 100, 100
graphicbox #main.graphicbox22, 26, 230, 100, 100
graphicbox #main.graphicbox23, 128, 230, 100, 100
graphicbox #main.graphicbox24, 230, 230, 100, 100
graphicbox #main.graphicbox25, 332, 230, 100, 100
graphicbox #main.graphicbox26, 434, 230, 100, 100
graphicbox #main.graphicbox27, 536, 230, 100, 100
graphicbox #main.graphicbox28, 638, 230, 100, 100
graphicbox #main.graphicbox29, 26, 128, 100, 100
graphicbox #main.graphicbox30, 128, 128, 100, 100
graphicbox #main.graphicbox31, 230, 128, 100, 100
graphicbox #main.graphicbox32, 332, 128, 100, 100
graphicbox #main.graphicbox33, 434, 128, 100, 100
graphicbox #main.graphicbox34, 536, 128, 100, 100
graphicbox #main.graphicbox35, 638, 128, 100, 100
graphicbox #main.graphicbox36, 26, 26, 100, 100
graphicbox #main.graphicbox37, 128, 26, 100, 100
graphicbox #main.graphicbox38, 230, 26, 100, 100
graphicbox #main.graphicbox39, 332, 26, 100, 100
graphicbox #main.graphicbox40, 434, 26, 100, 100
graphicbox #main.graphicbox41, 536, 26, 100, 100
graphicbox #main.graphicbox42, 638, 26, 100, 100
graphicbox #main.graphicbox43, 760, 340, 100, 100
button #main.button1, "CLICK", [button1Click], UL, 26, 644, 100, 50
button #main.button2, "Column 2", [button2Click], UL, 128, 644, 100, 50
button #main.button3, "Column 3", [button3Click], UL, 230, 644, 100, 50
button #main.button4, "Column 4", [button4Click], UL, 332, 644, 100, 50
button #main.button5, "Column 5", [button5Click], UL, 434, 644, 100, 50
button #main.button6, "Column 6", [button6Click], UL, 536, 644, 100, 50
button #main.button7, "Column 7", [button7Click], UL, 638, 644, 100, 50
button #main.button8, "Play Again", [button8Click], UL, 760, 450, 100, 50
button #main.button9, "QUIT", [button9Click], UL, 760, 510, 100, 50
button #main.button10, "End Game", [button10Click], UL, 760, 128, 100, 50
open "untitled" for window as #main
[RePlay]
DIM ee$(3) ' picking the game's colours
ee$(0) = " brown"
ee$(1) = " cyan"
ee$(2) = " black"
#main.button1, "!enable"
#main.button2, "!enable"
#main.button3, "!enable"
#main.button4, "!enable"
#main.button5, "!enable"
#main.button6, "!enable"
#main.button7, "!enable"
DIM cc$(42) ' making all the squares one colour to start the game
for dd = 1 to 42
cc$(dd) = ee$(0)
next dd
m = 0
k = 0
[TheGraphicBoxGrid] ' this is a loop back to fill the grid until someone "wins"
print #main.graphicbox1,"down; fill"; cc$(1);" flush"
print #main.graphicbox2,"down; fill"; cc$(2);" flush"
print #main.graphicbox3,"down; fill"; cc$(3);" flush"
print #main.graphicbox4,"down; fill"; cc$(4);" flush"
print #main.graphicbox5,"down; fill"; cc$(5);" flush"
print #main.graphicbox6,"down; fill"; cc$(6);" flush"
print #main.graphicbox7,"down; fill"; cc$(7);" flush"
print #main.graphicbox8,"down; fill"; cc$(8);" flush"
print #main.graphicbox9,"down; fill"; cc$(9);" flush"
print #main.graphicbox10,"down; fill"; cc$(10);" flush"
print #main.graphicbox11,"down; fill"; cc$(11);" flush"
print #main.graphicbox12,"down; fill"; cc$(12);" flush"
print #main.graphicbox13,"down; fill"; cc$(13);" flush"
print #main.graphicbox14,"down; fill"; cc$(14);" flush"
print #main.graphicbox15,"down; fill"; cc$(15);" flush"
print #main.graphicbox16,"down; fill"; cc$(16);" flush"
print #main.graphicbox17,"down; fill"; cc$(17);" flush"
print #main.graphicbox18,"down; fill"; cc$(18);" flush"
print #main.graphicbox19,"down; fill"; cc$(19);" flush"
print #main.graphicbox20,"down; fill"; cc$(20);" flush"
print #main.graphicbox21,"down; fill"; cc$(21);" flush"
print #main.graphicbox22,"down; fill"; cc$(22);" flush"
print #main.graphicbox23,"down; fill"; cc$(23);" flush"
print #main.graphicbox24,"down; fill"; cc$(24);" flush"
print #main.graphicbox25,"down; fill"; cc$(25);" flush"
print #main.graphicbox26,"down; fill"; cc$(26);" flush"
print #main.graphicbox27,"down; fill"; cc$(27);" flush"
print #main.graphicbox28,"down; fill"; cc$(28);" flush"
print #main.graphicbox29,"down; fill"; cc$(29);" flush"
print #main.graphicbox30,"down; fill"; cc$(30);" flush"
print #main.graphicbox31,"down; fill"; cc$(31);" flush"
print #main.graphicbox32,"down; fill"; cc$(32);" flush"
print #main.graphicbox33,"down; fill"; cc$(33);" flush"
print #main.graphicbox34,"down; fill"; cc$(34);" flush"
print #main.graphicbox35,"down; fill"; cc$(35);" flush"
print #main.graphicbox36,"down; fill"; cc$(36);" flush"
print #main.graphicbox37,"down; fill"; cc$(37);" flush"
print #main.graphicbox38,"down; fill"; cc$(38);" flush"
print #main.graphicbox39,"down; fill"; cc$(39);" flush"
print #main.graphicbox40,"down; fill"; cc$(40);" flush"
print #main.graphicbox41,"down; fill"; cc$(41);" flush"
print #main.graphicbox42,"down; fill"; cc$(42);" flush"
print #main.graphicbox43,"down; fill"; ee$(m + 1);" flush"
print #main.button1, "!font Arial_Black 10"
print #main.button2, "!font Arial_Black 10"
print #main.button3, "!font Arial_Black 10"
print #main.button4, "!font Arial_Black 10"
print #main.button5, "!font Arial_Black 10"
print #main.button6, "!font Arial_Black 10"
print #main.button7, "!font Arial_Black 10"
print #main.button8, "!font Arial_Black 10"
print #main.button9, "!font Arial_Black 10"
print #main.button10, "!font Arial_Black 10"
print #main, "font ms_sans_serif 0 16"
if k <> 0 then [RealWinner] ' this looks to see if a winner was found
wait
[quit.main]
Close #main
END
' here the program waits for a player to choose a column
[button1Click] 'Performs an action for the button named 'button1'
'Insert your own code here
tt = 1
goto [FillGraphicBox]
wait
[button2Click] 'Perform action for the button named 'button2'
'Insert your own code here
tt = 2
goto [FillGraphicBox]
wait
[button3Click] 'Perform action for the button named 'button3'
'Insert your own code here
tt = 3
goto [FillGraphicBox]
wait
[button4Click] 'Perform action for the button named 'button4'
'Insert your own code here
tt = 4
goto [FillGraphicBox]
wait
[button5Click] 'Perform action for the button named 'button5'
'Insert your own code here
tt = 5
goto [FillGraphicBox]
wait
[button6Click] 'Perform action for the button named 'button6'
'Insert your own code here
tt = 6
goto [FillGraphicBox]
wait
[button7Click] 'Perform action for the button named 'button7'
'Insert your own code here
tt = 7
goto [FillGraphicBox]
wait
[button8Click] 'Perform action for the button named 'button9'
'Insert your own code here
goto [RePlay] ' this returns to reset the grid so all buttons are again = to ee$(0)
wait ' it also resets 'k' to 0 so no "winner" is declared yet
[button9Click] 'Perform action for the button named 'button8'
'Insert your own code here
goto [EndGame]
wait
[button10Click] 'Performs an action for the button named 'button1'
'Insert your own code here
#main.button1, "!disable"
#main.button2, "!disable"
#main.button3, "!disable"
#main.button4, "!disable"
#main.button5, "!disable"
#main.button6, "!disable"
#main.button7, "!disable"
goto [EndCurrentGame]
wait
[FillGraphicBox] ' this checks the chosen column for the next available open spot
#main.button8, "!disable"
#main.button9, "!disable"
s = 0
if cc$(tt) = ee$(0) then goto [ColorSwitch]
s = s + 1
if cc$(tt + 7) = ee$(0) then goto [ColorSwitch]
s = s + 1
if cc$(tt + 14) = ee$(0) then goto [ColorSwitch]
s = s + 1
if cc$(tt + 21) = ee$(0) then goto [ColorSwitch]
s = s + 1
if cc$(tt + 28) = ee$(0) then goto [ColorSwitch]
s = s + 1
if cc$(tt + 35) = ee$(0) then goto [ColorSwitch]
notice "That column is full, click again!"
goto [TheGraphicBoxGrid]
[ColorSwitch] ' once an open spot is found, this puts the correct colour there
if m = 0 then cc$(tt + s*7) = ee$(1) else cc$(tt + s*7) = ee$(2)
m = m + 1
if m = 2 then m = 0
[CheckForWinner]
[Horizontal] ' checking for the 24 possible horizontal "wins"
if cc$(4) = ee$(0) then [Stage2]
for w = 1 to 4
if cc$(w) = cc$(w + 1) AND cc$(w) = cc$(w + 2) AND cc$(w) = cc$(w + 3) then goto [WINNER]
next w
[Stage2]
if cc$(11) = ee$(0) then [Stage3]
for w = 8 to 11
if cc$(w) = cc$(w + 1) AND cc$(w) = cc$(w + 2) AND cc$(w) = cc$(w + 3) then goto [WINNER]
next w
[Stage3]
if cc$(18) = ee$(0) then [Stage4]
for w = 15 to 18
if cc$(w) = cc$(w + 1) AND cc$(w) = cc$(w + 2) AND cc$(w) = cc$(w + 3) then goto [WINNER]
next w
[Stage4]
if cc$(25) = ee$(0) then [Stage5]
for w = 22 to 25
if cc$(w) = cc$(w + 1) AND cc$(w) = cc$(w + 2) AND cc$(w) = cc$(w + 3) then goto [WINNER]
next w
[Stage5]
if cc$(32) = ee$(0) then [Stage6]
for w = 29 to 32
if cc$(w) = cc$(w + 1) AND cc$(w) = cc$(w + 2) AND cc$(w) = cc$(w + 3) then goto [WINNER]
next w
[Stage6]
if cc$(39) = ee$(0) then [Stage7]
for w = 36 to 39
if cc$(w) = cc$(w + 1) AND cc$(w) = cc$(w + 2) AND cc$(w) = cc$(w + 3) then goto [WINNER]
next w
[Vertical] ' checking for the 21 possible vertical "wins"
[Stage7]
for w = 1 to 21
if cc$(w) = ee$(0) then [FUN14] else [FUN13]
[FUN13]
if cc$(w) = cc$(w + 7) AND cc$(w) = cc$(w + 14) AND cc$(w) = cc$(w + 21) then [WINNER]
[FUN14]
next w
[Diagonal] ' checking for the 24 possible diagonal "wins"
for w = 1 to 4
if cc$(w) <> ee$(0) then [FUN1] else [FUN2]
[FUN1]
if cc$(w) = cc$(w + 8) AND cc$(w) = cc$(w + 16) AND cc$(w) = cc$(w + 24) then [WINNER]
[FUN2]
next w
for w = 4 to 7
if cc$(w) <> ee$(0) then [FUN3] else [FUN4]
[FUN3]
if cc$(w) = cc$(w + 6) AND cc$(w) = cc$(w + 12) AND cc$(w) = cc$(w + 18) then [WINNER]
[FUN4]
next w
for w = 8 to 11
if cc$(w) <> ee$(0) then [FUN5] else [FUN6]
[FUN5]
if cc$(w) = cc$(w + 8) AND cc$(w) = cc$(w + 16) AND cc$(w) = cc$(w + 24) then [WINNER]
[FUN6]
next w
for w = 11 to 14
if cc$(w) <> ee$(0) then [FUN7] else [FUN8]
[FUN7]
if cc$(w) = cc$(w + 6) AND cc$(w) = cc$(w + 12) AND cc$(w) = cc$(w + 18) then [WINNER]
[FUN8]
next w
for w = 15 to 18
if cc$(w) <> ee$(0) then [FUN9] else [FUN10]
[FUN9]
if cc$(w) = cc$(w + 8) AND cc$(w) = cc$(w + 16) AND cc$(w) = cc$(w + 24) then [WINNER]
[FUN10]
next w
for w = 18 to 21
if cc$(w) <> ee$(0) then [FUN11] else [FUN12]
[FUN11]
if cc$(w) = cc$(w + 6) AND cc$(w) = cc$(w + 12) AND cc$(w) = cc$(w + 18) then [WINNER]
[FUN12]
next w
for w = 1 to 42
if cc$(w) = ee$(0) then [Stage8]
next w
[EndCurrentGame]
notice "TIE GAME!"
#main.button8, "!enable"
#main.button9, "!enable"
[Stage8]
goto [TheGraphicBoxGrid]
wait
[WINNER] ' a "winner was found, but first go back and fill in the winning move
k = k + 1
goto [TheGraphicBoxGrid]
[RealWinner]
#main.button8, "!enable"
#main.button9, "!enable"
DIM cc$(42) ' making all the squares the background colour to re-start the game
for nnn = 1 to 42
cc$(nnn) = ee$(0)
next nnn
if m = 1 then notice "Blue Wins!!" else notice "Black Wins!!"
#main.button1, "!disable"
#main.button2, "!disable"
#main.button3, "!disable"
#main.button4, "!disable"
#main.button5, "!disable"
#main.button6, "!disable"
#main.button7, "!disable"
wait
[EndGame] ' if "Quit" button is chosen, the game ends
goto [quit.main]