ADato88
3 years ago
6 changed files with 517 additions and 0 deletions
-
7BlazorSolution/MiniGames/Client/ViewModel/ITicTacToe.cs
-
25BlazorSolution/MiniGames/Client/ViewModel/TicTacToe.cs
-
127BlazorSolution/MiniGames/Shared/Models/TicTacToeBrett.cs
-
14BlazorSolution/MiniGames/Shared/Models/TicTacToeModel.cs
-
305BlazorSolution/MiniGamesTests/TicTacToeBrettTest.cs
-
39BlazorSolution/MiniGamesTests/TicTacToeTest.cs
@ -0,0 +1,7 @@ |
|||||
|
namespace MiniGames.Client.ViewModel |
||||
|
{ |
||||
|
public interface ITicTacToe |
||||
|
{ |
||||
|
bool SpielerInput(int spielerIndex, int posIndex); |
||||
|
} |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
using MiniGames.Shared.Models; |
||||
|
|
||||
|
namespace MiniGames.Client.ViewModel |
||||
|
{ |
||||
|
public class TicTacToe : ITicTacToe |
||||
|
{ |
||||
|
TicTacToeModel Model; |
||||
|
|
||||
|
public TicTacToe(TicTacToeModel model) |
||||
|
{ |
||||
|
this.Model = model; |
||||
|
} |
||||
|
|
||||
|
public TicTacToeBrett Brett |
||||
|
{ |
||||
|
get { return Model.Brett; } |
||||
|
set { Model.Brett = value; } |
||||
|
} |
||||
|
|
||||
|
public bool SpielerInput(int spielerIndex, int posIndex) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,127 @@ |
|||||
|
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[,] 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 < 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; |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace MiniGames.Shared.Models |
||||
|
{ |
||||
|
public class TicTacToeModel |
||||
|
{ |
||||
|
public SpielerModel[] Spieler { get; set; } |
||||
|
public TicTacToeBrett Brett { get; set; } |
||||
|
} |
||||
|
} |
@ -0,0 +1,305 @@ |
|||||
|
using MiniGames.Shared.Models; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace MiniGamesTests |
||||
|
{ |
||||
|
public class TicTacToeBrettTest |
||||
|
{ |
||||
|
public TicTacToeBrett TestBrett( |
||||
|
int a = TicTacToeBrett.LEER, int b = TicTacToeBrett.LEER, int c = TicTacToeBrett.LEER, |
||||
|
int d = TicTacToeBrett.LEER, int e = TicTacToeBrett.LEER, int f = TicTacToeBrett.LEER, |
||||
|
int g = TicTacToeBrett.LEER, int h = TicTacToeBrett.LEER, int i = TicTacToeBrett.LEER |
||||
|
) |
||||
|
{ |
||||
|
return new( |
||||
|
new int[,] |
||||
|
{ |
||||
|
{ a, b, c, }, |
||||
|
{ d, e, f, }, |
||||
|
{ g, h, i, }, |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Set_FreiesFeldBelegenTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett brett = new(); |
||||
|
TicTacToeBrett erwartetesBrett = TestBrett( |
||||
|
TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, |
||||
|
0 |
||||
|
); |
||||
|
int pos = 4; |
||||
|
int wert = 0; |
||||
|
bool erwartetGesetzt = true; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenGesetzt = brett.Set(pos, wert); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetGesetzt, erhaltenGesetzt); |
||||
|
Assert.True(brett.Gleich(erwartetesBrett)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Set_BelegtesFeldBelegenTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett brett = TestBrett(1); |
||||
|
TicTacToeBrett erwartetesBrett = TestBrett(1); |
||||
|
int pos = 0; |
||||
|
int wert = 1; |
||||
|
bool erwartetGesetzt = false; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenGesetzt = brett.Set(pos, wert); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetGesetzt, erhaltenGesetzt); |
||||
|
Assert.True(brett.Gleich(erwartetesBrett)); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(-1)] |
||||
|
[InlineData(9)] |
||||
|
public void Set_OutOfBoundsTest(int pos) |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett brett = new(); |
||||
|
TicTacToeBrett erwartetesBrett = new(); |
||||
|
int wert = 0; |
||||
|
bool erwartetGesetzt = false; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenGesetzt = brett.Set(pos, wert); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetGesetzt, erhaltenGesetzt); |
||||
|
Assert.True(brett.Gleich(erwartetesBrett)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Gleich_LeereBretterGleichTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett b1 = new(); |
||||
|
TicTacToeBrett b2 = new(); |
||||
|
bool erwartetGleich = true; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenGleich = b1.Gleich(b2); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetGleich, erhaltenGleich); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Gleich_NichtLeereBretterGleichTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett b1 = TestBrett(1, 2, 3, 4, 5, 6, 7, 8, 9); |
||||
|
TicTacToeBrett b2 = TestBrett(1, 2, 3, 4, 5, 6, 7, 8, 9); |
||||
|
bool erwartetGleich = true; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenGleich = b1.Gleich(b2); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetGleich, erhaltenGleich); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Gleich_ErsteUngleichTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett b1 = TestBrett(1); |
||||
|
TicTacToeBrett b2 = TestBrett(2); |
||||
|
bool erwartetGleich = false; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenGleich = b1.Gleich(b2); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetGleich, erhaltenGleich); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Gleich_LetzteUngleichTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett b1 = TestBrett(1, 2, 3, 4, 5, 6, 7, 8, 9); |
||||
|
TicTacToeBrett b2 = TestBrett(1, 2, 3, 4, 5, 6, 7, 8, 10); |
||||
|
bool erwartetGleich = false; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenGleich = b1.Gleich(b2); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetGleich, erhaltenGleich); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Voll_LeeresBrettTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett brett = TestBrett(); |
||||
|
bool erwartetVoll = false; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenVoll = brett.Voll(); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetVoll, erhaltenVoll); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Voll_VollesBrettTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett brett = TestBrett(1, 2, 3, 4, 5, 6, 7, 8, 9); |
||||
|
bool erwartetVoll = true; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenVoll = brett.Voll(); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetVoll, erhaltenVoll); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Voll_FastVollesBrettTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett brett = TestBrett(1, 2, 3, 4, 5, 6, 7, 8); |
||||
|
bool erwartetVoll = false; |
||||
|
|
||||
|
// act
|
||||
|
bool erhaltenVoll = brett.Voll(); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwartetVoll, erhaltenVoll); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Gewinner_LeeresFeldTest() |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett brett = TestBrett(); |
||||
|
int erwarteterGewinner = TicTacToeBrett.LEER; |
||||
|
|
||||
|
// act
|
||||
|
int erhaltenerGewinner = brett.Gewinner(); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwarteterGewinner, erhaltenerGewinner); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
// Vertikale Reihe 1
|
||||
|
[InlineData(0, |
||||
|
0, 0, 0, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, TicTacToeBrett.LEER |
||||
|
)] |
||||
|
// Vertikale Reihe 2
|
||||
|
[InlineData(1, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, TicTacToeBrett.LEER, |
||||
|
1, 1, 1, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, TicTacToeBrett.LEER |
||||
|
)] |
||||
|
// Vertikale Reihe 3
|
||||
|
[InlineData(2, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, TicTacToeBrett.LEER, |
||||
|
2, 2, 2 |
||||
|
)] |
||||
|
// Horizontale Reihe 1
|
||||
|
[InlineData(3, |
||||
|
3, TicTacToeBrett.LEER, TicTacToeBrett.LEER, |
||||
|
3, TicTacToeBrett.LEER, TicTacToeBrett.LEER, |
||||
|
3, TicTacToeBrett.LEER, TicTacToeBrett.LEER |
||||
|
)] |
||||
|
// Horizontale Reihe 2
|
||||
|
[InlineData(4, |
||||
|
TicTacToeBrett.LEER, 4, TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, 4, TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, 4, TicTacToeBrett.LEER |
||||
|
)] |
||||
|
// Horizontale Reihe 3
|
||||
|
[InlineData(5, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, 5, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, 5, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, 5 |
||||
|
)] |
||||
|
// Diagonale Reihe links oben nach rechts unten
|
||||
|
[InlineData(6, |
||||
|
6, TicTacToeBrett.LEER, TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, 6, TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, 6 |
||||
|
)] |
||||
|
// Diagonale Reihe rechts oben nach links unten
|
||||
|
[InlineData(7, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, 7, |
||||
|
TicTacToeBrett.LEER, 7, TicTacToeBrett.LEER, |
||||
|
7, TicTacToeBrett.LEER, TicTacToeBrett.LEER |
||||
|
)] |
||||
|
public void Gewinner_VolleReihenTest(int gewinner, |
||||
|
int a = TicTacToeBrett.LEER, int b = TicTacToeBrett.LEER, int c = TicTacToeBrett.LEER, |
||||
|
int d = TicTacToeBrett.LEER, int e = TicTacToeBrett.LEER, int f = TicTacToeBrett.LEER, |
||||
|
int g = TicTacToeBrett.LEER, int h = TicTacToeBrett.LEER, int i = TicTacToeBrett.LEER |
||||
|
) |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett brett = TestBrett(a, b, c, d, e, f, g, h, i); |
||||
|
int erwarteterGewinner = gewinner; |
||||
|
|
||||
|
// act
|
||||
|
int erhaltenerGewinner = brett.Gewinner(); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwarteterGewinner, erhaltenerGewinner); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData( |
||||
|
0, TicTacToeBrett.LEER, 0, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, TicTacToeBrett.LEER, |
||||
|
0, TicTacToeBrett.LEER, 0 |
||||
|
)] |
||||
|
[InlineData( |
||||
|
1, 1, TicTacToeBrett.LEER, |
||||
|
1, 1, TicTacToeBrett.LEER, |
||||
|
TicTacToeBrett.LEER, TicTacToeBrett.LEER, TicTacToeBrett.LEER |
||||
|
)] |
||||
|
[InlineData( |
||||
|
2, TicTacToeBrett.LEER, TicTacToeBrett.LEER, |
||||
|
2, TicTacToeBrett.LEER, 2, |
||||
|
TicTacToeBrett.LEER, 2, 2 |
||||
|
)] |
||||
|
[InlineData( |
||||
|
1, 1, 2, |
||||
|
1, 3, 2, |
||||
|
2, 3, 3 |
||||
|
)] |
||||
|
public void Gewinner_NichtVolleReihenTest( |
||||
|
int a = TicTacToeBrett.LEER, int b = TicTacToeBrett.LEER, int c = TicTacToeBrett.LEER, |
||||
|
int d = TicTacToeBrett.LEER, int e = TicTacToeBrett.LEER, int f = TicTacToeBrett.LEER, |
||||
|
int g = TicTacToeBrett.LEER, int h = TicTacToeBrett.LEER, int i = TicTacToeBrett.LEER |
||||
|
) |
||||
|
{ |
||||
|
// arrange
|
||||
|
TicTacToeBrett brett = TestBrett(a, b, c, d, e, f, g, h, i); |
||||
|
int erwarteterGewinner = TicTacToeBrett.LEER; |
||||
|
|
||||
|
// act
|
||||
|
int erhaltenerGewinner = brett.Gewinner(); |
||||
|
|
||||
|
// assert
|
||||
|
Assert.Equal(erwarteterGewinner, erhaltenerGewinner); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,39 @@ |
|||||
|
using MiniGames.Shared.Models; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace MiniGamesTests |
||||
|
{ |
||||
|
public class TicTacToeTest |
||||
|
{ |
||||
|
TicTacToeBrett StandardBrett() |
||||
|
{ |
||||
|
return new TicTacToeBrett(); |
||||
|
} |
||||
|
|
||||
|
SpielerModel[] StandardSpieler() |
||||
|
{ |
||||
|
return new SpielerModel[] |
||||
|
{ |
||||
|
new SpielerModel |
||||
|
{ |
||||
|
SpielerName = "Spieler 1", |
||||
|
Punkte = 0 |
||||
|
}, |
||||
|
new SpielerModel |
||||
|
{ |
||||
|
SpielerName = "Spieler 2", |
||||
|
Punkte = 1 |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
TicTacToeModel StandardModel() |
||||
|
{ |
||||
|
return new TicTacToeModel |
||||
|
{ |
||||
|
Spieler = StandardSpieler(), |
||||
|
Brett = StandardBrett(), |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue