Jump to content

Dice game help


brentman

Recommended Posts

I am trying to program a variant of a dice game called cee-lo. User rolls three dice... best to worst listed below....

 

4,5,6 - automatic win

three 6's

three 5's

three 4's

three 3's

three 2's

three 1's

any pair and 6

any pair and 5

any pair and 4

any pair and 3

any pair and 2

any pair and 1

1,2,3 - automatic loss

 

Any non matching to these which would reroll.

 

They are compared to the banker who wins in a tie.

 

How can i program this without a GIANT if/else nightmare? or is that whats required? a little guidance in the logic of this would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/287243-dice-game-help/
Share on other sites

In no particular order,

1. Check for 4,5,6

2. Check that array_count_values() returns 1 item and rank accordingly (the one with 3 quantity, sum / 3, only item in the array, etc.)

3. Check that array_count_values() returns 2 items and rank according to the one with the most instances (ie, 2)

4. Check for 1,2,3

Otherwise reroll.

Link to comment
https://forums.phpfreaks.com/topic/287243-dice-game-help/#findComment-1473771
Share on other sites

Assign a "score" value to each of your "possible" rolls. Then calculate the "score" of each roll and compare.

 

Hint: The "scores" do not have to be incremental, just sequential.

 

 

 

/*
	calcScore($paRoll) - returns int score
	
	Calculates a "score" for the Dice.
	
	$paRoll - An array of 3 die values
	
	Score:
		100		4,5,6 - automatic win
		60		three 6's
		50		three 5's
		40		three 4's
		30		three 3's
		20		three 2's
		10		three 1's
		 6		any pair and 6
		 5		any pair and 5
		 4		any pair and 4
		 3		any pair and 3
		 2		any pair and 2
		 1		any pair and 1
		-1		1,2,3 - automatic loss
		 0		Any non matching to these which would reroll.

*/
function calcScore($paRoll) {

	// For the "Automatic" scores, we need them in sequence - does not affect other checks
	sort($paRoll);
	
	// Automatic WIN or Automatic LOOSE
	$check = implode(',', $paRoll);
	if ($check == '4,5,6') 		return 100;
	elseif ($check == '1,2,3')	return -1;
	
	// Three of a kind - score one die times 10
	if ( ($paRoll[0] == $paRoll[1]) and ($paRoll[1] == $paRoll[2]))	return ($paRoll[0] * 10);
	
	// One Pair - score the matched die value
	if ($paRoll[0] == $paRoll[1]) 	return $paRoll[0];
	if ($paRoll[1] == $paRoll[2])	return $paRoll[1];
	if ($paRoll[0] == $paRoll[2])	return $paRoll[2];
	
	// Nothing
	return 0;
}

 

Link to comment
https://forums.phpfreaks.com/topic/287243-dice-game-help/#findComment-1473774
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.