✅ Trying to add tic tac toe board snapshot to variable for display of full match later

Code: https://paste.ofcode.org/n4KiPrNa8pyHiyWGuks93e I'm trying to check some weird nonsense with ttt, specifically automating games to check some data. Part of that was seeing how the game behaves when you can overwrite symbols but your moves are random. For that, I'm trying to keep a snapshot of every move. I have tried it with just outputting which cell it had overwritten, but because of my implementation that doesn't show the cell number if there was already a mark placed. I can't seem to find a way to add the Board() output into a variable for later calling
7 Replies
Angius
Angius5mo ago
That's because Board currently prints the board to the console, only What you would do, is first create a string variable with the board, then add it to a list for snapshotting, and then print it out Something like
private static List<string> _snapshots = new();

private static void Board()
{
var board = $"""
| |
{arr[1]} | {arr[2]} | {arr[3]}
_____|_____|_____
| |
{arr[4]} | {arr[5]} | {arr[6]}
_____|_____|_____
| |
{arr[7]} | {arr[8]} | {arr[9]}
| |
""";
_snapshots.Add(board);
Console.WriteLine(board);
}
private static List<string> _snapshots = new();

private static void Board()
{
var board = $"""
| |
{arr[1]} | {arr[2]} | {arr[3]}
_____|_____|_____
| |
{arr[4]} | {arr[5]} | {arr[6]}
_____|_____|_____
| |
{arr[7]} | {arr[8]} | {arr[9]}
| |
""";
_snapshots.Add(board);
Console.WriteLine(board);
}
Nomnomfighter
NomnomfighterOP5mo ago
Ah cool, thanks! Actually testing it, I get an System.Collections.Generic.List`1[System.String] error. A quick search doesn't tell me much (too much of a beginner to understand how to fix the problem)
Buddy
Buddy5mo ago
Instead of a string I'd recommend saving the moves only and not the whole game render A collection cannot be printed as a string. It must be iterated
Angius
Angius5mo ago
What error? Ah, printing Not an error, just calling the default .ToString() method of object
Nomnomfighter
NomnomfighterOP5mo ago
I did that before, but I'd have to rework the placing logic to do that. Currently, if it would land on a field that already has a symbol, it would not tell me which of the fields it has overridden, just the symbol that field had before
MODiX
MODiX5mo ago
Angius
REPL Result: Success
var s = new List<string>() { "a", "b", "c", "d", "e" };
Console.WriteLine(s); // no worky
Console.WriteLine(string.Join(", ", s)); // worky
foreach (var l in s)
{
Console.WriteLine(l); // worky
}
var s = new List<string>() { "a", "b", "c", "d", "e" };
Console.WriteLine(s); // no worky
Console.WriteLine(string.Join(", ", s)); // worky
foreach (var l in s)
{
Console.WriteLine(l); // worky
}
Console Output
System.Collections.Generic.List`1[System.String]
a, b, c, d, e
a
b
c
d
e
System.Collections.Generic.List`1[System.String]
a, b, c, d, e
a
b
c
d
e
Compile: 475.646ms | Execution: 46.854ms | React with ❌ to remove this embed.
Nomnomfighter
NomnomfighterOP5mo ago
Nice, that works now!
Want results from more Discord servers?
Add your server