i am trying to build a replay feature to my chess game.

The way the chess program works in a nutshell. is that i have an array of the chess pieces that are printed on a board in winforms.
here is a function:

public  void PrintPieces(Pieces [,] pieces)
        {      
            for (int i = 1; i < 9; i++)
            {             
                for (int j = 1; j < 9; j++)
                {
                    if (pieces[i, j] is Object )
                    {
                       chessPics[i, j].Load(pieces[i, j].print()); //print method has the path where the files are saved.                 **** nuller point exception is thrown, when the Replay method below is triggered
                    }
                    else
                    {
                        chessPics[i, j].Image = null;
                    }
                }             
            }
        }

now to reset the board , i put this method.
i built a control that resets the board:

private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            codeFile.resetBoard();// basically, sets the array in my code to the way a chess array should look like when the chess game starts.
            PrintPieces(codeFile.PieceState());
           
        }

it works.

now the problematic part, i used serialization to record the moves made.. this also worked.. but what didnt work is when i try to reset the board after i deserialize the class that is responsible for holding the List moves made throught the game.

okay, so as soon as there is Mate , thats what happens:

public void Lost()
        {
            MessageBox.Show("You have lost the game");
            DeSierializeNow();// the class is deserialized (trust me serialization part saved all the moves).
            ReplayGame();
        }
public void ReplayGame()
        {
            codeFile.resetBoard();// same code as in the control
            PrintPieces(codeFile.PieceState());// ****same code as in the control. it would throw a nuller exception
            List<Dictionary<int, int[]>> replay;

           
        
   
            replay=serializeMeh.giveBackDictionary();// the Serialized class has got that giveBackDictionary method. it simply returns the saved dictionary (it is a List, actually) with all the moves
            int[] makeSelfMoves=new int[4];

    

            int a = 0;
      

     
            //resetAll();
            foreach (Dictionary<int, int[]> item in replay)
            {
                a++;
               
                    makeSelfMoves = item[a];
                    codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
                    PrintPieces(codeFile.PieceState());
                    MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3] + "____a is: " + a);// this message printed me all the moves, when i didnt put the code that resets my board, first 2 statements at the beginning of the method..
             
            }
        }

i suspect that it is a serialization problem, cause no matter in which control i put these two statements:
codeFile.resetBoard();
PrintPieces(codeFile.PieceState());//

The board will reset itself.

another behavior that i dont understand, is if i feed the picturebox instance with a new constructor, (new Picturebox[9,9]) it wont give me a nullerpoint exception, but it wont reset the board either..

help,, i start pulling my hair out, i cant understand why it behaves the way it behaves

Basically, if i try to reset , the array, it tells me that i have a nuller point exception here:

PictureBox[,] chessPics = new PictureBox[9, 9];
        public  void PrintPieces(Pieces [,] pieces)
        {      
            for (int i = 1; i < 9; i++)
            {             
                for (int j = 1; j < 9; j++)
                {
                    if (pieces[i, j] is Object )
                    {
                       chessPics[i, j].Load(pieces[i, j].print());// nuller point exception always,it says that this is null (chessPics)              
                    }
                    else
                    {
                        chessPics[i, j].Image = null;
                    }
                }             
            }
        }

chessPics is a 2D array that can hold 81 PictureBox elements.
You still have to allocate these elements.
Example chessPics[0, 0] = new PictureBox(); will do that for the first element in your array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.