2 Replies - 2200 Views - Last Post: 28 May 2011 - 04:16 AM Rate Topic: -----

#1 Crash95  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 40
  • Joined: 08-April 11

Tic Tac Toe game

Posted 28 May 2011 - 01:50 AM

Hi all. I've been working on tic tac toe game and i have done this code:
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 IksOks
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int n = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            if (n ==0)
            {
                button1.Text = "X";
                n = 1;
            }
            else if (n==1)
            {
                button1.Text = "O";
                n = 0;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (n == 0)
            {
                button2.Text = "X";
                n = 1;
            }
            else if (n == 1)
            {
                button2.Text = "O";
                n = 0;
            }
        }

and for all 9 buttons is like this, so now i have a problem. I don't know where can i put to check for combinations, i can't put it outside of button event,but if i put it in button event that's stupid, because it will check only when i click that button the last, and i would have to write a bunch of code. Can anyone help me where can i put to check combinations?

This post has been edited by Crash95: 28 May 2011 - 01:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe game

#2 aetion  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 21-May 11

Re: Tic Tac Toe game

Posted 28 May 2011 - 04:01 AM

I am just giving a bird's eye view of this :

You could make two functions which check for the combinations of X and O, such as CheckComboX() and CheckComboO().

Then call them from the button event handler:
       private void button1_Click(object sender, EventArgs e)
        {
            if (n ==0)
            {
                button1.Text = "X";
                n = 1;

                //This checks for X combos
                CheckComboX(); 
            }
            else if (n==1)
            {
                button1.Text = "O";
                n = 0;

                //This checks for O combos
                CheckComboO();              
            }
            
        }



And do the above, for the event handlers of all the other 8 buttons, so that everytime, a player makes a move(i.e. clicks a button), it will check whether a combination of three Xs or three Os has occured or not.

This post has been edited by aetion: 28 May 2011 - 04:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 Crash95  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 40
  • Joined: 08-April 11

Re: Tic Tac Toe game

Posted 28 May 2011 - 04:16 AM

Yea that sounds like a good idea i can put if statements in that method for all solutions :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1