create account

TicTacToe by sulev

View this thread on: hive.blogpeakd.comecency.com
· @sulev · (edited)
$15.39
TicTacToe
![Untitled1.jpg](https://cdn.steemitimages.com/DQmUBUrMbQCsh5GoSSxS1DU9Caag1iNmFYh3HyPZGoQTeso/Untitled1.jpg)

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

https://ufile.io/h6hotxz1

The Xaml:
<code>


    <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>
</code>

The C# Main file:
<code>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
    }
}
</code>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 108 others
properties (23)
authorsulev
permlinktictactoe
categorycoding
json_metadata{"tags":["coding","tictactoe","game","curate","region"],"image":["https://cdn.steemitimages.com/DQmUBUrMbQCsh5GoSSxS1DU9Caag1iNmFYh3HyPZGoQTeso/Untitled1.jpg"],"links":["https://ufile.io/h6hotxz1"],"app":"steemit/0.1","format":"markdown"}
created2019-05-13 10:20:39
last_update2019-05-13 17:55:48
depth0
children3
last_payout2019-05-20 10:20:39
cashout_time1969-12-31 23:59:59
total_payout_value11.703 HBD
curator_payout_value3.689 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8,387
author_reputation315,005,876,598,228
root_titleTicTacToe
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id84,768,961
net_rshares29,672,608,290,918
author_curate_reward""
vote details (172)
@ace108 ·
I think your 2nd tag has a typo
properties (22)
authorace108
permlinkre-sulev-tictactoe-20190513t163618343z
categorycoding
json_metadata{"tags":["coding"],"app":"steemit/0.1"}
created2019-05-13 16:36:15
last_update2019-05-13 16:36:15
depth1
children1
last_payout2019-05-20 16:36:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length31
author_reputation1,229,119,536,181,200
root_titleTicTacToe
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id84,786,309
net_rshares0
@sulev ·
indeed
properties (22)
authorsulev
permlinkre-ace108-re-sulev-tictactoe-20190513t175534461z
categorycoding
json_metadata{"tags":["coding"],"app":"steemit/0.1"}
created2019-05-13 17:55:33
last_update2019-05-13 17:55:33
depth2
children0
last_payout2019-05-20 17:55:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6
author_reputation315,005,876,598,228
root_titleTicTacToe
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id84,789,804
net_rshares0
@partiko ·
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!
properties (22)
authorpartiko
permlinkre-tictactoe-20190513t113021
categorycoding
json_metadata"{"app": "partiko"}"
created2019-05-13 11:30:24
last_update2019-05-13 11:30:24
depth1
children0
last_payout2019-05-20 11:30:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length213
author_reputation39,207,160,334,751
root_titleTicTacToe
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id84,772,267
net_rshares0