PDA

View Full Version : Help!! trouble implementing "same game"



yawbird
18-01-2003, 07:39 PM
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

Alimonster
18-01-2003, 08:16 PM
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:

[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;

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 :wink:.

yawbird
27-01-2003, 03:42 PM
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)

Useless Hacker
27-01-2003, 09:17 PM
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;



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;


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...

yawbird
30-01-2003, 07:20 PM
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?


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;



thanks in advance

Derek

Useless Hacker
31-01-2003, 09:26 AM
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.


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;


However, a better way to do this would be:


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;


I don't see why you want to do this recursively.

Xorcist
31-01-2003, 07:22 PM
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:

[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;



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

BlueCat
31-01-2003, 08:48 PM
P.S. Please don't use me to cheat on your homework

lol :lol: :lol: I think it's called 'research'!

yawbird
01-02-2003, 01:14 PM
Thanks allot guys!
really appreciate your detailed replys. cheers :D

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

Stevie56
01-02-2003, 01:41 PM
"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!

TheLion
01-02-2003, 05:50 PM
If a student is in school, I think all queries concerning his coursework are properly addressed to the relevant teacher/tutor/lecturer.

When I was still in school, my tutors where never able to learn me, but mostly learned FROM me. When they where trying to learn me something their explanation was mostly faulty or inaccurate and if I asked them something the answer mostly was: "I don't know.". In my total time of studying Technical Informatics (2.5 years) I had only 1 good teacher and he was the only one I ever learned stuff from... :(

I'm not defending him, since he should make his homework himself, however addressing questions to teachers isn't really usefull anymore, since most of them did a quick course into the subject they are teaching! :(

Stevie56
02-02-2003, 01:43 PM
Point taken.

On my last course my elected subject was Artificial Intelligence.

When asked why, I replied "Because there isn't enough of the real stuff here to fill a course"

Heard myself being called 'The Student From Hell' in the staffroom.

TheLion
03-02-2003, 09:31 AM
ROFLMAO :lol:

I think they used names like that for me in the staff room too, however I seemed to be on of the only students who could read the rules & regulations, one teacher actually asked me if it wouldn't be a good idea to go and study law or something with computers and law :)

I actually think they stopped seeing students as human beings with social lifes at the school I attended.