Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Shorter code for tic-tac-toe game code

algorithm shortcode tic tac toe tic-tac-toe

  • Please log in to reply
5 replies to this topic

#1 aazamsajid

aazamsajid

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 08 April 2012 - 11:50 AM

i am working on a tic tac toe game this is loop code ....how can write this in short way like in algorthm or arrays etc

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();
		    }
		  
	   
	   
	    }

  • 0

#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 08 April 2012 - 12:42 PM

Here is your shortie :

Bool checkForWin( int c1, int c2, int c3 )
{
	if (b[c1].Text == b[c2].Text && b[c2].Text == b[c3].Text && b[c1].Text != "")
    {
	    return true;
    }

    return false;
}

void check() 
{
    if ( checkForWin( 0,1,2 ) || checkForWin( 3, 4, 5 ) || // put here all the combinations )
    {
		 MessageBox.Show(b[0].Text + " wins");
	     reset();
	}
}

  • 1

#3 Soryy708

Soryy708

    CC Resident

  • Banned
  • PipPipPipPip
  • 59 posts

Posted 08 April 2012 - 12:43 PM

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[1 + 1*3].Text == b[1 + 2*3].Text ||
	   b[3 + 2*3].Text == b[2 + 2*3] == b[1 + 1*3])
		   return true;

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

  • 0

#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 08 April 2012 - 12:58 PM

Is_row does not work for diagonals, you will get index out of bounds error :

this should be the correct version of is_row "

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;
}


  • 0

#5 Soryy708

Soryy708

    CC Resident

  • Banned
  • PipPipPipPip
  • 59 posts

Posted 08 April 2012 - 07:22 PM

This is the beauty of open source and free software.
  • 0

#6 aazamsajid

aazamsajid

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 09 April 2012 - 10:45 AM

when i apply is_row method this error is occrng.

An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.b'
  • 0





Also tagged with one or more of these keywords: algorithm, shortcode, tic tac toe, tic-tac-toe

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download