end of game

test_old_branch
Jérémy Mouyon 11 months ago
parent b9035a17fa
commit 2e3d7299ef

@ -16,7 +16,7 @@ namespace QwirkleClassLibrary
{
public ReadOnlyDictionary<Player, int> ScoreBoard => scoreBoard.AsReadOnly();
private readonly Dictionary<Player, int> 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;
}
/// <summary>
/// Allows a player to draw tiles from the bag as soon as he has less than 6 tiles
/// </summary>
@ -223,20 +223,19 @@ namespace QwirkleClassLibrary
/// <returns></returns>
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<Cell> 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<int> CheckTilesBag()
{
List<int> PlayerTilesBagPos = new List<int>();
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<int> PlayerTilesBagPos)
{
for(int i=0; i<PlayerTilesBagPos.Count(); i++)
{
for(int j=0; j < this.players[PlayerTilesBagPos[i]].Tiles.Count; j++)
{
for(int b=0; b < this.board.ReadCells.Count(); b++)
{
int x = this.board.ReadCells[b].GetX;
int y = this.board.ReadCells[b].GetY;
if (IsMoveCorrect(this.players[PlayerTilesBagPos[i]].Tiles[j], x, y, this.board))
{
return true;
}
}
}
}
return false;
}
public bool IsGameOver(Player player)
public bool CheckGameOver(Player player)
{
OnEndOfGame(new EndOfGameNotifiedEventArgs(player));
GameRunning = false;
return true;
List<int> PlayerTilesBagPos = CheckTilesBag();
if (PlayerTilesBagPos.Count() != 0)
{
if (!CheckBoardTile(PlayerTilesBagPos))
{
OnEndOfGame(new EndOfGameNotifiedEventArgs(player));
GameRunning = false;
return true;
}
}
return false;
}
}
}

@ -18,6 +18,6 @@ namespace QwirkleClassLibrary
bool CheckTilesInLine(List<Cell> cells, Board b, int x, int y);
bool IsGameOver(Player player);
bool CheckGameOver(Player player);
}
}

@ -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();
}

@ -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();
}
}
}

@ -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<Player, int> 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();

Loading…
Cancel
Save