I plan to create a command-line program with dotnet which uses a menu to get a user to decide what game they'd like to play (ive already made but not implemented tictactoe, and am trying to create an awful version of Snake) then plays them. I have three separate classes, one for the menu, one for Snake and one for Tictactoe (not implemented, not shown).
For Snake, I've tried to use System.Threading.Timer to constantly refresh the board state.
Menu Class:
using System;
using System.Threading;
namespace Boolean_Operators
{
public class Menu
{
static Snake SnakeGame = new Snake();
// var Tictactoe = new Tictactoe();
static void Main(string[] args)
{
RunGame();
}
static void RunGame()
{
Timer refreshTimer = new Timer(SnakeGame.PrintBoard, null, 0, 1000);
if(SnakeGame.invokeCount == SnakeGame.maxCount)
{
refreshTimer.Dispose();
}
}
}
}
Snake Class:
using System;
using System.Threading;
namespace Boolean_Operators
{
public class Snake
{
public int invokeCount = 0;
public int maxCount = 20;
string[,] board = new string[,]{
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",}
};
public void PrintBoard(object state)
{
invokeCount++;
for(int i = 0; i <= 10; i++)
{
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.Write($" {board[i, 0]} | {board[i, 1]} | {board[i, 2]} | {board[i, 3]} | {board[i, 4]} | {board[i, 5]} | {board[i, 6]} | {board[i, 7]} | {board[i, 8]} | {board[i, 9]} ");
Console.BackgroundColor = ConsoleColor.Black;
Console.Write("\n");
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.Write("-----+-----+-----+-----+-----+-----+-----+-----+-----+-----");
Console.BackgroundColor = ConsoleColor.Black;
Console.Write("\n");
}
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.Write($" {board[9, 0]} | {board[9, 1]} | {board[9, 2]} | {board[9, 3]} | {board[9, 4]} | {board[9, 5]} | {board[9, 6]} | {board[9, 7]} | {board[9, 8]} | {board[9, 9]} ");
Console.BackgroundColor = ConsoleColor.Black;
Console.Write("\n");
}
}
}
For whatever reason, all of the code appears to execute however the PrintBoard method code never executes, and when running the program nothing occurs:
I'm hoping the answer is something blindingly obvious I've missed since I've tried a lot of garbage stuff to try to get around it.