From 5c66fd76f9cb70af614bc881420f2a1cd58f570d Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Sat, 4 May 2024 09:52:42 +0200 Subject: [PATCH] Enhanced PlaceTile method, added SwapTiles method, began to implement IRules.cs into Game.cs --- Qwirkle/QwirkleClassLibrary/Game.cs | 51 +++++++++++++++++++++++--- Qwirkle/QwirkleClassLibrary/IPlayer.cs | 2 +- Qwirkle/QwirkleClassLibrary/IRules.cs | 5 +-- Qwirkle/QwirkleConsoleApp/Program.cs | 40 +++++++++++++++++++- 4 files changed, 86 insertions(+), 12 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 21aa5ca..3783c16 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -9,7 +9,7 @@ using System.Security.Cryptography; namespace QwirkleClassLibrary { - public class Game : IPlayer + public class Game : IPlayer, IRules { private TileBag bag; public bool GameRunning { get; private set; } @@ -20,8 +20,8 @@ namespace QwirkleClassLibrary public Game() { - board = new Board(); bag = new TileBag(3); + board = CreateBoard(); } public bool AddPlayerInGame(string? playerTag) @@ -55,6 +55,12 @@ namespace QwirkleClassLibrary return player; } + public Board CreateBoard() + { + board = new Board(); + return board; + } + public void StartGame() { this.GameRunning = true; @@ -88,7 +94,7 @@ namespace QwirkleClassLibrary { for (int j = 0; j < 6; j++) { - int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count); + int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count + 1); p.AddTileToPlayer(bag.TilesBag[val]); bag.RemoveTileInBag(bag.TilesBag[val]); @@ -138,7 +144,7 @@ namespace QwirkleClassLibrary return false; } - int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count); + int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count + 1); player.AddTileToPlayer(bag.TilesBag[val]); bag.RemoveTileInBag(bag.TilesBag[val]); @@ -146,10 +152,43 @@ namespace QwirkleClassLibrary return true; } - - public bool SwapTiles(Player player) + + public bool SwapTiles(Player player, List tilesToSwap) + { + if (tilesToSwap.Count == 0) + { + return false; + } + + foreach (var t in tilesToSwap) + { + if (player.RemoveTileToPlayer(t) == false) + { + return false; + } + } + + if (DrawTiles(player) == false) + { + return false; + } + + foreach (var t in tilesToSwap) + { + bag.AddTileInBag(t); + } + + return true; + } + + public bool IsMoveCorrect(Tile t, Board b) { return false; } + + public bool IsGameOver() + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/IPlayer.cs b/Qwirkle/QwirkleClassLibrary/IPlayer.cs index 48eac76..264ea7c 100644 --- a/Qwirkle/QwirkleClassLibrary/IPlayer.cs +++ b/Qwirkle/QwirkleClassLibrary/IPlayer.cs @@ -10,5 +10,5 @@ public interface IPlayer public bool DrawTiles(Player player); - public bool SwapTiles(Player player); + public bool SwapTiles(Player player, List tilesToSwap); } \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/IRules.cs b/Qwirkle/QwirkleClassLibrary/IRules.cs index 83180f4..4d0456e 100644 --- a/Qwirkle/QwirkleClassLibrary/IRules.cs +++ b/Qwirkle/QwirkleClassLibrary/IRules.cs @@ -9,8 +9,7 @@ namespace QwirkleClassLibrary public interface IRules { Board CreateBoard(); - bool isMoveCorrect(Tile t, Cell c); - bool isGameOver(); - + bool IsMoveCorrect(Tile t, Board b); + bool IsGameOver(); } } diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index f329073..ef1a41e 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -58,7 +58,7 @@ static void AddTile(Game game) Write("Enter the number of the tile you want to place : "); int no = Convert.ToInt32(ReadLine()); - while (no < 1 || no > 6) + while (no is < 1 or > 6) { Write("ERROR : Enter a number between 1 and 6 ! : "); no = Convert.ToInt32(ReadLine()); @@ -81,6 +81,39 @@ static void AddTile(Game game) } } +static void SwapTile(Game game) +{ + var tilesToSwap = new List(); + bool continueSwap = true; + + ShowTiles(game); + + while (continueSwap == true) + { + Write("Enter the number of the tile you want to swap : "); + int no = Convert.ToInt32(ReadLine()); + + if (no is < 1 or > 6) + { + WriteLine("ERROR : Enter a number between 1 and 6 !"); + } + else + { + tilesToSwap.Add(game.TileOfPlayerWithPos(no - 1)); + } + + Write("Do you want to swap another tile ? (y/n) : "); + string? answer = ReadLine(); + + if (answer == "n") + { + continueSwap = false; + } + } + + game.SwapTiles(game.GetPlayingPlayer(), tilesToSwap); +} + static void MenuSwitch(Game game) { int enter = 0; @@ -103,6 +136,8 @@ static void MenuSwitch(Game game) AddTile(game); break; case 2: + SwapTile(game); + enter = 3; break; case 3: return; @@ -125,6 +160,7 @@ static void MainMenu(Game game) WriteLine(" --------------------- GAME ! ------------------------"); WriteLine(tagPlayerPlay + "'s turn !"); + game.DrawTiles(game.GetPlayingPlayer()); MenuSwitch(game); } while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1); @@ -146,4 +182,4 @@ static void MainGame() MainMenu(game); } -MainGame(); +MainGame(); \ No newline at end of file