Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Kyoto Die Game

Name: Anonymous 2007-02-27 21:40 ID:gtys9iUO

Has anyone ever made the Kyoto'99 Die Game?

Name: Anonymous 2007-02-28 14:12 ID:jxGqxyAc

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. :)

Name: Anonymous 2007-03-01 10:32 ID:Jf7oKAdO

Looks hard.

Let's see this shit in all programming languages.

Name: Anonymous 2007-03-01 11:07 ID:J3+K9+Rn

Too bad Ruby on Rails is slow as fuck.

Name: Anonymous 2007-03-01 11:46 ID:YInJVJip

Haskell.
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 :: 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: Anonymous 2007-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: Anonymous 2007-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?)

Name: Anonymous 2007-03-01 18:06 ID:nJTz30MN

Java is more legible

You are the cancer that is killing programming.

Name: Anonymous 2007-03-01 19:01 ID:3J9v72KY

>>8

I'm human... sue me.

Name: VROOM VROOM 2007-03-01 19:53 ID:K+Qksj1D

Pascal.

program kyoto;

const t: array[0..3,0..23] of Byte = (
  (16,12,5,9,15,21,10,0,4,20,17,1,22,6,3,19,23,13,2,8,14,18,11,7),
  (7,11,18,14,8,2,13,23,19,3,6,22,1,17,20,4,0,10,21,15,9,5,12,16),
  (15,4,8,19,20,9,3,14,23,16,0,7,5,2,18,21,12,1,11,22,17,10,6,13),
  (10,17,13,6,1,12,22,11,2,5,21,18,16,23,7,0,9,20,14,3,4,15,19,8)
);

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.

Name: Anonymous 2007-03-01 20:16 ID:0TqXwEp9

>>7
>>9

Go die. Horrifically.

Name: Anonymous 2007-03-01 20:57 ID:Heaven

FREEBASIC OR GTFO

Name: Anonymous 2007-03-02 3:09 ID:fhNZ5nV+

Fuck OO. GOTO or GTFO.

Name: Anonymous 2007-03-02 4:47 ID:znaBJ9Hh

Fuck procedural programming.  foldl or GTFO.

Name: Anonymous 2007-03-02 9:21 ID:bqHIH7GO

>>14
I'll second that.

Name: Zuhgzarakracht 2007-03-02 10:39 ID:w6+A3P++

PHP
<?php
if (!defined('STDIN')) {
    define('STDIN', fopen("php://stdin", "r"));
}
$die = array(6,array(3,5,4),1,2);
while(($t=intval(fgets(STDIN))) > 0) {
    for(;$t > 0;--$t) {
        switch(trim(fgets(STDIN))) {
            case "north":
                $die = array($die[1][1], array($die[1][0], $die[2], $die[1][2]), $die[3], $die[0]);
                break;
            case "south":
                $die=array($die[3], array($die[1][0], $die[0], $die[1][2]), $die[1][1], $die[2]);
                break;
            case "west":
                $die[1]=array($die[1][2], $die[1][0], $die[1][1]);
                break;
            case "east":
                $die[1]=array($die[1][1], $die[1][2], $die[1][0]);
                break;
            default:
                echo "WTF?\n";
        }
    }
    echo $die[1][1], "\n";
}
?>

Name: Anonymous 2007-03-02 11:31 ID:w6+A3P++

>>16
Whuuups, I forgot to move the bottom number in east and west.

Ah, well.

Name: Anonymous 2007-03-02 17:53 ID:MqONbDh8

holy crap, i have to do this in java for hw...

gonna quit

Name: Anonymous 2007-03-02 23:41 ID:M/nnP8fc

To hell with number tricks. Here's a C solution:

    #include <stdio.h>
    #include <stdlib.h>
   
    enum { TOP, EAST, NORTH, WEST, SOUTH, BOTTOM };
   
    void init(unsigned int die[]);
    void roll(unsigned int die[], int a, int b, int c, int d);
   
    int main()
    {
      char dir[] = "\0\0\0\0\0";
      unsigned int die[6];
      int c;
     
      while (scanf("%d", &c) && c > 0)
      {
        init(die);
        while (c-- > 0 && scanf("%s", dir))
        {
          switch (dir[0])
          {
            case 'e':
              roll(die, TOP, WEST, BOTTOM, EAST);
              break;
            case 'n':
              roll(die, TOP, SOUTH, BOTTOM, NORTH);
              break;
            case 'w':
              roll(die, TOP, EAST, BOTTOM, WEST);
              break;
            case 's':
              roll(die, TOP, NORTH, BOTTOM, SOUTH);
              break;
          }
        }
        printf("%d\n", die[TOP]);
      }
     
      return 0;
    }
   
    void init(unsigned int die[])
    {
      die[TOP] = 1;
      die[NORTH] = 2;
      die[WEST] = 3;
      die[EAST] = 4;
      die[SOUTH] = 5;
      die[BOTTOM] = 6;
    }
   
    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: Anonymous 2007-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: Anonymous 2007-03-03 5:44 ID:OGy810ll

I write enterprise-level PHP because I am an EXPERT PROGRAMMER.

#!/usr/bin/env php
<?php

if(!defined('STDIN')) {
  define('STDIN', fopen('php://stdin','r'));
}

function out($str = '', $nl = 1) {
  echo $str.str_repeat("\n", $nl);
}

function in($str = '> ') {
  out($str, 0);
  $input = trim(fread(STDIN, 1024));
  return (!feof(STDIN) ? $input : false);
}

class Dice {
  protected $defaultPos, $currentSides;
  const Top    =  1;
  const Bottom = -1;
  const North  =  2;
  const South  = -2;
  const West   =  3;
  const East   = -3;
  const ResultRotation  = 1;
  const ResultShow      = 2;
  const ResultReset     = 3;
  const ResultError     = 4;
  protected $combinations = array(
    1 => array(2, 3, 5, 4),
    2 => array(1, 4, 6, 3),
    3 => array(1, 2, 6, 5)
  );
  protected $rotations = array(
    'north' => array(self::South, self::Top),
    'east'  => array(self::West, self::North),
    'south' => array(self::North, self::Bottom),
    'west'  => array(self::East, self::North)
  ); 
  protected $commands = array(
    'north' => array('north', 'n', '8'),
    'east'  => array('east', 'e', '6'),
    'south' => array('south', 's', '2', '5'),
    'west'  => array('west', 'w', '4'),
    'show'  => array('show', '0'),
    'reset' => array('reset', '=')
  );
 
  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');
    }
  }
   
  protected function setPosition($top, $north) {
    $sides = array();
    $combKeys = array_map('array_flip', $this->combinations);
    $smallTop = ($top <= 3);
    $topKey = ($smallTop ? $top : self::oppositeNumber($top));
    if(isset($combKeys[$topKey][$north])) {
      $westCombRaw = self::incrementIndex($combKeys[$topKey][$north], ($smallTop ? 1 : -1), 0, 3);
      $west = $this->combinations[$topKey][$westCombRaw - floor($westCombRaw / 4) * 4];
    } else {
      return false;
    }
    $sides[self::Top]    = $top;
    $sides[self::Bottom] = self::oppositeNumber($top);
    $sides[self::North]  = $north;
    $sides[self::South]  = self::oppositeNumber($north);
    $sides[self::West]   = $west;
    $sides[self::East]   = self::oppositeNumber($west);
    $this->currentSides  = $sides;
    return true;
  }
 
  public function command($rawCommand) {
    $rawCommand = preg_split('# +#', trim($rawCommand));
    $rawCommandCode = $rawCommand[0];
    $command        = null;
    $args           = array_slice($rawCommand, 1);
    foreach($this->commands as $commandName => $commandCodes) {
      if(in_array($rawCommandCode, $commandCodes)) {
        $command = $commandName;
        break;
      }
    }
    switch($command) {     
      case 'north':
      case 'east':
      case 'south':
      case 'west':        
        $rotation = $this->rotations[$command];
        $newTop   = $this->currentSides[$rotation[0]];
        $newNorth = $this->currentSides[$rotation[1]];
        $this->setPosition($newTop, $newNorth);
        return array(self::ResultRotation, self::array_merge_noReindex(array('direction' => $command), $this->currentSides));
        break;
      case 'show':
        return array(self::ResultShow, $this->currentSides);
        break;
      case 'reset':
        $resetTop   = (isset($args[0]) ? $args[0] : $this->defaultPos[0]);
        $resetNorth = (isset($args[1]) ? $args[1] : $this->defaultPos[1]);
        if($this->setPosition($resetTop, $resetNorth)) {
          return array(self::ResultReset, $this->currentSides);
        } else {
          return array(self::ResultError, 'Could not reset die to Top = '.$resetTop.', North = '.$resetNorth.'.');
        }
        break;
      default:
        return array(self::ResultError, '"'.$rawCommandCode.'": unknown command.');
        break;
    }
  }
 
  public function getSides() {
    return $this->currentSides;
  }
 
  protected static function oppositeNumber($number) {
    return -($number - 7);
  }
 
  protected static function incrementIndex($index, $increment, $rangeMin, $rangeMax) {
    if($index < $rangeMin || $index > $rangeMax || $rangeMin >= $rangeMax) return false;
    $index += $increment;
    while(($tooSmall = $index < $rangeMin) || $index > $rangeMax) {
      $index += ($tooSmall ? 1 : -1) * ($rangeMax - $rangeMin + 1);
    }
    return $index;
  }
 
  protected static function array_merge_noReindex($array) {
    $args = func_get_args();
    $args = array_slice($args, 1);
    foreach($args as $arg) {
      foreach($arg as $key => $value) {
        $array[$key] = $value;
      }
    }
    return $array;
  }
}
 
out('Die Rotation Simulator', 2);

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: Anonymous 2007-03-03 14:37 ID:Yjl5mW2T

Java it, puleaz

Name: Anonymous 2007-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.");
        }
    }
}

Name: Anonymous 2007-03-03 18:43 ID:Yjl5mW2T

>>22

java is a piece of shit and this game is even moar shittier than java (which is outstanding), so I ASSURE YOU it CAN'T be done

