From 6c9fc338d29124685d6381b1a8e8ced94248bd5f Mon Sep 17 00:00:00 2001 From: Felix Detig Date: Fri, 11 Feb 2022 23:39:05 +0100 Subject: [PATCH] =?UTF-8?q?TicTacToeBrett.Set=20gibt=20bei=20ung=C3=BCltig?= =?UTF-8?q?em=20Index=20false=20zur=C3=BCck=20und=20ver=C3=A4ndert=20das?= =?UTF-8?q?=20Brett=20nicht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MiniGames/Shared/Models/TicTacToeBrett.cs | 11 +++++++++-- .../MiniGamesTests/TicTacToeBrettTest.cs | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/BlazorSolution/MiniGames/Shared/Models/TicTacToeBrett.cs b/BlazorSolution/MiniGames/Shared/Models/TicTacToeBrett.cs index 460e7f2..e0dfb2b 100644 --- a/BlazorSolution/MiniGames/Shared/Models/TicTacToeBrett.cs +++ b/BlazorSolution/MiniGames/Shared/Models/TicTacToeBrett.cs @@ -45,9 +45,16 @@ namespace MiniGames.Shared.Models { int x = pos / 3; int y = pos % 3; - - if (Felder[x, y] != -1) + + try + { + if (Felder[x, y] != -1) + return false; + } + catch (IndexOutOfRangeException e) + { return false; + } Felder[x, y] = wert; diff --git a/BlazorSolution/MiniGamesTests/TicTacToeBrettTest.cs b/BlazorSolution/MiniGamesTests/TicTacToeBrettTest.cs index ac787a9..3c70b96 100644 --- a/BlazorSolution/MiniGamesTests/TicTacToeBrettTest.cs +++ b/BlazorSolution/MiniGamesTests/TicTacToeBrettTest.cs @@ -62,6 +62,25 @@ namespace MiniGamesTests 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() {