consolidate all repos to one for archive
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="TicTacToe.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:TicTacToe"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace TicTacToe
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
@@ -0,0 +1,27 @@
|
||||
<Window x:Class="TicTacToe.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:TicTacToe"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="200" Width="230" ResizeMode="NoResize">
|
||||
<Grid>
|
||||
<Button Name="Field_0_0" Content="" Width="30" Height="30" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="Field_Click"/>
|
||||
<Button Name="Field_0_1" Content="" Width="30" Height="30" HorizontalAlignment="Left" Margin="50,10,0,0" VerticalAlignment="Top" Click="Field_Click"/>
|
||||
<Button Name="Field_0_2" Content="" Width="30" Height="30" HorizontalAlignment="Left" Margin="90,10,0,0" VerticalAlignment="Top" Click="Field_Click"/>
|
||||
<Button Name="Field_1_0" Content="" Width="30" Height="30" HorizontalAlignment="Left" Margin="10,50,0,0" VerticalAlignment="Top" Click="Field_Click"/>
|
||||
<Button Name="Field_1_1" Content="" Width="30" Height="30" HorizontalAlignment="Left" Margin="50,50,0,0" VerticalAlignment="Top" Click="Field_Click"/>
|
||||
<Button Name="Field_1_2" Content="" Width="30" Height="30" HorizontalAlignment="Left" Margin="90,50,0,0" VerticalAlignment="Top" Click="Field_Click"/>
|
||||
<Button Name="Field_2_0" Content="" Width="30" Height="30" HorizontalAlignment="Left" Margin="10,90,0,0" VerticalAlignment="Top" Click="Field_Click"/>
|
||||
<Button Name="Field_2_1" Content="" Width="30" Height="30" HorizontalAlignment="Left" Margin="50,90,0,0" VerticalAlignment="Top" Click="Field_Click"/>
|
||||
<Button Name="Field_2_2" Content="" Width="30" Height="30" HorizontalAlignment="Left" Margin="90,90,0,0" VerticalAlignment="Top" Click="Field_Click"/>
|
||||
|
||||
<Button Name="StartButton" Content="Star" HorizontalAlignment="Left" Margin="130,10,0,0" VerticalAlignment="Top" Click="Start_Click"/>
|
||||
<!--<Button Name="StopButton" Content="Sto" HorizontalAlignment="Left" Margin="130,40,0,0" VerticalAlignment="Top" Click="Stop_Click"/>-->
|
||||
<CheckBox Name="SinglePlay" Content="SinglePlay" HorizontalAlignment="Left" Margin="130,100,0,0" VerticalAlignment="Top"/>
|
||||
<CheckBox Name="Client" Content="Client" HorizontalAlignment="Left" Margin="130,75,0,0" VerticalAlignment="Top"/>
|
||||
<Label Name="Label" HorizontalAlignment="Left" Margin="130,120,0,0" VerticalAlignment="Top"/>
|
||||
<Label Name="Result" Content="" HorizontalAlignment="Left" Margin="10,130,0,0" VerticalAlignment="Top"/>
|
||||
</Grid>
|
||||
</Window>
|
@@ -0,0 +1,337 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Media.Media3D;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace TicTacToe
|
||||
{
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
const int STD_PORT = 1234;
|
||||
const string STD_IP = "127.0.0.1";
|
||||
const int STD_MSG_SIZE = 1024;
|
||||
List<Button> buttons = new List<Button>();
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
buttons.Add(Field_0_0);
|
||||
buttons.Add(Field_0_1);
|
||||
buttons.Add(Field_0_2);
|
||||
buttons.Add(Field_1_0);
|
||||
buttons.Add(Field_1_1);
|
||||
buttons.Add(Field_1_2);
|
||||
buttons.Add(Field_2_0);
|
||||
buttons.Add(Field_2_1);
|
||||
buttons.Add(Field_2_2);
|
||||
|
||||
DisableButtns(false);
|
||||
}
|
||||
|
||||
private void DisableButtns(bool change)
|
||||
{
|
||||
foreach (Button button in buttons)
|
||||
{
|
||||
button.IsEnabled = change;
|
||||
if (button.Content.ToString() != "")
|
||||
{
|
||||
button.IsEnabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int moveCount = 0;
|
||||
int[,] board = new int[3, 3];
|
||||
int n = 3;
|
||||
|
||||
private void ReportResult(int i)
|
||||
{
|
||||
if (i == 1) Result.Content = "I win";
|
||||
if (i == 2) Result.Content = "Oponent win";
|
||||
if (i == 0) Result.Content = "Draw";
|
||||
}
|
||||
private bool NextMove(int x, int y, int s)
|
||||
{
|
||||
if (board[x, y] == 0)
|
||||
{
|
||||
board[x, y] = s;
|
||||
}
|
||||
moveCount++;
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if (board[x, i] != s) break;
|
||||
if (i == 2)
|
||||
{
|
||||
ReportResult(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if (board[i, y] != s) break;
|
||||
if (i == 2)
|
||||
{
|
||||
ReportResult(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (x == y)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if (board[i, i] != s) break;
|
||||
if (i == 2)
|
||||
{
|
||||
ReportResult(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (x + y == 2)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if (board[i, 2 - i] != s) break;
|
||||
if (i == 2)
|
||||
{
|
||||
ReportResult(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (moveCount == 9)
|
||||
{
|
||||
ReportResult(0);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
private void ResetBoard()
|
||||
{
|
||||
moveCount = 0;
|
||||
for(int i = 0; i < 3; i++)
|
||||
{
|
||||
for(int j = 0; j < 3; j++)
|
||||
{
|
||||
board[i,j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async void Recive()
|
||||
{
|
||||
byte[] reciveMessage = Array.Empty<byte>();
|
||||
int lenght = 0;
|
||||
try
|
||||
{
|
||||
reciveMessage = new byte[STD_MSG_SIZE];
|
||||
lenght = await networkStream.ReadAsync(reciveMessage, 0, reciveMessage.Length);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message + e.StackTrace);
|
||||
StopApp();
|
||||
}
|
||||
|
||||
if(lenght <= 0) return;
|
||||
|
||||
string response = Encoding.UTF8.GetString(reciveMessage, 0, lenght);
|
||||
string[] stringArray = response.Split('_');
|
||||
|
||||
if (Int32.TryParse(stringArray[0], out int x))
|
||||
{
|
||||
if (Int32.TryParse(stringArray[1], out int y))
|
||||
{
|
||||
buttons[3 * x + y].Content = "X";
|
||||
if (NextMove(x, y, 2))
|
||||
{
|
||||
StopApp();
|
||||
}
|
||||
else
|
||||
{
|
||||
DisableButtns(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Send(string Message)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(Message);
|
||||
networkStream?.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
StopApp();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
NetworkStream ?networkStream;
|
||||
TcpClient ?tcpClient;
|
||||
TcpListener? tcpListener;
|
||||
|
||||
private async void StartApp()
|
||||
{
|
||||
|
||||
ResetBoard();
|
||||
foreach (Button button in buttons)
|
||||
{
|
||||
button.Content = "";
|
||||
}
|
||||
Label.Content = "";
|
||||
Result.Content = "";
|
||||
|
||||
if (!SinglePlay.IsChecked ?? false)
|
||||
{
|
||||
Label.Content = "Connecting";
|
||||
|
||||
if (Client.IsChecked ?? false)
|
||||
{
|
||||
try
|
||||
{
|
||||
tcpClient = new TcpClient();
|
||||
await tcpClient.ConnectAsync(STD_IP, STD_PORT);
|
||||
networkStream = tcpClient.GetStream();
|
||||
Recive();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
Label.Content = "Disconected";
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
tcpListener = new TcpListener(IPAddress.Parse(STD_IP), STD_PORT);
|
||||
tcpListener.Start();
|
||||
tcpClient = await tcpListener.AcceptTcpClientAsync();
|
||||
networkStream = tcpClient.GetStream();
|
||||
DisableButtns(true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
Label.Content = "Disconected";
|
||||
return;
|
||||
}
|
||||
}
|
||||
Label.Content = "Connected";
|
||||
}
|
||||
else
|
||||
{
|
||||
DisableButtns(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void StopApp()
|
||||
{
|
||||
try
|
||||
{
|
||||
networkStream?.Close();
|
||||
tcpClient?.Close();
|
||||
tcpListener?.Stop();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
|
||||
DisableButtns(false);
|
||||
Label.Content = "Disconnected";
|
||||
}
|
||||
|
||||
private void CompNextMove()
|
||||
{
|
||||
|
||||
int field = 0;
|
||||
while (buttons[field].Content.ToString() != ""){
|
||||
field++;
|
||||
}
|
||||
|
||||
buttons[field].Content = "X";
|
||||
|
||||
if(NextMove(field / 3, field % 3, 2))
|
||||
{
|
||||
DisableButtns(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
DisableButtns(true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Field_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if(sender is Button btn)
|
||||
{
|
||||
string[] stringArray = btn.Name.Split('_');
|
||||
if (Int32.TryParse(stringArray[1], out int x))
|
||||
{
|
||||
if (Int32.TryParse(stringArray[2], out int y))
|
||||
{
|
||||
btn.Content = "O";
|
||||
if (SinglePlay.IsChecked ?? false)
|
||||
{
|
||||
DisableButtns(false);
|
||||
if (!NextMove(x, y, 1))
|
||||
{
|
||||
CompNextMove();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Send(String.Format("{0}_{1}", x, y));
|
||||
if (!NextMove(x, y, 1))
|
||||
{
|
||||
Recive();
|
||||
}
|
||||
else
|
||||
{
|
||||
StopApp();
|
||||
}
|
||||
DisableButtns(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Start_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
StartApp();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user