From 8e9f6221722527fce1742fdafde20698dcbe3415 Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Thu, 10 Feb 2022 20:14:32 +0100 Subject: [PATCH] tictactoe: added test case to check if player 1 has won in column 0 with full board --- src/main/java/de/tims/tictactoe/GameLogic.java | 9 ++++++--- src/test/java/de/tims/tictactoe/GameLogicTest.java | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index 2a71983..b60bf3d 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/src/main/java/de/tims/tictactoe/GameLogic.java @@ -47,16 +47,19 @@ public class GameLogic { // check columns for (int i = 0; i < this.board.length; i++) { + if(countFields == this.board.length) return finished = true; + countFields = 0; for (int j = 0; j < this.board[0].length; j++) { - if(board[i][j] == player) countFields++; + if(board[j][i] == player) countFields++; } + } - if(countFields == this.board.length) finished = true; + countFields = 0; // check rows for (int i = 0; i < this.board[0].length; i++) { for (int j = 0; j < this.board.length; j++) { - if(board[j][i] == player) countFields++; + if(board[i][j] == player) countFields++; } } if(countFields == this.board.length) finished = true; diff --git a/src/test/java/de/tims/tictactoe/GameLogicTest.java b/src/test/java/de/tims/tictactoe/GameLogicTest.java index a9a184c..075945d 100644 --- a/src/test/java/de/tims/tictactoe/GameLogicTest.java +++ b/src/test/java/de/tims/tictactoe/GameLogicTest.java @@ -169,7 +169,11 @@ class GameLogicTest { Arguments.of("check win in row 2 for player 2", 'o', true, new char[][] {{'-', '-', '-'}, {'-', '-', '-'}, - {'o', 'o', 'o'}}) + {'o', 'o', 'o'}}), + Arguments.of("check win in column 0 for player 1 with full board", 'x', true, new char[][] + {{'x', 'o', 'o'}, + {'x', 'o', 'x'}, + {'x', 'x', 'o'}}) ); }