From 2e3d7299efa77b26e4d813833c3f20d88bb44113 Mon Sep 17 00:00:00 2001 From: "jeremy.mouyon" Date: Fri, 17 May 2024 15:27:02 +0200 Subject: [PATCH] end of game --- Qwirkle/QwirkleClassLibrary/Game.cs | 108 +++++++++++++----- Qwirkle/QwirkleClassLibrary/IRules.cs | 2 +- Qwirkle/QwirkleClassLibrary/TileBag.cs | 36 +++--- .../QwirkleConsoleApp/NotificationClass.cs | 26 ++++- Qwirkle/QwirkleConsoleApp/Program.cs | 95 ++++++++++----- 5 files changed, 188 insertions(+), 79 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 0d618b8..fea389c 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -16,7 +16,7 @@ namespace QwirkleClassLibrary { public ReadOnlyDictionary ScoreBoard => scoreBoard.AsReadOnly(); private readonly Dictionary scoreBoard = new(); - + private TileBag bag; public bool GameRunning { get; private set; } private Board board; @@ -62,7 +62,7 @@ namespace QwirkleClassLibrary return false; } - if(this.GameRunning) + if (this.GameRunning) { OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is running.")); return false; @@ -106,7 +106,7 @@ namespace QwirkleClassLibrary board = new Board(8, 8); return board; } - + public TileBag CreateTileBag(int nbSet) { bag = new TileBag(nbSet); @@ -119,20 +119,20 @@ namespace QwirkleClassLibrary this.GameRunning = true; return true; } - + public void AddCellUsed(Cell? c) { if (c != null) cellUsed.Add(c); } - + public void EmptyCellUsed() { cellUsed.Clear(); } - + public Player GetPlayingPlayer() { - if(GetPlayingPlayerPosition() == -1) + if (GetPlayingPlayerPosition() == -1) { throw new ArgumentException("No player play."); } @@ -160,7 +160,7 @@ namespace QwirkleClassLibrary { foreach (var p in players) { - for (int j = 0; j < 6; j++) + for (int j = 0; j < 1; j++) // 6 { int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count); @@ -214,8 +214,8 @@ namespace QwirkleClassLibrary OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Cell already used")); return false; } - - + + /// /// Allows a player to draw tiles from the bag as soon as he has less than 6 tiles /// @@ -223,20 +223,19 @@ namespace QwirkleClassLibrary /// public bool DrawTiles(Player player) { - while(player.Tiles.Count < 6) + while (player.Tiles.Count < 6) { if (bag.TilesBag.Count == 0) { - IsGameOver(player); return false; } int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count + 1); - + player.AddTileToPlayer(bag.TilesBag[val]); bag.RemoveTileInBag(bag.TilesBag[val]); } - + return true; } @@ -284,8 +283,8 @@ namespace QwirkleClassLibrary OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Color / Shape does not match with the surrounding tiles !")); return false; } - - if(extendedCell.GetTile.GetColor == tile.GetColor && extendedCell.GetTile.GetShape == tile.GetShape) + + if (extendedCell.GetTile.GetColor == tile.GetColor && extendedCell.GetTile.GetShape == tile.GetShape) { OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Tile already placed on the same line / column !")); return false; @@ -303,15 +302,15 @@ namespace QwirkleClassLibrary public bool CheckTilesInLine(List cells, Board b, int x, int y) { - if(cells.Count == 0) + if (cells.Count == 0) { return true; } - + var x1 = cells[0].GetX; var y1 = cells[0].GetY; - - if(cells.Count < 2 && (x1 == x || y1 == y)) + + if (cells.Count < 2 && (x1 == x || y1 == y)) { return true; } @@ -320,7 +319,7 @@ namespace QwirkleClassLibrary { return false; } - + var x2 = cells[1].GetX; var y2 = cells[1].GetY; @@ -332,7 +331,7 @@ namespace QwirkleClassLibrary { return y == y1; } - + return false; } @@ -385,7 +384,7 @@ namespace QwirkleClassLibrary OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "isn't on the same line as the ones previously placed !")); return false; } - + return surroundingCells.Any(cell => cell?.GetTile != null); } @@ -398,8 +397,8 @@ namespace QwirkleClassLibrary score += CalculateAdjacentScore(cell, b, cellsPlayed); } - - if(!scoreBoard.TryAdd(player, score)) + + if (!scoreBoard.TryAdd(player, score)) { scoreBoard[player] += score; } @@ -454,12 +453,63 @@ namespace QwirkleClassLibrary return score; } + public List CheckTilesBag() + { + List PlayerTilesBagPos = new List(); + + if (bag.TilesBag.Count == 0) + { + for (int i = 0; i < players.Count(); i++) + { + if (players[i].Tiles.Count != 0) + { + PlayerTilesBagPos.Add(i); + } + } + + } + + return PlayerTilesBagPos; + } + + public bool CheckBoardTile(List PlayerTilesBagPos) + { + for(int i=0; i PlayerTilesBagPos = CheckTilesBag(); + + if (PlayerTilesBagPos.Count() != 0) + { + if (!CheckBoardTile(PlayerTilesBagPos)) + { + OnEndOfGame(new EndOfGameNotifiedEventArgs(player)); + GameRunning = false; + return true; + } + } + + return false; } } } \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/IRules.cs b/Qwirkle/QwirkleClassLibrary/IRules.cs index 652c023..78c1759 100644 --- a/Qwirkle/QwirkleClassLibrary/IRules.cs +++ b/Qwirkle/QwirkleClassLibrary/IRules.cs @@ -18,6 +18,6 @@ namespace QwirkleClassLibrary bool CheckTilesInLine(List cells, Board b, int x, int y); - bool IsGameOver(Player player); + bool CheckGameOver(Player player); } } diff --git a/Qwirkle/QwirkleClassLibrary/TileBag.cs b/Qwirkle/QwirkleClassLibrary/TileBag.cs index 01ed06d..7c74188 100644 --- a/Qwirkle/QwirkleClassLibrary/TileBag.cs +++ b/Qwirkle/QwirkleClassLibrary/TileBag.cs @@ -14,22 +14,28 @@ namespace QwirkleClassLibrary public TileBag(int nbSet) { - if (nbSet < 0 || nbSet > 3) - { - throw new ArgumentException(nbSet.ToString()); - } + /* if (nbSet < 0 || nbSet > 3) + { + throw new ArgumentException(nbSet.ToString()); + } + + for (int i = 0; i < nbSet; i++) + { + foreach (Shape s in Enum.GetValues(typeof(Shape))) + { + foreach (Color c in Enum.GetValues(typeof(Color))) + { + Tile t = new Tile(s, c); + tiles.Add(t); + } + } + }*/ + + Tile t1 = new Tile(Shape.Square, Color.Red); + tiles.Add(t1); + Tile t2 = new Tile(Shape.Star, Color.Yellow); + tiles.Add(t2); - for (int i = 0; i < nbSet; i++) - { - foreach (Shape s in Enum.GetValues(typeof(Shape))) - { - foreach (Color c in Enum.GetValues(typeof(Color))) - { - Tile t = new Tile(s, c); - tiles.Add(t); - } - } - } TilesBag = tiles.AsReadOnly(); } diff --git a/Qwirkle/QwirkleConsoleApp/NotificationClass.cs b/Qwirkle/QwirkleConsoleApp/NotificationClass.cs index 890eb6f..b7c9ef2 100644 --- a/Qwirkle/QwirkleConsoleApp/NotificationClass.cs +++ b/Qwirkle/QwirkleConsoleApp/NotificationClass.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using static System.Console; namespace QwirkleConsoleApp { @@ -11,22 +12,39 @@ namespace QwirkleConsoleApp { public void NotificationPlayerAdd(object? sender, AddPlayerNotifiedEventArgs args) { - Console.WriteLine(args.returnedNotified); + ForegroundColor = ConsoleColor.Yellow; + WriteLine(); + WriteLine(args.returnedNotified); + WriteLine(); + ResetColor(); } public void NotificationNextPlayer(object? sender, NextPlayerNotifiedEventArgs args) { - Console.WriteLine(args.player.NameTag + "'s turn"); + ForegroundColor = ConsoleColor.Yellow; + WriteLine(); + WriteLine(args.player.NameTag + "'s turn"); + WriteLine(); + ResetColor(); } public void NotificationAddTile(object? sender, PlaceTileNotifiedEventArgs args) { - Console.WriteLine("The tile [" + args.tile.ToString() + "] " + args.reason); + ForegroundColor = ConsoleColor.Magenta; + WriteLine(); + WriteLine("The tile [" + args.tile.ToString() + "] " + args.reason); + WriteLine(); + ResetColor(); } public void NotificationEndOfGame(object? sender, EndOfGameNotifiedEventArgs args) { - Console.WriteLine("This end of game ! The last player is " + args.player.NameTag + " !"); + ForegroundColor = ConsoleColor.Red; + WriteLine(); + WriteLine("This end of game ! The last player is " + args.player.NameTag + " !"); + WriteLine(); + ResetColor(); + } } } diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index 829b4db..a530b80 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -31,7 +31,10 @@ static void AddPlayers(Game game) } if (answer == "n" && game.PlayerList.Count < 2) { + ForegroundColor = ConsoleColor.Red; + WriteLine(); WriteLine("ERROR : You must have at least 2 players !"); + ResetColor(); } } } @@ -62,7 +65,8 @@ static void AddTile(Game game) game.PlaceTileNotified += nc.NotificationAddTile; Tile? tile = null; - Write("Enter the number of the tile you want to place : "); + + WriteLine("Enter the number of the tile you want to place : "); int no = -1; int x = -1; @@ -77,39 +81,50 @@ static void AddTile(Game game) if (no is < 1 or > 6) { - Write("ERROR : Enter a number between 1 and 6 ! : "); + ForegroundColor = ConsoleColor.Red; + WriteLine(); + WriteLine("ERROR : Enter a number between 1 and 6 ! : "); + ResetColor(); } - } - } - catch - { - WriteLine("ERROR : You must type. Please retry : "); - } - + else + { + tile = game.TileOfPlayerWithPos(no - 1); - tile = game.TileOfPlayerWithPos(no - 1); + Write("Enter the x of the cell: "); + try + { + x = Convert.ToInt32(ReadLine()); + } + catch + { + ForegroundColor = ConsoleColor.Red; + WriteLine(); + WriteLine("ERROR : You must type. Please retry : "); + ResetColor(); + } + Write("Enter the y of the cell: "); + try + { + y = Convert.ToInt32(ReadLine()); + } + catch + { + ForegroundColor = ConsoleColor.Red; + WriteLine(); + WriteLine("ERROR : You must type. Please retry : "); + ResetColor(); + } - Write("Enter the x of the cell: "); - try - { - x = Convert.ToInt32(ReadLine()); - } - catch - { - WriteLine("ERROR : You must type. Please retry : "); - } - Write("Enter the y of the cell: "); - try - { - y = Convert.ToInt32(ReadLine()); + } + } } catch { + ForegroundColor = ConsoleColor.Red; + WriteLine(); WriteLine("ERROR : You must type. Please retry : "); + ResetColor(); } - - game.PlaceTile(game.GetPlayingPlayer(), tile, x, y); - game.PlaceTileNotified -= nc.NotificationAddTile; } static void SwapTile(Game game) @@ -126,7 +141,10 @@ static void SwapTile(Game game) if (no is < 1 or > 6) { + ForegroundColor = ConsoleColor.Red; + WriteLine(); WriteLine("ERROR : Enter a number between 1 and 6 !"); + ResetColor(); } else { @@ -155,6 +173,8 @@ static void MenuSwitch(Game game) ShowTiles(game); + ShowScoreBoard(game); + WriteLine("\n --------------------- CHOICES ------------------------"); WriteLine("[1] Place your tiles"); @@ -168,7 +188,10 @@ static void MenuSwitch(Game game) } catch { + ForegroundColor = ConsoleColor.Red; + WriteLine(); WriteLine("ERROR : You must type (1 / 2 / 3). Please retry : "); + ResetColor(); } @@ -185,6 +208,7 @@ static void MenuSwitch(Game game) WriteLine("Your score on this turn : " + game.GetPlayerScore(game.GetPlayingPlayer(), game.CellsUsed, game.GetBoard())); game.EmptyCellUsed(); game.DrawTiles(game.GetPlayingPlayer()); + game.CheckGameOver(game.GetPlayingPlayer()); return; } } @@ -217,6 +241,8 @@ static void ShowBoard(Game game) static void ShowScoreBoard(Game g) { + WriteLine(" --------------------- THE SCORE BOARD : ---------------------"); + int i = 0; foreach (KeyValuePair pair in g.ScoreBoard) { @@ -235,9 +261,10 @@ static void MainMenu(Game game) WriteLine("Game is starting !"); Console.ResetColor(); + NotificationClass nc = new NotificationClass(); + do { - NotificationClass nc = new NotificationClass(); game.NextPlayerNotified += nc.NotificationNextPlayer; game.EndOfGameNotified += nc.NotificationEndOfGame; @@ -248,13 +275,13 @@ static void MainMenu(Game game) MenuSwitch(game); game.NextPlayerNotified -= nc.NotificationNextPlayer; + game.EndOfGameNotified -= nc.NotificationEndOfGame; } while (game.GameRunning); WriteLine(" --------------------- END OF GAME ! ------------------------"); WriteLine(" --------------------- THE BOARD : --------------------------"); ShowBoard(game); - WriteLine(" --------------------- THE SCORE BOARD : ---------------------"); ShowScoreBoard(game); } @@ -269,11 +296,13 @@ static void MainGame() while (enter != 3) { - + Console.ForegroundColor = ConsoleColor.DarkCyan; WriteLine("[1] Create game"); WriteLine("[2] Show leaderboard"); WriteLine("[3] Exit"); - Write("Enter your choice : "); + WriteLine(); + ResetColor(); + WriteLine("Enter your choice : "); try { @@ -281,14 +310,20 @@ static void MainGame() } catch { + ForegroundColor = ConsoleColor.Red; + WriteLine(); WriteLine("ERROR : You must type (1 / 2 / 3). Please retry : "); + ResetColor(); } switch (enter) { case 1: + ForegroundColor = ConsoleColor.DarkYellow; WriteLine("Enter minimun 2 player and max 4 player !"); + WriteLine(); + ResetColor(); Game game = new Game(); AddPlayers(game); game.StartGame();