Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Problems with Connect 4 (Grade 10 level)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Rich0591




PostPosted: Fri Nov 17, 2006 6:00 pm   Post subject: Problems with Connect 4 (Grade 10 level)

So Im having some big problems with my connect 4 game. I asked my teacher for help but she is taking way too long and its been over due. Could someone nice help me: make it so the game lets players take turns, reognize that 4 in a row means win (not diagonal, then my teacher will know I asked for help) and maybe make the circles fall from the top. I know its alot but I would really appreciate it if comeone could help me out. Also, if someone was to do it, could you do it at a simpler level? Thanks! Heres the program:

code:
%Connect 4%
%Large Program%

setscreen ("graphics:vga")
procedure gui
    drawfillbox (10, 10, maxx - 10, maxy - 10, 14)
    drawbox (10, 10, maxx - 10, maxy - 10, 16)
    %draws game outline
    drawline (98, 10, 98, maxy - 10, 16)
    drawline (186, 10, 186, maxy - 10, 16)
    drawline (274, 10, 274, maxy - 10, 16)
    drawline (362, 10, 362, maxy - 10, 16)
    drawline (450, 10, 450, maxy - 10, 16)
    drawline (538, 10, 538, maxy - 10, 16)
    %draws the 7 columns required for game
    drawfilloval (55, 39, 25, 25, white)
    drawfilloval (55, 94, 25, 25, white)
    drawfilloval (55, 150, 25, 25, white)
    drawfilloval (55, 205, 25, 25, white)
    drawfilloval (55, 260, 25, 25, white)
    drawfilloval (55, 315, 25, 25, white)
    %fills the first column with white circles
    drawoval (55, 39, 25, 25, 16)
    drawoval (55, 94, 25, 25, 16)
    drawoval (55, 150, 25, 25, 16)
    drawoval (55, 205, 25, 25, 16)
    drawoval (55, 260, 25, 25, 16)
    drawoval (55, 315, 25, 25, 16)
    %outlines the circles in black in first column
    drawfilloval (145, 39, 25, 25, white)
    drawfilloval (145, 94, 25, 25, white)
    drawfilloval (145, 150, 25, 25, white)
    drawfilloval (145, 205, 25, 25, white)
    drawfilloval (145, 260, 25, 25, white)
    drawfilloval (145, 315, 25, 25, white)
    %fills second column with white circles
    drawoval (145, 39, 25, 25, 16)
    drawoval (145, 94, 25, 25, 16)
    drawoval (145, 150, 25, 25, 16)
    drawoval (145, 205, 25, 25, 16)
    drawoval (145, 260, 25, 25, 16)
    drawoval (145, 315, 25, 25, 16)
    %outlines the circles in black in second column
    drawfilloval (235, 39, 25, 25, white)
    drawfilloval (235, 94, 25, 25, white)
    drawfilloval (235, 150, 25, 25, white)
    drawfilloval (235, 205, 25, 25, white)
    drawfilloval (235, 260, 25, 25, white)
    drawfilloval (235, 315, 25, 25, white)
    %fills the third column with white circles
    drawoval (235, 39, 25, 25, 16)
    drawoval (235, 94, 25, 25, 16)
    drawoval (235, 150, 25, 25, 16)
    drawoval (235, 205, 25, 25, 16)
    drawoval (235, 260, 25, 25, 16)
    drawoval (235, 315, 25, 25, 16)
    %outlines the circles in black in third column
    drawfilloval (320, 39, 25, 25, white)
    drawfilloval (320, 94, 25, 25, white)
    drawfilloval (320, 150, 25, 25, white)
    drawfilloval (320, 205, 25, 25, white)
    drawfilloval (320, 260, 25, 25, white)
    drawfilloval (320, 315, 25, 25, white)
    %fills the fourth column with white circles
    drawoval (320, 39, 25, 25, 16)
    drawoval (320, 94, 25, 25, 16)
    drawoval (320, 150, 25, 25, 16)
    drawoval (320, 205, 25, 25, 16)
    drawoval (320, 260, 25, 25, 16)
    drawoval (320, 315, 25, 25, 16)
    %outlines the circles in black in fourth column
    drawfilloval (410, 39, 25, 25, white)
    drawfilloval (410, 94, 25, 25, white)
    drawfilloval (410, 150, 25, 25, white)
    drawfilloval (410, 205, 25, 25, white)
    drawfilloval (410, 260, 25, 25, white)
    drawfilloval (410, 315, 25, 25, white)
    %fills the fifth column with white circles
    drawoval (410, 39, 25, 25, 16)
    drawoval (410, 94, 25, 25, 16)
    drawoval (410, 150, 25, 25, 16)
    drawoval (410, 205, 25, 25, 16)
    drawoval (410, 260, 25, 25, 16)
    drawoval (410, 315, 25, 25, 16)
    %outlines the circles in black in fifth column
    drawfilloval (500, 39, 25, 25, white)
    drawfilloval (500, 94, 25, 25, white)
    drawfilloval (500, 150, 25, 25, white)
    drawfilloval (500, 205, 25, 25, white)
    drawfilloval (500, 260, 25, 25, white)
    drawfilloval (500, 315, 25, 25, white)
    %fills the sixth column with white circles%
    drawoval (500, 39, 25, 25, 16)
    drawoval (500, 94, 25, 25, 16)
    drawoval (500, 150, 25, 25, 16)
    drawoval (500, 205, 25, 25, 16)
    drawoval (500, 260, 25, 25, 16)
    drawoval (500, 315, 25, 25, 16)
    %outlines the circles in black in sixth column%
    drawfilloval (590, 39, 25, 25, white)
    drawfilloval (590, 94, 25, 25, white)
    drawfilloval (590, 150, 25, 25, white)
    drawfilloval (590, 205, 25, 25, white)
    drawfilloval (590, 260, 25, 25, white)
    drawfilloval (590, 315, 25, 25, white)
    %fills the seventh column with white circles%
    drawoval (590, 39, 25, 25, 16)
    drawoval (590, 94, 25, 25, 16)
    drawoval (590, 150, 25, 25, 16)
    drawoval (590, 205, 25, 25, 16)
    drawoval (590, 260, 25, 25, 16)
    drawoval (590, 315, 25, 25, 16)
    %outlines the circles in black in seventh column%
