From 1cc13d28a4a8baf1dba32da3ea95da1f37059d50 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Tue, 23 Apr 2024 16:09:47 +0200 Subject: [PATCH] Big modification of the Game for it to feature the first part of the IPlayer interface --- Qwirkle/QwirkleClassLibrary/Game.cs | 105 ++++++++----------------- Qwirkle/QwirkleClassLibrary/IPlayer.cs | 2 +- Qwirkle/QwirkleClassLibrary/Player.cs | 6 +- Qwirkle/QwirkleConsoleApp/Program.cs | 103 ++++++++++++------------ Qwirkle/TestBase/UnitTest1.cs | 4 +- 5 files changed, 89 insertions(+), 131 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 57f4a5d..45f94fa 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -6,16 +6,15 @@ using System.Text; using System.Threading.Tasks; using System.Xml.Linq; - namespace QwirkleClassLibrary { public class Game : IPlayer { - private TileBag bag; - private bool gameRunning; + private TileBag bag; + public bool GameRunning { get; private set; } private Board board; - public ReadOnlyCollection PlayerList { get; private set; } + public ReadOnlyCollection PlayerList { get; } private readonly List players; public Game() @@ -27,11 +26,9 @@ namespace QwirkleClassLibrary PlayerList = players.AsReadOnly(); } - public bool AddPlayerInGame(string? PlayerTag) + public bool AddPlayerInGame(string? playerTag) { - bool nameInvalid = string.IsNullOrWhiteSpace(PlayerTag); - - if (nameInvalid == true || this.gameRunning == true || PlayerTag == null) + if (string.IsNullOrWhiteSpace(playerTag) == true || this.GameRunning == true) { return false; } @@ -41,16 +38,15 @@ namespace QwirkleClassLibrary return false; } - for (int i = 0; i < players.Count; i++) + foreach (var p in players) { - if (players[i].NameTag == PlayerTag) + if (p.NameTag == playerTag) { return false; } } - Player p = new Player(PlayerTag); - players.Add(p); + players.Add(CreatePlayer(playerTag)); return true; } @@ -61,15 +57,9 @@ namespace QwirkleClassLibrary return player; } - public bool StartGame() + public void StartGame() { - if (players.Count < 2) - { - return false; - } - - this.gameRunning = true; - return true; + this.GameRunning = true; } public int GetPlayingPlayerPosition() @@ -83,71 +73,34 @@ namespace QwirkleClassLibrary } return -1; } - - public int GetNbPlayers - { - get { return players.Count; } - } - - + public Tile TileOfPlayerWithPos(int postile) { return players[GetPlayingPlayerPosition()].Tiles[postile]; } - public void SetNextPlayer(int old, int neew) - { - if (old >= 0 || old != -1) - { - players[old].IsPlaying = false; - } - players[neew].IsPlaying = true; - - } - - public void TilsBagPlayer() + public void GiveTilesToPlayers() { - for (int i = 0; i < players.Count; i++) + foreach (var p in players) { for (int j = 0; j < 6; j++) { Random random = new Random(); int val = random.Next(0, bag.TilesBag.Count); - Tile tile = bag.TilesBag[val]; - players[i].AddTilePlayer(tile); - bag.RemoveTileInBag(tile); + + p.AddTileToPlayer(bag.TilesBag[val]); + bag.RemoveTileInBag(bag.TilesBag[val]); } } } - public string NextPlayer() - { - int posPlayerPlay = GetPlayingPlayerPosition(); - - int posPlayerNextPlay = GetPlayingPlayerPosition() + 1; - - if (posPlayerNextPlay > GetNbPlayers) - { - posPlayerNextPlay = 0; - } - - SetNextPlayer(posPlayerPlay, posPlayerNextPlay); - - return (players[posPlayerNextPlay].NameTag); - } - - public bool GameRunning - { - get { return gameRunning; } - } - public bool PlaceTileGame(Tile tile, int x, int y) { - bool checkremove=false; + bool checkremove = false; bool checkaddcell = board.AddTileInCell(x, y, tile); if (checkaddcell == true) { - checkremove = players[GetPlayingPlayerPosition()].RemoveTilePlayer(tile); + checkremove = players[GetPlayingPlayerPosition()].RemoveTileToPlayer(tile); } if (checkaddcell == checkremove) @@ -157,15 +110,25 @@ namespace QwirkleClassLibrary return false; } - public void SetNextPlayer(List playersList) + public string SetFirstPlayer() + { + players[0].IsPlaying = true; + return players[0].NameTag; + } + + public string SetNextPlayer() { - for(int i = 0; i < PlayerList.Count; i++) + int i = GetPlayingPlayerPosition(); + + if (i == -1) { - if (PlayerList[i].IsPlaying != true) continue; - - PlayerList[i].IsPlaying = false; - PlayerList[(i + 1) % PlayerList.Count].IsPlaying = true; + return SetFirstPlayer(); } + + players[i].IsPlaying = false; + players[(i + 1) % players.Count].IsPlaying = true; + + return players[GetPlayingPlayerPosition()].NameTag; } public void PlaceTile(Player player, Tile tile, int x, int y) diff --git a/Qwirkle/QwirkleClassLibrary/IPlayer.cs b/Qwirkle/QwirkleClassLibrary/IPlayer.cs index 8b9a4a1..014a6b9 100644 --- a/Qwirkle/QwirkleClassLibrary/IPlayer.cs +++ b/Qwirkle/QwirkleClassLibrary/IPlayer.cs @@ -4,7 +4,7 @@ public interface IPlayer { public Player CreatePlayer(string playerTag); - public void SetNextPlayer(List players); + public string SetNextPlayer(); public void PlaceTile(Player player, Tile tile, int x, int y); diff --git a/Qwirkle/QwirkleClassLibrary/Player.cs b/Qwirkle/QwirkleClassLibrary/Player.cs index 3e424c6..60ef662 100644 --- a/Qwirkle/QwirkleClassLibrary/Player.cs +++ b/Qwirkle/QwirkleClassLibrary/Player.cs @@ -25,16 +25,16 @@ namespace QwirkleClassLibrary Tiles = playerTiles.AsReadOnly(); } - public string NameTag { get; set; } + public string NameTag { get; } public bool IsPlaying { get; set; } = false; - public void AddTilePlayer(Tile tile) + public void AddTileToPlayer(Tile tile) { playerTiles.Add(tile); } - public bool RemoveTilePlayer(Tile tile) + public bool RemoveTileToPlayer(Tile tile) { return playerTiles.Remove(tile); } diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index 0678497..47fbd49 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -4,56 +4,61 @@ using System.Text; using System.Transactions; using static System.Console; - -static void addPlayer(Game game) +static void AddPlayers(Game game) { - string? enterline = ""; - - do + while (game.PlayerList.Count < 4) { - WriteLine("Enter name of player (enter quit to quit) : "); - enterline = ReadLine(); - if (enterline != "quit") + Write("Enter player tag : "); + string? playerTag = ReadLine(); + + if (game.AddPlayerInGame(playerTag) == false) { - bool r = game.AddPlayerInGame(enterline); - if (r == false) - { - WriteLine("ERROR : Name is invalid."); - } + WriteLine("ERROR : Player already exist or game is running !"); } + else + { + WriteLine("Player " + playerTag + " added !"); + } + + Write("Do you want to add another player ? (y/n) : "); + string? answer = ReadLine(); - } while (game.PlayerList.Count<4 && enterline !="quit"); + if (answer == "n" && game.PlayerList.Count >= 2) + { + break; + } + if (answer == "n" && game.PlayerList.Count < 2) + { + WriteLine("ERROR : You must have at least 2 players !"); + } + } } static void MainMenu(Game game) { - game.TilsBagPlayer(); + game.GiveTilesToPlayers(); Console.ForegroundColor = ConsoleColor.Green; - Write("The loading of the game is well done! Good game :p\n"); + WriteLine("Game is starting !"); Console.ResetColor(); - + do { - String TagPlayerPlay; - TagPlayerPlay = game.NextPlayer(); - - Write(" --------------------- GAME ! ------------------------ \n"); - - - Write(TagPlayerPlay + " you have main now ! \n"); + string tagPlayerPlay = game.SetNextPlayer(); + + WriteLine(" --------------------- GAME ! ------------------------"); + WriteLine(tagPlayerPlay + "'s turn !"); MenuSwitch(game); - } while (game.GetPlayingPlayerPosition() != 3); // cette boucle permet juste de faire un tour, quand le score fonctionnera il faudra la faire arreter quand la partie sera finis :) + } while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1); } static void ShowTiles(Game game) { - Write("\n --------------------- YOUR TILES ------------------------ \n"); - - + WriteLine("\n --------------------- YOUR TILES ------------------------"); + int pos = game.GetPlayingPlayerPosition(); StringBuilder stringBuilder = new StringBuilder(); @@ -72,15 +77,16 @@ static void MenuSwitch(Game game) { int enter = 0; - do + + while (enter != 3) { ShowTiles(game); - Write("\n --------------------- CHOICES ------------------------ \n"); + WriteLine("\n --------------------- CHOICES ------------------------"); - Write("[1] Place your tiles \n"); - Write("[2] Swap your tiles \n"); - Write("[3] Skip \n"); + WriteLine("[1] Place your tiles"); + WriteLine("[2] Swap your tiles"); + WriteLine("[3] Skip your turn"); enter = Convert.ToInt32(ReadLine()); @@ -95,7 +101,8 @@ static void MenuSwitch(Game game) return; } - } while (enter != 3); + + } } @@ -133,30 +140,18 @@ static void CaseOneAddTile(Game game) static void MainGame() { Game game = new Game(); - - - Console.BackgroundColor = ConsoleColor.DarkGreen; - Write("WELCOME IN QWIRKLE GAME ! \n For start the game please enter 2 / 4 players ;) \n \n"); + + Console.ForegroundColor = ConsoleColor.DarkGreen; + WriteLine(" ===================== WELCOME TO QWIRKLE ! ====================="); + WriteLine("Enter the players' nametags (2 to 4 players) : "); Console.ResetColor(); - addPlayer(game); + + AddPlayers(game); game.StartGame(); - - - while (game.GameRunning == false) - { - game = new Game(); - Console.ForegroundColor= ConsoleColor.Red; - Write("ERROR : Please enter minimun 2 valid player ! \n"); - Console.ResetColor(); - addPlayer(game); - - game.StartGame(); - - } + MainMenu(game); } - -MainGame(); +MainGame(); \ No newline at end of file diff --git a/Qwirkle/TestBase/UnitTest1.cs b/Qwirkle/TestBase/UnitTest1.cs index c60e5bf..a619600 100644 --- a/Qwirkle/TestBase/UnitTest1.cs +++ b/Qwirkle/TestBase/UnitTest1.cs @@ -26,7 +26,7 @@ namespace TestBase Tile tile = new Tile(Shape.Round, Color.Orange); - p.AddTilePlayer(tile); + p.AddTileToPlayer(tile); bool r = false; @@ -48,7 +48,7 @@ namespace TestBase Tile tile = new Tile(Shape.Round, Color.Orange); - p.RemoveTilePlayer(tile); + p.RemoveTileToPlayer(tile); bool r = true;