Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Help!! trouble implementing "same game"

  1. #1

    Help!! trouble implementing "same game"

    Hello all

    Im back again. Im starting the second part of my course, this time i need to implement a version of the "same game" on delphi. The first task reads;

    Define an enumerated type TColours, with four possible values: Blue, Red, Green, Black.
    Declare two 1 dimensional arrays of type TColours with 4 and 12 elements respectively
    Write a procedure SetColour that will randomly set the elements of these arrays to: Blue, Red or Green.
    Write a procedure RemovePairs that will accept one of these arrays and
    replace adjacent elements of the same colour by the Black element.
    Note procedures mean user defined procedures not event handlers

    I believe to define the arrays and types i need to do something like this:

    type TColours = (Blue, Red, Green, Black);

    type TColoursA = array [0..4] of TColours ?
    type TColoursA = array [0..12] of TColours ?

    I cant figure out how to start this. Any pointers will be gratefully appreciated. A version of the so called same game can be found here
    http://javascript.internet.com/games/same-game.html

    cant help but be a delphi newb grrrrr thanks for your time guys

  2. #2

    Help!! trouble implementing "same game"

    You have the enum right - it's declaring a new type with four possible values. As a side note, it's usual to add a 2 or 3 letter prefix before each value of the type to avoid name clashes - e.g. TColor -> "clBlack", "clWhite", (cl there) or TBorderStyle -> "bsSingle", "bsDialog" (bs) In this case, do not bother about it. There's no point trying to be smart if you're on a course, just do as you're told by the teacher . Your enum'd type is entirely correct.

    You've gone slightly off track with the arrays, though. Consider the three main sections when declaring things: var, const and type. You want to declare new types in the "type" section - this is what you have done with declaration of TColours. Your next task is to declare the arrays of a certain type. For this, you want to declare variables of an existing type. To do this, you use the var section.

    type section = declare new types here
    var section = declare variables *of a pre-declared type*
    const section = constant values, cannot be changed

    Therefore, you want to do this:

    [pascal][background=#FFFFFF][comment=#0000FF][normal=#000000]
    [number=#C00000][reserved=#000000][string=#00C000]// step one: declare a new enum'd type
    type
    TColours = (Blue, Red, Green, Black);

    // step two: declare arrays *based on the above type*
    var
    first_stuff: array[0..3] of TColours;
    second_stuff: array[0..11] of TColours;[/pascal]

    Now, notice the array declarations - we've used the standard "variable_name: variable_type" style that you'd use elsewhere (e.g. "some_var: Integer"). The type, in this case, is an array of your TColours.

    BEWARE when declaring zero-based arrays! Your example was slightly off - 0 is a number too! . Remember that the array bounds are inclusive of the first and last number. Your array[0..4] declared the following:

    array[0] <-- notice this!
    array[1]
    array[2]
    array[3]
    array[4]

    That's five elements, not four! You would want to declare your array either as array[0..3] or array[1..4]. This is a nasty trap - if you're using zero-based arrays, it's always [0..(number of elements - 1)].

    Incidentally, I have an article about declaring and using new types. You might be interested: http://www.alistairkeys.co.uk/newtypes.shtml.

    That's the first few parts explained. I'm willing to explain the rest too, but I'll let you have a crack at it and you can report back with your efforts first .
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  3. #3

    Help!! trouble implementing "same game"

    Hi there!!
    sorry for gettin back to you so late... had a pile of stuff due in
    Right Iv had a little bash at writing those 2 procedures. They dont seem to compile properly
    as i dont seem to grasp them that well...yet


    type TColours = (Blue, Red, Green, Black);
    var A: array[0..3] of TColours;
    B: array[0..11] of TColours;


    procedure SetColour;
    {will this randomly set the elements to different different colours or just set a whole array to a random colour. Also am i using the logic notation correctly?}
    begin
    repeat
    randomize;
    random(A);
    random(B);

    until
    B = not(black) and A = not(black);

    end;

    procedure RemovePairs(var C: array of TColours);
    {am i declarin the param correctly? basically i wish to write removepairs(X) where X is the array to be checked}
    var i : integer;
    begin

    for i = 1 to high(C) do
    begin
    if C[i] = C[i-1] then
    C[i-1] := black
    if not C[i] = C[i+1] then
    C[i] := black
    end;
    end;

    {Does high return the last element of the array? and secondly does this make any sense? im trying to get it to go through each element and if 2 next to each other are the same colour then they both turn black}


    i really appreciate your time and detailed replys almonster your a star!
    (in our so called practical sessions there arn't enough helpers to go around im lucky if i get help for one particular thing so thanks again all of you lot)

  4. #4

    Help!! trouble implementing "same game"

    Quote Originally Posted by yawbird
    Hi there!!
    sorry for gettin back to you so late... had a pile of stuff due in
    Right Iv had a little bash at writing those 2 procedures. They dont seem to compile properly
    as i dont seem to grasp them that well...yet


    type TColours = (Blue, Red, Green, Black);
    var A: array[0..3] of TColours;
    B: array[0..11] of TColours;


    procedure SetColour;
    {will this randomly set the elements to different different colours or just set a whole array to a random colour. Also am i using the logic notation correctly?}
    begin
    repeat
    randomize;
    random(A);
    random(B);

    until
    B = not(black) and A = not(black);

    end;
    [pascal]

    type TColours = (Blue, Red, Green, Black);
    var A: array[0..3] of TColours;
    B: array[0..11] of TColours;

    procedure SetColour;
    { This sets all the elements of A and B to a random value, except black }
    var
    i: Integer;
    begin
    for i := 0 to 3 do // Iterate though elements of array A
    A[i] := TColours(Random(3)); // Returns random integer from 0..2 (black is 3, so is excluded)

    for i := 0 to 11 do
    B[i] := TColours(Random(3));
    end;
    [/pascal]

    Note, you should only call Randomize once, at the beginning of your application.

    procedure RemovePairs(var C: array of TColours);
    {am i declarin the param correctly? basically i wish to write removepairs(X) where X is the array to be checked}
    var i : integer;
    begin

    for i = 1 to high(C) do
    begin
    if C[i] = C[i-1] then
    C[i-1] := black
    if not C[i] = C[i+1] then
    C[i] := black
    end;
    end;

    {Does high return the last element of the array? and secondly does this make any sense? im trying to get it to go through each element and if 2 next to each other are the same colour then they both turn black}
    That looks okay...
    [size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]

  5. #5

    Help!! trouble implementing "same game"

    Hi there

    I need to implement a function to check if all the elements are red
    This is what i have come up with. How can I do this recursively?

    [pascal]
    Function AllRed(A:array of TColours): Boolean;
    var i,j : integer;
    begin
    begin
    for i := 0 to high(A) do
    begin
    if A[i] = red then
    inc(j)
    end;
    end;
    if j = high(A) then
    result := true;
    end;
    [/pascal]


    thanks in advance

    Derek

  6. #6

    Help!! trouble implementing "same game"

    Local variable are not initialised, so that code will not work. Also High returns the number of elements minus one, and if all the elements are red, j will be the number of elements, and therefore will not equal High.

    [pascal]
    function AllRed(A: array of TColours): Boolean;
    var
    i,j : Integer;
    begin
    j := 0; // Initialise j to zero.
    for i := 0 to High(A) do
    begin
    if A[i] = red then
    Inc(j);
    end;
    Result := (j = Length(A));
    end;
    [/pascal]

    However, a better way to do this would be:

    [pascal]
    function AllRed(A: array of TColours): Boolean;
    var
    i: Integer;
    begin
    for i := 0 to High(A) do
    begin
    if (A[i] <> red) then
    begin
    Result := False;
    Exit; // Don't bother checking futher once one non-red one is found
    end;
    end;
    Result := True;
    end;
    [/pascal]

    I don't see why you want to do this recursively.
    [size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]

  7. #7

    Help!! trouble implementing "same game"

    Look at you Hacker optimizing the algorithm and all

    Please note that the iterative version is not only faster and less resource intensive than it's recursive counterpart, but it's also easier to follow. That said here is a recursive version:

    [pascal][background=#FFFFFF][comment=#8080FF][normal=#000080][number=#C00000][reserved=#000000][string=#00C000]
    function AllRed(A: array of TColours): Boolean;
    const
    i: integer = 0; //This is a static variable declaration
    begin
    if A[i] = Red then begin
    i := i + 1;
    if (i < Length(A)) then begin
    Result := AllRed(A);
    end else begin
    Result := True;
    end;
    i := i - 1;
    end else begin
    Result := False;
    end;
    end;

    [/pascal]

    P.S. Please don't use me to cheat on your homework
    My DGDev forum pascal syntax highlight settings:
    <br />[background=#FFFFFF][comment=#8080FF][normal=#000080]
    <br />[number=#C00000][reserved=#000000][string=#00C000]

  8. #8

    Help!! trouble implementing "same game"

    Quote Originally Posted by Xorcist

    P.S. Please don't use me to cheat on your homework
    lol :lol: :lol: I think it's called 'research'!

  9. #9

    Help!! trouble implementing "same game"

    Thanks allot guys!
    really appreciate your detailed replys. cheers

    dont worry i dont intend to use your answers to "cheat" lol however i will mention you lot in my acknowledgements when i come to write it up

    u lot have been great!

    Derek

  10. #10

    Help!! trouble implementing "same game"

    "P.S. Please don't use me to cheat on your homework "

    Good point. And the reason I do not reply to questions of this type.

    If a student wants help understanding how something is done, or is boxed in by the technicality of Windows, Delphi or optimized coding, then I will respond where I can.

    If a student is in school, I think all queries concerning his coursework are properly addressed to the relevant teacher/tutor/lecturer.

    A student who wants code for his project or assignment is not going to learn anything from me completing his coursework for him.

    But I'm just an old fuddy duddy, so ignore me!
    Stevie
    <br />
    <br />Don't follow me..... I'm lost.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •