Created
February 17, 2021 16:44
-
-
Save ericlippert/89ec67a0aa56c37f0cd1dec8d631a0f7 to your computer and use it in GitHub Desktop.
1978 BASIC Life line-by-line ported to C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a line-for-line C# port of a 1978 BASIC implementation of Conway's Life; | |
// bugs are included as they were in the original program. | |
// See https://ericlippert.com/2021/02/17/life-part-38/ and | |
// https://ericlippert.com/2021/02/23/life-part-39/ for details. | |
using System; | |
using static System.Math; | |
public class Program | |
{ | |
static void TAB(int x) => Console.CursorLeft = x; | |
static void PRINT(string s = "") => Console.WriteLine(s); | |
static void PRINTSEMI(string s) => Console.Write(s); | |
static string INPUT() => Console.ReadLine(); | |
static string LEFT(string s, int length) => s.Substring(0, Min(length, s.Length)); | |
static string RIGHT(string s, int length) => s.Substring(s.Length - length); | |
static int LEN(string s) => s.Length; | |
static string MID(string s, int index, int length) => s.Substring(index - 1, length); | |
public static void Main() | |
{ | |
TAB(34); | |
PRINT("LIFE"); | |
TAB(15); | |
PRINT("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); | |
PRINT(); | |
PRINT(); | |
PRINT(); | |
PRINT("ENTER YOUR PATTERN:"); | |
int X1 = 1; | |
int Y1 = 1; | |
int X2 = 24; | |
int Y2 = 70; | |
int[,] A = new int[24 + 1, 70 + 1]; | |
string[] B = new string[24 + 1]; | |
int C = 1; | |
_30: | |
B[C] = INPUT(); | |
if (B[C] == "DONE") | |
{ | |
B[C] = ""; | |
goto _80; | |
} | |
if (LEFT(B[C], 1) == ".") | |
{ | |
B[C] = " " + RIGHT(B[C], LEN(B[C]) - 1); | |
} | |
C = C + 1; | |
goto _30; | |
_80: | |
C = C - 1; | |
int L = 0; | |
int P = 0; | |
for (int X = 1; X <= C - 1; X += 1) | |
{ | |
if (LEN(B[X]) > L) | |
{ | |
L = LEN(B[X]); | |
} | |
} | |
X1 = 11 - C / 2; | |
Y1 = 33 - L / 2; | |
for (int X = 1; X <= C; X += 1) | |
{ | |
for (int Y = 1; Y <= LEN(B[X]); Y += 1) | |
{ | |
if (MID(B[X], Y, 1) != " ") | |
{ | |
A[X1 + X, Y1 + Y] = 1; | |
P = P + 1; | |
} | |
} | |
} | |
PRINT(); | |
PRINT(); | |
PRINT(); | |
int G = 0; | |
int I9 = 0; | |
_210: | |
PRINTSEMI("GENERATION:" + G + "POPULATION:" + P); | |
if (I9 != 0) | |
{ | |
PRINTSEMI("INVALID!"); | |
} | |
int X3 = 24; | |
int Y3 = 70; | |
int X4 = 1; | |
int Y4 = 1; | |
P = 0; | |
G = G + 1; | |
for (int X = 1; X <= X1 - 1; X += 1) | |
{ | |
PRINT(); | |
} | |
for (int X = X1; X <= X2; X += 1) | |
{ | |
PRINT(); | |
for (int Y = Y1; Y <= Y2; Y += 1) | |
{ | |
if (A[X, Y] == 2) | |
{ | |
A[X, Y] = 0; | |
goto _270; | |
} | |
if (A[X, Y] == 3) | |
{ | |
A[X, Y] = 1; | |
goto _261; | |
} | |
if (A[X, Y] != 1) | |
{ | |
goto _270; | |
} | |
_261: | |
TAB(Y); | |
PRINTSEMI("*"); | |
if (X < X3) | |
{ | |
X3 = X; | |
} | |
if (X > X4) | |
{ | |
X4 = X; | |
} | |
if (Y < Y3) | |
{ | |
Y3 = Y; | |
} | |
if (Y > Y4) | |
{ | |
Y4 = Y; | |
} | |
_270:; | |
} | |
} | |
for (int X = X2 + 1; X <= 24; X += 1) | |
{ | |
PRINT(); | |
} | |
X1 = X3; | |
X2 = X4; | |
Y1 = Y3; | |
Y2 = Y4; | |
if (X1 < 3) | |
{ | |
X1 = 3; | |
I9 = -1; | |
} | |
if (X2 > 22) | |
{ | |
X2 = 22; | |
I9 = -1; | |
} | |
if (Y1 < 3) | |
{ | |
Y1 = 3; | |
I9 = -1; | |
} | |
if (Y2 > 68) | |
{ | |
Y2 = 68; | |
I9 = -1; | |
} | |
P = 0; | |
for (int X = X1 - 1; X <= X2 + 1; X += 1) | |
{ | |
for (int Y = Y1 - 1; Y <= Y2 + 1; Y += 1) | |
{ | |
C = 0; | |
for (int I = X - 1; I <= X + 1; I += 1) | |
{ | |
for (int J = Y - 1; J <= Y + 1; J += 1) | |
{ | |
if (A[I, J] == 1 || A[I, J] == 2) | |
{ | |
C = C + 1; | |
} | |
} | |
} | |
if (A[X, Y] == 0) | |
{ | |
goto _610; | |
} | |
if (C < 3 || C > 4) | |
{ | |
A[X, Y] = 2; | |
goto _600; | |
} | |
P = P + 1; | |
_600: | |
goto _620; | |
_610: | |
if (C == 3) | |
{ | |
A[X, Y] = 3; | |
P = P + 1; | |
} | |
_620:; | |
} | |
} | |
X1 = X1 - 1; | |
Y1 = Y1 - 1; | |
X2 = X2 + 1; | |
Y2 = Y2 + 1; | |
goto _210; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment