I hadn't heard of this until today, but I'm implementing it in PHP right now (I know it's not exactly the most suitable language for this, but PHP is what I know best). It's basically a Dice class whose instances you can pass commands to via a public method, wrapped in a script that reads input from stdin. On a north/east/south/west command it just switches around the appropriate sides of the dice (array elements).
The instructions I'm using are at http://acm.uva.es/p/v104/10409.html, and I'm wondering why you should "start" each game by entering the number of commands to be executed. Why not just make it possible to input as many lines as you want to, and have one command that displays the current number at the top and another command that resets the dice to its original position?
If there's anyone else who's bored and has absolutely no life, I'd like to hear thoughts on this stuff and maybe other methods to make this game. :)
data Direction = NORTH | EAST | SOUTH | WEST deriving (Show, Read)
type Dice = (Int,Int,Int)
type Program = [Direction]
roll :: Direction -> Dice -> Dice
roll NORTH (t,n,w) = (7-n, t, w)
roll EAST (t,n,w) = ( w, n, 7-t)
roll SOUTH (t,n,w) = ( n, 7-t, w)
roll WEST (t,n,w) = (7-w, n, t)
exec :: Program -> Int
exec p = let (t,n,w) = foldl (flip roll) (1,2,3) p in t
programs :: [String] -> [Program]
programs = unfoldr program
where
program (x:xs) = case read x of
0 -> Nothing
n -> Just $ first (map read) (splitAt n xs)
main :: IO ()
main = getContents >>=
putStr
. unlines
. map show
. map exec
. programs
. words
. map toUpper
Name:
Anonymous2007-03-01 15:57 ID:YInJVJip
And here's a slight retake.
import Control.Arrow (first)
import Data.Char (toUpper)
import Data.List (unfoldr)
data Direction = NORTH | EAST | SOUTH | WEST deriving (Show, Read)
type Dice = (Int,Int,Int)
type Program = [Direction]
roll :: Dice -> Direction -> Dice
roll (t,n,w) d = case d of
NORTH -> (7-n, t, w)
SOUTH -> ( n, 7-t, w)
EAST -> ( w, n, 7-t)
WEST -> (7-w, n, t)
exec :: Program -> Int
exec p = let (t,n,w) = foldl roll (1,2,3) p in t
programs :: [String] -> [Program]
programs = unfoldr program
where
program (x:xs) = case read x of
0 -> Nothing
n -> Just $ first (map read) (splitAt n xs)
main :: IO ()
main = getContents >>=
putStr
. unlines
. map show
. map exec
. programs
. words
. map toUpper
Name:
Anonymous2007-03-01 17:01 ID:3J9v72KY
I don't understand this Haskell stuff! @___@ Java is more legible. (Can it be done in Java or C?)
var c: Cardinal; s: Byte; d: Char;
begin
Readln(c);
while c > 0 do
begin
s := 0;
repeat
Readln(d); Dec(c);
case d of
'n': s := t[0,s];
's': s := t[1,s];
'w': s := t[2,s];
'e': s := t[3,s];
end;
until c = 0;
Writeln(s shr 2 + 1);
Readln(c);
end;
end.
void roll(unsigned int die[], int a, int b, int c, int d)
{
unsigned int tmp = die[a];
die[a] = die[b];
die[b] = die[c];
die[c] = die[d];
die[d] = tmp;
}
Name:
Anonymous2007-03-02 23:43 ID:M/nnP8fc
>>19
Ignore stdlib.h. I was planning to do something with it, but I forgot what it was.
Name:
Anonymous2007-03-03 5:44 ID:OGy810ll
I write enterprise-level PHP because I am an EXPERT PROGRAMMER.
public function __construct($defaultTop, $defaultNorth) {
$this->defaultPos = array($defaultTop, $defaultNorth);
if(!$this->setPosition($defaultTop, $defaultNorth)) {
throw new Exception('Failed to construct '.get_class($this).': Invalid position arguments');
}
}
try {
$dice = new Dice(1, 2);
list($messageType, $message) = $dice->command('show');
$run = true;
while(true) {
switch($messageType) {
case Dice::ResultRotation:
out('The die was rotated '.$message['direction'].'. It shows a '.$message[Dice::Top].'.');
break;
case Dice::ResultShow:
out('The die shows a '.$message[Dice::Top].'.');
break;
case Dice::ResultReset:
out('The die was reset. It shows a '.$message[Dice::Top].'.');
break;
case Dice::ResultError:
out('Error: '.$message);
break;
}
$commandString = (($tempCmdStr = in()) !== false ? strtolower($tempCmdStr) : false);
if($commandString == 'quit') {
break;
} elseif($commandString === false) {
out('quit');
break;
}
list($messageType, $message) = $dice->command($commandString);
}
} catch(Exception $e) {
out($e->getMessage());
}
?>
Name:
Anonymous2007-03-03 14:37 ID:Yjl5mW2T
Java it, puleaz
Name:
Anonymous2007-03-03 16:15 ID:iEHHE8B4
import java.util.*;
public class BreastFuck { public static void main(String[] args) { while (true) { System.out.println(">>22 is a complete retard and should GTFO."); } } }
private static final int NORTH = dieData[4] - 32;
private static final int SOUTH = dieData[11] - 108;
private static final int EAST = dieData[dieData.length - 1] - 10;
private static final int WEST = dieData[1] - 2 * 105 + 21 * 5;
public static void main(String[] args) throws IOException {
ArrayList result;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num, pos;
while (( num = Integer.valueOf(br.readLine()) ) > 0) {
result = new ArrayList();
pos = 0;
for (int i = 0; i < num; ++i) {
char dir = br.readLine().charAt(0);
switch (dir) {
case 'n':
result.add(result.size() - NORTH, new Character((char) dieData[pos++]));
break;
case 's':
result.add(result.size() - SOUTH, new Character((char) dieData[pos++]));
break;
case 'e':
result.add(result.size() - EAST, new Character((char) dieData[pos++]));
break;
case 'w':
result.add(result.size() - WEST, new Character((char) dieData[pos++]));
break;
}
}
for (int i = pos; i < dieData.length; ++i)
result.add(new Character((char) dieData[i]));
for (int i = 0; i < result.size(); ++i)
System.out.print(((Character) result.get(i)).charValue());
}
br.close();
}
private static int max(int a, int b) {
return (a < b) ? b : a;
}
}
>>33
dieData = "kill yourself\n". The rest of the program seems to just print that after every nswe sequence. It's not obfuscated very well.
Name:
Anonymous2007-03-04 19:10 ID:I7mOigi6
>>29 here
Good to see some people are at least marginally competent programmers. Anybody remotely familiar with ASCII could have seen that coming a mile away.
>>40
Would you just GTFO already? Not only are you a Java programmer, you're a terribly stupid Java programmer, which is somewhat redundant. Just go play in traffic or something.
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy