Portfolio

Saturday, September 19, 2015

How to Create a Tic Tac Toe Game in C#











How to Create a Tic Tac Toe Game


Here is a simple game created in Visual Studio 2015.  It’s awesome!  Try it out for yourself.  Test your skills.

What you need:


·        A menu strip at the top of the form
·        9 buttons
·        Determination to finish.
·        Oh, Visual Studio 2015




This is the copy of the source code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TicTacToe
{
    public partial class Form1 : Form
    {
        bool turn = true;  //true = X turn, false = Y turn
        int turn_count = 0;


        public Form1()
        {
            InitializeComponent();

           

            
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("By Roxanne Reyes", "Tic tac toe About");
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

        private void button_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (turn)
                b.Text = "X";
            else
                b.Text = "O";
            turn = !turn;
            b.Enabled = false;

            turn_count++;
            checkForWinner();

        }
        private void checkForWinner()
        {
            bool there_is_a_winner = false;

            //Horizontal checks
            if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
                there_is_a_winner = true;
            else if((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
                there_is_a_winner = true;
            else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
                there_is_a_winner = true;

            //Verticle checks
            if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
                there_is_a_winner = true;
            else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
                there_is_a_winner = true;
            else if ((A3.Text == B3.Text) && (C2.Text == C3.Text) && (!A3.Enabled))
                there_is_a_winner = true;

            //Diagonal checks
            if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
                there_is_a_winner = true;
            else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled))
                there_is_a_winner = true;
           

            if (there_is_a_winner)
            {
                disableButtons();

                string winner = "";
                if (turn)
                    winner = "0";
                else
                    winner = "X";
                MessageBox.Show(winner + " wins!" + " Congratulations!");
            }//end if
            else
            {
                if(turn_count == 9)
                    MessageBox.Show("It's a draw!", "Try again!");
            }




        }
        //end check for winner

        private void disableButtons()
        {
            try
            {

           
            foreach(Control c in Controls)
            {
                Button b = (Button)c;
                b.Enabled = false;

            }//end foreach
        }//end try
            catch
            {

            }
}

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            turn = true;
            turn_count = 0;

            try
            {


                foreach (Control c in Controls)
                {
                    Button b = (Button)c;
                    b.Enabled = true;
                    b.Text = "";


                }//end foreach
            }//end try
            catch
            {

            }
        }
    }
}


1 comment:

  1. Roxanne, love this blog. Found you through simple programmer. Will be modifying some of these programs in the next few weeks.

    ReplyDelete