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

Username:   Password: 
 RegisterRegister   
 Two Player Connect Four
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xAXISx




PostPosted: Wed Jun 28, 2006 9:21 pm   Post subject: Two Player Connect Four

Took me a day and a half, but I made it. Checking for lines was the hardest part, but I really suggest anyone who's new to program simple games like this. They really help.

Easy to use, just type in your names and click the column to place a piece.

Here goes:
code:


/* Connect 4, by Joey Robert
 * Backup Final Project for ICS 3M1 - May 19 - 20/2006
 */
% VARIABLE DECLARATION
const ROWS := 6
const COLUMNS := 7

var Pieces : array 1 .. ROWS, 1 .. COLUMNS of int
var Names : array 1 .. 2 of string
var Score : array 1 .. 2 of real := init (0, 0)
var counter, Focus, Turn, Value, FontVar : int
var PlayAgain : string (1)

FontVar := Font.New ("sans serif:10:Bold")

% FUNCTION/PROCEDURE DECLARATION

procedure TurnColour (player, rule : int)
    var Holder : string
    if player = 1 then
        drawfillbox (0, maxy - 40, maxx, maxy - 25, red)
    elsif player = 2 then
        drawfillbox (0, maxy - 40, maxx, maxy - 25, blue)
    end if
    if rule = 1 then
        Holder := Names (1) + ": " + realstr (Score (1), 0) + "  " + Names (2) + ": " + realstr (Score (2), 0)
        Draw.Text (Holder, 4, maxy - 37, FontVar, white)
    end if
end TurnColour

/* Format for Position: 21
 * 2 as in Row, 1 as in Column
 */
procedure DisplayScreen
    for i : 1 .. ROWS
        for j : 1 .. COLUMNS
            if Pieces (i, j) = 1 then
                drawfilloval ((50 * j), (50 * i), 20, 20, red)
            elsif Pieces (i, j) = 2 then
                drawfilloval ((50 * j), (50 * i), 20, 20, blue)
            else
                drawfilloval ((50 * j), (50 * i), 20, 20, gray)
            end if

        end for
        Draw.Text (intstr (i), 8, (50 * i) - 4, FontVar, black)
    end for
    for i : 1 .. COLUMNS
        Draw.Text (intstr (i), (50 * i) - 4, 8, FontVar, black)
    end for
end DisplayScreen

procedure RawInsert (player : int)
    var x, y, btnNumber, btnUpDown : int
    Mouse.ButtonWait ("up", x, y, btnNumber, btnUpDown)
    Value := ((x + 25) div 50)
end RawInsert

procedure Insert (player : int)
    put Names (player), ", Select a column to input it in."
    RawInsert (player)
    loop
        if Value > COLUMNS or Value < 1 then
            cls
            TurnColour (player, 1)
            DisplayScreen
            put Names (player), ", Incorrect Selection. Choose Again."
            RawInsert (player)
        elsif Pieces (ROWS, Value) not= 0 then
            cls
            TurnColour (player, 1)
            DisplayScreen
            put Names (player), ", Row is full. Choose Again."
            RawInsert (player)
        else
            exit
        end if
    end loop
    for i : 1 .. ROWS
        if Pieces (i, Value) = 0 then
            Pieces (i, Value) := player
            counter := i
            Music.PlayFile ("check.WAV")
            exit
        end if
    end for

    for decreasing j : ROWS .. 1 + counter
        if player = 1 then
            drawfilloval ((50 * Value), (50 * j), 20, 20, red)
        elsif player = 2 then
            drawfilloval ((50 * Value), (50 * j), 20, 20, blue)
        end if
        delay (50)
        drawfilloval ((50 * Value), (50 * j), 20, 20, gray)
    end for
end Insert

% Rules = 0 to exclude k, 1 to add k, and -1 to minus k.
function RawLineCheck (player, istart, iend, jstart, jend, rule1, rule2 : int) : boolean
    for i : istart .. iend
        for j : jstart .. jend
            counter := 0
            for k : 0 .. 3
                if Pieces (i + (rule1 * k), j + (rule2 * k)) = player then
                    counter := counter + 1
                end if
                if counter = 4 then
                    result true
                end if
            end for
        end for
    end for
    result false
end RawLineCheck

% Result 0 = NO WIN
% Result 1 = Win
% FYI - The reason it minus's 3 for the for loops is because you can't
% connect 4 if there is only 3 spaces available.
function LineCheck (player : int) : int
    if RawLineCheck (player, 1, ROWS, 1, COLUMNS - 3, 0, 1) then % HORIZONTAL
        result 1
    elsif RawLineCheck (player, 1, ROWS - 3, 1, COLUMNS, 1, 0) then % VERTICAL
        result 1
    elsif RawLineCheck (player, 1, ROWS - 3, 1, COLUMNS - 3, 1, 1) then % DIAGONAL /
        result 1
    elsif RawLineCheck (player, 1, ROWS - 3, 4, COLUMNS, 1, -1) then % DIAGONAL \
        result 1
    else
        counter := 0
        for i : 1 .. COLUMNS
            if Pieces (ROWS, i) not= 0 then
                counter := counter + 1
            end if
        end for
        if counter = COLUMNS then
            result 2
        end if
    end if
    result 0