fuck you, fuck java and fuck die

Name: Anonymous 2007-03-03 21:04 ID:uGE3B6YF

>>23
Why did you import java.util.*?

Name: Anonymous 2007-03-03 21:13 ID:HvU7fwxH

import java.util.*;

class BreastFuck {
  public static void main(String[] args) {
    LinkedList<Character> DieGame = new LinkedList<Character>();
   
    DieGame.add(new Character('>'));
    DieGame.add(new Character('>'));
    DieGame.add(new Character('2'));
    DieGame.add(new Character('4'));
    DieGame.add(new Character(' '));
    DieGame.add(new Character('N'));
    DieGame.add(new Character('e'));
    DieGame.add(new Character('e'));
    DieGame.add(new Character('d'));
    DieGame.add(new Character('s'));
    DieGame.add(new Character(' '));
    DieGame.add(new Character('t'));
    DieGame.add(new Character('o'));
    DieGame.add(new Character(' '));
    DieGame.add(new Character('b'));
    DieGame.add(new Character('e'));
    DieGame.add(new Character('c'));
    DieGame.add(new Character('o'));
    DieGame.add(new Character('m'));
    DieGame.add(new Character('e'));
    DieGame.add(new Character(' '));
    DieGame.add(new Character('a'));
    DieGame.add(new Character('n'));
    DieGame.add(new Character(' '));
    DieGame.add(new Character('h'));
    DieGame.add(new Character('e'));
    DieGame.add(new Character('r'));
    DieGame.add(new Character('o'));
    DieGame.add(new Character('.'));
   
    Iterator<Character> it = DieGame.iterator();
    Character tmp;
    while (tmp = it.next())
      System.out.println(tmp.charValue());
  }
}

Name: Anonymous 2007-03-03 22:55 ID:iEHHE8B4

>>25

That should be java.lang.*

I taught myself C and forgot all the Java I was force-fed in my Uni classes.

Name: Anonymous 2007-03-03 23:12 ID:HvU7fwxH

java.lang.* is imported by default.

Name: Anonymous 2007-03-04 1:04 ID:I7mOigi6

Fine, here's a serious attempt. Didn't check for exceptions, but it compiles and works fine.

import java.io.*;
import java.util.ArrayList;

public class KyotoDie {

  private static final int[] dieData = { 107, 105, 108, 108, 32, 121, 111, 117, 114, 115, 101, 108, 102, 10 };

  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;
  }
}

Name: Anonymous 2007-03-04 1:22 ID:HSfvmSpA

>>28

Like I said, I've forgotten all the Java I learned. God, that was a long time ago. Must be at least three months now.

Name: Anonymous 2007-03-04 6:07 ID:Heaven

>>30
Keep it up, you're almost clean

Name: Anonymous 2007-03-04 16:27 ID:7phzgbon

Mix >>19 and >>29 and you'll get the perfect Java solution.

DO IT, MOOT!

Name: Anonymous 2007-03-04 17:33 ID:I7mOigi6

>>29
Does anybody realise what this actually does?

Name: Anonymous 2007-03-04 18:00 ID:p7VFLDOW

>>33

No idea, but I can't understand the dieData and the extra max function.

Name: Anonymous 2007-03-04 18:42 ID:Heaven

>>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: Anonymous 2007-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.

The extra max() is just a brain fragment.

Name: Anonymous 2007-03-04 19:21 ID:p7VFLDOW

>>36

ASCII related?

Are you telling me that all the number and Character things are ASCII mojo?

Name: Anonymous 2007-03-04 19:27 ID:p7VFLDOW

«[wrote something.class]
[total 781ms]
Note: something.java uses unchecked or unsafe operations. **
Note: Recompile with -Xlint:unchecked for details. »

It's related to the usage of .add method

Name: Anonymous 2007-03-04 19:46 ID:I7mOigi6

Ignore the warning. Otherwise I'd have to put ArrayList<Character> bleh = new ArrayList<Character>() and so on. Java already has too much typing.

Name: Anonymous 2007-03-04 23:31 ID:p7VFLDOW

>>32

Tried, but I can't use enumerate types like that in Java.

It's kind of a mix between enum and an array. @__@

Name: Anonymous 2007-03-05 8:43 ID:NZ7GuXJb

>>19
while (c-- > 0 && scanf("%s", &dir))

>>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.

Name: Anonymous 2007-03-05 17:08 ID:w3fniSvB

>>41
dir == &dir[0]

Name: Anonymous 2007-03-09 7:22 ID:7kwJacvU

I just did it in Java...

It's easy as sticking it in your mom's pooper

Name: Anonymous 2009-01-14 14:15

I AM AN EXPERT PROGRAMMER

Name: Anonymous 2009-03-06 10:10


The XHTML MIME type   Oh shit there   are tons of   windows open you   always know where   to start from   scratch or take   it private 02.

Name: Anonymous 2009-03-06 13:16

when can he give me a blow   job he said   link but nothing   more than that   of Common Lisp   succeed but Common.

Name: Sgt.Kabu됯kiman쥅႞ 2012-05-28 22:54

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

Don't change these.
Name: Email:
Entire Thread Thread List