mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Clean up logic, names and strings
This commit is contained in:
@@ -5,145 +5,143 @@ var io = new ConsoleIO();
|
||||
|
||||
io.Write(Streams.Title);
|
||||
|
||||
var N = 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 A = new[] { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, 1, -1, -1, 1, 1, 1 };
|
||||
var _cells = new int[7, 7];
|
||||
var _willLive = new[] { 3, 102, 103, 120, 130, 121, 112, 111, 12, 21, 30, 1020, 1030, 1011, 1021, 1003, 1002, 1012 };
|
||||
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 Y = new int[3];
|
||||
int M2, M3;
|
||||
int _player1Count, _player2Count;
|
||||
|
||||
void L50()
|
||||
void CalculateNeighbors()
|
||||
{
|
||||
for (var j = 1; j <= 5; j++)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
io.WriteLine();
|
||||
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} ");
|
||||
io.Write($" {k % 6} ");
|
||||
}
|
||||
else if (k == 0 || k == 6)
|
||||
else if (k % 6 == 0)
|
||||
{
|
||||
if (j == 6) { io.WriteLine(" 0 "); return; }
|
||||
io.Write($" {j} ");
|
||||
io.Write($" {j % 6} ");
|
||||
}
|
||||
else
|
||||
{
|
||||
L300(j, k);
|
||||
CalculateAndDisplayCell(j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
void L200(int j, int k)
|
||||
void CalculateAndDisplayCell(int j, int k)
|
||||
{
|
||||
int B = N[j, k] > 999 ? 10 : 1;
|
||||
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)
|
||||
if (_cells[j, k] >= 3)
|
||||
{
|
||||
for (var O1 = 0; O1 < 18; O1++)
|
||||
{
|
||||
if (N[j, k] == K[O1])
|
||||
if (_cells[j, k] == _willLive[O1])
|
||||
{
|
||||
if (O1 < 9)
|
||||
{
|
||||
N[j, k] = 100; M2++; io.Write(" * ");
|
||||
return;
|
||||
_cells[j, k] = 100; _player1Count++; io.Write(" * ");
|
||||
}
|
||||
else
|
||||
{
|
||||
N[j, k] = 1000; M3++; io.Write(" # ");
|
||||
_cells[j, k] = 1000; _player2Count++; io.Write(" # ");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_cells[j, k] = 0;
|
||||
io.Write(" ");
|
||||
}
|
||||
|
||||
N[j, k] = 0; io.Write(" ");
|
||||
for (var _player = 1; _player <= 2; _player++)
|
||||
{
|
||||
var P1 = _player == 2 ? 30 : 3;
|
||||
io.WriteLine(Formats.InitialPieces, _player);
|
||||
for (var i = 1; i <= 3; i++)
|
||||
{
|
||||
ReadCoordinates(_player);
|
||||
_cells[X[_player], Y[_player]] = P1;
|
||||
}
|
||||
}
|
||||
|
||||
for (var j = 1; j <= 5; j++)
|
||||
{
|
||||
for (var k = 1; k <= 5; k++)
|
||||
{
|
||||
N[j, k] = 0;
|
||||
}
|
||||
}
|
||||
for (var B = 1; B <= 2; B++)
|
||||
{
|
||||
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();
|
||||
CalculateAndDisplayNext();
|
||||
|
||||
while (true)
|
||||
{
|
||||
io.WriteLine();
|
||||
L50();
|
||||
CalculateNeighbors();
|
||||
CalculateAndDisplayNext();
|
||||
|
||||
if (M2 == 0 && M3 == 0) { io.WriteLine(); io.WriteLine("A DRAW"); return; }
|
||||
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; }
|
||||
if (_player1Count == 0 || _player2Count == 0) { break; }
|
||||
|
||||
for (var B = 1; B <= 2; B++)
|
||||
for (var _player = 1; _player <= 2; _player++)
|
||||
{
|
||||
io.WriteLine();
|
||||
io.WriteLine();
|
||||
io.WriteLine($"PLAYER {B}");
|
||||
B = L700(B);
|
||||
if (B == 2) { N[X[1], Y[1]] = 100; N[X[2], Y[2]] = 1000; }
|
||||
io.WriteLine(Formats.Player, _player);
|
||||
if (ReadCoordinates(_player))
|
||||
{
|
||||
_cells[X[1], Y[1]] = 100;
|
||||
_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)
|
||||
{
|
||||
io.WriteLine("X,Y");
|
||||
var (y, x) = io.Read2Numbers("&&&&&&\r");
|
||||
(Y[B], X[B]) = ((int)y, (int)x);
|
||||
if (X[B] <= 5 && X[B] > 0 && Y[B] <= 5 && Y[B] > 0 && N[X[B], Y[B]] == 0)
|
||||
(Y[_player], X[_player]) = ((int)y, (int)x);
|
||||
if (X[_player] <= 5 && X[_player] > 0 && Y[_player] <= 5 && Y[_player] > 0 && _cells[X[_player], Y[_player]] == 0)
|
||||
{
|
||||
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");
|
||||
N[X[B] + 1, Y[B] + 1] = 0;
|
||||
B = 99;
|
||||
io.Write(Streams.SameCoords);
|
||||
// This is a bug existing in the original code. The line should be N[X[B], Y[B]] = 0;
|
||||
_cells[X[_player] + 1, Y[_player] + 1] = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
return B;
|
||||
return _player == 2;
|
||||
}
|
||||
2
56_Life_for_Two/csharp/Resources/Draw.txt
Normal file
2
56_Life_for_Two/csharp/Resources/Draw.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
A draw
|
||||
1
56_Life_for_Two/csharp/Resources/IllegalCoords.txt
Normal file
1
56_Life_for_Two/csharp/Resources/IllegalCoords.txt
Normal file
@@ -0,0 +1 @@
|
||||
Illegal coords. Retype
|
||||
2
56_Life_for_Two/csharp/Resources/InitialPieces.txt
Normal file
2
56_Life_for_Two/csharp/Resources/InitialPieces.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
Player {0} - 3 live pieces
|
||||
3
56_Life_for_Two/csharp/Resources/Player.txt
Normal file
3
56_Life_for_Two/csharp/Resources/Player.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
|
||||
Player {0}
|
||||
@@ -8,10 +8,16 @@ internal static class Resource
|
||||
internal static class Streams
|
||||
{
|
||||
public static Stream Title => GetStream();
|
||||
public static Stream Draw => GetStream();
|
||||
public static Stream IllegalCoords => GetStream();
|
||||
public static Stream SameCoords => GetStream();
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
1
56_Life_for_Two/csharp/Resources/SameCoords.txt
Normal file
1
56_Life_for_Two/csharp/Resources/SameCoords.txt
Normal file
@@ -0,0 +1 @@
|
||||
Same coord. Set to 0
|
||||
2
56_Life_for_Two/csharp/Resources/Winner.txt
Normal file
2
56_Life_for_Two/csharp/Resources/Winner.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
Player {0} is the winner
|
||||
Reference in New Issue
Block a user