TicTacToe/tictactoe/TicTacToeViewModel.cs

97 lines
2.8 KiB
C#
Raw Normal View History

2023-03-13 12:59:49 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-03-13 20:49:24 +00:00
using System.Windows;
2023-03-13 12:59:49 +00:00
using CommunityToolkit.Mvvm.ComponentModel;
2023-03-13 20:49:24 +00:00
using CommunityToolkit.Mvvm.Input;
using Microsoft.VisualBasic;
2023-03-13 12:59:49 +00:00
namespace TicTacToe
{
partial class TicTacToeViewModel : ObservableObject
{
[ObservableProperty]
2023-03-13 20:49:24 +00:00
char field1 = ' ';
2023-03-13 12:59:49 +00:00
2023-03-13 20:49:24 +00:00
[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;
2023-03-13 12:59:49 +00:00
public TicTacToeViewModel()
{
2023-03-13 20:49:24 +00:00
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 },
};
2023-03-13 12:59:49 +00:00
2023-03-13 20:49:24 +00:00
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 = ' ';
}
}
2023-03-13 12:59:49 +00:00
}
}
}