Clean up logic, names and strings

This commit is contained in:
Andrew Cooper
2022-08-20 21:33:25 +10:00
parent fafddfbf1a
commit 48eda0785d
8 changed files with 82 additions and 67 deletions

View File

@@ -5,145 +5,143 @@ var io = new ConsoleIO();
io.Write(Streams.Title); io.Write(Streams.Title);
var N = new int[7, 7]; var _cells = new int[7, 7];
var K = new[] { 3, 102, 103, 120, 130, 121, 112, 111, 12, 21, 30, 1020, 1030, 1011, 1021, 1003, 1002, 1012 }; var _willLive = new[] { 3, 102, 103, 120, 130, 121, 112, 111, 12, 21, 30, 1020, 1030, 1011, 1021, 1003, 1002, 1012 };
var A = new[] { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, 1, -1, -1, 1, 1, 1 }; var _offsets = new[] { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, 1, -1, -1, 1, 1, 1 };
var X = new int[3]; var X = new int[3];
var Y = new int[3]; var Y = new int[3];
int M2, M3; int _player1Count, _player2Count;
void L50() void CalculateNeighbors()
{ {
for (var j = 1; j <= 5; j++) for (var j = 1; j <= 5; j++)
{ {
for (var k = 1; k <= 5; k++) for (var k = 1; k <= 5; k++)
{ {
if (N[j, k] > 99) if (_cells[j, k] > 99)
{ {
L200(j, k); int B = _cells[j, k] > 999 ? 10 : 1;
for (var o = 0; o < 15; o += 2)
{
_cells[j + _offsets[o], k + _offsets[o + 1]] += B;
}
} }
} }
} }
L90();
} }
void L90() void CalculateAndDisplayNext()
{ {
M2 = M3 = 0; _player1Count = _player2Count = 0;
for (var j = 0; j <= 6; j++) for (var j = 0; j <= 6; j++)
{ {
io.WriteLine(); io.WriteLine();
for (var k = 0; k <= 6; k++) for (var k = 0; k <= 6; k++)
{ {
if (j == 0 || j == 6) if (j % 6 == 0)
{ {
if (k == 6) { io.Write(" 0 "); break; } io.Write($" {k % 6} ");
io.Write($" {k} ");
} }
else if (k == 0 || k == 6) else if (k % 6 == 0)
{ {
if (j == 6) { io.WriteLine(" 0 "); return; } io.Write($" {j % 6} ");
io.Write($" {j} ");
} }
else else
{ {
L300(j, k); CalculateAndDisplayCell(j, k);
} }
} }
} }
return; return;
}
} void CalculateAndDisplayCell(int j, int k)
void L200(int j, int k)
{ {
int B = N[j, k] > 999 ? 10 : 1; if (_cells[j, k] >= 3)
for (var O1 = 0; O1 < 15; O1 += 2)
{
N[j + A[O1], k + A[O1 + 1]] = N[j + A[O1], k + A[O1 + 1]] + B;
}
}
void L300(int j, int k)
{
if (N[j, k] >= 3)
{ {
for (var O1 = 0; O1 < 18; O1++) for (var O1 = 0; O1 < 18; O1++)
{ {
if (N[j, k] == K[O1]) if (_cells[j, k] == _willLive[O1])
{ {
if (O1 < 9) if (O1 < 9)
{ {
N[j, k] = 100; M2++; io.Write(" * "); _cells[j, k] = 100; _player1Count++; io.Write(" * ");
return;
} }
else else
{ {
N[j, k] = 1000; M3++; io.Write(" # "); _cells[j, k] = 1000; _player2Count++; io.Write(" # ");
return;
} }
return;
} }
} }
} }
N[j, k] = 0; io.Write(" "); _cells[j, k] = 0;
io.Write(" ");
} }
for (var j = 1; j <= 5; j++) for (var _player = 1; _player <= 2; _player++)
{ {
for (var k = 1; k <= 5; k++) var P1 = _player == 2 ? 30 : 3;
io.WriteLine(Formats.InitialPieces, _player);
for (var i = 1; i <= 3; i++)
{ {
N[j, k] = 0; ReadCoordinates(_player);
_cells[X[_player], Y[_player]] = P1;
} }
} }
for (var B = 1; B <= 2; B++)
{ CalculateAndDisplayNext();
var P1 = B == 2 ? 30 : 3;
io.WriteLine(); io.WriteLine($"PLAYER {B} - 3 LIVE PIECES.");
for (var K1 = 1; K1 <= 3; K1++)
{
L700(B);
N[X[B], Y[B]] = P1;
}
}
L90();
while (true) while (true)
{ {
io.WriteLine(); io.WriteLine();
L50(); CalculateNeighbors();
CalculateAndDisplayNext();
if (M2 == 0 && M3 == 0) { io.WriteLine(); io.WriteLine("A DRAW"); return; } if (_player1Count == 0 || _player2Count == 0) { break; }
else if (M3 == 0) { var B = 1; io.WriteLine(); io.WriteLine($"PLAYER {B} IS THE WINNER"); return; }
else if (M2 == 0) { var B = 2; io.WriteLine($"PLAYER {B} IS THE WINNER"); return; }
for (var B = 1; B <= 2; B++) for (var _player = 1; _player <= 2; _player++)
{ {
io.WriteLine(); io.WriteLine(Formats.Player, _player);
io.WriteLine(); if (ReadCoordinates(_player))
io.WriteLine($"PLAYER {B}"); {
B = L700(B); _cells[X[1], Y[1]] = 100;
if (B == 2) { N[X[1], Y[1]] = 100; N[X[2], Y[2]] = 1000; } _cells[X[2], Y[2]] = 1000;
}
} }
} }
int L700(int B) if (_player1Count == 0 && _player2Count == 0)
{
io.Write(Streams.Draw);
}
else
{
io.WriteLine(Formats.Winner, _player2Count == 0 ? 1 : 2);
}
bool ReadCoordinates(int _player)
{ {
while (true) while (true)
{ {
io.WriteLine("X,Y"); io.WriteLine("X,Y");
var (y, x) = io.Read2Numbers("&&&&&&\r"); var (y, x) = io.Read2Numbers("&&&&&&\r");
(Y[B], X[B]) = ((int)y, (int)x); (Y[_player], X[_player]) = ((int)y, (int)x);
if (X[B] <= 5 && X[B] > 0 && Y[B] <= 5 && Y[B] > 0 && N[X[B], Y[B]] == 0) if (X[_player] <= 5 && X[_player] > 0 && Y[_player] <= 5 && Y[_player] > 0 && _cells[X[_player], Y[_player]] == 0)
{ {
break; break;
} }
io.WriteLine("Illegal Coords. Retype"); io.Write(Streams.IllegalCoords);
} }
if (B == 2 && X[1] == X[2] && Y[1] == Y[2]) if (_player == 2 && X[1] == X[2] && Y[1] == Y[2])
{ {
io.WriteLine("SAME COORD. SET TO 0"); io.Write(Streams.SameCoords);
N[X[B] + 1, Y[B] + 1] = 0; // This is a bug existing in the original code. The line should be N[X[B], Y[B]] = 0;
B = 99; _cells[X[_player] + 1, Y[_player] + 1] = 0;
return false;
} }
return B; return _player == 2;
} }

View File

@@ -0,0 +1,2 @@
A draw

View File

@@ -0,0 +1 @@
Illegal coords. Retype

View File

@@ -0,0 +1,2 @@
Player {0} - 3 live pieces

View File

@@ -0,0 +1,3 @@
Player {0}

View File

@@ -8,10 +8,16 @@ internal static class Resource
internal static class Streams internal static class Streams
{ {
public static Stream Title => GetStream(); public static Stream Title => GetStream();
public static Stream Draw => GetStream();
public static Stream IllegalCoords => GetStream();
public static Stream SameCoords => GetStream();
} }
internal static class Formats internal static class Formats
{ {
public static string InitialPieces => GetString();
public static string Player => GetString();
public static string Winner => GetString();
} }
private static string GetString([CallerMemberName] string? name = null) private static string GetString([CallerMemberName] string? name = null)

View File

@@ -0,0 +1 @@
Same coord. Set to 0

View File

@@ -0,0 +1,2 @@
Player {0} is the winner