From 4bdc3eeb5dd3178e100165dc4d57cad30251f530 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Fri, 3 May 2024 19:15:35 +0200 Subject: [PATCH] Updated the player's narrow gameplay with PlaceTile on Game.cs and made Program.cs acceptable --- Qwirkle/QwirkleClassLibrary/Game.cs | 59 ++++++++------ Qwirkle/QwirkleClassLibrary/IPlayer.cs | 6 +- Qwirkle/QwirkleConsoleApp/Program.cs | 102 ++++++++++++------------- 3 files changed, 86 insertions(+), 81 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index f6d7623..412b2b0 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -59,6 +59,11 @@ namespace QwirkleClassLibrary { this.GameRunning = true; } + + public Player GetPlayingPlayer() + { + return players[GetPlayingPlayerPosition()]; + } public int GetPlayingPlayerPosition() { @@ -91,22 +96,6 @@ namespace QwirkleClassLibrary } } - public bool PlaceTileGame(Tile tile, int x, int y) - { - bool checkremove = false; - bool checkaddcell = board.AddTileInCell(x, y, tile); - if (checkaddcell == true) - { - checkremove = players[GetPlayingPlayerPosition()].RemoveTileToPlayer(tile); - } - - if (checkaddcell == checkremove) - { - return checkaddcell; - } - return false; - } - public string SetFirstPlayer() { players[0].IsPlaying = true; @@ -128,18 +117,40 @@ namespace QwirkleClassLibrary return players[GetPlayingPlayerPosition()].NameTag; } - public void PlaceTile(Player player, Tile tile, int x, int y) + public bool PlaceTile(Player player, Tile tile, int x, int y) { - throw new NotImplementedException(); + if (board.AddTileInCell(x, y, tile) == true) + { + return player.RemoveTileToPlayer(tile) == true; + } + else + { + return false; + } } - - public bool ContinueToPlay() + + public bool SwapTiles(Player player) { - throw new NotImplementedException(); - } - } + if (bag.TilesBag.Count < 6) + { + return false; + } + for (int i = 0; i < player.Tiles.Count; i++) + { + Random random = new Random(); + int val = random.Next(0, bag.TilesBag.Count); + player.AddTileToPlayer(bag.TilesBag[val]); + bag.RemoveTileInBag(bag.TilesBag[val]); + } -} + return true; + } + public bool DrawTile(Player player) + { + return true; + } + } +} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/IPlayer.cs b/Qwirkle/QwirkleClassLibrary/IPlayer.cs index 014a6b9..0dd494a 100644 --- a/Qwirkle/QwirkleClassLibrary/IPlayer.cs +++ b/Qwirkle/QwirkleClassLibrary/IPlayer.cs @@ -6,7 +6,9 @@ public interface IPlayer public string SetNextPlayer(); - public void PlaceTile(Player player, Tile tile, int x, int y); + public bool PlaceTile(Player player, Tile tile, int x, int y); - public bool ContinueToPlay(); + public bool SwapTiles(Player player); + + public bool DrawTile(Player player); } \ No newline at end of file diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index 47fbd49..f329073 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -34,48 +34,55 @@ static void AddPlayers(Game game) } } -static void MainMenu(Game game) -{ - game.GiveTilesToPlayers(); - - Console.ForegroundColor = ConsoleColor.Green; - WriteLine("Game is starting !"); - Console.ResetColor(); - - do - { - string tagPlayerPlay = game.SetNextPlayer(); - - WriteLine(" --------------------- GAME ! ------------------------"); - WriteLine(tagPlayerPlay + "'s turn !"); - - MenuSwitch(game); - - } while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1); - -} static void ShowTiles(Game game) { WriteLine("\n --------------------- YOUR TILES ------------------------"); - int pos = game.GetPlayingPlayerPosition(); + var currentPlayer = game.GetPlayingPlayer(); - StringBuilder stringBuilder = new StringBuilder(); + var stringBuilder = new StringBuilder(); - for (int i = 0; i < game.PlayerList[pos].Tiles.Count(); i++) + for (int i = 0; i < currentPlayer.Tiles.Count(); i++) { stringBuilder.Append("[" + (i+1) + "] "); - stringBuilder.AppendLine(game.PlayerList[pos].Tiles[i].ToString()); + stringBuilder.AppendLine(currentPlayer.Tiles[i].ToString()); } Write(stringBuilder); +} + +static void AddTile(Game game) +{ + Tile? tile = null; + Write("Enter the number of the tile you want to place : "); + int no = Convert.ToInt32(ReadLine()); + while (no < 1 || no > 6) + { + Write("ERROR : Enter a number between 1 and 6 ! : "); + no = Convert.ToInt32(ReadLine()); + } + + tile = game.TileOfPlayerWithPos(no - 1); + + Write("Enter the x of the cell: "); + int x = Convert.ToInt32(ReadLine()); + Write("Enter the y of the cell : "); + int y = Convert.ToInt32(ReadLine()); + + if (game.PlaceTile(game.GetPlayingPlayer(), tile, x, y) == true) + { + WriteLine("ok ! your tile is placed"); + } + else + { + WriteLine("ERROR : Cell already used"); + } } static void MenuSwitch(Game game) { - int enter = 0; while (enter != 3) @@ -93,48 +100,34 @@ static void MenuSwitch(Game game) switch (enter) { case 1: - CaseOneAddTile(game); + AddTile(game); break; case 2: break; case 3: return; - } - } - - } -static void CaseOneAddTile(Game game) +static void MainMenu(Game game) { - Tile? tile = null; - Write("Enter no tile : "); - int no = Convert.ToInt32(ReadLine()); + game.GiveTilesToPlayers(); - if (no >= 0 && no <= 5) + Console.ForegroundColor = ConsoleColor.Green; + WriteLine("Game is starting !"); + Console.ResetColor(); + + do { - tile = game.TileOfPlayerWithPos(no+1); - Write("Enter x : "); - int x = Convert.ToInt32(ReadLine()); - Write("Enter y : "); - int y = Convert.ToInt32(ReadLine()); - if (game.PlaceTileGame(tile, x, y) == true) - { - Write("ok ! your tile is placed \n"); + string tagPlayerPlay = game.SetNextPlayer(); + + WriteLine(" --------------------- GAME ! ------------------------"); + WriteLine(tagPlayerPlay + "'s turn !"); - } - else - { - Write("ERROR : Cell already use \n"); - } - } - else - { - Write("No is out of range\n"); - } + MenuSwitch(game); + } while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1); } static void MainGame() @@ -151,7 +144,6 @@ static void MainGame() game.StartGame(); MainMenu(game); - } -MainGame(); \ No newline at end of file +MainGame();