Monday 14 April 2014

How to create Not and Cross Game in WPF with C# .Net

How to create Not and Cross Game in WPF with C# .Net

Note: In this topic we will discuss about how to create WPF Games with cross and not which is very popular and people generally play this game on paper with pen 

Code for Window2.xaml………………………….

<Window x:Class="WpfApplicationPanels.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Tic Tok Game in WPF" Height="375" Width="400" Background="Azure">

<Grid Width="300" Height="300" Background="Azure" Name="Grid1">

<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>

</Grid.ColumnDefinitions>

<Button Grid.Row="0" Grid.Column="0" Name="Button1" Background="Aqua" Click="btnallClick"></Button>
<Button Grid.Row="0" Grid.Column="1" Name="Button2" Background="Aqua" Click="btnallClick"></Button>
<Button Grid.Row="0" Grid.Column="2" Name="Button3" Background="Aqua" Click="btnallClick"></Button>
<Button Grid.Row="1" Grid.Column="0" Name="Button4" Background="Aqua" Click="btnallClick"></Button>
<Button Grid.Row="1" Grid.Column="1" Name="Button5" Background="Aqua" Click="btnallClick"></Button>
<Button Grid.Row="1" Grid.Column="2" Name="Button6" Background="Aqua" Click="btnallClick"></Button>
<Button Grid.Row="2" Grid.Column="0" Name="Button7" Background="Aqua" Click="btnallClick"></Button>
<Button Grid.Row="2" Grid.Column="1" Name="Button8" Background="Aqua" Click="btnallClick"></Button>
<Button Grid.Row="2" Grid.Column="2" Name="Button9" Background="Aqua" Click="btnallClick"></Button>

</Grid>

</Window>

Code for Window2.xaml.cs…………………………
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;


namespace WpfApplicationPanels
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}

int Count = 1;
// create click event for all  Button...........................

private void btnallClick(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
b.Content = (Count % 2 == 0) ? "0" : "x";

b.IsEnabled = false;
Count++;
check();
}

// code for reset all Button…………………

void reset()
{
foreach (UIElement element in Grid1.Children)
{
Button btn = (Button)element;
btn.Content = null;
btn.IsEnabled = true;
Count = 1;
}
}

//condition for 8 logic...with row column diagonal......................
void check()
{
if (Button1.Content == Button2.Content && Button2.Content == Button3.Content && Button1.Content != null)
{
result((string)Button1.Content);
}
else if (Button4.Content== Button5.Content && Button5.Content == Button6.Content && Button4.Content != null)
{
result((string)Button4.Content);
}
if (Button7.Content == Button8.Content && Button8.Content == Button9.Content && Button7.Content != null)
{
result((string)Button7.Content);
}
else if (Button1.Content == Button4.Content && Button4.Content == Button7.Content && Button1.Content != null)
{
result((string)Button1.Content);
}
if (Button2.Content == Button5.Content && Button5.Content == Button8.Content && Button2.Content != null)
{
result((string)Button2.Content);
}
else if (Button3.Content == Button6.Content && Button6.Content == Button9.Content && Button3.Content != null)
{
result((string)Button3.Content);
}
if (Button1.Content == Button5.Content && Button5.Content == Button9.Content && Button1.Content != null)
{
result((string)Button1.Content);
}
else if (Button3.Content == Button5.Content && Button5.Content == Button7.Content && Button3.Content != null)
{
result((string)Button4.Content);
}

else if (Count == 10)
{
result("");
}
}

//Code for Result
void result(string text)
{
if (text == "0")
{

MessageBoxResult result = MessageBox.Show("Second Player won the game ,Do you want to Play Again", "My Tic Tok Game", MessageBoxButton.YesNoCancel);

switch (result)
{

case MessageBoxResult.Yes:
MessageBox.Show("Ok Welcome you to play again", "My Tic Tok Game");
reset();
break;

case MessageBoxResult.No:
MessageBox.Show("Thank u ", "My Tic Tok Game");
reset();
break;
case MessageBoxResult.Cancel:
MessageBox.Show("Nevermind then...", "My Tic Tok Game");
break;
}
}
else if (text == "x")
{
MessageBoxResult result = MessageBox.Show("First Player  won the GAME ,Do you want to Play it again", "My Tic Tok Game", MessageBoxButton.YesNoCancel);

switch (result)
{

case MessageBoxResult.Yes:
MessageBox.Show("Ok Welcome you to play again", "My Tic Tok Game");
reset();
break;

case MessageBoxResult.No:
MessageBox.Show("Thank u ", "My Tic Tok Game");
reset();
break;

case MessageBoxResult.Cancel:
MessageBox.Show("Nevermind then...", "My Tic Tok Game");
reset();
break;

}
}
else if(text=="")
{
MessageBox.Show("Game Draw!!!!!");
reset();
}
}

}
}

Result




0 comments:

Post a Comment