diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index bb41dac..50a5c80 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -50,47 +50,75 @@ namespace QwirkleClassLibrary.Games protected virtual void OnEndOfGame(EndOfGameNotifiedEventArgs args) => EndOfGameNotified?.Invoke(this, args); - + /// /// Adds a player in the game if the game is not running, if the name is correct, if the game is not full and if the name is not already taken. /// /// /// boolean to check it - public bool AddPlayerInGame(string? playerTag) + public bool AddPlayerInGame(List PlayersTag) { - if (string.IsNullOrWhiteSpace(playerTag)) + for (int i = PlayersTag.Count - 1; i >= 0; i--) + { + if (string.IsNullOrWhiteSpace(PlayersTag[i])) + { + PlayersTag.RemoveAt(i); + } + } + + if (PlayersTag.Count <= 1 || PlayersTag.Count > 4) { - OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The name is null or white space.")); + PlayersTag.Clear(); + OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : It takes a minimum of 2 players and a maximum of 4 players to start a game.")); return false; } + for (int i = PlayersTag.Count - 1; i >= 0; i--) + { + if (!CheckPlayerTag(PlayersTag, i)) + { + PlayersTag.RemoveAt(i); + return false; + } + } - if (GameRunning) + foreach (var tag in PlayersTag) { - OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is running.")); + Player pl = CreatePlayer(tag); + players.Add(pl); + scoreBoard.Add(pl, 0); + } + + OnPlayerNotified(new AddPlayerNotifiedEventArgs("Players were correctly added.")); + return true; + } + + public bool CheckPlayerTag(List PlayersTag, int pos) + { + if (string.IsNullOrWhiteSpace(PlayersTag[pos])) + { + OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR with " + (pos + 1) + " entry : The name is null or white space.")); return false; } - if (players.Count >= 4) + if (GameRunning) { - OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is full.")); + OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR with " + (pos + 1) + " entry : The game is running.")); return false; } - foreach (var p in players) + for(int i=0; i /// Board public Board? GetBoard() { return board; } - + /// /// Returns the tile bag of the game /// @@ -240,7 +268,7 @@ namespace QwirkleClassLibrary.Games OnNextPlayer(new NextPlayerNotifiedEventArgs(players[0])); return players[0].NameTag; } - + throw new ArgumentException("Game is not running"); } @@ -386,7 +414,7 @@ namespace QwirkleClassLibrary.Games return true; } - + /// /// Extension of IsMoveCorrect to check if the tiles are on the same line /// @@ -481,7 +509,7 @@ namespace QwirkleClassLibrary.Games return false; } } - + if (!CheckTilesInLine(cellUsed, b, x, y)) { @@ -494,11 +522,11 @@ namespace QwirkleClassLibrary.Games OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't place a tile that isn't adjacent to another one !")); return false; } - + return true; } - + /// /// Main method to get the score of the player after he played his turn /// @@ -512,14 +540,14 @@ namespace QwirkleClassLibrary.Games { return 0; } - + int score = cellsPlayed.Count; - + if (cellsPlayed.Count == 6) { score += 6; } - + int cellsX = cellsPlayed[0].GetX; int cellsY = cellsPlayed[0].GetY; @@ -531,7 +559,7 @@ namespace QwirkleClassLibrary.Games { cellsX = -1; } - + else if (cellsY != cell.GetY && cellsY != -1) { cellsY = -1; @@ -566,7 +594,7 @@ namespace QwirkleClassLibrary.Games public int CalculateAdjacentScore(Cell cell, Board b, ReadOnlyCollection cellsPlayed, int cellsX, int cellsY) { int score = 0; - + var surroundingCells = new[] { b.GetCell(cell.GetX + 1, cell.GetY), @@ -574,20 +602,20 @@ namespace QwirkleClassLibrary.Games b.GetCell(cell.GetX, cell.GetY + 1), b.GetCell(cell.GetX, cell.GetY - 1) }; - + foreach (var adjacentCell in surroundingCells) { if (adjacentCell?.GetTile == null || cellsPlayed.Contains(adjacentCell)) { continue; } - + int dx = adjacentCell.GetX - cell.GetX; int dy = adjacentCell.GetY - cell.GetY; - + score += CalculateLineScore(cell, dx, dy, b, cellsX, cellsY, cellsPlayed.Count); } - + return score; } @@ -623,7 +651,7 @@ namespace QwirkleClassLibrary.Games if (dx != 0 && cellsY != -1 && i + nbCellsPlayed == 6 || dy != 0 && cellsX != -1 && i + nbCellsPlayed == 6) { score += 6; - } + } score++; } @@ -635,7 +663,7 @@ namespace QwirkleClassLibrary.Games return score; } - + /// /// Returns the list of the positions of the players who still have tiles in their bag diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index e98a73b..989895b 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -1,4 +1,5 @@ using QwirkleClassLibrary.Boards; +using QwirkleClassLibrary.Events; using QwirkleClassLibrary.Games; using QwirkleClassLibrary.Players; using QwirkleClassLibrary.Tiles; @@ -17,28 +18,23 @@ static void AddPlayers(Game game) NotificationClass nc = new NotificationClass(); game.PlayerAddNotified += nc.NotificationPlayerAdd; + List playerstag = []; + while (game.PlayerList.Count < 4) { Write("Enter player tag : "); - string? playerTag = ReadLine(); + string? tag = ReadLine(); - game.AddPlayerInGame(playerTag); + playerstag.Add(tag!); Write("Do you want to add another player ? (y/n) : "); string? answer = ReadLine(); - if (answer == "n" && game.PlayerList.Count >= 2) + if (answer == "n" && game.AddPlayerInGame(playerstag)) { break; } - if (answer == "n" && game.PlayerList.Count < 2) - { - ForegroundColor = ConsoleColor.Red; - WriteLine(); - WriteLine("ERROR : You must have at least 2 players !"); - ResetColor(); - } } } diff --git a/Qwirkle/QwirkleViews/App.xaml.cs b/Qwirkle/QwirkleViews/App.xaml.cs index 5d6fc10..263106d 100644 --- a/Qwirkle/QwirkleViews/App.xaml.cs +++ b/Qwirkle/QwirkleViews/App.xaml.cs @@ -14,6 +14,7 @@ namespace Qwirkle MainPage = new AppShell(); Routing.RegisterRoute(nameof(SetPlayers), typeof(SetPlayers)); + Routing.RegisterRoute(nameof(Gameboard), typeof(Gameboard)); } diff --git a/Qwirkle/QwirkleViews/MainPage.xaml b/Qwirkle/QwirkleViews/MainPage.xaml index 364a29c..413e741 100644 --- a/Qwirkle/QwirkleViews/MainPage.xaml +++ b/Qwirkle/QwirkleViews/MainPage.xaml @@ -26,8 +26,6 @@ InfoClicked="OnInfoClicked" /> - - diff --git a/Qwirkle/QwirkleViews/Pages/SetPlayers.xaml.cs b/Qwirkle/QwirkleViews/Pages/SetPlayers.xaml.cs index 50f14e9..391aeff 100644 --- a/Qwirkle/QwirkleViews/Pages/SetPlayers.xaml.cs +++ b/Qwirkle/QwirkleViews/Pages/SetPlayers.xaml.cs @@ -46,16 +46,15 @@ public partial class SetPlayers : ContentPage { game.PlayerAddNotified += Game_PlayerAddNotified; - string entry1 = Entry1.TextIn!; - string entry2 = Entry2.TextIn!; - string entry3 = Entry3.TextIn!; - string entry4 = Entry4.TextIn!; - DisplayAlert("Game notification", entry1 + "\n" + entry2 + "\n" + entry3 + "\n" + entry4, "Ok ! Lets's go !"); + List playerstag = [Entry1.TextIn!, Entry2.TextIn!, Entry3.TextIn!, Entry3.TextIn!]; + + if (game.AddPlayerInGame(playerstag)) + { + Navigation.PushAsync(new Gameboard()); + } + + game.PlayerAddNotified -= Game_PlayerAddNotified; - game.AddPlayerInGame(entry1); - game.AddPlayerInGame(entry2); - game.AddPlayerInGame(entry3); - game.AddPlayerInGame(entry4); } private void Game_PlayerAddNotified(object? sender, QwirkleClassLibrary.Events.AddPlayerNotifiedEventArgs args) diff --git a/Qwirkle/TestBase/TestGame.cs b/Qwirkle/TestBase/TestGame.cs index 82704f7..ad43a73 100644 --- a/Qwirkle/TestBase/TestGame.cs +++ b/Qwirkle/TestBase/TestGame.cs @@ -8,33 +8,30 @@ namespace TestBase; public class TestGame { - - - [Theory] - [InlineData(true, false, "testt")] - [InlineData(false, false, "testt")] - [InlineData(false, true, "testt")] - public void Test_GameAddPlayerIngame(bool result, bool gamestate, string p) + [InlineData(true, false, "test", "test1")] + [InlineData(false, false, "test", "test1")] + [InlineData(false, true, "test", "test1")] + public void Test_GameAddPlayerIngame(bool result, bool gamestate, string p1, string p2) { Game game = new Game(); + List players = [p1, p2]; if (!result) { - game.AddPlayerInGame(p); - Assert.False(game.AddPlayerInGame(p)); + game.AddPlayerInGame(players); + Assert.False(game.AddPlayerInGame(players)); } else { - Assert.True(game.AddPlayerInGame(p)); + Assert.True(game.AddPlayerInGame(players)); } if (gamestate) { game.StartGame(); - Assert.False(game.AddPlayerInGame(p)); + Assert.False(game.AddPlayerInGame(players)); } - } [Theory] @@ -43,21 +40,32 @@ public class TestGame public void Test_GameAddPlayerIngame2(bool result, string p) { Game game = new Game(); + List players = new List(); if (!result) { - Assert.False(game.AddPlayerInGame(p)); + players.Add(p); + Assert.False(game.AddPlayerInGame(players)); } + players.Clear(); for (int i = 0; i < 4; i++) { string name = p + i; - game.AddPlayerInGame(name); + players.Add(name); } - Assert.False(game.AddPlayerInGame(p)); + Assert.True(game.AddPlayerInGame(players)); + } + [Fact] + public void Test_AddPlayers() + { + Game game = new Game(); + List playerstest = new List { "test", "test1" }; + Assert.True(game.AddPlayerInGame(playerstest)); } + [Theory] [InlineData("test")] public void Test_GameCreatePlayers(string p) @@ -79,8 +87,8 @@ public class TestGame game.StartGame(); Assert.False(game.GameRunning); } - game.AddPlayerInGame(p1); - game.AddPlayerInGame(p2); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); Assert.True(game.GameRunning); @@ -93,9 +101,8 @@ public class TestGame public void Test_GameGetPlayingPlayerPosition(bool result, string p1, string p2, string p3) { Game game = new Game(); - game.AddPlayerInGame(p1); - game.AddPlayerInGame(p2); - game.AddPlayerInGame(p3); + List playerstest = ["test", "test1", "test2"]; + game.AddPlayerInGame(playerstest); if (!result) { @@ -113,8 +120,8 @@ public class TestGame public void Test_GameGetPlaylingPlayer(bool result) { Game game = new Game(); - game.AddPlayerInGame("patrick"); - game.AddPlayerInGame("jean"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); if (!result) { Assert.Throws(() => game.GetPlayingPlayer()); @@ -130,8 +137,8 @@ public class TestGame public void Test_BoardReturn() { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetFirstPlayer(); @@ -144,8 +151,8 @@ public class TestGame Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetFirstPlayer(); @@ -164,8 +171,8 @@ public class TestGame public void Test_TileOfPlayer() { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetFirstPlayer(); @@ -181,8 +188,8 @@ public class TestGame public void Test_SetFirstPlayer(bool except) { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); if (except) { @@ -201,8 +208,8 @@ public class TestGame public void Test_Setnextplayer(bool except) { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); if (except) { @@ -224,8 +231,8 @@ public class TestGame public void Test_PlaceTile(bool except) { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); if (except) { @@ -247,7 +254,7 @@ public class TestGame // { // Game game = new Game(); // game.AddPlayerInGame("Test1"); - // game.AddPlayerInGame("Test2"); + // game.AddPlayerInGame(playerstest); // // game.StartGame(); // game.SetNextPlayer(); @@ -267,8 +274,8 @@ public class TestGame public void Test_SwapTiles(bool except) { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetNextPlayer(); @@ -302,8 +309,8 @@ public class TestGame public void Test_IsMoveCorrectOne(bool except) { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetNextPlayer(); @@ -328,8 +335,8 @@ public class TestGame public void Test_IsMoveCorrectSixLine() { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetNextPlayer(); @@ -364,10 +371,10 @@ public class TestGame var game = new Game(); var player = new Player("TestPlayer"); var board = new Board(8, 8); - + board.AddTileInCell(1, 1, new Tile(Shape.Club, Color.Red)); board.AddTileInCell(2, 1, new Tile(Shape.Square, Color.Red)); - + var c1 = new Cell(x1, y1); var c2 = new Cell(x2, y2); var c3 = new Cell(x3, y3); @@ -387,33 +394,34 @@ public class TestGame Assert.Equal(expectedScore, score); } - + [Fact] public void Test_EndOfGame() { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetFirstPlayer(); game.CheckGameOver(game.GetPlayingPlayer()); - + } - + [Fact] public void Test_ClearGame() { Game game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetFirstPlayer(); game.ClearGame(); - + Assert.Empty(game.PlayerList); Assert.Null(game.GetTileBag()); Assert.Equal(-1, game.GetPlayingPlayerPosition()); @@ -421,3 +429,4 @@ public class TestGame } + diff --git a/Qwirkle/TestBase/TestLeaderboard.cs b/Qwirkle/TestBase/TestLeaderboard.cs index d9e4dce..a1db4ee 100644 --- a/Qwirkle/TestBase/TestLeaderboard.cs +++ b/Qwirkle/TestBase/TestLeaderboard.cs @@ -17,8 +17,8 @@ public class TestLeaderboard var leaderboard = new Leaderboard(); var game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetFirstPlayer(); @@ -37,8 +37,8 @@ public class TestLeaderboard var leaderboard = new Leaderboard(); var game = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + List playerstest = ["test", "test1"]; + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetFirstPlayer(); @@ -50,8 +50,7 @@ public class TestLeaderboard var game2 = new Game(); - game.AddPlayerInGame("Test1"); - game.AddPlayerInGame("Test2"); + game.AddPlayerInGame(playerstest); game.StartGame(); game.SetFirstPlayer();