You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.2 KiB
101 lines
2.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MiniGames.Shared.Models
|
|
{
|
|
public class TicTacToeBrett
|
|
{
|
|
public const int LEER = -1;
|
|
|
|
int[,] Felder;
|
|
|
|
public TicTacToeBrett()
|
|
{
|
|
Felder = new[,]
|
|
{
|
|
{ LEER, LEER, LEER },
|
|
{ LEER, LEER, LEER },
|
|
{ LEER, LEER, LEER },
|
|
};
|
|
}
|
|
|
|
public TicTacToeBrett(int[,] felder)
|
|
{
|
|
Felder = felder;
|
|
}
|
|
|
|
public bool Gleich(TicTacToeBrett anderes)
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
for (int j = 0; j < 3; j++)
|
|
{
|
|
if (Felder[i, j] != anderes.Felder[i, j])
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool Set(int pos, int wert)
|
|
{
|
|
int x = pos / 3;
|
|
int y = pos % 3;
|
|
|
|
try
|
|
{
|
|
if (Felder[x, y] == LEER)
|
|
{
|
|
Felder[x, y] = wert;
|
|
return true;
|
|
}
|
|
}
|
|
catch (IndexOutOfRangeException e)
|
|
{
|
|
// absichtlich leer
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool Voll()
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
for (int j = 0; j < 3; j++)
|
|
{
|
|
if (Felder[i, j] == LEER)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public int Gewinner()
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
if (Felder[i, 0] == Felder[i, 1] && Felder[i, 1] == Felder[i, 2] && Felder[i, 0] != LEER)
|
|
{
|
|
return Felder[i, 0];
|
|
}
|
|
}
|
|
|
|
if (Felder[0, 0] == Felder[1, 0] && Felder[1, 0] == Felder[2, 0] && Felder[0, 0] != LEER)
|
|
{
|
|
return Felder[0, 0];
|
|
}
|
|
|
|
return LEER;
|
|
}
|
|
}
|
|
}
|