This commit is contained in:
ReimarPB 2023-03-13 21:49:24 +01:00
parent 58849edeeb
commit 3d99c10285
3 changed files with 89 additions and 20 deletions

View File

@ -5,11 +5,10 @@
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="TextBlock" x:Key="Field">
<Setter Property="FontSize" Value="48" />
<!--<Setter Property="HorizontalAlignment" Value="Center" />-->
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="TextAlignment" Value="Center" />
<Style TargetType="Button" x:Key="Field">
<Setter Property="FontSize" Value="36" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
</Application.Resources>

View File

@ -22,31 +22,31 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="0" Grid.Column="0">
<TextBlock Text="{ Binding Grid[0][0] }" Style="{ StaticResource Field }" />
<Button Content="{ Binding Field1 }" Style="{ StaticResource Field }" Command="{ Binding FieldClickCommand }" CommandParameter="1" />
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="1" Grid.Column="0">
<TextBlock Text="{ Binding Grid[0][1] }" Style="{ StaticResource Field }" />
<Button Content="{ Binding Field2 }" Style="{ StaticResource Field }" Command="{ Binding FieldClickCommand }" CommandParameter="2" />
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="2" Grid.Column="0">
<TextBlock Text="{ Binding Grid[0][2] }" Style="{ StaticResource Field }" />
<Button Content="{ Binding Field3 }" Style="{ StaticResource Field }" Command="{ Binding FieldClickCommand }" CommandParameter="3" />
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="0" Grid.Column="1">
<TextBlock Text="{ Binding Grid[1][0] }" Style="{ StaticResource Field }" />
<Button Content="{ Binding Field4 }" Style="{ StaticResource Field }" Command="{ Binding FieldClickCommand }" CommandParameter="4" />
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="1" Grid.Column="1">
<TextBlock Text="{ Binding Grid[1][1] }" Style="{ StaticResource Field }" />
<Button Content="{ Binding Field5 }" Style="{ StaticResource Field }" Command="{ Binding FieldClickCommand }" CommandParameter="5" />
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="2" Grid.Column="1">
<TextBlock Text="{ Binding Grid[1][2] }" Style="{ StaticResource Field }" />
<Button Content="{ Binding Field6 }" Style="{ StaticResource Field }" Command="{ Binding FieldClickCommand }" CommandParameter="6" />
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="0" Grid.Column="2">
<TextBlock Text="{ Binding Grid[2][0] }" Style="{ StaticResource Field }" />
<Button Content="{ Binding Field7 }" Style="{ StaticResource Field }" Command="{ Binding FieldClickCommand }" CommandParameter="7" />
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="1" Grid.Column="2">
<TextBlock Text="{ Binding Grid[2][1] }" Style="{ StaticResource Field }" />
<Button Content="{ Binding Field8 }" Style="{ StaticResource Field }" Command="{ Binding FieldClickCommand }" CommandParameter="8" />
</Border>
<Border BorderBrush="Black" BorderThickness="2" Grid.Row="2" Grid.Column="2">
<TextBlock Text="{ Binding Grid[2][2] }" Style="{ StaticResource Field }" />
<Button Content="{ Binding Field9 }" Style="{ StaticResource Field }" Command="{ Binding FieldClickCommand }" CommandParameter="9" />
</Border>
</Grid>
</Window>

View File

@ -3,24 +3,94 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.VisualBasic;
namespace TicTacToe
{
partial class TicTacToeViewModel : ObservableObject
{
[ObservableProperty]
char[][] grid = new char[][] {
new char[] { ' ', ' ', ' ' },
new char[] { ' ', ' ', ' ' },
new char[] { ' ', ' ', ' ' },
};
char field1 = ' ';
char currentTurn = 'O';
[ObservableProperty]
char field2 = ' ';
[ObservableProperty]
char field3 = ' ';
[ObservableProperty]
char field4 = ' ';
[ObservableProperty]
char field5 = ' ';
[ObservableProperty]
char field6 = ' ';
[ObservableProperty]
char field7 = ' ';
[ObservableProperty]
char field8 = ' ';
[ObservableProperty]
char field9 = ' ';
char currentTurn;
public TicTacToeViewModel()
{
currentTurn = 'O';
}
[RelayCommand]
public void OnFieldClick(string index)
{
// Karlkodning
switch (index)
{
case "1": if (Field1 == ' ') Field1 = currentTurn; else return; break;
case "2": if (Field2 == ' ') Field2 = currentTurn; else return; break;
case "3": if (Field3 == ' ') Field3 = currentTurn; else return; break;
case "4": if (Field4 == ' ') Field4 = currentTurn; else return; break;
case "5": if (Field5 == ' ') Field5 = currentTurn; else return; break;
case "6": if (Field6 == ' ') Field6 = currentTurn; else return; break;
case "7": if (Field7 == ' ') Field7 = currentTurn; else return; break;
case "8": if (Field8 == ' ') Field8 = currentTurn; else return; break;
case "9": if (Field9 == ' ') Field9 = currentTurn; else return; break;
}
if (currentTurn == 'O') currentTurn = 'X';
else currentTurn = 'O';
char[][] wins = new char[][]
{
new char[] { Field1, Field2, Field3 },
new char[] { Field4, Field5, Field6 },
new char[] { Field7, Field8, Field9 },
new char[] { Field1, Field5, Field9 },
new char[] { Field3, Field5, Field7 },
};
foreach (var win in wins)
{
if (win[0] != ' ' && win[0] == win[1] && win[1] == win[2])
{
MessageBox.Show($"{win[0]} VANDT!!!!");
Field1 = ' ';
Field2 = ' ';
Field3 = ' ';
Field4 = ' ';
Field5 = ' ';
Field6 = ' ';
Field7 = ' ';
Field8 = ' ';
Field9 = ' ';
}
}
}
}
}