SDL Konnect4
This program was translated by Johnno56 from NaaLaa to SDLBasic.
It is a remake of an old game called "Connect4"
SDLkonnect4.zip Size : 13.742 Kb Type : zip |
|
'
' Konnect4 - Rick3137 - N6
'
common size, event, sqrcolor, mx, my, square, column, row, column2
common gameover, move, BackGround
dim Board[99]
dim Board2[80]
dim TopSquare[12]
dim BestMove[12]
click = loadsound("data/click.wav")
beep2 = loadsound("data/beep2.wav")
beep3 = loadsound("data/beep3.wav")
image1 = loadImage("data/image1.png",1) ' red player
image2 = loadImage("data/image2.png",2) ' blue player
randomize(timer)
setDisplay(1200,700,32,1)
move = 1
'==================================================================================
sub start()
ThisIsAPlaceHolder = 1
end sub
sub Setup()
'rem Setup Variables and other things here.
gameover = 0
event = 1
for a= 1 to 64
Board[a] = 0
next
end sub
sub SetupBackground
x = 0
y = 0
BackGround = 42
'create image BackGround,1200,700
'set image BackGround
ink(rgb(0,5,15))
rectangle(0,0,1200,700,1)
ink(rgb(0,30,40)) ',5
for a = 1 to 46600
rectangle(x,y,50,50,0)
x = x + 4
if x > 1150 then
x = 0
y = y + 4
end if
next
'set image primary
'set color 255,255,255
end sub
sub SetupSound()
click = loadsound("data/click.wav")
beep3 = loadsound("data/beep3.wav")
beep2 = loadsound("data/beep2.wav")
end sub
sub SetupPieces
' image1 = loadImage("data/image1.png",1) ' red player
' image2 = loadImage("data/image2.png",2) ' blue player
for a = 1 to 64
Board[a] = 0
next
end sub
sub DrawBoard
'rem set color 0,10,30
'rem cls
'draw image BackGround,0,0
ink(rgb(255,255,255))
rectangle(250,10,674,674,0)
rectangle(252,12,670,670,0)
ink(rgb(200,245,255))
rectangle(254,14,666,666,1)
ink(rgb(0,0,0))
rectangle(254,14,666,666,0)
rectangle(256,16,662,662,0)
rectangle(258,18,658,658,0)
rectangle(260,20,654,654,0)
ink(rgb(0,0,255)) ',20
rectangle(262,22,650,650,0)
ink(rgb(255,10,10))
rectangle(264,24,646,646,0)
ink(rgb(255,255,255))
rectangle(266,26,642,642,0)
end sub
sub MakeSquares()
'x;y;a;b
x = 267
y = 27
size = 80
ink(rgb(0,0,80))
rectangle(267,27,640,640,0)
rectangle(266,26,642,642,0)
for a = 1 to 8
for b = 1 to 8
rectangle(x,y,size,size,0)
y = y + size
next
x = x + size
y = 27
next
'set color 255,255,255
end sub
sub DisplayPieces()
'set color 255,255,255
'x=0:y=0:a=0:b=0:c=0
x = 267
y = 29
for a = 1 to 64
b = Board[a]
'if b > 0 then
if b = 1 then: pasteIcon(x,y,image1): end if
if b = 2 then: pasteIcon(x,y,image2): end if
'end if
c = Board2[a]
x = x + 80
if x > 900 then
x = 267
y = y + 80
end if
next
'set color 255,255,255
end sub
sub PlayerMove
' ink(rgb(255,255,0))
' text(0,0,24,"PlayerMove - Accessed")
ofset = 0
for a = 1 to 8
if Board[column + ofset ] = 0 then
Board[column + ofset] = 1
if ofset > 0 then: Board[column + ofset - 8 ] = 0: end if
playsound(click,1)
ofset = ofset + 8
end if
DrawBoard
MakeSquares
DisplayPieces
'redraw
wait(100)
next
move = 2
end sub
sub CheckMouse
'a;b;c;x;y;x2;y2
if mousebutton then
mx = mousex
my = mousey
square = 0
playsound(click,1)
x = 267
x2 = 347
y = 27
y2 = 107
for b = 1 to 8
if mx > x and mx < x2 then: column = b: end if
x = x + 80
x2 = x2 + 80
next
for c = 1 to 8
if my > y and my < y2 then: row = c: end if
y = y + 80
y2 = y2 + 80
next
if column > 0 then
square = row * 8 - 8 + column
' rem proc SelectSquare square
end if
if move = 1 then
PlayerMove
end if
end if
end sub
sub EndGame()
a = 0
b = 0
if gameover > 0 then: event = 3: end if
end sub
sub HowToPlay
a = 1
'create font 0, "arial", 24
ink(rgb(255,255,255))
'set caret 200,100
text(200,100,24," To play the game, select any column")
text(200,130,24,"and click it to drop your piece into it.")
text(200,160,24," You win if you get four in a row before ")
text(200,190,24,"the computer does. ")
'prints("") 220
'prints("") 250
'prints("") 280
'prints("") 310
text(200,340,24," For more information, see Google; 'connect four game' ")
'prints("") 370
ink(rgb(255,255,0))
text(200,400,24," Hit any key to continue. ")
ink(rgb(255,255,255))
'prints("") 430
text(200,460,24," http://rb23.yolasite.com " )
event=2
'redraw
waitkey
end sub
sub AfterGame
playsound(beep2,1)
'create font 0, "arial", 48
ink(rgb(255,255,255))
if gameover = 1 then: text(5,200,24," YOU WIN !"): end if
if gameover = 2 then: text(5,200,24,"COMPUTER"): end if
if gameover = 2 then: text(5,230,24," WINS..."): end if
if gameover = 3 then: text(2,200,24," A DRAW !"): end if
'create font 0, "arial", 16
'set caret 20,300
'wln " "
ink(rgb(0,255,0))
text(20,290,16,"Philipians 4:8")
ink(rgb(255,255,0))
text(20,320,16,"Finally, brothers,whatever is")
text(20,340,16,"true, whatever is noble,")
text(20,360,16,"whatever is right, whatever")
text(20,380,16,"is pure, whatever is lovely,")
text(20,400,16,"whatever is admirable -- if")
text(20,420,16,"anything is excellent or")
text(20,440,16,"praiseworthy -- think about")
text(20,460,16,"such things.")
'redraw
waitkey
cls
end sub
sub SetColor(a)
if a = 1 then: ink(rgb(60,255,50)): end if
if a = 2 then: ink(rgb(20,20,255)): end if
if a = 3 then: ink(rgb(250,50,50)): end if
if a = 4 then: ink(rgb(50,250,250)): end if
if a = 5 then: ink(rgb(250,50,250)): end if
if a = 6 then: ink(rgb(250,250,50)): end if
if a = 7 then: ink(rgb(150,20,180)): end if
if a = 8 then: ink(rgb(20,130,130)): end if
sqrcolor = a
end sub
sub GetTopSquares
for a = 1 to 8
ofset = 0
for b = 1 to 8
square = Board[a+ofset]
if square = 0 and a+ ofset< 65 then: TopSquare[a] = a + ofset: end if
ofset = ofset + 8
next
next
end sub
sub ScanMoves
for a = 1 to 8
b = TopSquare[a]
if b > 0 then
if a > 3 and a < 6 then: BestMove[a] = 1: end if
if Board[b+8] = 1 and Board[b+16]=1 and Board[b+24]=1 then: BestMove[a] = 10: end if
if Board[b+8] = 2 and Board[b+16]=2 and Board[b+24]=2 then: BestMove[a] = 20: end if
if a<7 and Board[b+1] = 2 then: BestMove[a] = BestMove[a] + 1: end if
if a<6 and Board[b+1] = 1 and Board[b+2]=1 and Board[b+3]=1 then: BestMove[a] = 10: end if
if a<6 and Board[b+1] = 2 and Board[b+2]=2 and Board[b+3]=2 then: BestMove[a] = 20: end if
if a<7 and Board[b+1] = 1 and Board[b+2] = 1 then: BestMove[a] = BestMove[a] + 2: end if
if b>2 and Board[b-1] = 1 and Board[b-2] = 1 and a>3 then: BestMove[a] = BestMove[a] + 2: end if
if a>1 and Board[b+1] = 2 and Board[b+2] = 2 then: BestMove[a] = BestMove[a] + 2: end if
if b>2 and Board[b-1] = 2 and Board[b-2] = 2 and a>2 then: BestMove[a] = BestMove[a] + 2: end if
if a>3 then
if Board[b-1] = 2 then: BestMove[a] = BestMove[a] + 1: end if
if Board[b-1] = 1 and Board[b-2]=1 and Board[b-3]=1 then: BestMove[a] = 10: end if
if Board[b-1] = 2 and Board[b-2]=2 and Board[b-3]=2 then: BestMove[a] = 20: end if
end if
end if
next
end sub
sub ScanMoves2
for a = 1 to 8
b = TopSquare[a]
if b > 0 then
if a<6 then
if Board[b+9] = 1 and Board[b+18]=1 and Board[b+27]=1 then: BestMove[a] = 10: end if
if Board[b+9] = 2 and Board[b+18]=2 and Board[b+27]=2 then: BestMove[a] = 20: end if
if Board[b+1] = 1 and Board[b+9]=1 and Board[b+18]=1 then: BestMove[a] = 0: end if
end if
if a>3 then
if Board[b+7] = 1 and Board[b+14]=1 and Board[b+21]=1 then: BestMove[a] = 10: end if
if Board[b+7] = 2 and Board[b+14]=2 and Board[b+21]=2 then: BestMove[a] = 20: end if
if Board[b-1] = 1 and Board[b+8]=1 and Board[b+16]=1 then: BestMove[a] = 0: end if
end if
end if
next
end sub
sub ScanMoves3
for a = 1 to 8
b = TopSquare[a]
if b > 0 then
if a<7 and a>1 and Board[b+1] = 1 and Board[b+2]=1 and Board[b-1]=1 then: BestMove[a] = 10: end if
if a<8 and a>2 and Board[b-1] = 1 and Board[b-2]=1 and Board[b+1]=1 then: BestMove[a] = 10: end if
if a<6 and Board[b+1] = 2 and Board[b+2]=2 and Board[b+3] = 0 then: BestMove[a] = BestMove[a] + 3: end if
if a>3 and Board[b-1] = 2 and Board[b-2]=2 and Board[b-3] = 0 then: BestMove[a] = BestMove[a] + 3: end if
if a<9 and a>3 and b>14 and Board[b-7] = 1 and Board[b-14]=1 and Board[b+7]=1 then: BestMove[a] = BestMove[a] + 10: end if
if a<9 and a>3 and b>14 and Board[b-7] = 2 and Board[b-14]=2 and Board[b+7]=2 then: BestMove[a] = 20: end if
if a<9 and a>3 and b>7 and Board[b-7] = 1 and Board[b+14]=1 and Board[b+7]=1 then: BestMove[a] = BestMove[a] + 10: end if
if a<9 and a>3 and b>7 and Board[b-7] = 2 and Board[b+14]=2 and Board[b+7]=2 then: BestMove[a] = 20: end if
if a<9 and a>3 and b>18 and Board[b-9] = 1 and Board[b-18]=1 and Board[b+9]=1 then: BestMove[a] = BestMove[a] + 10: end if
if a<9 and a>3 and b>18 and Board[b-9] = 2 and Board[b-18]=2 and Board[b+9]=2 then: BestMove[a] = 20: end if
if a<9 and a>3 and b>9 and Board[b-9] = 1 and Board[b+18]=1 and Board[b+9]=1 then: BestMove[a] = BestMove[a] + 10: end if
if a<9 and a>3 and b>9 and Board[b-9] = 2 and Board[b+18]=2 and Board[b+9]=2 then: BestMove[a] = 20: end if
end if
next
end sub
sub ScanMoves4
for a = 1 to 8
b = TopSquare[a]
if b > 0 then
if a<6 and a>0 and b>7 and Board[b-7] = 1 and Board[b-6]=1 and Board[b-5]=1 then: BestMove[a] = BestMove[a] - 9: end if
if a<7 and a>1 and b>9 and Board[b-9] = 1 and Board[b-7]=1 and Board[b-6]=1 then: BestMove[a] = BestMove[a] - 9: end if
if a<9 and a>3 and b>11 and Board[b-9] = 1 and Board[b-10]=1 and Board[b-11]=1 then: BestMove[a] = BestMove[a] - 9: end if
if a<7 and a>2 and b>10 and Board[b-9] = 1 and Board[b-10]=1 and Board[b-7]=1 then: BestMove[a] = BestMove[a] - 9: end if
if a<6 and a>0 and b>28 and Board[b-14] = 1 and Board[b-21]=1 and Board[b-28]=1 then: BestMove[a] = BestMove[a] - 9: end if
if a<6 and a>0 and b>36 and Board[b-18] = 1 and Board[b-27]=1 and Board[b-36]=1 then: BestMove[a] = BestMove[a] - 9: end if
if a<6 and a>3 and b>11 and Board[b+1] = 1 and Board[b+10]=1 and Board[b+19]=1 then: BestMove[a] = BestMove[a] - 9: end if
if a<7 and a>2 and b>1 and Board[b-1] = 1 and Board[b+6]=1 and Board[b+13]=1 then: BestMove[a] = BestMove[a] - 9: end if
if a = 1 or a = 8 or a = 2 or a = 7 then: BestMove[a] = BestMove[a] - 2: end if
end if
next
end sub
function PickBestMove(m)
b=0
c=0
for a = 1 to 8
if BestMove[a] > b then
b = BestMove[a]
c = a
end if
next
if b > 0 then: return c: end if
return m
end function
sub ComputerMove
ofset = 0
a = 1
for a = 1 to 8
BestMove[a] = 0
next
while a > 0
column2 = rnd(8)+1
col = column2
a = Board[col]
wend
GetTopSquares
ScanMoves
ScanMoves2
ScanMoves3
ScanMoves4
col = PickBestMove(col)
column2 = col
for a = 1 to 8
if Board[col + ofset ] = 0 then
Board[col + ofset] = 2
if ofset > 0 then: Board[col + ofset - 8 ] = 0: end if
playsound(beep2,1)
ofset = ofset + 8
end if
DrawBoard
MakeSquares
DisplayPieces
'redraw
wait(100)
next
move = 1
end sub
sub CheckForPlayerWin
col = 1
rw = 1
if Board[1] > 0 and Board[2] > 0 and Board[3] > 0 and Board[4] > 0 and Board[5] > 0 and Board[6] > 0 and Board[7] > 0 and Board[8] > 0 then: gameover = 3: end if
for a = 1 to 64
b = Board[a]
if b = 1 and col < 6 and Board[a+1] = 1 and Board[a+2] = 1 and Board[a+3] = 1 then: gameover = 1: end if
if b = 1 and rw < 6 and Board[a+8] = 1 and Board[a+16] = 1 and Board[a+24] = 1 then: gameover = 1: end if
if b = 1 and rw < 6 and col<6 and Board[a+9] = 1 and Board[a+18] = 1 and Board[a+27] = 1 then: gameover = 1: end if
if b = 1 and rw > 3 and col<6 and Board[a-7] = 1 and Board[a-14] = 1 and Board[a-21] = 1 then: gameover = 1: end if
col = col + 1
if col = 9 then
col = 1
rw = rw + 1
end if
next
end sub
sub CheckForComputerWin
col = 1
rw = 1
if Board[1] > 0 and Board[2] > 0 and Board[3] > 0 and Board[4] > 0 and Board[5] > 0 and Board[6] > 0 and Board[7] > 0 and Board[8] > 0 then: gameover = 3: end if
for a = 1 to 64
b = Board[a]
if b = 2 and col < 6 and Board[a+1] = 2 and Board[a+2] = 2 and Board[a+3] = 2 then: gameover = 2: end if
if b = 2 and rw < 6 and Board[a+8] = 2 and Board[a+16] = 2 and Board[a+24] = 2 then: gameover = 2: end if
if b = 2 and rw < 6 and col<6 and Board[a+9] = 2 and Board[a+18] = 2 and Board[a+27] = 2 then: gameover = 2: end if
if b = 2 and rw > 3 and col<6 and Board[a-7] = 2 and Board[a-14] = 2 and Board[a-21] = 2 then: gameover = 2: end if
col = col + 1
if col = 9 then
col = 1
rw = rw + 1
end if
next
end sub
'==============================================================================
SetupPieces
Setup
'SetupSound
'SetupBackground
do
wait(50)
if event = 2 then
if gameover = 0 then
DrawBoard
DisplayPieces
MakeSquares
CheckMouse
if gameover < 1 then: CheckForPlayerWin: end if
if move = 2 and gameover < 1 then: ComputerMove: end if
if gameover<1 then: CheckForComputerWin: end if
end if
if gameover > 0 then: EndGame: end if
end if
if event = 1 then: HowToPlay: cls: SetupBackground: wait(100): end if
'if event = 1 then: HowToPlay: cls: end if
'redraw
if event = 3 then: exit do: end if
loop
AfterGame
end
waitkey(27)