end of game

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

@ -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);
@ -227,7 +227,6 @@ namespace QwirkleClassLibrary
{
if (bag.TilesBag.Count == 0)
{
IsGameOver(player);
return false;
}
@ -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 IsGameOver(Player player)
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 CheckGameOver(Player player)
{
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,7 +14,7 @@ namespace QwirkleClassLibrary
public TileBag(int nbSet)
{
if (nbSet < 0 || nbSet > 3)
/* if (nbSet < 0 || nbSet > 3)
{
throw new ArgumentException(nbSet.ToString());
}
@ -29,7 +29,13 @@ namespace QwirkleClassLibrary
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);
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,16 +81,13 @@ 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
else
{
WriteLine("ERROR : You must type. Please retry : ");
}
tile = game.TileOfPlayerWithPos(no - 1);
Write("Enter the x of the cell: ");
@ -96,7 +97,10 @@ static void AddTile(Game game)
}
catch
{
ForegroundColor = ConsoleColor.Red;
WriteLine();
WriteLine("ERROR : You must type. Please retry : ");
ResetColor();
}
Write("Enter the y of the cell: ");
try
@ -105,11 +109,22 @@ static void AddTile(Game game)
}
catch
{
ForegroundColor = ConsoleColor.Red;
WriteLine();
WriteLine("ERROR : You must type. Please retry : ");
ResetColor();
}
game.PlaceTile(game.GetPlayingPlayer(), tile, x, y);
game.PlaceTileNotified -= nc.NotificationAddTile;
}
}
}
catch
{
ForegroundColor = ConsoleColor.Red;
WriteLine();
WriteLine("ERROR : You must type. Please retry : ");
ResetColor();
}
}
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