My class instance is null when initiated

  • Thread starter Thread starter Miwse
  • Start date Start date
M

Miwse

Guest
Hello

So I am trying to play around with a tic tac toe validation principle, nvm - what I am trying to say, I am not interested in any 'game' mechanics.
(If below does not tell you enough, then I have uploaded the project here: Uploadfiles.io - Download tictacktoe-cls.zip for free )
So I have 3 classes that holds all the game logic;
GameBoard.cs:

namespace tic_tac_toe
{
public enum Player
{
X = 1,
O = 2,
Open = 0,
}

/// <summary>
/// Abstract class for creating new game boards, contains all of the logic needed for the AI to play the game.
/// </summary>
public abstract class GameBoard
{
/// <summary>
/// Array that contains all of the spaces on the board.
/// </summary>
public Player[,] squares;

/// <summary>
/// Gets or sets a space on the board.
/// </summary>
public abstract Player this[int x, int y] { get; set; }

/// <summary>
/// Determines if all spaces on the board are full.
/// </summary>
public abstract bool IsFull { get; }
etc...


TicTacToeBoard.cs:

namespace tic_tac_toe
{
/// <summary>
/// Contains all of the functions and logic needed for the game of TicTacToe
/// </summary>
class TicTacToeBoard : GameBoard
{
public TicTacToeBoard()
{
squares = new Player[3, 3] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
}

public override int Size { get { return 9; } }

public override Player this[int x, int y]
{
get
{
return squares[x, y];
}

set
{
squares[x, y] = value;
}
}

public override bool IsFull
{
get
{
foreach (Player i in squares)
if (i == Player.Open) { return false; }
return true;
}
}

etc...

AI.cs:

namespace tic_tac_toe
{
class AI
{
/// <summary>
/// Implementation of the minimax algorithm. Determines the best move for the current
/// board by playing every move combination until the end of the game.
/// </summary>
public static Space GetBestMove(GameBoard gb, Player p)
{
Space? bestSpace = null;
List<Space> openSpaces = gb.OpenSquares;
GameBoard newBoard;

for (int i = 0; i < openSpaces.Count; i++)
{
newBoard = gb.Clone();
Space newSpace = openSpaces;

newBoard[newSpace.X, newSpace.Y] = p;

if (newBoard.Winner == Player.Open && newBoard.OpenSquares.Count > 0)
{
Space tempMove = GetBestMove(newBoard, ((Player)(-(int)p))); //a little hacky, inverts the current player
newSpace.Rank = tempMove.Rank;
}

etc:


My main program to test the above classes:

namespace tic_tac_toe
{
public class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main(string[] args)
{
GameBoard gb;

//Space s = new Space(0, 0);
gb[1, 1] = Player.O;
//gb[s.X, s.Y] = Player.O;
Console.WriteLine(gb.OpenSquares.ToString());


In my main the Gameboard instance is unassigned, what is it that I am missing since I am unable to get the class to initiate properly?
Error at gb[1,1] .... first time accesses the class properties it throws:

Severity Code Description Project File Line Suppression State
Error CS0165 Use of unassigned local variable 'gb'


I hope I provided enough information above, as it's probably something to do with the class access modifiers?

thank you in advance, I hate being stuck :)

Continue reading...
 
Back
Top