I am making a small game, just to sharpen up my Delphi knowledge before school starts again, and also to show of to my friends. So in this game I have five integers and I need to check if at least three of them equal each other. How would I do this without checking every single possible combination of equal variables?

Thanks for the assistance.

Recommended Answers

All 20 Replies

Use an array with those five integers, so you can loop it. Then make a function to count distinct values. One of them should have a count of three or more.

Okay, so we haven't actually done arrays in school yet. How do they work? My game is a poker type game. I have vars A, B, C, D and E and they can each have a value between one and six. I have a button for each possible combination (3 of a kind, 4 of a kind, 5 of a kind, full house, small straight, large straight). When a button is clicked it must check if they have said combination and give them some points and if not, it must give them a zero.

Okay so im going to loop the array and let it count all the ones, twos, threes, fours, fives and sixes, and then check if one of the counts are equal to three or more?

I'm kind off struggling to figure it out. Right now I have to arrays. One array is aDiceScores and it's range is 1 to 5. It stores the count of each dice. Then I have an array aCount that's from 1 to 6. aCount should count how many variables in aDiceScores are the same. What would be my code for this? Also, what would be the code to actually calculate the score? (It should be the sum of the 3 vars that are the same)

In pseudo code:

for each aCount
    set to zero

for each aDiceScore
    increment aCount[aDiceScore]

for each aCount, index 1..6
    if aCount[index] >= 3
        score = aCount[index] * index

Okay thanks I got it! Just the last part, what am I supposed to do where you say index?

This is what I have:

for K := 1 to 6 do
  aCount[K] := 0;

for K := 1 to 5 do
  Inc(aCount[aDiceScores[K]]);

for K := 1 to 6 do
  if aCount[index] >= 3 then
    iScore := aCount[K] * 3;

For some reason the answer is always 9.

What was the problem with my code that I posted then? K is my index.

You left one index, which should be K (and you added your code after I replied, messages got mixed up :) )

I've fixed it, but now my problem is that when the player gets 4 of the same, but they have already used that option, and they put it in 3 of a kind, they still get the score for all 4 of the same dices, where the should only get three.

Then perhaps you should add a variable, to check whether it is 3, 4, or 5 (instead of checking for 3+).

So I keep this code I have now and add some way of checking it? Or how am I supposed to do it? Sorry for this, I'm still a noob.

Instead of:

if aCount[index] >= 3 then

use:

if aCount[index] = myVariable then

myVariable will depend on what the user selects to count, 3, 4 or 5

The problem when I do that is that if the user has four, but chooses three of a kind, it doesn't work and gives him a zero. For example you roll a four 6s, but you have already previously used four of a kind, and can only use each option once. Now if you select the three of a kind it gives you zero, instead of 18.

Sorry, typo, use >= This is what I would do:

for K := 1 to 6 do
  if aCount[K] >= myVariable then
  begin
    iScore := aCount[K] * myVariable;
    Break; // get out of the loop, to avoid further processing
  end;

This is my code, but it still isn't right somewhere. It results a nine when there are three 2s for example.

for K := 1 to 6 do
  aCount[K] := 0;

for K := 1 to 5 do
  Inc(aCount[aDiceScores[K]]);

for K := 1 to 6 do
  if aCount[K] >= 3 then begin
    iScore3Kind := aCount[K] * 3;
  Break;
  end;

So, what would you say is wrong with that up there? I know it's probably something stupid I'm missing, but help me please?

for K := 1 to 6 do
  if aCount[K] >= myVariable then
  begin
    iScore := K * myVariable; // <-- here was the problem, multiply by K, not the value of K
    Break;
  end;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.