TicTacToesteemCreated with Sketch.

in #coding5 years ago (edited)

Untitled1.jpg

I made a TicTacToe game in Visual Studio - C#/XAML.

https://ufile.io/h6hotxz1

The Xaml:

<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 Click="Button_Click" x:Name= "Button0_0" Grid.Column="0" Grid.Row="0" Content="X" />
    <Button Click="Button_Click" x:Name= "Button1_0" Grid.Column="1" Grid.Row="0" Content="X" />
    <Button Click="Button_Click" x:Name= "Button2_0" Grid.Column="2" Grid.Row="0" Content="X" />
                   
    <Button Click="Button_Click" x:Name= "Button0_1" Grid.Column="0" Grid.Row="1" Content="X" />
    <Button Click="Button_Click" x:Name= "Button1_1" Grid.Column="1" Grid.Row="1" Content="X" />
    <Button Click="Button_Click" x:Name= "Button2_1" Grid.Column="2" Grid.Row="1" Content="X" />
                   
    <Button Click="Button_Click" x:Name= "Button0_2" Grid.Column="0" Grid.Row="2" Content="X" />
    <Button Click="Button_Click" x:Name= "Button1_2" Grid.Column="1" Grid.Row="2" Content="X" />
    <Button Click="Button_Click" x:Name= "Button2_2" Grid.Column="2" Grid.Row="2" Content="X" />
</Grid>

The C# Main file:
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TicTacToe
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
#region Private members

    /// 
    /// Holds the current results of cells in the active game
    /// 
    private MarkedType[] mResults;

    /// 
    /// True if it is player 1's turn (x) or player 2's turn (o)
    /// 
    private bool mPlayerTurn;

    /// 
    /// Whether the game has ended or not
    /// 
    private bool mGameEnded;


    #endregion

    #region Constructor

    /// 
    /// Default Constructor
    /// 

    public MainWindow()
    {
        InitializeComponent();

        NewGame();
    }
    #endregion

    #region NewGame

    /// 
    /// Starts a new game
    /// 
    private void NewGame()
    {
        // Create new blank array of free cells
        mResults = new MarkedType[9];

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

        // Make sure P1 is current player
        mPlayerTurn = true;

        // Iterate every button on the grid
        Container.Children.Cast<Button>().ToList().ForEach(button =>
        {
            // Set the content and colors to the default values
            button.Content = string.Empty;
            button.Background = Brushes.White;
            button.Foreground = Brushes.Blue;
        });

        mGameEnded = false;

    }

    #endregion

    #region ButtonClick

    ///
    /// Handles a button click event
    /// 
    /// <param name="sender">The button that was clicked</param>
    /// <param name="e">The events of the click</param>
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (mGameEnded == true)
        {
            NewGame();
            return;
        }

        // cast the sender to a button
        var button = (Button)sender;

        // Find the button's position in the array
        var column = Grid.GetColumn(button);
        var row = Grid.GetRow(button);

        var index = column + (row * 3);

        // check if the button is free and set the button to a new value (x)
        if (mResults[index] != MarkedType.Free)
            return;

        // Set the cell value based on which player's turn it is 
        // and change the player's turn.
        if (mPlayerTurn)
        {
            mResults[index] = MarkedType.Cross;
                mPlayerTurn = false;
            button.Content = "X";
        }
        else
        {
            mResults[index] = MarkedType.Nought;
            mPlayerTurn = true;
            button.Content = "O";
            button.Foreground = Brushes.Green;
        }

        //Check for winner
        CheckForWinner();
    }

    #endregion

    #region Check for winners and paint the background

    ///
    /// Checks for the winning condition 
    ///
    private void CheckForWinner()
    {
        // Check for horizontal wins
        for (var i = 0; i <= 8; i += 3) {
            if (mResults[0 + i] != MarkedType.Free && (mResults[0 +i] & mResults[1+i] & mResults[2+i]) == mResults[0+i])
            {
                //Highlighth winning cells
                if (i == 0)
                { 
                    Button0_0.Background = Button1_0.Background = Button2_0.Background = Brushes.Gold;
                }

                if (i == 3)
                {
                    Button0_1.Background = Button1_1.Background = Button2_1.Background = Brushes.Gold;
                }
                if (i == 6)
                {
                    Button0_2.Background = Button1_2.Background = Button2_2.Background = Brushes.Gold;
                }

                // game ended
                mGameEnded = true;
            }
        }

        // Check for vertical wins
        for (var i = 0; i <= 2; i++)
        {
            if (mResults[0 + i] != MarkedType.Free && (mResults[0 + i] & mResults[3 + i] & mResults[6 + i]) == mResults[0 + i])
            {
                //Highlighth winning cells
                if (i == 0)
                {
                    Button0_0.Background = Button0_1.Background = Button0_2.Background = Brushes.Gold;
                }

                if (i == 1)
                {
                    Button1_0.Background = Button1_1.Background = Button1_2.Background = Brushes.Gold;
                }
                if (i == 2)
                {
                    Button2_0.Background = Button2_1.Background = Button2_2.Background = Brushes.Gold;
                }

                // game ended
                mGameEnded = true;
            }
        }

        // Check for diagonal wins
        
        
        if (mResults[0] != MarkedType.Free && (mResults[0] & mResults[4] & mResults[8]) == mResults[0])
        {
            //Highlighth winning cells
              Button0_0.Background = Button1_1.Background = Button2_2.Background = Brushes.Gold;
           
            // game ended
            mGameEnded = true;
        }

        if (mResults[2] != MarkedType.Free && (mResults[2] & mResults[4] & mResults[6]) == mResults[2])
        {
            //Highlighth winning cells
            Button2_0.Background = Button1_1.Background = Button0_2.Background = Brushes.Gold;

            // game ended
            mGameEnded = true;
        }




        // End the game if no one wins and turn the background orange
        if (!mResults.Any(results => results == MarkedType.Free) && !mGameEnded)
        {
            mGameEnded = true;

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

            
    }
    #endregion
}

}

Sort:  

Thank you so much for participating in the Partiko Delegation Plan Round 1! We really appreciate your support! As part of the delegation benefits, we just gave you a 3.00% upvote! Together, let’s change the world!

I think your 2nd tag has a typo

Coin Marketplace

STEEM 0.31
TRX 0.11
JST 0.033
BTC 64275.02
ETH 3139.81
USDT 1.00
SBD 4.14