Connect 4
This code will enable you to, in ASP, play a game of connect 4 with 2 people sitting at the same PC.
Original Author: Steve Fitzpatrick
Inputs
Just a click
Assumptions

Returns
Calculates when a winning line has been entered.
Side Affects

API Declarations
This is visitware, you are permitted to use the ware, as long as you wisit the author's other site at http://www.cxcx.cx
Code
PLAY.ASP'''''''''''''''''''''''''''''''''''''''
<%
'Initialise variables
dim NewMatrix(12,14)
'Initialise matrix for start of game
for counterrow = 1 to 12
for countercol = 1 to 14
NewMatrix(counterrow, countercol) = 0
next
next
session("matrix") = NewMatrix
session("player") = 1
response.redirect "connect4.asp"
%>
''''''''''''''''''''''''''''''''''''''''''''''''''
CONNECT4.ASP''''''''''''''''''''''''''''''''''''''
''Connect 4 in ASP written by Steve Fitzpatrick April 2000
''
''Program is for 2 players sitting at the same pc
''
''This program is freeware and may be freely distributed so
''long as this banner remains at the top of the source code
''
''Contact Steve Fitzpatrick at steve@cxcx.cx
''Website is http://www.cxcx.cx
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
%>
<%
dim player
dim counterrow
dim countercol
dim row
dim col
dim theimage
dim matrix
dim onebelow
dim flagged
player = session("player")
matrix = session("matrix")
%>
<%
flagged = 0
row = request.querystring("row")
col = request.querystring("col")
if player mod 2 = 1 then
matrix(row,col) = 1
else
matrix(row,col) = 10
end if
%>
<%for row = 4 to 9%>
<%
for col = 4 to 10
if matrix(row,col) = 1 then theimage="images/c4blue.gif"
if matrix(row,col) = 10 then theimage="images/c4red.gif"
if matrix(row,col) = 0 then theimage="images/c4non.gif"
if row = 9 then
onebelow = true
else
if matrix(row+1,col) = 0 then
onebelow = false
else
onebelow = true
end if
end if
if matrix(row,col) = 0 and onebelow=True then %>
<% else %>
<% end if %>
<%
next
%>
<%
next
%>
<%
'Code to check if 4 have been placed in a row, can only happen after the 7th go at teh earliest.
if player>= 7 then
'declare target sum of 4 adjascent squares (4 for player one, 40 for player two)
dim target
if player mod 2 = 1 then target = 4
if player mod 2 = 0 then target = 40
dim total1, total2, total3, total4
'declare constant for check algorithm
dim k
row = request.querystring("row")
col = request.querystring("col")
'Check for 4 in a row  Killer Bob style
for k = 0 to 3
total1 = matrix(rowk,col) + matrix(rowk+1,col) + matrix(rowk+2,col) + matrix(rowk+3,col)
total2 = matrix(row,colk) + matrix(row,colk+1) + matrix(row,colk+2) + matrix(row,colk+3)
total3 = matrix(rowk,colk) + matrix(rowk+1,colk+1) + matrix(rowk+2,colk+2) + matrix(rowk+3,colk+3)
total4 = matrix(row+k,colk) + matrix(row+k1,colk+1) + matrix(row+k2,colk+2) + matrix(row+k3,colk+3)
if ((total1=4) or (total2=4) or (total3=4) or (total4=4) or (total1=40) or (total2=40) or (total3=40) or (total4=40)) and flagged=0 then
response.write "player " & (player mod 2) +1 & " wins!"
flagged = 2
%>
Click here for a new game
<%
end if
next
end if
%>
<%
if (player=43 and flagged=0) then
flagged = 2
%>
It's a draw!!
Click here for a new game
<%
end if
player = player + 1
session("row") = row
session("col") = col
session("matrix") = matrix
session("player") = player
%>
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.