end gui
var x, y, button : int
var player : int
gui
 player := 12
loop
   
    mousewhere (x, y, button)
    if button = 1 and 30 <= x and x <= 80 and 39 - 25 <= y and y <= 39 + 25 then
        drawfilloval (55, 39, 25, 25, player)
    elsif button = 1 and 30 <= x and x <= 80 and 94 - 25 <= y and y <= 94 + 25 then
        drawfilloval (55, 94, 25, 25, player)
    elsif button = 1 and 30 <= x and x <= 80 and 150 - 25 <= y and y <= 150 + 25 then
        drawfilloval (55, 150, 25, 25, player)
    elsif button = 1 and 30 <= x and x <= 80 and 205 - 25 <= y and y <= 205 + 25 then
        drawfilloval (55, 205, 25, 25, player)
    elsif button = 1 and 30 <= x and x <= 80 and 260 - 25 <= y and y <= 260 + 25 then
        drawfilloval (55, 260, 25, 25, player)
    elsif button = 1 and 30 <= x and x <= 80 and 315 - 25 <= y and y <= 315 + 25 then
        drawfilloval (55, 315, 25, 25, player)
        %fills the circles in first column with colour; either red or blue%
    elsif button = 1 and 145 - 25 <= x and x <= 145 + 25 and 39 - 25 <= y and y <= 39 + 25 then
        drawfilloval (145, 39, 25, 25, player)
    elsif button = 1 and 145 - 25 <= x and x <= 145 + 25 and 94 - 25 <= y and y <= 94 + 25 then
        drawfilloval (145, 94, 25, 25, player)
    elsif button = 1 and 145 - 25 <= x and x <= 145 + 25 and 150 - 25 <= y and y <= 150 + 25 then
        drawfilloval (145, 150, 25, 25, player)
    elsif button = 1 and 145 - 25 <= x and x <= 145 + 25 and 205 - 25 <= y and y <= 205 + 25 then
        drawfilloval (145, 205, 25, 25, player)
    elsif button = 1 and 145 - 25 <= x and x <= 145 + 25 and 260 - 25 <= y and y <= 260 + 25 then
        drawfilloval (145, 260, 25, 25, player)
    elsif button = 1 and 145 - 25 <= x and x <= 145 + 25 and 315 - 25 <= y and y <= 315 + 25 then
        drawfilloval (145, 315, 25, 25, player)
        %fills the circles in second column with colour; either red or blue%
    elsif button = 1 and 235 - 25 <= x and x <= 235 + 25 and 39 - 25 <= y and y <= 39 + 25 then
        drawfilloval (235, 39, 25, 25, player)
    elsif button = 1 and 235 - 25 <= x and x <= 235 + 25 and 94 - 25 <= y and y <= 94 + 25 then
        drawfilloval (235, 94, 25, 25, player)
    elsif button = 1 and 235 - 25 <= x and x <= 235 + 25 and 150 - 25 <= y and y <= 150 + 25 then
        drawfilloval (235, 150, 25, 25, player)
    elsif button = 1 and 235 - 25 <= x and x <= 235 + 25 and 205 - 25 <= y and y <= 205 + 25 then
        drawfilloval (235, 205, 25, 25, player)
    elsif button = 1 and 235 - 25 <= x and x <= 235 + 25 and 260 - 25 <= y and y <= 260 + 25 then
        drawfilloval (235, 260, 25, 25, player)
    elsif button = 1 and 235 - 25 <= x and x <= 235 + 25 and 315 - 25 <= y and y <= 315 + 25 then
        drawfilloval (235, 315, 25, 25, player)
        %fills the circles in third column with colour; either red or blue%
    elsif button = 1 and 320 - 25 <= x and x <= 320 + 25 and 39 - 25 <= y and y <= 39 + 25 then
        drawfilloval (320, 39, 25, 25, player)
    elsif button = 1 and 320 - 25 <= x and x <= 320 + 25 and 94 - 25 <= y and y <= 94 + 25 then
        drawfilloval (320, 94, 25, 25, player)
    elsif button = 1 and 320 - 25 <= x and x <= 320 + 25 and 150 - 25 <= y and y <= 150 + 25 then
        drawfilloval (320, 150, 25, 25, player)
    elsif button = 1 and 320 - 25 <= x and x <= 320 + 25 and 205 - 25 <= y and y <= 205 + 25 then
        drawfilloval (320, 205, 25, 25, player)
    elsif button = 1 and 320 - 25 <= x and x <= 320 + 25 and 260 - 25 <= y and y <= 260 + 25 then
        drawfilloval (320, 260, 25, 25, player)
    elsif button = 1 and 320 - 25 <= x and x <= 320 + 25 and 315 - 25 <= y and y <= 315 + 25 then
        drawfilloval (320, 315, 25, 25, player)
        %fills the circles in fourth column with colour; either red or blue%
    elsif button = 1 and 410 - 25 <= x and x <= 410 + 25 and 39 - 25 <= y and y <= 39 + 25 then
        drawfilloval (410, 39, 25, 25, player)
    elsif button = 1 and 410 - 25 <= x and x <= 410 + 25 and 94 - 25 <= y and y <= 94 + 25 then
        drawfilloval (410, 94, 25, 25, player)
    elsif button = 1 and 410 - 25 <= x and x <= 410 + 25 and 150 - 25 <= y and y <= 150 + 25 then
        drawfilloval (410, 150, 25, 25, player)
    elsif button = 1 and 410 - 25 <= x and x <= 410 + 25 and 205 - 25 <= y and y <= 205 + 25 then
        drawfilloval (410, 205, 25, 25, player)
    elsif button = 1 and 410 - 25 <= x and x <= 410 + 25 and 260 - 25 <= y and y <= 260 + 25 then
        drawfilloval (410, 260, 25, 25, player)
    elsif button = 1 and 410 - 25 <= x and x <= 410 + 25 and 315 - 25 <= y and y <= 315 + 25 then
        drawfilloval (410, 315, 25, 25, player)
        %fills the circles in fifth column with colour; either red or blue%
    elsif button = 1 and 500 - 25 <= x and x <= 500 + 25 and 39 - 25 <= y and y <= 39 + 25 then
        drawfilloval (500, 39, 25, 25, player)
    elsif button = 1 and 500 - 25 <= x and x <= 500 + 25 and 94 - 25 <= y and y <= 94 + 25 then
        drawfilloval (500, 94, 25, 25, player)
    elsif button = 1 and 500 - 25 <= x and x <= 500 + 25 and 150 - 25 <= y and y <= 150 + 25 then
        drawfilloval (500, 150, 25, 25, player)
    elsif button = 1 and 500 - 25 <= x and x <= 500 + 25 and 205 - 25 <= y and y <= 205 + 25 then
        drawfilloval (500, 205, 25, 25, player)
    elsif button = 1 and 500 - 25 <= x and x <= 500 + 25 and 260 - 25 <= y and y <= 260 + 25 then
        drawfilloval (500, 260, 25, 25, player)
    elsif button = 1 and 500 - 25 <= x and x <= 500 + 25 and 315 - 25 <= y and y <= 315 + 25 then
        drawfilloval (500, 315, 25, 25, player)
        %fills sixth colum with colour; either red or blue%
    elsif button = 1 and 590 - 25 <= x and x <= 590 + 25 and 39 - 25 <= y and y <= 39 + 25 then
        drawfilloval (590, 39, 25, 25, player)
    elsif button = 1 and 590 - 25 <= x and x <= 590 + 25 and 94 - 25 <= y and y <= 94 + 25 then
        drawfilloval (590, 94, 25, 25, player)
    elsif button = 1 and 590 - 25 <= x and x <= 590 + 25 and 150 - 25 <= y and y <= 150 + 25 then
        drawfilloval (590, 150, 25, 25, player)
    elsif button = 1 and 590 - 25 <= x and x <= 590 + 25 and 205 - 25 <= y and y <= 205 + 25 then
        drawfilloval (590, 205, 25, 25, player)
    elsif button = 1 and 590 - 25 <= x and x <= 590 + 25 and 260 - 25 <= y and y <= 260 + 25 then
        drawfilloval (590, 260, 25, 25, player)
    elsif button = 1 and 590 - 25 <= x and x <= 590 + 25 and 315 - 25 <= y and y <= 315 + 25 then
        drawfilloval (590, 315, 25, 25, player)
        %fills seventh column with colour; either red or blue%


    end if
   
     exit when button = 1 and 1000 <= x and x <=900 and 150 <=y and y<= 250
    if player = 12 then
        player := 16
    else
        player := 12
    end if
   
