User:BoundedBeans/INTERCAL Tic-Tac-Toe

From Esolang
Jump to navigation Jump to search

I wrote Tic-Tac-Toe in INTERCAL. It is a two-player game with alternating input, not a tic-tac-toe AI (I have no idea how to even make one).

How to use

The game will display the board, then whose turn it is. Enter the X coordinate, followed by the Y coordinate (ONE, TWO, or THREE for each). If those coordinates have already been played, you lose your turn. It repeats this cycle until someone wins, then displays "WIN" and halts (whoever had the last turn is the winner). You can end the game early by playing an invalid coordinate (it will display the VARIABLES MAY NOT BE STORED IN WEST HYPERSPACE error). There is no draw check.

Compatibility

This has only been tested on cringe, the INTERCAL compiler for .NET, but it can probably be run elsewhere. A few notes on this:

  • To get cringe working, I had to modify the declaration of the variable compiler in the EmitBinary method of spew.cs to have my specific path to csc, the C# compiler for Windows (.NET Framework). However, cringe does output valid cross-platform C# code, so you can alternatively compile that using any C# compiler as an extra step.
    • Edit: Confirmed to work on C-INTERCAL (finally got it running on WSL)
  • cringe uses wimpmode numerical I/O rather than spelled out digits/roman numerals, so that's why it looks like that in the example game. If you run it on another compiler you will have to input in spelled out digits. This game does not output numerically, so you don't have to worry about roman numerals at all.
  • cringe raises a lot of warnings when compiling this program; this is just because cringe raises warnings for comments (since they are syntax errors). The program compiles and runs fine, though.

Code

        DO NOTE === DECLARATIONS
        DO NOTE === GAME BOARD
                #1 = X
                #2 = O
                #3 = BLANK
        PLEASE ,10 <- #3 BY #3
        DO ,10SUB#1#1 <- #3
        DO ,10SUB#1#2 <- #3
        PLEASE ,10SUB#1#3 <- #3
        DO ,10SUB#2#1 <- #3
        DO ,10SUB#2#2 <- #3
        PLEASE ,10SUB#2#3 <- #3
        DO ,10SUB#3#1 <- #3
        DO ,10SUB#3#2 <- #3
        PLEASE ,10SUB#3#3 <- #3
        DO NOTE === #2 IF IT IS O'S TURN
        DO .10 <- #1
        
        DO NOTE === MAIN LOOP
(1)     DO FORGET #1
        PLEASE (10) NEXT
        DO (100) NEXT
        DO .10 <- #1
        DO (20) NEXT
        DO (30) NEXT
        PLEASE (10) NEXT
        DO (101) NEXT
        DO .10 <- #2
        DO (20) NEXT
        DO (30) NEXT
        PLEASE (1) NEXT

        DO NOTE === DISPLAY BOARD ROUTINE
                B: 01000010, 01000010, 66 , 190
                O: 01001111, 11110010, 242, 80
                A: 01000001, 10000010, 130, 368
                R: 01010010, 01001010, 74 , 312
                D: 01000100, 00100010, 34 , 296
                :: 00111010, 01011100, 92 , 198
               LF: 00001010, 01010000, 80 , 268
                |: 01111100, 00111110, 62 , 274
(10)    DO ,11 <- #33
        DO ,11SUB#1 <- #190
        PLEASE ,11SUB#2 <- #80
        DO ,11SUB#3 <- #368
        DO ,11SUB#4 <- #312
        DO ,11SUB#5 <- #296
        PLEASE ,11SUB#6 <- #198
        DO ,11SUB#7 <- #268
        DO ,11SUB#8 <- #274
        DO .1 <- ,10SUB#1#1
        PLEASE (40) NEXT
        DO ,11SUB#9 <- .2
        DO ,11SUB#10 <- .3
        DO .1 <- ,10SUB#1#2
        PLEASE (40) NEXT
        DO ,11SUB#11 <- .2
        DO ,11SUB#12 <- .3
        DO .1 <- ,10SUB#1#3
        PLEASE (40) NEXT
        DO ,11SUB#13 <- .2
        DO ,11SUB#14 <- .3
        PLEASE ,11SUB#15 <- #238
        DO ,11SUB#16 <- #274
        DO .1 <- ,10SUB#2#1
        PLEASE (40) NEXT
        DO ,11SUB#17 <- .2
        DO ,11SUB#18 <- .3
        DO .1 <- ,10SUB#2#2
        PLEASE (40) NEXT
        DO ,11SUB#19 <- .2
        DO ,11SUB#20 <- .3
        DO .1 <- ,10SUB#2#3
        PLEASE (40) NEXT
        DO ,11SUB#21 <- .2
        DO ,11SUB#22 <- .3
        PLEASE ,11SUB#23 <- #238
        DO ,11SUB#24 <- #274
        DO .1 <- ,10SUB#3#1
        PLEASE (40) NEXT
        DO ,11SUB#25 <- .2
        DO ,11SUB#26 <- .3
        DO .1 <- ,10SUB#3#2
        PLEASE (40) NEXT
        DO ,11SUB#27 <- .2
        DO ,11SUB#28 <- .3
        DO .1 <- ,10SUB#3#3
        PLEASE (40) NEXT
        DO ,11SUB#29 <- .2
        DO ,11SUB#30 <- .3
        DO ,11SUB#31 <- #238
        DO ,11SUB#32 <- #0
        PLEASE ,11SUB#33 <- #336
        DO READ OUT ,11
        DO RESUME #1
        
        DO NOTE === TURN
