none
My class instance is null when initiated RRS feed

  • Question

  • 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: https://ufile.io/f1wps )
    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[i];
    
            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 :)


    • Edited by Miwse Saturday, February 23, 2019 3:18 PM linked to project
    Saturday, February 23, 2019 2:57 PM

Answers

  • Try replacing

       GameBoard gb;

    with

       GameBoard gb = new TicTacToeBoard( );

    or with

       TicTacToeBoard gb = new TicTacToeBoard( );

    • Proposed as answer by BonnieBMVP, Moderator Saturday, February 23, 2019 5:48 PM
    • Marked as answer by Miwse Sunday, February 24, 2019 10:50 PM
    Saturday, February 23, 2019 3:34 PM

All replies

  • Try replacing

       GameBoard gb;

    with

       GameBoard gb = new TicTacToeBoard( );

    or with

       TicTacToeBoard gb = new TicTacToeBoard( );

    • Proposed as answer by BonnieBMVP, Moderator Saturday, February 23, 2019 5:48 PM
    • Marked as answer by Miwse Sunday, February 24, 2019 10:50 PM
    Saturday, February 23, 2019 3:34 PM
  • Thanks mate, you were right - can't see how I missed it :) or didn't try it.
    Sunday, February 24, 2019 10:51 PM