954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Checking if various vars equal each other

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.

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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.

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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?

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Right. There are more ways to do that, but you should try first.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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)

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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.

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
for index := 1 to 6 do
  rest here

sorry, was a bit unclear.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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.

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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.

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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.

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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;
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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;
Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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?

Fireprufe15
Light Poster
41 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You