using System; 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 field1 = ' '; [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 = ' '; } } } } }