Posted: Tue Mar 09, 2004 8:24 pm Post subject: my connect 4
This is my connect 4 project, which is finished, except that I wanted to allow the players to press y to begin another game without haveing to press run again for 3 reasons:
1. so they dont have to press run again
2. so they dont have to enter their names again
3. so that the program can keep a tally of how many games each player has won.
the problem is that for some reason at the beginning of the second game the program is drawing a circle when it shouldn't be.
code:
% 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
winlocc (1) := a
winlocc (2) := a + 1
winlocc (3) := a + 2
winlocc (4) := a + 3
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 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 (35, 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
Sponsor Sponsor
jonos
Posted: Tue Mar 09, 2004 8:26 pm Post subject: (No subject)
could you please inlcude the whole code, even the variables or else it will not work. you have 21 errors or seomting because of that.
SuperGenius
Posted: Tue Mar 09, 2004 8:29 pm Post subject: (No subject)
oops... I thought I had copied everything but i guess not... ill just edit the first post instead of taking up more space.
Paul
Posted: Tue Mar 09, 2004 8:30 pm Post subject: (No subject)
Yes, do that, AND all those if statements are unneeded, like below:
code:
if board (a, b) = checkcolour then
if board (a, b + 1) = checkcolour then
if board (a, b + 2) = checkcolour then
if board (a, b + 3) = checkcolour then
winner := checkcolour
exit
end if
end if
end if
end if
can be
code:
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
exit
end if
correct me if Im wrong.
SuperGenius
Posted: Tue Mar 09, 2004 8:37 pm Post subject: (No subject)
i think you're correct so i made the fix you suggested, and the program still runs the same incorrect way it did before.
Paul
Posted: Tue Mar 09, 2004 8:51 pm Post subject: (No subject)
Well, you I think you should be using recursion to do this (though I have no idea how to use recursion), but I think I figured out the problem, I think the program isn't even putting the "RED" or "GREEN" into the arrays, I added
code:
cls
put board(8,1)
delay (5000)
right after the wincheck in you main loop, and nothing shows up, when you have a red piece in (8,1)
SuperGenius
Posted: Tue Mar 09, 2004 8:58 pm Post subject: (No subject)
Paul Bian wrote:
code:
cls
put board(8,1)
delay (5000)
this just screws it up, becuase the array holds blank spaces at the start, and as you play the array is changed to either "RED" of "GREEN", and there is a procedure that will update the board like so...
code:
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
trying to put out board(8,1) wont work because i used a little formula to find the right place and then draw a circle of either colour in it based on the contents of the 'board' array.
Paul
Posted: Tue Mar 09, 2004 9:02 pm Post subject: (No subject)
oh I see, that would complicate things cause I thought it was like this
your program checks the text inside each variable in the array? so if I played on col 1, then the variable (8,1) would change from " " to RED, wouldn't it? so then you check every variable in the 2D array, and when it gets to 8,1 it should find RED right? but it doesn't, though my way of checking might be wrong.
Sponsor Sponsor
SuperGenius
Posted: Tue Mar 09, 2004 9:07 pm Post subject: (No subject)
Paul Bian wrote:
so if I played on col 1, then the variable (8,1) would change from " " to RED, wouldn't it?
i think (1,1) would change for the first play in col 1, but im not sure... i tried your suggestion with (1,1) instead and it returns RED... so the problem is not in the contents of the array for sure... becuase it it wasnt updating the array with "RED" and "GREEN" then it wouldnt be drawing the circles either
Paul
Posted: Tue Mar 09, 2004 9:21 pm Post subject: (No subject)
OMFG, it freaking works, just change the checking code a bit, OMGI
I cant believe this
code:
if winner = "RED" then
locate (33, 1)
put "RED WINS"
delay (5000) %<----ADD THIS FREAKING PIECE OF CODE!!!
elsif winner = "GREEN"
then
even if it won, which it did for me, we wouldn't see it, cause it restarts so fast!!!
god, I put a delay on it and it worked, it worked in the first place, just check it with this:
because it changes from 1, 1 to 2, 1 and so on
code:
if board (a, b) = checkcolour and a =4 and
board (a - 1, b) = checkcolour and
board (a - 2, b) = checkcolour and
board (a - 3, b) = checkcolour then
winner := checkcolour
by putting 4 pieces vertically into the first column!!!
all this discussion has been totally useless
here just change your checking coding to this:
code:
for a : 1 .. 4
for b : 1 .. 4
if board (a, b) = checkcolour and b<=4and
board (a, b + 1) = checkcolour and
board (a, b + 2) = checkcolour and
board (a, b + 3) = checkcolour then
winner := checkcolour
exit
end if
if board (a, b) = checkcolour and a >=4 and
board (a - 1, b) = checkcolour and
board (a - 2, b) = checkcolour and
board (a - 3, b) = checkcolour then
winner := checkcolour
exit
end if
end for
end for
and it works perfectly... just not for diagonally.
SuperGenius
Posted: Tue Mar 09, 2004 9:27 pm Post subject: (No subject)
holy shit.... the little things like this have a way of biting me in the ass... but thank you for pointing it out to me before I went crazy.
thanks again
white_dragon
Posted: Wed Mar 10, 2004 5:55 pm Post subject: (No subject)
haha lol. paul's the real genius j/k. yeah those tings u gotta correct when u go to higher grades in programmin
SuperGenius
Posted: Wed Mar 10, 2004 7:47 pm Post subject: (No subject)
you better tell me why you are called white dragon or i'll have to find it out from someone else
SuperGenius
Posted: Thu Mar 11, 2004 4:55 pm Post subject: (No subject)
hey guys.... ive got another little problem... if anyone would like to help me with it that would be awesome. ive updated my 1st post with the new source to conserve space.
Paul
Posted: Thu Mar 11, 2004 5:02 pm Post subject: (No subject)
First: Press alt+c before you post your code
Second: You didn't state your problem.
Third: I think I know what your problem is. When a person wins, and you want to play again, it automatically screws up. That is because you didn't reset the variables. And the 2 D array is still filled, and so it wins automattically when you restart. Empty your variables with something like this:
code:
for a: 1..8
for b: 1..8
board (a, b):=""
end for
end for