end loop

gui
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Fri Nov 17, 2006 6:10 pm   Post subject: (No subject)

errrr, no we can't do it for you, however we can help you with it, ask some concise, detailed questions about problems you are having, and I'm sure someone would be glad to help.

Take a gander through the Turing Walkthrough, and I'm sure, if you take the time, that you will find some tips to help you.
Rich0591




PostPosted: Fri Nov 17, 2006 6:15 pm   Post subject: (No subject)

Ok well heres the thing. My teacher edited it, and we both dont understand why each circle can turn BOTH red and black. This is what i first need to understand, maybe someone could point out what we did wrong?
wtd




PostPosted: Fri Nov 17, 2006 6:21 pm   Post subject: (No subject)

Code tags would help, as well.
Rich0591




PostPosted: Fri Nov 17, 2006 6:50 pm   Post subject: (No subject)

This is what I have to determine when some one wins.

code:
var scorered : real := 0
procedure swap
    var temp : real
    var newscore : real
    temp := (scorered)
    scorered := temp
    if scorered = 4 then
    end if
end swap
var scoreblue : real := 0
procedure swap2
    var temp2 : real
    var newscore2 : real
    temp2 := (scoreblue)
    scoreblue := temp2
    if scoreblue = 4 then
    end if
end swap2


On my mainline program, I added something like this
code:
elsif button = 1 and 145 - 25 <= x and x <= 145 + 25 and 260 - 25 <= y and y <= 260 + 25 then
            drawfilloval (145, 260, 25, 25, 12)
            scorered := +1

This determines the score for one person. But I still can't figure out how to take turns and make it so a person can only win if it is 4 in a ROW.[/code]
Wolf_Destiny




PostPosted: Fri Nov 17, 2006 10:52 pm   Post subject: (No subject)

Fairly quick thought, your life is going to be easier if you use variables (even easier with arrays [dare I say two dimensional arrays? {dare I go too far with types and records? No} No]No) Anyways...If you had variables for each one of your circles you could store whether they're red, black, or empty. Then in your "gui" procedure you could just draw each circle according to their stored colour. Then to check for a win, just see if c1 c2 c3 c4 all = red or black or if c5 c6 c7 c8 all equal red or black ect.

Also try to use less "linear" programming you already have a couple of procedures, but gui is hardly ever called upon. You should be using it to draw any/all changes made to the game board. http://www.compsci.ca/v2/viewtopic.php?t=407

Hope any of that actually helped instead of confusing you!
~Wolf_Destiny
Rich0591




PostPosted: Sat Nov 18, 2006 7:00 pm   Post subject: (No subject)

code:
var circle:array 1..42 of string
I just got around to my program now could and is this a way to declare a variable for each one of my circles?
Mr. T




PostPosted: Sat Nov 18, 2006 8:01 pm   Post subject: Alex's Opinion