end LineCheck

procedure ClearBoard
    for j : 1 .. ROWS
        for k : 1 .. COLUMNS
            Pieces (j, k) := 0
        end for
    end for
end ClearBoard

% GAME CODE
ClearBoard
setscreen ("graphics:400;400, nobuttonbar")
Turn := 1
TurnColour (1, 0)
DisplayScreen
put "What is Player One's Name?"
get Names (1)
cls
TurnColour (2, 0)
DisplayScreen
put "What is Player Two's Name?"
get Names (2)
loop
    cls
    DisplayScreen
    TurnColour (Turn, 1)
    Insert (Turn)
    cls
    if LineCheck (Turn) = 1 then
        DisplayScreen
        TurnColour (Turn, 1)
        put Names (Turn), " Wins! Play Again? (n to exit)"
        Score (Turn) := Score (Turn) + 1
        delay (100)
    elsif LineCheck (Turn) = 2 then
        DisplayScreen
        TurnColour (Turn, 1)
        put "Draw Game! Play Again? (n to exit)"
        Score (1) := Score (1) + 0.5
        Score (2) := Score (2) + 0.5
        delay (100)
    end if
    if LineCheck (Turn) not= 0 then
        getch (PlayAgain)

        if PlayAgain = 'n' then
            exit
        else
            ClearBoard
        end if
    end if
    if Turn = 1 then
        Turn := 2
    elsif Turn = 2 then
        Turn := 1
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Thu Jun 29, 2006 11:39 am   Post subject: (No subject)

Nice, excellent game, couldnt find any game bugs. good job, I liked how you used loop and end loop now instead of processes, notice how much faster it runs? good job

+bits
Clayton




PostPosted: Thu Jun 29, 2006 11:42 am   Post subject: (No subject)

not bad, i like the little dropping motion thing, pretty cool Very Happy for your LineCheck function, there are only two possible ways to win right? win or lose, so why are you returning an int, why not just return a boolean (win-true lose-false), it just makes it easier to keep track of things in the main, instead of having to memorize "hm what does 1 do again" you just have to know true=win Very Happy other than that not bad
xAXISx




PostPosted: Thu Jun 29, 2006 2:43 pm   Post subject: (No subject)

Thanks, and I made this a little while ago, but I think it's in the case of a draw, I'll look at it a little later.

Edit: Ahh yes, it results 2 incase of a draw.
Clayton




PostPosted: Thu Jun 29, 2006 5:08 pm   Post subject: (No subject)

how can you have a draw? in real life the player checks after each turn (after a reasonable amount of turns for each player) whether theyve won or not right? so the only logical answers to the question is a)yes (true) or b)no (false), so unless there had been a third color added to connect four (and it doesnt look like it from your game) you cant possibly come up with a draw Very Happy
xAXISx




PostPosted: Thu Jun 29, 2006 6:04 pm   Post subject: (No subject)

SuperFreak82 wrote:
how can you have a draw? in real life the player checks after each turn (after a reasonable amount of turns for each player) whether theyve won or not right? so the only logical answers to the question is a)yes (true) or b)no (false), so unless there had been a third color added to connect four (and it doesnt look like it from your game) you cant possibly come up with a draw Very Happy


Well, if all columns are full and no player has won yet, that's when a draw occurs.

Posted Image, might have been reduced in size. Click Image to view fullscreen.



draw.gif
 Description:
 Filesize:  9.02 KB
 Viewed:  2200 Time(s)

draw.gif


TheOneTrueGod




PostPosted: Thu Jun 29, 2006 6:04 pm   Post subject: (No subject)

What if all spaces are covered, but there is no winner? This would result in a draw.
Clayton




PostPosted: Thu Jun 29, 2006 7:02 pm   Post subject: (No subject)

all right you win, i still think you should have a boolean win check, then just have a counter to keep track of how many turns have gone by, if all possible turns have been taken, (when the counter is at 42) and there is no win, then call it a draw, but whatever
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Thu Jun 29, 2006 7:37 pm   Post subject: (No subject)

Just to avoid " = 1, = 2, = 0", why make 1 more variable called counter to keep track of the # of moves? you are using more memory...
xAXISx




PostPosted: Thu Jun 29, 2006 9:58 pm   Post subject: (No subject)

MysticVegeta wrote:
Just to avoid " = 1, = 2, = 0", why make 1 more variable called counter to keep track of the # of moves? you are using more memory...


Okay, better than my current approach, which checks if each column is full after every turn, that's kind of inefficient. Thanks.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: