Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question for the math genius' here :) 3

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
Hello, all I'm currently making my own "Who wants to be a Millionaire?", just for the fun of it.
Now I'm working on making all the helpline, I've completed the 50/50 and the Call helpline, but the
ask the audience helpline has got me absolutely stumped, how do I generate 4 numbers that get closer
to eachother the further I get in the game and always amount to 100 in total? I've got a global variable that
keeps track of the level called "Level" which is an integer.

I would post some code, but I have absolutely no idea of where to start (my math sucks in other words hahaha)

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
Bobba

I would do a loop (or call the function recursivly) that keeps calling random 4 times to get the 4 values, you can make random work within a range.

Add the 4 results together on each itteration, if the results are greater than 100 disgard them.
If less subatract the total from 100 divide the result by 4 and add the same to each result (then the 4 results add up to 100)
use a range variable which varies with time to force the random results into the range you require.



a pseudonym for ??????
 
But that doesnt cause the results to get closer to eachother the further you progress into the game, I want to mimick the audience finding the questions harder, thats why the results from asking the audience need to be closer
when you get closer to the million dollar question.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
start by calling

Range := 100;
Random(range);

Each value will be in the range 0..100 Disgard totals > 100
e.g random genarate 60,10,20 and 2 = 92 difference is 8 / 4 = 2 gives
62,12,22 and 4 = 100

As the game progesses reduce the value of range towards 25.
and apply the addition.
e.g
5,2,8,5 = 20 diffrence from 100 is 80 div 4 = 20 gives
25,22,28,25



a pseudonym for ??????
 
Ahhh right, ok thanks dude, now I get you!

BobbaFet is a pseudonym for me ;)

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
You might find this code helpful:
Code:
type
  TAnswer = 'A'..'D';
  TResponses = array [TAnswer] of integer;
  TLevel = integer;

const
// Levels indicates the percentage probability of an 
// audience member voting correctly.  
// For example, at level 1 90% of the audience will vote 
// correctly. You can adjust these values if you wish.
  Levels: array [ 1..15 ] of TLevel = ( 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20 );


function GetAudienceHelp(correct: TAnswer; level: TLevel ): TResponses;
var
  person: integer;
  response: TAnswer;
  responses: TResponses;
  guess: TAnswer;
begin
// Clear responses array
  for response := Low(response) to High(response) do
     responses[response] := 0;

// Find out how each person is going to vote
  for person := 1 to 100 do
    if Random(100) < Levels[level] then
      inc ( responses [ correct ] )
    else begin  // audience member guesses
      guess := 'ABCD' [ Random(4) + 1 ];
      inc ( responses [ guess ] );
    end;
    
    result := responses;

end;
You call the GetAudienceHelp function by specifying the correct answer and the level the question is set at. Level 1 is the easiest and level 15 the most difficult. The function returns an array which tells you how many of the audience voted for each possible answer.

Here is an example calling of GetAudienceHelp. The responses are then put in four TLabel components:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  responses: TResponses;
begin
  responses := GetAudienceHelp ( TAnswer(edCorrect.text[1]), StrToInt(edLevel.text) );
  A.caption := IntToStr(responses['A']);
  B.caption := IntToStr(responses['B']);
  C.caption := IntToStr(responses['C']);
  D.caption := IntToStr(responses['D']);
end;

Andrew
Hampshire, UK
 
Thanks Towerbase! That is simply awesome!

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
A star from me too Andrew - that's a very elegant piece of code. I linked it up to a TChart Bar Series to try and mimick Millionaire and it looks pretty neat!

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Using TCharBar is a cool idea, Clive. Another alternative might be to use four TProgressBar components.

Good luck with the game, BobbaFet.

Andrew
Hampshire, UK
 
Thanks TowerBase!

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top