Continous Integration in der Praxis Gruppenarbeit
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.
 
 
 
 

130 lines
3.0 KiB

using System;
namespace MiniGames.Shared.Models
{
public class TicTacToeBrett
{
public const int LEER = -1;
int[,] Felder;
public int Groesse
{
get { return Felder.GetLength(0) * Felder.GetLength(1); }
}
public TicTacToeBrett()
{
Felder = new[,]
{
{ LEER, LEER, LEER },
{ LEER, LEER, LEER },
{ LEER, LEER, LEER },
};
}
public TicTacToeBrett(int[,] werte)
{
Felder = new int[3, 3];
if (werte == null)
{
werte = new int[0, 0];
}
for (int i = 0; i < Felder.GetLength(0); i++)
{
for (int j = 0; j < Felder.GetLength(1); j++)
{
if (i < werte.GetLength(0) && j < werte.GetLength(1))
{
Felder[i, j] = werte[i, j];
}
else
{
Felder[i, j] = LEER;
}
}
}
}
public bool Gleich(TicTacToeBrett anderes)
{
for (int i = 0; i < Groesse; i++)
{
if (Get(i) != anderes.Get(i))
{
return false;
}
}
return true;
}
public int Get(int pos)
{
int x = pos % 3;
int y = pos / 3;
return Felder[x, y];
}
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 < Groesse; i++)
{
if (Get(i) == LEER)
{
return false;
}
}
return true;
}
protected bool dreiGleichGefuellt(int a, int b, int c)
{
return a != LEER && a == b && b == c;
}
public int Gewinner()
{
for (int i = 0; i < 3; i++)
{
if (dreiGleichGefuellt(Felder[i, 0], Felder[i, 1], Felder[i, 2])) return Felder[i, 0];
if (dreiGleichGefuellt(Felder[0, i], Felder[1, i], Felder[2, i])) return Felder[0, i];
}
if (
dreiGleichGefuellt(Felder[0, 0], Felder[1, 1], Felder[2, 2]) ||
dreiGleichGefuellt(Felder[2, 0], Felder[1, 1], Felder[0, 2])
)
{
return Felder[1, 1];
}
return LEER;
}
}
}