# Turnfunge

Turnfunge is a fungeoid that is a minimalization of Nopfunge Solid (designed by Keymaker in 2023), which is based on Nopfunge (designed by Hubert Lamontagne in 2015).

Turnfunge takes Nopfunge Solid into a more minimal direction and on the way sheds certain core aspects of Nopfunge that were preserved in Nopfunge Solid. A Nopfunge Solid program (a Minsky Machine to Nopfunge Solid translation) translated into Turnfunge. The simulated program has halted and entered an infinite loop to signify it. The small red dot is the instruction pointer.

## Program and execution

As in Nopfunge Solid, the playfield is a two-dimensional area that repeats infinitely to right and down. It consists of cells that are either empty (space character) or solid (any other character). The program defines the dimensions and contents of this repeating area, like in Nopfunge Solid. There is an instruction pointer that initially is in 0,0 (topmost, leftmost cell) of the topmost, leftmost copy. It has four possible directions (right, down, left, up) and it is initially set to 'right', like in Nopfunge Solid.

Like in Nopfunge Solid, one cycle consists of first (possibly) altering the pointer's direction, then moving the pointer into its direction. Where the language differs from Nopfunge Solid is the way the pointer is affected: not by the cell that is under the pointer, but by the cell that is behind the pointer. Behind the pointer is defined as the cell that is one step from the pointer's location in the direction opposite of the pointer's current direction. The cell behind the pointer may be in another copy if the pointer is on an edge. If the cell behind is out of bounds, as it is when the program begins, the cell is read as empty. If the cell behind the pointer (for instance, one above the pointer if the pointer's direction is 'down') is empty, the direction is unaffected. If the cell behind the pointer is solid, then the pointer's direction is rotated 90 degrees clockwise ('right' becomes 'down', 'down' becomes 'left', and so on). Then the pointer is moved one step into its direction.

The edge cases are almost like in Nopfunge Solid but slightly different. During the cycle, after the pointer's direction is possibly altered and it is ready to move, the edges are checked. If the pointer is on the topmost row of the topmost copies and its direction is 'up', then its direction is reversed ('up' becomes 'down'). Similarly, if the pointer is on the leftmost column of the leftmost copies and its direction is 'left', its direction is reversed to 'right'. After this, the pointer is moved into its direction.

The program never halts. The pointer will always move to another cell.

## Computational class

The language is Turing-complete, which is proven via Nopfunge Solid to Turnfunge translation. The translation method generates a structure that simulates the Nopfunge Solid program. (See link for translator in external resources.)

## Example translation

Here is a simple Nopfunge Solid program that does the following. The program starts at 0,0 with direction 'right'. The cell under the pointer is `v` so the direction is set to 'down'. Then pointer is moved one down. In cell 0,1 there is space, so the direction stays the same and the pointer moves to 0,2. There the pointer is set to 'right' and is moved to 1,2. There it is set 'down' and is moved to 1,0 in the copy below on y-axis. 1,0 is space so the direction stays the same and the pointer moves to 1,1 which is `>` which sets the direction 'right' and moves it to 2,1. There the pointer is set to move 'up' and moves up until moving to the above copy on y-axis and warping to 2,2 and then moving up again until at 2,0 the edge is detected and the direction is set to 'right' and the pointer is moved to 3,0 which has the halting extension (which in the translation becomes an infinite loop).

```v  .
>^
>v
```

The translator creates this:

```y              n          n          n          n
a     oo   a     oo   a     oo   a     oo
o  o       o  o       o  o       o  o
aa         aa         aa         aa
a  ww      a  ww      a  ww      a  ww
w          w          w          w
oo         oo         oo         oo
y          o          o          o          o
o                                o
a o nn     a   nn     a   nn     a o nn
aa           n          n          n          n
o  a            x          x          x          x
oo   o    a          a          a          a      o
x oo       o                                o
o    a          a          a          a
o         a          a          a          a
o
a   nn     a o nn     a   nn     a   nn
aa           n          n          n          n
o  a            x          x          x          x
oo   o    a          a      o   a      o   a
x oo                  o          o
o    a          a          a          a
o         a          a          a          a
o
a o nn     a o nn     a   nn     a   nn
aa           n          n          n          n
o  a            x          x          x          x
oo   o    a      o   a          a          a
x oo       o          o
o    a          a          a          a
o         a          a          a          a
```

The solid cells could all be the same character, the translation uses different characters for clarity. There is a simple visual interpreter available, see external resources. This program can be copied into it and its execution inspected.

## External resources

• Turnfunge page (detailed explanations of the language, translator program, visual interpreter in Javascript)