C# TicTacToe

As explained in my previous post, learning the basics of C# was one of my big projects for this summer, and well, I think it is safe to say that it is finally completed, this took longer than expected because I went on a trip in which I didn’t have access to my laptop, but regardless of that C# was really easy for me to learn. I think this is due to the fact that started with C# already knowing python, this meant that I only needed to learn new syntax and another couple of things that are done differently between the languages because I already came in with the “programming mindset”. All of that aside the project that I’ve done to showcase my new knowledge of C# is a TicTacToe game which uses both C# and HTML as it needed to have a graphical interface, I also want to say that I used help from the internet to program this little game and that I didn’t completely do it on my own in that regard, but it was a great exercise that helped further improve my C# skills, the code is the following:

<Window x:Class="Tic_Tac_Toe.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Tic_Tac_Toe"
        mc:Ignorable="d"
        ResizeMode="NoResize"
        Title="TicTacToe" Height="500" Width="500">

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderThickness" Value="0.5"/>
            <Setter Property="FontSize" Value="70"/>
        </Style>
    </Window.Resources>
    
    
    <Grid x:Name="Container">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/> 
        </Grid.RowDefinitions>

        <Button x:Name="Button0_0" Grid.Column="0" Grid.Row="0" Click="Button_Click" />
        <Button x:Name="Button0_1" Grid.Column="0" Grid.Row="1" Click="Button_Click" />
        <Button x:Name="Button0_2" Grid.Column="0" Grid.Row="2" Click="Button_Click" />

        <Button x:Name="Button1_0" Grid.Column="1" Grid.Row="0" Click="Button_Click" />
        <Button x:Name="Button1_1" Grid.Column="1" Grid.Row="1" Click="Button_Click" />
        <Button x:Name="Button1_2" Grid.Column="1" Grid.Row="2" Click="Button_Click" />

        <Button x:Name="Button2_0" Grid.Column="2" Grid.Row="0" Click="Button_Click" />
        <Button x:Name="Button2_1" Grid.Column="2" Grid.Row="1" Click="Button_Click" />
        <Button x:Name="Button2_2" Grid.Column="2" Grid.Row="2" Click="Button_Click" />
        
    </Grid>
</Window>
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Tic_Tac_Toe
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Private Members

        private MarkType[] mResults;


        private bool mPlayer1Turn;


        private bool mGameEnded;

        public object CheckForWinner { get; private set; }


        #endregion

        #region Constructor

        public MainWindow()
        {
            InitializeComponent();

            NewGame();
        }
        #endregion

        private void NewGame()
        {
            mResults = new MarkType[9];

            for (var i = 0; i < mResults.Length; i++)
                mResults[i] = MarkType.Free;

            mPlayer1Turn = true;

            Container.Children.Cast<Button>().ToList().ForEach(button =>
            {
                button.Content = string.Empty;
                button.Background = Brushes.White;
                button.Foreground = Brushes.Red;
            });

            mGameEnded = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (mGameEnded) {  
                NewGame();
                return;
            }

            var button = (Button)sender;

            var column = Grid.GetColumn(button); 
            var row = Grid.GetRow(button);


            var index = column + (row * 3);

            if (mResults[index] != MarkType.Free)
                return;

            if (mPlayer1Turn)
                mResults[index] = MarkType.Cross;
            else
                mResults[index] = MarkType.Nought;

            button.Content = mPlayer1Turn ? "X" : "O";

            mPlayer1Turn ^= true;

            if (mPlayer1Turn)
                button.Foreground = Brushes.Green;

            CheckForWinners();
        }

        private void CheckForWinners()
        {

            #region Horizontal Wins
            if (mResults[0] != MarkType.Free && (mResults[0] & mResults[1] & mResults[2]) == mResults[0]) 
            {
                mGameEnded = true;
                Button0_0.Background = Button1_0.Background = Button2_0.Background = Brushes.CadetBlue;
            }

            if (mResults[3] != MarkType.Free && (mResults[3] & mResults[4] & mResults[5]) == mResults[3])
            {
                mGameEnded = true;
                Button0_1.Background = Button1_1.Background = Button2_1.Background = Brushes.CadetBlue;
            }

            if (mResults[6] != MarkType.Free && (mResults[6] & mResults[7] & mResults[8]) == mResults[6])
            {
                mGameEnded = true;
                Button0_2.Background = Button1_2.Background = Button2_2.Background = Brushes.CadetBlue;
            }
            #endregion

            #region Vertical Wins
            if (mResults[0] != MarkType.Free && (mResults[0] & mResults[3] & mResults[6]) == mResults[0])
            {
                mGameEnded = true;
                Button0_0.Background = Button0_1.Background = Button0_2.Background = Brushes.CadetBlue;
            }

            if (mResults[1] != MarkType.Free && (mResults[1] & mResults[4] & mResults[7]) == mResults[1])
            {
                mGameEnded = true;
                Button1_0.Background = Button1_1.Background = Button1_2.Background = Brushes.CadetBlue;
            }

            if (mResults[2] != MarkType.Free && (mResults[2] & mResults[5] & mResults[8]) == mResults[2])
            {
                mGameEnded = true;
                Button2_0.Background = Button2_1.Background = Button2_2.Background = Brushes.CadetBlue;
            }
            #endregion

            #region Diagonal Wins
            if (mResults[0] != MarkType.Free && (mResults[0] & mResults[4] & mResults[8]) == mResults[0])
            {
                mGameEnded = true;
                Button0_0.Background = Button1_1.Background = Button2_2.Background = Brushes.CadetBlue;
            }

            if (mResults[2] != MarkType.Free && (mResults[2] & mResults[4] & mResults[6]) == mResults[2])
            {
                mGameEnded = true;
                Button0_2.Background = Button1_1.Background = Button2_0.Background = Brushes.CadetBlue;
            }

            #endregion

            #region No Winners
            if (!mResults.Any(result => result == MarkType.Free))
            {
                mGameEnded = true;

                Container.Children.Cast<Button>().ToList().ForEach(button =>
                {
                    button.Background = Brushes.Yellow;
                   
                });
                #endregion
            }
        }
    }
}

namespace Tic_Tac_Toe
{
    public enum MarkType
    {
        Free,
        Nought,
        Cross
    }
}

Leave a comment

Website Powered by WordPress.com.

Up ↑