EDN Admin
Well-known member
Hi, I am creating a game on C# and windows form, and i am getting an error and it is "Object reference not set to an instance of an object."
and i am not sure why
here is all the code and the problem in bold!
thanks using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Portfolio_5
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Button btn; //Get the data that has been clicked by the button
int turn; //to find out whos turn it is
int counter;
bool[,] X/*X Player Buttons*/, O/*O Player Buttons*/, cells/*Ocupied Cells*/, Current/*X or O*/;
string player; //chose the player X or O
bool finished; //True when the game has finished
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
int x, y;
//Make sure that the user has clicked a button
if (!wasClickedBefore(sender as Button, out x, out y) && !finished)
{
btn = (sender as Button); //get the button that has been clicked.
if (turn == 0)
{
(sender as Button).ForeColor = Color.Blue; //change that colour to blue
player = "X";//Player X is blue
X[x, y] = true; //that position will now be ocupied by an X
Current = X;
}
else
{
(sender as Button).ForeColor = Color.Red;//Chnage that colour to red
player = "O"; //This is the player O
O[x, y] = true; //That position will now be occupied by an O
Current = O;
}
(sender as Button).Text = player;
cells[x, y] = true; //The cell will now be no longer empty
turn = (turn + 1) % 2; //This will switch the turns over and let people give each other a go
//If a win accours
if (counter >= 5)
//Check for a win
if (Row(0) || Row(1) || Row(2) || Column(0) || Column(1) || Column(2) || Diagonals())
{
//Updating Score
if (player == "X")
xLabel.Text = (int.Parse(xLabel.Text) + 1).ToString();
else
OLabel.Text = (int.Parse(OLabel.Text) + 1).ToString();
finished = true; //The game has finished
MessageBox.Show(string.Format("{0} Wins!", player));//Showing Result
}
if (counter == 9 && !finished) // this is a draw
{
finished = true;
MessageBox.Show("Draw");
}
}
}
//Making sure that it was never clicked before
private bool wasClickedBefore(Button button, out int x, out int y)
{
x = y = 0;
switch (button.Name) //THE PROBLEM IS HERE
{
case "button1": x = 0; y = 0; break;
case "button2": x = 0; y = 1; break;
case "button3": x = 0; y = 2; break;
case "button4": x = 1; y = 0; break;
case "button5": x = 1; y = 1; break;
case "button6": x = 1; y = 2; break;
case "button7": x = 2; y = 0; break;
case "button8": x = 2; y = 1; break;
case "button9": x = 2; y = 2; break;
}
return (X[x, y] || O[x, y]);
}
private bool Diagonals()
{
return (
(Current[0, 0] && Current[1, 1] && Current[2, 2])
||
(Current[0, 2] && Current[1, 1] && Current[2, 0])
);
}
private bool Row(int p)
{
return (Current[p, 0] && Current[p, 1] && Current[p, 2]);
}
private bool Column(int p)
{
return (Current[0, p] && Current[1, p] && Current[2, p]);
}
private void resetVariables()
{
finished = false;
turn = 0;
O = new bool[3, 3];
X = new bool[3, 3];
cells = new bool[3, 3];
counter = 0;
}
private void Form2_Load(object sender, EventArgs e)
{
resetVariables();
}
private void ResetBtn_Click(object sender, EventArgs e)
{
//Clear all the buttons of text
button1.Text =
button2.Text =
button3.Text =
button4.Text =
button5.Text =
button6.Text =
button7.Text =
button8.Text =
button9.Text =
"";
resetVariables();
}
private void backbtn_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f1 = new Form1();
f1.Show();
}
//Checking rows and columns
}
}
View the full article
and i am not sure why
here is all the code and the problem in bold!
thanks using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Portfolio_5
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Button btn; //Get the data that has been clicked by the button
int turn; //to find out whos turn it is
int counter;
bool[,] X/*X Player Buttons*/, O/*O Player Buttons*/, cells/*Ocupied Cells*/, Current/*X or O*/;
string player; //chose the player X or O
bool finished; //True when the game has finished
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
int x, y;
//Make sure that the user has clicked a button
if (!wasClickedBefore(sender as Button, out x, out y) && !finished)
{
btn = (sender as Button); //get the button that has been clicked.
if (turn == 0)
{
(sender as Button).ForeColor = Color.Blue; //change that colour to blue
player = "X";//Player X is blue
X[x, y] = true; //that position will now be ocupied by an X
Current = X;
}
else
{
(sender as Button).ForeColor = Color.Red;//Chnage that colour to red
player = "O"; //This is the player O
O[x, y] = true; //That position will now be occupied by an O
Current = O;
}
(sender as Button).Text = player;
cells[x, y] = true; //The cell will now be no longer empty
turn = (turn + 1) % 2; //This will switch the turns over and let people give each other a go
//If a win accours
if (counter >= 5)
//Check for a win
if (Row(0) || Row(1) || Row(2) || Column(0) || Column(1) || Column(2) || Diagonals())
{
//Updating Score
if (player == "X")
xLabel.Text = (int.Parse(xLabel.Text) + 1).ToString();
else
OLabel.Text = (int.Parse(OLabel.Text) + 1).ToString();
finished = true; //The game has finished
MessageBox.Show(string.Format("{0} Wins!", player));//Showing Result
}
if (counter == 9 && !finished) // this is a draw
{
finished = true;
MessageBox.Show("Draw");
}
}
}
//Making sure that it was never clicked before
private bool wasClickedBefore(Button button, out int x, out int y)
{
x = y = 0;
switch (button.Name) //THE PROBLEM IS HERE
{
case "button1": x = 0; y = 0; break;
case "button2": x = 0; y = 1; break;
case "button3": x = 0; y = 2; break;
case "button4": x = 1; y = 0; break;
case "button5": x = 1; y = 1; break;
case "button6": x = 1; y = 2; break;
case "button7": x = 2; y = 0; break;
case "button8": x = 2; y = 1; break;
case "button9": x = 2; y = 2; break;
}
return (X[x, y] || O[x, y]);
}
private bool Diagonals()
{
return (
(Current[0, 0] && Current[1, 1] && Current[2, 2])
||
(Current[0, 2] && Current[1, 1] && Current[2, 0])
);
}
private bool Row(int p)
{
return (Current[p, 0] && Current[p, 1] && Current[p, 2]);
}
private bool Column(int p)
{
return (Current[0, p] && Current[1, p] && Current[2, p]);
}
private void resetVariables()
{
finished = false;
turn = 0;
O = new bool[3, 3];
X = new bool[3, 3];
cells = new bool[3, 3];
counter = 0;
}
private void Form2_Load(object sender, EventArgs e)
{
resetVariables();
}
private void ResetBtn_Click(object sender, EventArgs e)
{
//Clear all the buttons of text
button1.Text =
button2.Text =
button3.Text =
button4.Text =
button5.Text =
button6.Text =
button7.Text =
button8.Text =
button9.Text =
"";
resetVariables();
}
private void backbtn_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f1 = new Form1();
f1.Show();
}
//Checking rows and columns
}
}
View the full article