Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > Apps

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 01-03-2012, 02:14 PM
Fireprufe15 Fireprufe15 is offline
Junior Member
 
Join Date: Sep 2011
Posts: 13
Default Checking if variables in an array form a sequence

I'm working on a Yahtzee game, and I'm struggling with scoring on the large and small straights. In these combinations each of the dice needs to follow on the next, for example for a large straight you need a 1, 2, 3,4 and 5 or a 2, 3, 4, 5 and 6.

The values of the dice are stored in an array.
I got some code off the internet for sorting the array from small to large and then I wrote my own to check if they are in sequence. What's wrong with the code?


Code:
var
counter, min, look, K : integer;
temp, iSequence : integer;

  for counter:=1 to Top do begin
    min:=counter;
    for look:=counter+1 to top do
      if aDiceScores[look]<aDiceScores[min] then min:=look;
    temp:= aDiceScores[min];
    aDiceScores[min]:=aDiceScores[counter];
    aDiceScores[counter]:=temp;
  end;

 iSequence := 0;
 for K := 1 to 5 do
    if aDiceScores[K] + 1 = aDiceScores[K + 1]
    then inc(iSequence);


  if iSequence = 5 then
  iScoreLarge := 40;
Reply With Quote
  #2  
Old 01-03-2012, 02:46 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

What is the error? or do you get none and is the result not as you expected? What is Top, since it's not declared? And isn't there a begin/end construction missing in the sorting of the array?

But in all situations: The debugger will help you solve this, learn how to use it...
Reply With Quote
  #3  
Old 01-03-2012, 03:04 PM
Fireprufe15 Fireprufe15 is offline
Junior Member
 
Join Date: Sep 2011
Posts: 13
Default

There is no error, but the result always seems to be zero. And I don't know about top, when I got this code off of the internet I assumed top is the top of the array. Just tell me if something in the code is causing this whole sorting and then checking if they are in sequence thing to not make iSequence a 5.

Last edited by Fireprufe15; 01-03-2012 at 04:03 PM.
Reply With Quote
  #4  
Old 01-04-2012, 09:16 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

Top (if not defined) is the Top property of your form...
I can solve this issue for you, but it's better that you do it yourself. Solving this for you makes you not learn anything.
In my previous reply I told you to use the debugger. When developping this is the 1 thing you need to understand and be able to use...
Reply With Quote
  #5  
Old 01-05-2012, 08:20 AM
Fireprufe15 Fireprufe15 is offline
Junior Member
 
Join Date: Sep 2011
Posts: 13
Default

Okay so I changed the top thing in the code with a 6 (the top of my array). Now for some reason the code always replaces my last value with a zero when sorting and places it at the start. I'm going to go and try and fix it with trial and error, but I cannot figure out why it does this.
Reply With Quote
  #6  
Old 01-05-2012, 09:21 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

You don't need trail and error, learn to use the debugger and see where it changes your value... Place a breakpoint, add some watches and step...
Reply With Quote
  #7  
Old 01-05-2012, 11:10 AM
Fireprufe15 Fireprufe15 is offline
Junior Member
 
Join Date: Sep 2011
Posts: 13
Default

Okay I figured it out this is my new code:

Code:
var
counter, min, look, K : integer;
temp, iSequence : integer;
begin

if (iRollcount > 0) AND (iRollcount < 4) then begin
  //VERWERKING:
  for counter:=1 to 5 do begin
    min:=counter;
    for look:=counter+1 to 5 do
      if aDiceScores[look]<aDiceScores[min] then min:=look;
    temp:= aDiceScores[min];
    aDiceScores[min]:=aDiceScores[counter];
    aDiceScores[counter]:=temp;
  end;

  iSequence := 0;

  for K := 1 to 5 do
    if aDiceScores[K] + 1 = aDiceScores[K + 1]
    then inc(iSequence);

  if iSequence = 4 then
  iScoreLarge := 40;

  //AFVOER:
  pnlLargeScore.Caption := IntToStr(iScoreLarge);
  btnLarge.Enabled := False;
  btnNext.Enabled := True;
  iRollcount := 4;
  btnRoll.Enabled := False;

end;
Thank you for the help.
Reply With Quote
  #8  
Old 01-05-2012, 11:13 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

for i := 1 to 5 is now hardcoded, look at Low(aDiceScores)/High(aDiceScores) to make it more flexible
Reply With Quote
  #9  
Old 01-05-2012, 11:38 AM
Fireprufe15 Fireprufe15 is offline
Junior Member
 
Join Date: Sep 2011
Posts: 13
Default

This isn't really a problem because there will always only be 5 dice, thus 5 scores.
Reply With Quote
  #10  
Old 01-05-2012, 11:41 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

And what if you want to enhance your program to make it for 7 dice (not that that will happen, but what if)?

It's better to take these things into account now to save yourself a lot of work afterwards... This is an easy example, but if you don't start now it will be a mess in larger (and more complex) applications...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:35 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.