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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't figure this out!?

Status
Not open for further replies.

dasphatman

Programmer
May 26, 2005
6
DK
I'm trying to create a golf score script, and are doing ok so far, but has stumbled on an issue.

To make this explanation easy I have decided to only use 3 holes in my score card.

The scorecard have 3 holes (hole1, hole2, hole3) which has a preferred number of strokes (par5, par4, par4). Now each hole has a difficult rating (hcp2, hcp3, hcp1) where one is the most difficult one. So each hole looks like this:

Hole1, Par5, Hcp2
Hole2, Par4, Hcp3
Hole3, Par4, Hcp1

Now a golf player has what is called a handicap depending on how good the player is. The lower handicap the better the player. The handicap is made so a player can use extra strokes on a hole to his round. Lets say a player has handicap 4.5 then the below formal is deciding how many extra strokes he has:
Code:
<?php	
$maleslope = '131';
$malerating = '71.7';
$coursepar = '72';
?>
var handicap = validNum(document.form1.handicapindex.value)
 
var strokes = handicap * <? echo $maleslope; ?>  / 113 + <? echo $malerating; ?> - <? echo $coursepar; ?>;

newstroke = Math.round(strokes)
 
document.form1.strokecount.value = newstroke

The maleslope, malerating and coursepar is normally from a mysql DB.

In this case the player has 5 extra strokes on the 3 holes.

Now for the tricky part!

These 5 extra strokes should be divided to the 3 holes in such a way that the easiest hole (hole2) should get 1 extra stroke while the 2 others should get 2 extra strokes, So what I want is to get a output like this, problary in a loop:

var stroke1 = 5 + 2
var stroke2 = 4 + 1
var stroke3 = 4 + 2

These vars I can then use in the last part of the script. I have stumbled over this script which does some of the thing, but I'm not that good at javascript, so I can't figure out how to get the output like above:

Code:
var i=0, field=[0,1,2], number=document.form1.handicapindex.value;
  		while (number!==0)
  		{
    		field[i]+=1;
    		number-=1;
    		if (i===field.length-1) i=0
      		else i+=1;
  		}

The script would be getting its hcp and par variables like this:
Code:
<?php
$par1 = $row['par1'];
$par2 = $row['par2'];
$par3 = $row['par3'];

$hcp1 = $row['hcp1'];
$hcp2 = $row['hcp2'];
$hcp3 = $row['hcp3'];
?>

I hope somebody can help me getting the pieces together... Thanks alot:)



 
You are missing a bunch of connections here so I can't seem to see how this all fits together. And I am tired and not real familiar with JS myself.

However, I don't understand why you used "while (number!==0)" in your second snippet. It looks like you know exactly how many times you will iterate through the loop. If so, using a "for" construction would simplify things.

As for the problem, I think I would attack it something along these lines.

1) Find out what is the largest number to put in the easiest field: largestnumber = integer(extrastrokes/numberofholes)
2) Find out the number of holes that need extra strokes: holecount = modulus(extrastrokes/numberofholes)
3) Create array with 3 columns to hold temporary values for each hole.
4) Fill column 1 with "hole" numbers.
5) Fill column 2 with easiness number corresponding to the hole.
6) Sort on column 2.
7) Fill all column 3 array elements with "largestnumber"
8) Loop from highest to lowest number in column 2 adding 1 to column 3 until "holecount" equals zero.
9) Sort on column 1.
10) Add values in column 3 to corresponding pars for each hole.

You will need to figure out how to do that in JS. I am simply too tired to do it now. Also, since I am so tired I may have missed something or completely screwed it up.

And I am sure there are better ways to do this. I just can't think of them.


mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top