Advertisement
Guest User

saunderl

a guest
Dec 6th, 2008
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.08 KB | None | 0 0
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Markup;
  6. using System.Windows.Media;
  7. using System.Windows.Shapes;
  8. using System.Windows.Threading;
  9.  
  10. namespace Switcheroo
  11. {
  12.     public partial class Page
  13.     {
  14.         private class MyColors
  15.         {
  16.             public static Color Aliceblue { get { return GenerateColorStruct(0xFFF0F8FF); } }
  17.             public static Color Cyan { get { return GenerateColorStruct(0xFF00FFFF); } }
  18.             public static Color Darkcyan { get { return GenerateColorStruct(0xFF008B8B); } }
  19.             public static Color Darkgoldenrod { get { return GenerateColorStruct(0xFFB8860B); } }
  20.             public static Color Gold { get { return GenerateColorStruct(0xFFFFD700); } }
  21.  
  22.             private static Color GenerateColorStruct(uint color)
  23.             {
  24.                 var mask = 0x000000FF;
  25.  
  26.                 var returnval = new Color
  27.                 {
  28.                     A = ((byte)((color >> 24) & mask)),
  29.                     R = ((byte)((color >> 16) & mask)),
  30.                     G = ((byte)((color >> 8) & mask)),
  31.                     B = ((byte)((color) & mask))
  32.                 };
  33.  
  34.                 return returnval;
  35.             }
  36.         }
  37.  
  38.         private class GameBoard
  39.         {
  40.             internal readonly int[,] Board = new int[5, 5];
  41.  
  42.             public GameBoard()
  43.             {
  44.                 for (var i = 0; i < 5; i++)
  45.                 {
  46.                     for (var j = 0; j < 5; j++)
  47.                     {
  48.                         Board[i, j] = -1;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.  
  54.         readonly DispatcherTimer dt = new DispatcherTimer();
  55.        
  56.         private bool GameOver;
  57.         private int GameWinner = -1;
  58.         private GameBoard Switcheroo = new GameBoard();
  59.  
  60.         public Page()
  61.         {
  62.             InitializeComponent();
  63.         }
  64.  
  65.         private void Start_Click(object sender, RoutedEventArgs e)
  66.         {
  67.             Switcheroo = new GameBoard();
  68.             LayoutRoot.Children.Clear();
  69.             CreateGameBoard();
  70.             GameOver = false;
  71.         }
  72.  
  73.         private void dt_Tick(object sender, EventArgs e)
  74.         {
  75.             dt.Stop();
  76.             DisplayGameOverSplash();
  77.         }
  78.  
  79.         private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  80.         {
  81.             GameOver = true;
  82.  
  83.             GameWinner = GameWinner == 0 ? 1 : 0;
  84.  
  85.             if (GameOver)
  86.             {
  87.                 dt.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds
  88.                 dt.Tick += dt_Tick;
  89.                 dt.Start();
  90.             }
  91.         }
  92.  
  93.         private void CreateGameBoard()
  94.         {
  95.             var CellColor = false;
  96.             Canvas c;
  97.  
  98.             for (var i = 0; i < 7; i++)
  99.             {
  100.                 for (var j = 0; j < 7; j++)
  101.                 {
  102.                     if (i != 0 && i != 6 && j != 0 && j != 6)
  103.                     {
  104.                         c = new Canvas
  105.                                 {
  106.                                     Tag = (i + ":" + j),
  107.                                     Background =
  108.                                         (CellColor
  109.                                              ? new SolidColorBrush(MyColors.Aliceblue)
  110.                                              : new SolidColorBrush(MyColors.Darkcyan))
  111.                                 };
  112.                     }
  113.                     else
  114.                     {
  115.                         c = new Canvas
  116.                                 {
  117.                                     Tag = (i + ":" + j),
  118.                                     Background = new SolidColorBrush(MyColors.Cyan)
  119.                                 };
  120.  
  121.  
  122.                         if (j == 6 && i != 0 && i != 6)
  123.                         {
  124.                             var p = new Polygon
  125.                                         {
  126.                                             Fill = new SolidColorBrush(MyColors.Gold),
  127.                                             Stroke = new SolidColorBrush(MyColors.Darkgoldenrod)
  128.                                         };
  129.  
  130.                             p.Points.Add(new Point(10, 5));
  131.                             p.Points.Add(new Point(40, 5));
  132.                             p.Points.Add(new Point(25, 15));
  133.  
  134.                             c.Children.Add(p);
  135.                         }
  136.                         else if (j == 0 && i != 0 && i != 6)
  137.                         {
  138.                             var p = new Polygon
  139.                                         {
  140.                                             Fill = new SolidColorBrush(MyColors.Gold),
  141.                                             Stroke = new SolidColorBrush(MyColors.Darkgoldenrod)
  142.                                         };
  143.  
  144.                             p.Points.Add(new Point(10, 20));
  145.                             p.Points.Add(new Point(40, 20));
  146.                             p.Points.Add(new Point(25, 10));
  147.  
  148.                             c.Children.Add(p);
  149.  
  150.                         }
  151.                         else if (i == 6 && j != 0 && j != 6)
  152.                         {
  153.                             var p = new Polygon
  154.                                         {
  155.                                             Fill = new SolidColorBrush(MyColors.Gold),
  156.                                             Stroke = new SolidColorBrush(MyColors.Darkgoldenrod)
  157.                                         };
  158.  
  159.                             p.Points.Add(new Point(5, 10));
  160.                             p.Points.Add(new Point(5, 40));
  161.                             p.Points.Add(new Point(15, 25));
  162.  
  163.                             c.Children.Add(p);
  164.  
  165.                         }
  166.                         else if (i == 0 && j != 0 && j != 6)
  167.                         {
  168.                             var p = new Polygon
  169.                                         {
  170.                                             Fill = new SolidColorBrush(MyColors.Gold),
  171.                                             Stroke = new SolidColorBrush(MyColors.Darkgoldenrod)
  172.                                         };
  173.  
  174.                             p.Points.Add(new Point(20, 10));
  175.                             p.Points.Add(new Point(20, 40));
  176.                             p.Points.Add(new Point(10, 25));
  177.  
  178.                             c.Children.Add(p);
  179.  
  180.                         }
  181.                     }
  182.  
  183.                     c.MouseLeftButtonDown += Canvas_MouseLeftButtonDown;
  184.                     Grid.SetColumn(c, i);
  185.                     Grid.SetRow(c, j);
  186.  
  187.                     LayoutRoot.Children.Add(c);
  188.  
  189.                     CellColor = !CellColor;
  190.                 }
  191.             }
  192.         }
  193.  
  194.         private void DisplayGameOverSplash()
  195.         {
  196.             string ButtonText = GameWinner != 0 ? "You Win! Play Again?" : "You Loose! Play Again?";
  197.  
  198.             const string Splash = "<Image xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
  199.                 "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " +
  200.                 "x:Name='XamlBoard' Grid.RowSpan='7' Grid.ColumnSpan='7' Source='Splash.png' /> ";
  201.  
  202.             const string Stack = "<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
  203.                 "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' VerticalAlignment='Center' " +
  204.                 "HorizontalAlignment='Center' Grid.RowSpan='7' Grid.ColumnSpan='7' />";
  205.  
  206.             string Button = "<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
  207.                 "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Content='" + ButtonText + "'></Button>";
  208.  
  209.             LayoutRoot.Children.Clear();
  210.             LayoutRoot.Children.Add((Image)XamlReader.Load(Splash));
  211.             LayoutRoot.Children.Add((StackPanel)XamlReader.Load(Stack));
  212.             ((StackPanel)LayoutRoot.Children[1]).Children.Add((Button)XamlReader.Load(Button));
  213.             ((Button)((StackPanel)LayoutRoot.Children[1]).Children[0]).Click += Start_Click;
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement