2Deadfish

From Esolang
Jump to navigation Jump to search
This language is a derivative of Deadfish.

2Deadfish is a 2D variant of Deadfish in which whenever a command is read, the program pointer direction changes based on the command and the direction it was originally going (except for o, technically).

Commands

Like Deadfish, 2Deadfish has a counter which starts at 0 and can be incremented, decremented, squared, and output. The counter also cannot be 256 or -1. In Deadfish, the program pointer always moves left-to-right. In 2Deadfish, it only starts out moving left-to-right.

Command Description
i Increment the counter and rotate the traversal direction 90 degrees clockwise.
s Square the counter and reverse the traversal direction.
d Decrement the counter and rotate the traversal direction 90 degrees counterclockwise.
o Output the counter's value and rotate the counter's direction 360 degrees / 0 degrees.

Any other characters will be ignored.

The playfield does not wrap around itself, and if the pointer exits the playfield, the code ends.

Examples

This is the equivalent of the iissso Deadfish test.

 i
si
 ds
si
 do

Deadfish equivalent: iisidsdisido

This is the equivalent of the diissisdo Deadfish test.

i  s
 iidi
ddssio
    dd

Deadfish equivalent: iddiisidsdiisiddo

In the spirit of Deadfish/Constants, I have made 2Deadfish/Constants which you can update.

Computational Class

This is a finite-state automaton because it only affects a single number which can have only a finite amount of states. In terms of instructions read, any 2Deadfish code has a corresponding Deadfish equivalent, but not vice versa (for example, it is impossible for any 2Deadfish code to start with d or s being the first command read unless that is the only command).

Interpreter

This interpreter is in Python 3. It can take in text files or string input and returns a Deadfish equivalent of the code which was run.

def run_2deadfish(code_string: str, file_name=False):
    # This is the code itself.
    code = []

    # This debug string prints the equivalent code in Deadfish
    debug_string = ''

    # You can use text files
    # Newline characters don't affect anything
    # Therefore, the code is different when using or not using a text file
    if file_name:
        with open(code_string, 'r') as file:
            code = file.readlines()
    else:
        code = code_string.split('\n')

    # This makes sure you can still go down
    # when one line is shorter than all the rest
    max_line = max([len(line) for line in code])

    pointer = (0, 0)
    # This is the actual counter.
    counter = 0
    # The direction is equal to direction * 90 degrees counterclockwise from right.
    # I couldn't make math.sin and math.cos work with this, even using radians.
    # It would say int(math.cos(radians(270))) was -1
    direction = 0
    while -1 < pointer[0] < len(code) and -1 < pointer[1] < max_line:
        row = pointer[0]
        column = pointer[1]
        if column < len(code[row]) and code[row][column] in 'diso':
            instruction = code[row][column]
            debug_string += instruction

            if instruction == 'o':
                print(counter)
            elif instruction == 'i':
                counter += 1
                direction += 1
            elif instruction == 's':
                counter **= 2
                direction += 2
            elif instruction == 'd':
                counter -= 1
                direction += 3

        # 4 * 90 = 360 degrees = 0 degrees
        direction %= 4

        if counter == 256 or counter == -1:
            counter = 0

        if direction == 0:
            pointer = (row, column+1)
        elif direction == 1:
            pointer = (row+1, column)
        elif direction == 2:
            pointer = (row, column-1)
        elif direction == 3:
            pointer = (row-1,column)

    return debug_string