I haven't taken a good look at ur code, but from what I see this has been terribly coded. What's more surprising is that if your teacher saw you code, why did not suggest obvious ways that it could be fixed? Anyways, I'm not trying to put down you or your teacher, as I plan on telling you how to go about fixing this program. First of all, the lines can be reduced drastically if you STOP HARD CODING. You can easily draw everything you need to draw in significantly less lines using variables --> arrays, and clever use of for loops. All that is required is simple math. I don't have the time at the moment, but I'm sure someone else on the forum will gladly show you an example of how to reduce your code using a non-hard coding technique. Second, all those if statements in may you main loop can be reduced significantly. If you make a grid using a 2D array, itll become much easier to determine which blocks are filled in, rather than checking each one individually. The way you did it is extraordinarily tidious, and very inflexible. So in summation, check the Turing Walkthrough for more information on variables, arrays, 2D arrays, (and some of the basics you may want to hone up on are your standard procedures, functions, and maybe even records).
Sponsor
Sponsor
Sponsor
sponsor
Rich0591




PostPosted: Sun Nov 19, 2006 12:13 pm   Post subject: (No subject)

Ok so I read about the 2D arrays ya I dont pay attention alot which may be why my program sucks anyway

code:
var grid : array 1..6, 1..7 of string
for row : 1..5
    for column : 1..10
        put grid (row,column)
    end for
    put ""
end for


I stuck this in my program. Now the colour don't work, which I think is a good thing cus now I need to store the colours in my array right? Thing is I dont know how lol so could somone like copy and paste something from my previous program so I know what I would know what to do? Again thanks guys!
ZeroPaladn




PostPosted: Mon Nov 20, 2006 1:13 pm   Post subject: (No subject)

First things first, remember that you need two diferent colours to represent both players, and maybe another colour to represent an empty space. So, when you think about it, its not that hard, and in fact, you have all the code you need right on your last post. (just fix the differneces between your for statements and your array declarations, because they should match) The idea is to have differnet declarations for each chunk of the array for each colour, not just one for both, cause the program can't tell the difference, cause there is no difference.)

Now we all know that just outputting the string on the screen isnt going to work when you do graphics with your program, so you'll have to use an if statement to determine the colour using the letter youve used to declare the array in that position. This may be geting too far aread of what you wanted, but its something to remember for later, if you do incorperate graphics into your program.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: