Microsoft Small Basic

Program Listing: RZT730
'=============================================
'Connect 4
'by Vince Long, December 2011
'=============================================


DrawBoard()
DrawTitle()
Init_Stuff()
Update_Player()

Main_Game_Loop()


'================================
'Begin_Game
'================================
Sub Main_Game_Loop


endSub

'================================
'Initialize things
'================================
Sub Init_Stuff

player = 1

' initialize keys
keyOne = "D1"
keyTwo = "D2"
keyThree = "D3"
keyFour = "D4"
keyM = "M"
keyQ = "Q"
right = "Right"
left = "Left"
returnKey = "Return"
escapeKey = "Escape"
GraphicsWindow.KeyDown=KeyPressed
p=0
key_lock = "Off"
Create_the_Piece()
endSub



'================================
'Draw Board
'================================
Sub DrawBoard
' set the screen size
GraphicsWindow.Width = 480
GraphicsWindow.Height = 320

' set the background; do it by drawing a big rectangle because
' GraphicsWindow.GetPixel on a background set with GraphicsWindow.BackgroundColor
' it will not return the correct color

GraphicsWindow.BrushColor = "LightBlue"
GraphicsWindow.FillRectangle(-10,-10,GraphicsWindow.Width+20,GraphicsWindow.Height+20)
GraphicsWindow.Show()

GraphicsWindow.PenWidth = 2
GraphicsWindow.PenColor = "black"
x=20
y=60
dia = 40
For col = 1 To 7
For row = 1 To 6
GraphicsWindow.DrawEllipse(x+dia*(col-1),y+dia*(row-1),40,40)
cell[col][row] = 0
EndFor
EndFor
EndSub

'================================
'Draw Title
'================================
Sub DrawTitle
GraphicsWindow.BrushColor = "DarkGreen"
GraphicsWindow.FontItalic = "False"
GraphicsWindow.FontName = "Comic Sans MS"
GraphicsWindow.FontSize = 20
GraphicsWindow.DrawText(330, 20, "Connect Four")

GraphicsWindow.FontSize = 18
GraphicsWindow.BrushColor = "Red"
GraphicsWindow.DrawText(320, 60, "Player 1 = Red")
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(320, 90, "Player 2 = Black")

GraphicsWindow.BrushColor = "DarkGreen"
GraphicsWindow.DrawText(320, 130, "Use Arrow Keys")
GraphicsWindow.DrawText(350, 150, "to move")
GraphicsWindow.DrawText(325, 180, "Use Enter Key")
GraphicsWindow.DrawText(350, 200, "to drop")
EndSub

'=================================
' Create the Piece
'=================================
Sub Create_the_Piece
column_pointer = 1
piecex = 20
piecey = 20
p=p+1
GraphicsWindow.PenWidth = 2
GraphicsWindow.PenColor = "black"

if player = 1 then
GraphicsWindow.BrushColor = "Red"
else
GraphicsWindow.BrushColor = "Black"
endIf

piece[p] = Shapes.AddEllipse(40, 40)

Shapes.HideShape(piece[p])
Shapes.Move(piece[p], piecex,piecey)
Shapes.ShowShape(piece[p])
endSub


'============================
'Keypressed subroutine
'==============================

Sub KeyPressed

If key_lock = "Off" then
key_lock = "On"
If GraphicsWindow.LastKey = left and piecex > 40 then
piecex = piecex - 40
Shapes.Move(piece[p], piecex, piecey)
column_pointer = column_pointer - 1
elseIf GraphicsWindow.LastKey = right and piecex < 260 then
piecex = piecex + 40
Shapes.Move(piece[p], piecex, piecey)
column_pointer = column_pointer + 1
elseIf GraphicsWindow.LastKey = returnKey then
DropPiece()
elseIf GraphicsWindow.LastKey = keyQ then
player = 2
elseIf GraphicsWindow.LastKey = escapeKey then
Show_Matrix()

EndIf
endIf
key_lock = "Off"
EndSub

'============================
'DropPiece subroutine
'==============================

Sub DropPiece

row_pointer = 1

While cell[column_pointer][row_pointer] = 0 And row_pointer < 7
piecey = piecey + 40

Shapes.Move(piece[p],piecex,piecey)
Shapes.ShowShape(piece[p])
Program.Delay(500)
row_pointer = row_pointer + 1
endWhile

If row_pointer <> 1 then
cell[column_pointer][row_pointer-1] = player

check_for_winner()

if player = 1 then
player = 2
else
player = 1
endIf

Update_Player()
Create_the_Piece()
endIf
key_lock = "Off"
endsub

'============================
'Update Player
'============================
Sub Update_Player
If player = 1 then
GraphicsWindow.BrushColor = "LightBlue"
GraphicsWindow.DrawText(330, 250, "Turn: Player 2")
GraphicsWindow.BrushColor = "Red"
GraphicsWindow.DrawText(330, 250, "Turn: Player 1")
ElseIf player = 2 then
GraphicsWindow.BrushColor = "LightBlue"
GraphicsWindow.DrawText(330, 250, "Turn: Player 1")
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(330, 250, "Turn: Player 2")
EndIf
endSub

'============================
'Check for Winner
'============================
Sub check_for_winner
' move a 4x4 map across the board and sum the rows and columns
' looking for 4 or an 8, depending on the player

' test for horizontal winner
For origin_column = 1 to 4
For origin_row = 1 to 3

For row = origin_row to origin_row + 4
For column = origin_column to origin_column + 4
if player = 1 AND cell[column][row] = 1 then
sum = sum + cell[column][row]
endIf
if player = 2 AND cell[column][row] = 2 then
sum = sum + cell[column][row]
endIf
endFor

if sum = player * 4 then
winner = 1
GraphicsWindow.DrawText(350, 220, "Winner")
endIf
sum = 0
endFor
endFor
endFor

' test for vertical winner
For origin_column = 1 to 4
For origin_row = 1 to 3

For column = origin_column to origin_column + 4
For row = origin_row to origin_row + 4
if player = 1 AND cell[column][row] = 1 then
sum = sum + cell[column][row]
endIf
if player = 2 AND cell[column][row] = 2 then
sum = sum + cell[column][row]
endIf
endFor

if sum = player * 4 then
winner = 1
GraphicsWindow.DrawText(350, 220, "Winner")
endIf
sum = 0
endFor
endFor
endFor

' test for diagonal downhill winner
For origin_column = 1 to 4
For origin_row = 1 to 3
For i = 0 to 3
if player = 1 AND cell[origin_column+i][origin_row+i] = 1 then
sum = sum + 1
endIf
if player = 2 AND cell[origin_column+i][origin_row+i] = 2 then
sum = sum + 2
endIf
endFor
if sum = player * 4 then
winner = 1
GraphicsWindow.DrawText(350, 220, "Winner")
endIf
sum = 0

endFor
endFor
if winner = 1 then
GraphicsWindow.DrawText(350, 220, "Winner")
endif


' test for diagonal uphill winner
For origin_column = 1 to 4
For origin_row = 1 to 3
if player = 1 and cell[origin_column][origin_row+3] = 1 Then
sum = sum + 1
endif
if player = 1 And cell[origin_column+1][origin_row+2] = 1 then
sum = sum + 1
EndIf
If player = 1 And cell[origin_column+2][origin_row+1] = 1 Then
sum = sum + 1
EndIf
If player = 1 And cell[origin_column+3][origin_row] = 1 Then
sum = sum + 1
Endif

if player = 2 and cell[origin_column][origin_row+3] = 2 Then
sum = sum + 2
endif
if player = 2 And cell[origin_column+1][origin_row+2] = 2 then
sum = sum + 2
EndIf
If player = 2 And cell[origin_column+2][origin_row+1] = 2 Then
sum = sum + 2
EndIf
If player = 2 And cell[origin_column+3][origin_row] = 2 Then
sum = sum + 2
Endif

TextWindow.WriteLine(sum)
If player = 1 and sum = 4 then
GraphicsWindow.DrawText(350, 205, "Player One Wins")
endIf
If player = 2 and sum = 8 then
GraphicsWindow.DrawText(350, 205, "Player Two Wins")
endIf
sum = 0
endFor
endFor
TextWindow.WriteLine("--")
endSub


'============================
'Show Matrix
'============================
Sub Show_Matrix
TextWindow.Show()


For row = 1 To 6
For col = 1 To 7
TextWindow.Write(cell[col][row]+"-")
EndFor
TextWindow.WriteLine("end")
EndFor

Program.Delay(10000)
endSub