(20)    PLEASE WRITE IN .2
        DO ,11 <- #1
        DO ,11SUB#1 <- #0
        PLEASE READ OUT ,11
        DO WRITE IN .1
        DO .9 <- '?"',10SUB.1.2'$#3"'~#5
        DO (51) NEXT
        PLEASE .9 <- '#1$.9'~#3
        DO (21) NEXT
        DO RESUME #1
(21)    DO (22) NEXT
        DO ,10SUB.1.2 <- .10
        PLEASE RESUME #2
(22)    DO (23) NEXT
        DO THROW AWAY INVALID BOOLEANS. THIS ERROR SHOULD NEVER APPEAR.
(23)    PLEASE RESUME .9


        DO NOTE === CHECK FOR WIN
(30)    DO .1 <- #0
        DO .2 <- #1
        DO .9 <- "'?"',10SUB#1#1'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#1#2'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#1#3'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .1 <- "V'.1$.2'"~#1

        DO .2 <- #1
        DO .9 <- "'?"',10SUB#2#1'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#2#2'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#2#3'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .1 <- "V'.1$.2'"~#1

        DO .2 <- #1
        DO .9 <- "'?"',10SUB#3#1'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#3#2'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#3#3'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .1 <- "V'.1$.2'"~#1

        DO .2 <- #1
        DO .9 <- "'?"',10SUB#1#1'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#2#1'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#3#1'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .1 <- "V'.1$.2'"~#1

        DO .2 <- #1
        DO .9 <- "'?"',10SUB#1#2'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#2#2'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#3#2'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .1 <- "V'.1$.2'"~#1

        DO .2 <- #1
        DO .9 <- "'?"',10SUB#1#3'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#2#3'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#3#3'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .1 <- "V'.1$.2'"~#1

        DO .2 <- #1
        DO .9 <- "'?"',10SUB#1#1'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#2#2'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#3#3'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .1 <- "V'.1$.2'"~#1

        DO .2 <- #1
        DO .9 <- "'?"',10SUB#1#3'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#2#2'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .9 <- "'?"',10SUB#3#1'$.10"'~'#0$#65535'"~#65535
        PLEASE (50) NEXT
        DO .2 <- "&'.2$.9'"~#1
        DO .1 <- "V'.1$.2'"~#1

        DO .1 <- '#1$.1'~#3

        PLEASE (31) NEXT
        DO (10) NEXT
        DO (102) NEXT
        DO ,11 <- #2
        DO ,11SUB#1 <- #176
        PLEASE ,11SUB#2 <- #80
        DO READ OUT ,11
        DO GIVE UP
(31)    DO (32) NEXT
        PLEASE DO RESUME #2
(32)    DO (33) NEXT
        DO THROW AWAY INVALID BOOLEANS. THIS ERROR SHOULD NEVER APPEAR.
(33)    PLEASE DO RESUME .1


        DO NOTE === ID TO X/O/SPACE (AFTER VERTICAL BAR) AND FOLLOWING VERTICAL BAR
                LETTER:  X        O        [SPACE]  |        [LF]
                NORMAL:  01011000 01001111 00100000 01111100 00001010
                REVERSE: 00011010 11110010 00000100 00111110 01010000
                DECIMAL: 26       242      4        62       80
                -|:      36       -180     58       0        -18
                +256:    292      76       314      256      238

                | AFTER OTHER:
                         X:     26  - 62 + 256 = 220
                         O:     242 - 62 + 256 = 436
                         SPACE: 4   - 62 + 256 = 198
                         LF:    80  - 62 + 256 = 274
(40)    PLEASE (41) NEXT
        DO .2 <- #314
        DO .3 <- #198
        PLEASE RESUME #1
(41)    DO (42) NEXT
        DO .2 <- #76
        PLEASE .3 <- #436
        DO RESUME #2
(42)    DO (43) NEXT
        DO .2 <- #292
        PLEASE .3 <- #220
        DO RESUME #3
(43)    PLEASE RESUME .1

        DO NOTE === ZERO CHECK
(50)    DO (51) NEXT
        PLEASE .9 <- "?'.9$#1'"~#1
        DO RESUME #1

        DO NOTE === NON-ZERO CHECK
(51)    DO .9 <- '.9~.9'~#1
        PLEASE RESUME #1

        DO NOTE === "X"
(100)   DO ,11 <- #3
        DO ,11SUB#1 <- #230
        PLEASE ,11SUB#2 <- #202
        DO ,11SUB#3 <- #336
        DO READ OUT ,11
        DO RESUME #1

        DO NOTE === "O"
(101)   DO ,11 <- #3
        DO ,11SUB#1 <- #14
        PLEASE ,11SUB#2 <- #418
        DO ,11SUB#3 <- #336
        DO READ OUT ,11
        DO RESUME #1

        DO NOTE === "WIN"
                W: 01010111, 11101010, 234, -234, 22
                I: 01001001, 10010010, 146, 88  , 344
                N: 01001110, 01110010, 114, 32  , 288
             NULL: 00000000, 00000000, 0  , 114 , 370
(102)   PLEASE ,11 <- #4
        DO ,11SUB#1 <- #22
        DO ,11SUB#2 <- #344
        DO ,11SUB#3 <- #288
        PLEASE ,11SUB#4 <- #370
        DO READ OUT ,11
        DO RESUME #1


(60000)
        PLEASE NOTE:
                THIS DID NOT END UP BEING USED IN THE PROGRAM, BUT IT MAY BE INTERESTING AS AN INTERCAL STRATEGY.
                I DON'T KNOW WHETHER THE CODE WORKS, BUT THE IDEA MIGHT BE USEFUL.S
        DO NOTE:
                THE FOLLOWING LINES ARE BASICALLY A SIMULATION OF THE TERNARY OPERATOR.
                THE PURPOSE OF THEM IS SELECTING EITHER THE NEW VALUE OR THE OLD VALUE,
                DEPENDING ON THE VALUE OF .9, WHICH HOLDS #1 IF THE TILE IS BLANK, AND
                #0 OTHERWISE.

                WHAT I CAME UP WITH WAS THIS:
                1. USE REPEATED SELF-INTERLEAVING TO PRODUCE A WHOLE 16 BITS FILLED WITH EITHER
                        ZERO OR ONE. CALL THE RESULT B.
                2. SELECT THE 'TRUE' VALUE WITH B.
                3. CREATE NOT B BY INTERLEAVING B WITH A NUMBER WITH ALL BITS AS 1 (#65535),
                        USE UNARY XOR (X XOR TRUE = NOT X),
                        SELECT EVERY OTHER BIT,
                        THEN SELECT WITH #65535 TO UNIVERSALLY GUARANTEE THAT IT WILL BE
                        ONE-SPOT.
                4. SELECT THE FALSE VALUE WITH NOT B.
                NOTE: ONE OF THE RESULTS SHOULD BE ALL ZEROES, THE OTHER SHOULD CONTAIN THE DESIRED VALUE.
                5. GET THE FINAL VALUE BY INTERLEAVING THE RESULTS TOGETHER,
                        USING UNARY OR,
                        SELECTING EVERY OTHER BIT,
                        THEN SELECTING WITH #65535 TO UNIVERSALLY GUARANTEE THAT IT WILL BE
                        ONE-SPOT.
        PLEASE .9 <- '.9$.9'~#3
        DO .9 <- '.9$.9'~#15
        DO .9 <- '.9$.9'~#255
        DO .9 <- '.9$.9'~#65535
        DO ,10SUB.1.2 <- "'V"'.10~.9'$'"'&"',10SUB.1.2'$'"'?".9$#65535"'~'#0$#65535'"~#65535'"'~'#0$#65535'"~#65535'"'~'#0$#65535'"~#65535
        PLEASE RESUME .1

Example game

BOARD:
| | | |
| | | |
| | | |

 X
 1
 3
BOARD:
| | | |
| | | |
|X| | |

 O
 2
 3
BOARD:
| | | |
| | | |
|X|O| |

 X
 2
 2
BOARD:
| | | |
| |X| |
|X|O| |

 O
 3
 1
BOARD:
| | |O|
| |X| |
|X|O| |

 X
 1
 1
BOARD:
|X| |O|
| |X| |
|X|O| |

 O
 1
 2
BOARD:
|X| |O|
|O|X| |
|X|O| |

 X
 3
 3
WIN