Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Markup;
- using System.Windows.Media;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace Switcheroo
- {
- public partial class Page
- {
- private class MyColors
- {
- public static Color Aliceblue { get { return GenerateColorStruct(0xFFF0F8FF); } }
- public static Color Cyan { get { return GenerateColorStruct(0xFF00FFFF); } }
- public static Color Darkcyan { get { return GenerateColorStruct(0xFF008B8B); } }
- public static Color Darkgoldenrod { get { return GenerateColorStruct(0xFFB8860B); } }
- public static Color Gold { get { return GenerateColorStruct(0xFFFFD700); } }
- private static Color GenerateColorStruct(uint color)
- {
- var mask = 0x000000FF;
- var returnval = new Color
- {
- A = ((byte)((color >> 24) & mask)),
- R = ((byte)((color >> 16) & mask)),
- G = ((byte)((color >> 8) & mask)),
- B = ((byte)((color) & mask))
- };
- return returnval;
- }
- }
- private class GameBoard
- {
- internal readonly int[,] Board = new int[5, 5];
- public GameBoard()
- {
- for (var i = 0; i < 5; i++)
- {
- for (var j = 0; j < 5; j++)
- {
- Board[i, j] = -1;
- }
- }
- }
- }
- readonly DispatcherTimer dt = new DispatcherTimer();
- private bool GameOver;
- private int GameWinner = -1;
- private GameBoard Switcheroo = new GameBoard();
- public Page()
- {
- InitializeComponent();
- }
- private void Start_Click(object sender, RoutedEventArgs e)
- {
- Switcheroo = new GameBoard();
- LayoutRoot.Children.Clear();
- CreateGameBoard();
- GameOver = false;
- }
- private void dt_Tick(object sender, EventArgs e)
- {
- dt.Stop();
- DisplayGameOverSplash();
- }
- private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- GameOver = true;
- GameWinner = GameWinner == 0 ? 1 : 0;
- if (GameOver)
- {
- dt.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds
- dt.Tick += dt_Tick;
- dt.Start();
- }
- }
- private void CreateGameBoard()
- {
- var CellColor = false;
- Canvas c;
- for (var i = 0; i < 7; i++)
- {
- for (var j = 0; j < 7; j++)
- {
- if (i != 0 && i != 6 && j != 0 && j != 6)
- {
- c = new Canvas
- {
- Tag = (i + ":" + j),
- Background =
- (CellColor
- ? new SolidColorBrush(MyColors.Aliceblue)
- : new SolidColorBrush(MyColors.Darkcyan))
- };
- }
- else
- {
- c = new Canvas
- {
- Tag = (i + ":" + j),
- Background = new SolidColorBrush(MyColors.Cyan)
- };
- if (j == 6 && i != 0 && i != 6)
- {
- var p = new Polygon
- {
- Fill = new SolidColorBrush(MyColors.Gold),
- Stroke = new SolidColorBrush(MyColors.Darkgoldenrod)
- };
- p.Points.Add(new Point(10, 5));
- p.Points.Add(new Point(40, 5));
- p.Points.Add(new Point(25, 15));
- c.Children.Add(p);
- }
- else if (j == 0 && i != 0 && i != 6)
- {
- var p = new Polygon
- {
- Fill = new SolidColorBrush(MyColors.Gold),
- Stroke = new SolidColorBrush(MyColors.Darkgoldenrod)
- };
- p.Points.Add(new Point(10, 20));
- p.Points.Add(new Point(40, 20));
- p.Points.Add(new Point(25, 10));
- c.Children.Add(p);
- }
- else if (i == 6 && j != 0 && j != 6)
- {
- var p = new Polygon
- {
- Fill = new SolidColorBrush(MyColors.Gold),
- Stroke = new SolidColorBrush(MyColors.Darkgoldenrod)
- };
- p.Points.Add(new Point(5, 10));
- p.Points.Add(new Point(5, 40));
- p.Points.Add(new Point(15, 25));
- c.Children.Add(p);
- }
- else if (i == 0 && j != 0 && j != 6)
- {
- var p = new Polygon
- {
- Fill = new SolidColorBrush(MyColors.Gold),
- Stroke = new SolidColorBrush(MyColors.Darkgoldenrod)
- };
- p.Points.Add(new Point(20, 10));
- p.Points.Add(new Point(20, 40));
- p.Points.Add(new Point(10, 25));
- c.Children.Add(p);
- }
- }
- c.MouseLeftButtonDown += Canvas_MouseLeftButtonDown;
- Grid.SetColumn(c, i);
- Grid.SetRow(c, j);
- LayoutRoot.Children.Add(c);
- CellColor = !CellColor;
- }
- }
- }
- private void DisplayGameOverSplash()
- {
- string ButtonText = GameWinner != 0 ? "You Win! Play Again?" : "You Loose! Play Again?";
- const string Splash = "<Image xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
- "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " +
- "x:Name='XamlBoard' Grid.RowSpan='7' Grid.ColumnSpan='7' Source='Splash.png' /> ";
- const string Stack = "<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
- "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' VerticalAlignment='Center' " +
- "HorizontalAlignment='Center' Grid.RowSpan='7' Grid.ColumnSpan='7' />";
- string Button = "<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
- "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Content='" + ButtonText + "'></Button>";
- LayoutRoot.Children.Clear();
- LayoutRoot.Children.Add((Image)XamlReader.Load(Splash));
- LayoutRoot.Children.Add((StackPanel)XamlReader.Load(Stack));
- ((StackPanel)LayoutRoot.Children[1]).Children.Add((Button)XamlReader.Load(Button));
- ((Button)((StackPanel)LayoutRoot.Children[1]).Children[0]).Click += Start_Click;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement