Jelajahi Sumber

добавление противников

WoI 7 bulan lalu
induk
melakukan
e6dd778307

+ 56 - 47
WpfApp21Point/DeskPage.xaml

@@ -6,7 +6,7 @@
       xmlns:local="clr-namespace:WpfApp21Point"
       mc:Ignorable="d" 
       d:DesignHeight="450" d:DesignWidth="900"
-      MinHeight="620"
+      MinHeight="640"
       Title="DeskPage">
 
     <Grid>
@@ -33,56 +33,65 @@
                        Orientation="Horizontal"/>
             </ScrollViewer>
         </Grid>
-        <StackPanel Grid.Column="1" Orientation="Vertical"
-                    VerticalAlignment="Center" HorizontalAlignment="Center" >
-            <Button x:Name="PackOfCards_Button" Grid.Column="1" 
+        <StackPanel Grid.Column="1" HorizontalAlignment="Center" 
+                    Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}},Path=ActualHeight}">
+            <StackPanel Margin="5" Orientation="Vertical" 
+                        HorizontalAlignment="Center">
+                <TextBlock Style="{StaticResource OnGreen}" Text="Противник"/>
+                <Image x:Name="ImageOpponent" Source="/Picture/AI/Шрек.jpg" Height="100" Width="100"/>
+                <TextBlock x:Name="NameOpponent" Style="{StaticResource OnGreen}" Text="Шрек"/>
+                <TextBlock x:Name="WinsOpponent" Style="{StaticResource OnGreen}" Text="Побед: "/>
+            </StackPanel>
+            <StackPanel Margin="0 30 0 0" Orientation="Vertical" VerticalAlignment="Center">
+                <Button x:Name="PackOfCards_Button" Grid.Column="1" 
         BorderBrush="{x:Null}"
         Height="280" Width="180" Click="TakeCardButton_Click">
-                <Button.Background>
-                    <ImageBrush ImageSource="/Picture/Resourse/Рубашка.gif"/>
-                </Button.Background>
-                <Button.ToolTip>
-                    <ToolTip>
-                        Это колода. Нажмите, чтобы взять карту.
-                    </ToolTip>
-                </Button.ToolTip>
-                <Button.Style>
-                    <Style TargetType="Button">
-                        <Style.Triggers>
-                            <DataTrigger Binding="{Binding IsEnabled}" Value="True">
-                                <Setter Property="Background">
-                                    <Setter.Value>
-                                        <ImageBrush ImageSource="/Picture/Resourse/Рубашка.gif"/>
-                                    </Setter.Value>
-                                </Setter>
-                            </DataTrigger>
-                        </Style.Triggers>
-                        <Setter Property="Template">
-                            <Setter.Value>
-                                <ControlTemplate TargetType="{x:Type Button}">
-                                    <Image Source="/Picture/Resourse/Рубашка.gif"/>
-                                </ControlTemplate>
-                            </Setter.Value>
-                        </Setter>
-                    </Style>
-                </Button.Style>
-            </Button>
-            <TextBlock x:Name="Score" Text="Ваш счёт: 0" Margin="0 25 0 0" Style="{StaticResource OnGreen}">
-                <TextBlock.ToolTip>
-                    <ToolTip>
-                        Счет ваших очков ведется автоматически.
-                    </ToolTip>
-                </TextBlock.ToolTip>
-            </TextBlock>
-            <Button x:Name="StopGameButton" Content="Хватит" Width="100" 
+                    <Button.Background>
+                        <ImageBrush ImageSource="/Picture/Resourse/Рубашка.gif"/>
+                    </Button.Background>
+                    <Button.ToolTip>
+                        <ToolTip>
+                            Это колода. Нажмите, чтобы взять карту.
+                        </ToolTip>
+                    </Button.ToolTip>
+                    <Button.Style>
+                        <Style TargetType="Button">
+                            <Style.Triggers>
+                                <DataTrigger Binding="{Binding IsEnabled}" Value="True">
+                                    <Setter Property="Background">
+                                        <Setter.Value>
+                                            <ImageBrush ImageSource="/Picture/Resourse/Рубашка.gif"/>
+                                        </Setter.Value>
+                                    </Setter>
+                                </DataTrigger>
+                            </Style.Triggers>
+                            <Setter Property="Template">
+                                <Setter.Value>
+                                    <ControlTemplate TargetType="{x:Type Button}">
+                                        <Image Source="/Picture/Resourse/Рубашка.gif" />
+                                    </ControlTemplate>
+                                </Setter.Value>
+                            </Setter>
+                        </Style>
+                    </Button.Style>
+                </Button>
+                <TextBlock x:Name="Score" Text="Ваш счёт: 0" Margin="0 25 0 0" Style="{StaticResource OnGreen}">
+                    <TextBlock.ToolTip>
+                        <ToolTip>
+                            Счет ваших очков ведется автоматически.
+                        </ToolTip>
+                    </TextBlock.ToolTip>
+                </TextBlock>
+                <Button x:Name="StopGameButton" Content="Хватит" Width="100" 
                     Style="{StaticResource OnGreenButton}" Margin="0 25 0 0"
                     Click="StopGameButton_Click">
-                <Button.ToolTip>
-                    <ToolTip>
-                        Нажмите, если больше не хотите брать карты из колоды.
-                    </ToolTip>
-                </Button.ToolTip>
-            </Button>
+                    <Button.ToolTip>
+                        <ToolTip>
+                            Нажмите, если больше не хотите брать карты из колоды.
+                        </ToolTip>
+                    </Button.ToolTip>
+                </Button>
+            </StackPanel>
         </StackPanel>
     </Grid>
 </Page>

+ 20 - 1
WpfApp21Point/DeskPage.xaml.cs

@@ -28,11 +28,23 @@ namespace WpfApp21Point
         List<Card> cardsComp = new();
         bool computerPlayOn = true;
         GameParty gameParty = new();
+        Opponent opponent;
 
         public DeskPage()
         {
             InitializeComponent();
             ControllPackOfCards();
+            SetOpponent();
+        }
+
+        void SetOpponent()
+        {
+            List<Opponent> opponents = GlobalData.DbConnect.Opponents.ToList();
+            opponent = opponents[new Random().Next(opponents.Count)];
+            string filePath = Environment.CurrentDirectory + opponent.PathAvatar;
+            ImageOpponent.Source = new BitmapImage(new Uri(filePath));
+            NameOpponent.Text = opponent.Name;
+            WinsOpponent.Text = "Побед: " + opponent.CountWin.ToString();
         }
 
         void ControllPackOfCards()
@@ -109,7 +121,7 @@ namespace WpfApp21Point
             }
             double probability = (double)countFavorableOutcomes/ (double)packOfCards.FullPack.Count();
             //double roundProbality = Math.Round(probability, 2);
-            if(probability < 0.4)
+            if(probability < opponent.MinimalProbability)
             {
                 computerPlayOn = false;
             }
@@ -153,6 +165,7 @@ namespace WpfApp21Point
             }
             else if (scoreComp == 21)
             {
+                opponent.CountWin = opponent.CountWin + 1;
                 gameParty.IsWin = false;
             }
             else if(scoreUser == 21)
@@ -163,6 +176,7 @@ namespace WpfApp21Point
             {
                 if (scoreUser < scoreComp && scoreComp < 21)
                 {
+                    opponent.CountWin = opponent.CountWin + 1;
                     gameParty.IsWin = false;
                 }
                 else
@@ -175,6 +189,11 @@ namespace WpfApp21Point
                 gameParty.User = GlobalData.User;
                 gameParty.UserId = GlobalData.User.Id;
             }
+            if(!gameParty.IsWin)
+            {
+                GlobalData.DbConnect.Opponents.Update(opponent);
+                GlobalData.DbConnect.SaveChanges();
+            }
             gameParty.Date = DateTime.Now;
             gameParty.AmountOfPointsUser = scoreUser;
             gameParty.AmountOfPointsOpponent = scoreComp;

+ 5 - 2
WpfApp21Point/MainWindow.xaml

@@ -6,7 +6,7 @@
         xmlns:local="clr-namespace:WpfApp21Point"
         mc:Ignorable="d"
         Title="Двадцать одно" Height="660" Width="1200"
-        MinHeight="660" MinWidth="1000"
+        MinHeight="680" MinWidth="1000"
         Background="#FF134825">
     <Grid>
         <Grid.ColumnDefinitions>
@@ -46,7 +46,10 @@
                     </DataGrid.Columns>
                 </DataGrid>
             </StackPanel>
-            <Button Content="Правила" DockPanel.Dock="Bottom" HorizontalAlignment="Left" Style="{StaticResource OnGreenButton}" Click="RulesButton_Click"/>
+            <Grid DockPanel.Dock="Bottom">
+                <Button Content="Правила" HorizontalAlignment="Left" Style="{StaticResource OnGreenButton}" Click="RulesButton_Click"/>
+                <Button x:Name="ExitButton" Content="Выйти" HorizontalAlignment="Right" Style="{StaticResource OnGreenButton}" Click="ExitButton_Click"/>
+            </Grid>
         </DockPanel>
         <GridSplitter Grid.Column="1" ShowsPreview="False" Width="3"
         HorizontalAlignment="Center" VerticalAlignment="Stretch" />

+ 14 - 0
WpfApp21Point/MainWindow.xaml.cs

@@ -61,6 +61,14 @@ namespace WpfApp21Point
                 AuthUser_Panel.Visibility = Visibility.Collapsed;
                 UserInfo_Panel.Visibility = Visibility.Visible;
                 UserName_TextBlock.Text = "Игрок: " + GlobalData.User.UserName;
+                ExitButton.Visibility = Visibility.Visible;
+            }
+            else
+            {
+                AuthUser_Panel.Visibility = Visibility.Visible;
+                UserInfo_Panel.Visibility = Visibility.Collapsed;
+                UserName_TextBlock.Text = "";
+                ExitButton.Visibility = Visibility.Collapsed;
             }
         }
 
@@ -89,5 +97,11 @@ namespace WpfApp21Point
             UserPartiesWindow userPartiesWindow = new();
             userPartiesWindow.Show();
         }
+
+        private void ExitButton_Click(object sender, RoutedEventArgs e)
+        {
+            GlobalData.User = null;
+            CheckAuth();
+        }
     }
 }

+ 17 - 0
WpfApp21Point/Models/Opponent.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+
+namespace WpfApp21Point.Models;
+
+public partial class Opponent
+{
+    public string Id { get; set; } = null!;
+
+    public string Name { get; set; } = null!;
+
+    public int CountWin { get; set; }
+
+    public string? PathAvatar { get; set; }
+
+    public float MinimalProbability { get; set; }
+}

+ 21 - 0
WpfApp21Point/Models/TwentyOnePointsContext.cs

@@ -21,6 +21,8 @@ public partial class TwentyOnePointsContext : DbContext
 
     public virtual DbSet<GameParty> GameParties { get; set; }
 
+    public virtual DbSet<Opponent> Opponents { get; set; }
+
     public virtual DbSet<User> Users { get; set; }
 
     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
@@ -38,6 +40,25 @@ public partial class TwentyOnePointsContext : DbContext
             entity.HasOne(d => d.User).WithMany(p => p.GameParties).HasForeignKey(d => d.UserId);
         });
 
+        modelBuilder.Entity<Opponent>(entity =>
+        {
+            entity.HasKey(e => e.Id).HasName("opponents_pk");
+
+            entity.ToTable("opponents");
+
+            entity.Property(e => e.Id)
+                .HasColumnType("character varying")
+                .HasColumnName("id");
+            entity.Property(e => e.CountWin).HasColumnName("count_win");
+            entity.Property(e => e.MinimalProbability).HasColumnName("minimal_probability");
+            entity.Property(e => e.Name)
+                .HasColumnType("character varying")
+                .HasColumnName("name");
+            entity.Property(e => e.PathAvatar)
+                .HasColumnType("character varying")
+                .HasColumnName("path_avatar");
+        });
+
         modelBuilder.Entity<User>(entity =>
         {
             entity.HasIndex(e => e.UserName, "IX_Users_UserName").IsUnique();

+ 15 - 0
WpfApp21Point/WpfApp21Point.csproj

@@ -36,6 +36,21 @@
   </ItemGroup>
 
   <ItemGroup>
+    <None Update="Picture\AI\Гриффин.jpg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </None>
+    <None Update="Picture\AI\Мистер Исключительный.jpg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </None>
+    <None Update="Picture\AI\Пепе.jpg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </None>
+    <None Update="Picture\AI\Халк.png">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </None>
+    <None Update="Picture\AI\Шрек.jpg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </None>
     <None Update="Picture\Cards\10 Буби.gif">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </None>