PHP:1284
This is my second CodeGolf, and I wasn't really aiming for shortness as much as code mutability and matching the gaming criteria.
You can generate 4 sided dice as well as 6 sided.
Maybe later I will shorten it and make it a little more dynamic.
function draw_dice($numdice=1,$sides=4)
{
/* Verify acceptable parameters. */
if($sides<4){die("You must choose 4 sides or greater.");}
if($numdice<1){die("You must have at least one dice.");}
/* End verification */
$a=' ';
$b=' ';
$c=' ';
$d=' ';
$e=' ';
$f=' ';
$g=' ';
$h=' ';
$i=' ';
$j=' ';
switch($sides)
{
case $sides%2==0:
if($sides==4)
{
$ran=rand(1,$sides);
switch($ran)
{
case 1:
$e="o";
break;
case 2:
$a="o";
$j="o";
break;
case 3:
$b="o";
$g="o";
$j="o";
break;
case 4:
$a="o";
$c="o";
$g="o";
$j="o";
break;
}
echo "<div style='text-align:center;display:inline-block;'>";
echo " - <br/>";
echo "| |<br/>";
echo "|$a$b$c|<br/>";
echo "| $d$e$f |<br/>";
echo "| $g$h$i$j |<br/>";
echo "---------<br/>";
echo "</div>";
}
if($sides==6)
{
$ran=rand(1,$sides);
switch($ran)
{
case 1:
$e="o";
break;
case 2:
$a="o";
$i="o";
break;
case 3:
$a="o";
$i="o";
$e="o";
break;
case 4:
$a="o";
$c="o";
$g="o";
$i="o";
break;
case 5:
$a="o";
$c="o";
$g="o";
$i="o";
$e="o";
break;
case 6:
$a="o";
$c="o";
$d="o";
$f="o";
$g="o";
$i="o";
break;
}
echo "-----<br/>";
echo "|$a$b$c|<br/>";
echo "|$d$e$f|<br/>";
echo "|$g$h$i|<br/>";
echo "-----<br/>";
}
if($sides!==4&&$sides!==6)
{
die("Only 4 and 6 sided are supported at this time.");
}
break;
case $sides%2==1:
die("Must have even number of sides.");
break;
}
}
draw_dice(1,4);
Output 4 sided:
-
| |
|o o|
| |
| o o |
---------
Output 6 sided:
-----
|o |
| o |
| o|
-----
9If you remove the ascii-art tag, I can offer my 39char-solution
print("⚀⚁⚂⚃⚄⚅"(util.Random.nextInt(6)))
(utf-art). – user unknown – 2012-06-14T22:40:22.0704
You have not defined the metric here. Is this meant to be a code golf? It is always worth discussing possible tasks in the puzzle lab chat or the sand box on meta so that you can address these kinds of questions before you go live.
– dmckee --- ex-moderator kitten – 2011-05-18T19:13:42.437Sorry, I thought the metric always is the number of characters? Thanks for pointing me to the chat and then sand box on meta. If this question falls flat on it face then I will delete it. I hate to do it right now, just in case someone already started working on it. – Jonas Elfström – 2011-05-18T19:18:37.767
Ah...note the site name "Programming puzzles and code golf" (and yeah, I thought it scanned better the other way round, too). You will also find one-liners, king-of-the-hill tournaments, and code-challenges (everything else, but you are still supposed to establish an objective metric for winning), so it is necessary to say and to apply the appropriate tag.
– dmckee --- ex-moderator kitten – 2011-05-18T19:35:20.077http://english.stackexchange.com/questions/243/is-it-ok-to-say-dice-and-dices-instead-of-die-and-dice – Jonas Elfström – 2011-05-26T07:01:31.333
13
According to XKCD #221,
– JiminP – 2011-09-03T00:08:30.970alert('-----\n|o o|\n| |\n|o o|\n-----');
is a correct program.