5 Replies - 2941 Views - Last Post: 10 April 2012 - 09:17 AM Rate Topic: -----

#1 azislovely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-April 12

error in check method of tic tac toe game project

Posted 10 April 2012 - 07:04 AM

i got a error >>
An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.b'

static bool is_Row()
{
    int CurX;
    int CurY;

    for(CurY = 0; CurY < 3; CurY++) /* Rows */
        if(b[CurY*3].Text == b[1 + CurY*3].Text == b[2 + CurY*3].Text)
            return true;

    for(CurX = 0; CurX < 3; CurX++) /* Columns */
        if(b[CurX].Text == b[CurX + 1*3] == b[CurX + 2*3])
            return true;

 if(b[0].Text == b[4].Text == b[8].Text ||
       b[2].Text == b[4] == b[6])
           return true;

    return false;
}

void check()
{
        if(is_Row())
        {
                MessageBox.Show(b[0].Text + " wins"));
                reset();
        }
}



Is This A Good Question/Topic? 0
  • +

Replies To: error in check method of tic tac toe game project

#2 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 401
  • Joined: 08-January 12

Re: error in check method of tic tac toe game project

Posted 10 April 2012 - 07:08 AM

Where are you getting the error what line
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: error in check method of tic tac toe game project

Posted 10 April 2012 - 07:08 AM

Oh my good God. Please tell us where the error is occurring. And please, please, give your variables better names than b, CurY, and CurX.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: error in check method of tic tac toe game project

Posted 10 April 2012 - 07:29 AM

Then, um, take the static out!

Using TextBoxes for this is not ideal. However, if you're going to do it, be consistent.

Make some methods for yourself. e.g.
private string GetText(int index) { return b[index].Text; }

private string GetText(int x, int y) { return b[x + y*3].Text; }

// note, how you wrote this, it will return true on an empty board...
private bool is_Row() {
    for(int y=0; y<3; y++) { 
    	if (GetText(y,0)==GetText(y,1)==GetText(y,2)) { return true; }
    }
    for(int x=0; x<3; x++) { 
    	if (GetText(0,x)==GetText(1,x)==GetText(2,x)) { return true; }
    }


	/* see how you forget the text halfway through
	 if(b[0].Text == b[4].Text == b[8].Text ||
       b[2].Text == b[4] == b[6])
           return true;
	*/
	
	if (GetText(0)==GetText(4)==GetText(8)) { return true; }
	if (GetText(2)==GetText(4)==GetText(6)) { return true; }
    return false;
}

private void check() {
	if(is_Row()) {
		// why b[0]?  Why not b[1]?
		// perhaps you need to rethink the approach
		MessageBox.Show(b[0].Text + " wins"));
		reset();
	}
}



Personally, I'd go with labels or panels that change when you click on them.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 azislovely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-April 12

Re: error in check method of tic tac toe game project

Posted 10 April 2012 - 08:14 AM

i want short way of this code

  void check()
        {
            if (b[0].Text == b[1].Text && b[1].Text == b[2].Text && b[0].Text != "")//
            {
                MessageBox.Show(b[0].Text + " wins");
                // reset();
            }
            if (b[3].Text == b[4].Text && b[4].Text == b[5].Text && b[3].Text != "")//
            {
                MessageBox.Show(b[0].Text + " wins");
                // reset();
            }
            if (b[6].Text == b[7].Text && b[7].Text == b[8].Text && b[6].Text != "")//
            {
                MessageBox.Show(b[0].Text + " wins");
                // reset();
            }
            if (b[0].Text == b[3].Text && b[3].Text == b[6].Text && b[0].Text != "")//
            {
                MessageBox.Show(b[0].Text + " wins");
                //  reset();
            }
            if (b[1].Text == b[4].Text && b[4].Text == b[7].Text && b[1].Text != "")//
            {
                MessageBox.Show(b[0].Text + " wins");
                // reset();
            }
            if (b[2].Text == b[5].Text && b[5].Text == b[8].Text && b[2].Text != "")//6
            {
                MessageBox.Show(b[0].Text + " wins");
                // reset();
            }
            if (b[0].Text == b[4].Text && b[4].Text == b[8].Text && b[0].Text != "")//7
            {
                MessageBox.Show(b[0].Text + " wins");
                // reset();
            }
            if (b[2].Text == b[4].Text && b[4].Text == b[6].Text && b[2].Text != "")//8
            {
                MessageBox.Show(b[0].Text + " wins");
                // reset();

            }
        }


Please suggest me a good one Object Oriented Book
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: error in check method of tic tac toe game project

Posted 10 April 2012 - 09:17 AM

 
if (b[0].Text == b[1].Text && b[1].Text == b[2].Text && b[0].Text != "") {
	MessageBox.Show(b[0].Text + " wins");
}

if (b[3].Text == b[4].Text && b[4].Text == b[5].Text && b[3].Text != "") {
	MessageBox.Show(b[0].Text + " wins");
}



Look at these two blocks. What's changed?

It's really just three values. Think about how to write a function that takes three values:
bool CheckForWin(int p1, int p2, int p3) {
	if (b[p1].Text == b[p2].Text && b[p1].Text == b[p3].Text && b[p1].Text != "") {
		MessageBox.Show(b[p1].Text + " wins");
		return true;
	}
	return false;
}

void check() {
	CheckForWin(0,1,2)
	|| CheckForWin(3,4,5)
	//...
}



However, it would probably be more useful if you knew who won for other parts of your program. e.g.
string GetWinner(int p1, int p2, int p3) {
	if (b[p1].Text == b[p2].Text && b[p1].Text == b[p3].Text && b[p1].Text != "") {
		return b[p1].Text;
	}
	return null
}

String GetWinner() {
	string winner = null;
	winner = GetWinner(0,1,2);
	if (winner==null) { winner = GetWinner(3,4,5); }
	if (winner==null) { winner = GetWinner(6,7,8); }
	//...
	return winner;
}

String check() {
	string winner = GetWinner();
	if (winner!=null) { MessageBox.Show(winner + " wins"); }
	return winner;
}



Hope this helps.

Any good book on C# should be concentrating on OOP. I trust O'Reilly, Wrox, and Apress for books. Pick what seems to suit your needs.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1