Search Tools Links Login

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(row-k,col) + matrix(row-k+1,col) + matrix(row-k+2,col) + matrix(row-k+3,col)
total2 = matrix(row,col-k) + matrix(row,col-k+1) + matrix(row,col-k+2) + matrix(row,col-k+3)
total3 = matrix(row-k,col-k) + matrix(row-k+1,col-k+1) + matrix(row-k+2,col-k+2) + matrix(row-k+3,col-k+3)
total4 = matrix(row+k,col-k) + matrix(row+k-1,col-k+1) + matrix(row+k-2,col-k+2) + matrix(row+k-3,col-k+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
%>

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 115 times

Categories

ASP/ HTML

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.