gempir

battleships wpf

Apr 21st, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. <Window x:Class="battleships.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MainWindow" Height="525" Width="525" ResizeMode="NoResize">
  5.     <Grid>
  6.         <Grid.ColumnDefinitions>
  7.             <ColumnDefinition Width="520" MaxWidth="520" MinWidth="520"/>
  8.             <ColumnDefinition Width="0*"/>
  9.         </Grid.ColumnDefinitions>
  10.         <TextBlock x:Name="maintext" Loaded="Output_Loaded" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Text="asd" Foreground="Black" FontSize="18" TextDecorations="{x:Null}" FontFamily="Arial" TextAlignment="Center"/>
  11.         <Button x:Name="startBtn" Height="26" HorizontalAlignment="Left" Margin="403,0,0,0"
  12.             VerticalAlignment="Bottom"
  13.             Width="106" Click="startGame">Start Game</Button>
  14.  
  15.  
  16.     </Grid>
  17. </Window>
  18.  
  19.  
  20.  
  21.  
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using System.Text;
  26. using System.Threading.Tasks;
  27. using System.Windows;
  28. using System.Windows.Controls;
  29. using System.Windows.Data;
  30. using System.Windows.Documents;
  31. using System.Windows.Input;
  32. using System.Windows.Media;
  33. using System.Windows.Media.Imaging;
  34. using System.Windows.Navigation;
  35. using System.Windows.Shapes;
  36. using System.Text.RegularExpressions;
  37.  
  38. namespace battleships
  39. {
  40.     public partial class MainWindow : Window
  41.     {
  42.         private Random rand;
  43.         private int[,] field;
  44.         private Regex reg;
  45.         private TextBlock output;
  46.         private Button Buttons;
  47.         private Dictionary<string, Button> ButtonDict = new Dictionary<string, Button>();
  48.        
  49.         public MainWindow()
  50.         {
  51.             InitializeComponent();
  52.             this.rand = new Random();
  53.             this.reg = new Regex(@"^\d,\d$", RegexOptions.IgnoreCase);
  54.             this.CreateField();
  55.         }
  56.  
  57.         private void Output_Loaded(object sender, RoutedEventArgs e)
  58.         {
  59.             this.output = sender as TextBlock;
  60.         }
  61.  
  62.         private void startGame(object sender, EventArgs e)
  63.         {
  64.             Button clicked = (Button)sender;
  65.             clicked.Content = "Restart";
  66.             this.output.Text = "The enemy has placed his ships. Ready your weapons";
  67.             this.createButtons();
  68.         }
  69.  
  70.         private void createButtons()
  71.         {
  72.             for (int i = 0; i < 10; i++)
  73.             {
  74.                 for (int j = 0; j < 10; j++)
  75.                 {
  76.                     Button newButton = new Button();
  77.                     ButtonDict.Add(i + "," + j , newButton);
  78.                 }
  79.             }
  80.         }
  81.  
  82.         private void CreateField()
  83.         {
  84.             // setup empty field
  85.            
  86.             this.field = new int[10, 10] {
  87.                 {0,0,0,0,0,0,0,0,0,0},
  88.                 {0,0,0,0,0,0,0,0,0,0},
  89.                 {0,0,0,0,0,0,0,0,0,0},
  90.                 {0,0,0,0,0,0,0,0,0,0},
  91.                 {0,0,0,0,0,0,0,0,0,0},
  92.                 {0,0,0,0,0,0,0,0,0,0},
  93.                 {0,0,0,0,0,0,0,0,0,0},
  94.                 {0,0,0,0,0,0,0,0,0,0},
  95.                 {0,0,0,0,0,0,0,0,0,0},
  96.                 {0,0,0,0,0,0,0,0,0,0}
  97.             };
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment