Advertisement
Festelo

Untitled

Sep 4th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using Resource = Шахматы.Properties.Resources;
  16. using Bitmap = System.Drawing.Bitmap;
  17. using System.IO;
  18.  
  19. namespace Шахматы
  20. {
  21.     /// <summary>
  22.     /// Логика взаимодействия для MainWindow.xaml
  23.     /// </summary>
  24.     public class imgs
  25.     {
  26.         public static class black
  27.         {
  28.             static public BitmapImage pawn = BitmapToImageSource(Resource.ЧПешка);
  29.             static public BitmapImage bishop = BitmapToImageSource(Resource.ЧСлон);
  30.             static public BitmapImage knight = BitmapToImageSource(Resource.ЧКонь);
  31.             static public BitmapImage castle = BitmapToImageSource(Resource.ЧЛадья);
  32.             static public BitmapImage queen = BitmapToImageSource(Resource.ЧФерзь);
  33.             static public BitmapImage king = BitmapToImageSource(Resource.ЧКороль);
  34.         }
  35.         public static class white
  36.         {
  37.             static public BitmapImage pawn = BitmapToImageSource(Resource.БПешка);
  38.             static public BitmapImage bishop = BitmapToImageSource(Resource.БСлон);
  39.             static public BitmapImage knight = BitmapToImageSource(Resource.БКонь);
  40.             static public BitmapImage castle = BitmapToImageSource(Resource.БЛадья);
  41.             static public BitmapImage queen = BitmapToImageSource(Resource.БФерзь);
  42.             static public BitmapImage king = BitmapToImageSource(Resource.БКороль);
  43.         }
  44.  
  45.         static BitmapImage BitmapToImageSource(Bitmap bitmap)
  46.         {
  47.             using (MemoryStream memory = new MemoryStream())
  48.             {
  49.                 bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
  50.                 memory.Position = 0;
  51.                 BitmapImage bitmapimage = new BitmapImage();
  52.                 bitmapimage.BeginInit();
  53.                 bitmapimage.StreamSource = memory;
  54.                 bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
  55.                 bitmapimage.EndInit();
  56.  
  57.                 return bitmapimage;
  58.             }
  59.         }
  60.     }
  61.     public partial class MainWindow : Window
  62.     {
  63.         public MainWindow()
  64.         {
  65.             InitializeComponent();
  66.         }
  67.  
  68.         private void button_Click(object sender, RoutedEventArgs e)
  69.         {
  70.             new Game(true);
  71.             this.Close();
  72.         }
  73.     }
  74.     public class Game
  75.     {
  76.         Grid gr = null;
  77.         public Game(bool White)
  78.         {
  79.             Window win = new Window();
  80.             win.SizeToContent = SizeToContent.WidthAndHeight;
  81.             //win.Show();
  82.             gr = new Grid();
  83.             gr.HorizontalAlignment = HorizontalAlignment.Left;
  84.             gr.VerticalAlignment = VerticalAlignment.Top;
  85.             char code = '\u0041';
  86.             for(int i = 0; i<9; i++)
  87.             {
  88.                 gr.RowDefinitions.Add(new RowDefinition());
  89.                 gr.ColumnDefinitions.Add(new ColumnDefinition());
  90.             }
  91.             bool tableColor = true;
  92.             for (int i = 1; i<9; i++, code++)
  93.             {
  94.                 TextBlock tbA = new TextBlock();
  95.                 tbA.Text = Convert.ToString(code);
  96.                 gr.Children.Add(tbA);
  97.                 tbA.VerticalAlignment = VerticalAlignment.Center;
  98.                 tbA.TextAlignment = TextAlignment.Center;
  99.                 //tbA.MaxHeight = 15;
  100.                 Grid.SetRow(tbA, 0);
  101.                 Grid.SetColumn(tbA, i);
  102.  
  103.                 TextBlock tb1 = new TextBlock();
  104.                 tb1.VerticalAlignment = VerticalAlignment.Center;
  105.                 tb1.TextAlignment = TextAlignment.Center;
  106.                 tb1.MinWidth = 20;
  107.                 tb1.Text = Convert.ToString(i);
  108.                 gr.Children.Add(tb1);
  109.                 Grid.SetColumn(tb1, 0);
  110.                 Grid.SetRow(tb1, i);
  111.  
  112.                 for (int j = 1; j < 9; j++)
  113.                 {
  114.                     Rectangle rect = new Rectangle();
  115.                     rect.Width = 80;
  116.                     rect.Height = 80;
  117.                     SolidColorBrush color = new SolidColorBrush();
  118.                     Panel.SetZIndex(rect, -1);
  119.                     Grid.SetColumn(rect, i);
  120.                     Grid.SetRow(rect, j);
  121.                     gr.Children.Add(rect);
  122.  
  123.                     if (tableColor)
  124.                     {
  125.                         color.Color = Color.FromRgb(175, 119, 92);
  126.                         tableColor = false;
  127.                     }
  128.                     else
  129.                     {
  130.                         color.Color = Color.FromRgb(231, 189, 140);
  131.                         tableColor = true;
  132.                     }
  133.                     rect.Fill = color;
  134.                 }
  135.                 tableColor = !tableColor;
  136.             }
  137.             initGame(White);
  138.             win.Content = gr;
  139.             win.Show();
  140.         }
  141.         private void initGame(bool White)
  142.         {
  143.             for (int i = 1; i < 3; i++)
  144.             {
  145.                 for(int j = 1; j< 9; j++)
  146.                 {
  147.                     Image img = new Image();
  148.                     img.Source = imgs.black.bishop;
  149.                     img.Height = 80;
  150.                     img.Width = 80;
  151.                     gr.Children.Add(img);
  152.                     Grid.SetColumn(img, j);
  153.                     Grid.SetRow(img, i);
  154.                 }
  155.             }
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement