% CONNECT 4 by Chris
% stupid mistake pointed out by Paul Bian... thanks x2
setscreen ("graphics:800;600,nobuttonbar")
var col, stop, tally : int
var act, colchoice : string (1) := ""
var actcolour, actname, checkcolour, winner : string := ""
var board : array 1 .. 8, 1 .. 8 of string
var height : array 1 .. 8 of int
var names : array 1 .. 2 of string
var wins : array 1 .. 2 of int := init (0, 0)
var winlocc, winloch : array 1 .. 4 of int
proc colget
loop
locate (33, 1)
put actname, "(", actcolour, ")", " Enter col choice" ..
getch (colchoice)
locate (34, 1)
put ""
if colchoice = "1" or colchoice = "2" or colchoice = "3" or colchoice = "4" or colchoice = "5" or colchoice = "6" or colchoice = "7" or colchoice = "8" then
exit
else
locate (34, 1)
put "numbers 1-8 only"
end if
end loop
col := strint (colchoice)
end colget
proc drawboard
for h : 1 .. 8
for c : 1 .. 8
if board (h, c) not= " " then
if board (h, c) = "RED" then
Draw.FillOval (130 + c * 60, 80 + h * 60, 25, 25, red)
elsif board (h, c) = "GREEN" then
Draw.FillOval (130 + c * 60, 80 + h * 60, 25, 25, green)
end if
end if
end for
end for
end drawboard
proc switch
if actcolour = "RED" then
actcolour := "GREEN"
actname := names (2)
else
actcolour := "RED"
actname := names (1)
end if
end switch
proc wincheck
loop
for a : 1 .. 8
for b : 1 .. 5
if board (a, b) = checkcolour and
board (a, b + 1) = checkcolour and
board (a, b + 2) = checkcolour and
board (a, b + 3) = checkcolour then
winner := checkcolour
for c : 1 .. 4
winlocc (c) := a
end for
winloch (1) := b
winloch (2) := b + 1
winloch (3) := b + 2
winloch (4) := b + 3
exit
end if
end for
end for
for a : 1 .. 5
for b : 1 .. 8
if board (a, b) = checkcolour and
board (a + 1, b) = checkcolour and
board (a + 2, b) = checkcolour and
board (a + 3, b) = checkcolour then
winner := checkcolour
for c : 1 .. 4
winloch (c) := b
end for
winlocc (1) := a
winlocc (2) := a + 1
winlocc (3) := a + 2
winlocc (4) := a + 3
exit
end if
end for
end for
for a : 1 .. 5
for b : 1 .. 5
if board (a, b) = checkcolour and
board (a + 1, b + 1) = checkcolour and
board (a + 2, b + 2) = checkcolour and
board (a + 3, b + 3) = checkcolour then
winner := checkcolour
winloch (1) := a
winloch (2) := a + 1
winloch (3) := a + 2
winloch (4) := a + 3
winlocc (1) := b
winlocc (2) := b + 1
winlocc (3) := b + 2
winlocc (4) := b + 3
exit
end if
end for
end for
for a : 1 .. 5
for decreasing b : 8 .. 4
if board (a, b) = checkcolour and
board (a + 1, b - 1) = checkcolour and
board (a + 2, b - 2) = checkcolour and
board (a + 3, b - 3) = checkcolour then
winner := checkcolour
winloch (1) := b
winloch (2) := b - 1
winloch (3) := b - 2
winloch (4) := b - 3
winlocc (1) := a
winlocc (2) := a + 1
winlocc (3) := a + 2
winlocc (4) := a + 3
exit
end if
end for
end for
exit
end loop
end wincheck
process flash
loop
Draw.FillOval (130 + winloch (1) * 60, 80 + winlocc (1) * 60, 25, 25, 9)
Draw.FillOval (130 + winloch (2) * 60, 80 + winlocc (2) * 60, 25, 25, 9)
Draw.FillOval (130 + winloch (3) * 60, 80 + winlocc (3) * 60, 25, 25, 9)
Draw.FillOval (130 + winloch (4) * 60, 80 + winlocc (4) * 60, 25, 25, 9)
delay (100)
Draw.FillOval (130 + winloch (1) * 60, 80 + winlocc (1) * 60, 25, 25, 42)
Draw.FillOval (130 + winloch (2) * 60, 80 + winlocc (2) * 60, 25, 25, 42)
Draw.FillOval (130 + winloch (3) * 60, 80 + winlocc (3) * 60, 25, 25, 42)
Draw.FillOval (130 + winloch (4) * 60, 80 + winlocc (4) * 60, 25, 25, 42)
delay (100)
exit when act = "y" or act = "n"
end loop
end flash
%process xMusic
% Music.PlayFile ("lc.mp3")
%end xMusic
loop
Music.PlayFileStop
%%%startup%%%
col := 0
stop := 0
tally := 0
winner := ""
for a : 1 .. 8
height (a) := 0
for b : 1 .. 8
board (a, b) := " "
end for
end for
for a : 1 .. 4
winlocc (a) := 0
winloch (a) := 0
end for
%%%get names%%%
if act not= "y" then
for a : 1 .. 2
put "ENTER NAME ", a
get names (a)
end for
end if
cls
%%%end names
act := ""
actcolour := "RED"
actname := names (1)
%%%Draws the grid of the board%%%
Draw.Line (160, 590, 160, 110, black)
Draw.Line (640, 590, 640, 110, black)
Draw.Line (160, 590, 640, 590, black)
Draw.Line (160, 110, 640, 110, black)
for a : 1 .. 7
Draw.Line (a * 60 + 160, 110, a * 60 + 160, 590, black)
Draw.Line (160, a * 60 + 110, 640, a * 60 + 110, black)
end for
locate (32, 1)
put " " : 23, "1" : 8, "2" : 7, "3" : 8, "4" : 7, "5" : 8, "6" : 7, "7" : 8, "8"
%%%end grid%%%
%%%end startup%%%
loop
loop
colget
if height (col) = 8 then
locate (34, 1)
put "That col is full."
else
height (col) := height (col) + 1
exit
end if
end loop
board (height (col), col) := actcolour
drawboard
checkcolour := "RED"
wincheck
checkcolour := "GREEN"
wincheck
if winner = "RED" then
locate (34, 1)
put names (1), " WINS!"
wins (1) := wins (1) + 1
exit
elsif winner = "GREEN" then
locate (34, 1)
put names (2), " WINS!"
wins (2) := wins (2) + 1
exit
end if
switch
end loop
locate (34, 1)
for a : 1 .. 2
put names (a), "'s wins: ", wins (a)
end for
if act not= "y" and winlocc (1) not= 0 then
fork flash
fork xMusic
end if
locate (33, 1)
put "WOULD YOU LIKE TO PLAY AGAIN?(y/n)" ..
loop
getch (act)
if act = "n" or act = "y" then
exit
else
locate (33, 1)
put "enter y or n, lower case " ..
end if
end loop
exit when act = "n"
delay (500)
end loop |