ok, add event end of game, add main menu for create game.. WAIT REMY SCOREEEEE PLZZZZZZE
continuous-integration/drone/push Build is passing Details

test_old_branch
Jérémy Mouyon 11 months ago
parent 92adef32f4
commit b2a987a663

@ -0,0 +1,12 @@
namespace QwirkleClassLibrary
{
public class EndOfGameNotifiedEventArgs
{
public Player player { get; private set; }
public EndOfGameNotifiedEventArgs(Player player)
{
this.player = player;
}
}
}

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using System.Xml.Linq; using System.Xml.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Collections; using System.Collections;
using System.Collections.Immutable;
namespace QwirkleClassLibrary namespace QwirkleClassLibrary
{ {
@ -41,6 +42,11 @@ namespace QwirkleClassLibrary
protected virtual void OnPlaceTile(PlaceTileNotifiedEventArgs args) protected virtual void OnPlaceTile(PlaceTileNotifiedEventArgs args)
=> PlaceTileNotified?.Invoke(this, args); => PlaceTileNotified?.Invoke(this, args);
public event EventHandler<EndOfGameNotifiedEventArgs>? EndOfGameNotified;
protected virtual void OnEndOfGame(EndOfGameNotifiedEventArgs args)
=> EndOfGameNotified?.Invoke(this, args);
public Game() public Game()
{ {
bag = CreateTileBag(3); bag = CreateTileBag(3);
@ -78,7 +84,10 @@ namespace QwirkleClassLibrary
} }
players.Add(CreatePlayer(playerTag)); Player pl = CreatePlayer(playerTag);
players.Add(pl);
scoreBoard.Add(pl, 0);
OnPlayerNotified(new AddPlayerNotifiedEventArgs("Player was correctly added")); OnPlayerNotified(new AddPlayerNotifiedEventArgs("Player was correctly added"));
return true; return true;
} }
@ -151,7 +160,7 @@ namespace QwirkleClassLibrary
{ {
foreach (var p in players) foreach (var p in players)
{ {
for (int j = 0; j < 6; j++) for (int j = 0; j < 6; j++) // ici pour test
{ {
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count); int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count);
@ -218,6 +227,7 @@ namespace QwirkleClassLibrary
{ {
if (bag.TilesBag.Count == 0) if (bag.TilesBag.Count == 0)
{ {
IsGameOver(player);
return false; return false;
} }
@ -444,8 +454,12 @@ namespace QwirkleClassLibrary
return score; return score;
} }
public bool IsGameOver()
public bool IsGameOver(Player player)
{ {
OnEndOfGame(new EndOfGameNotifiedEventArgs(player));
GameRunning = false;
//ScoreBoard.ToImmutableSortedDictionary();
return true; return true;
} }
} }

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

@ -30,6 +30,12 @@ namespace QwirkleClassLibrary
} }
} }
} }
/* Tile t = new Tile(Shape.Club, Color.Red);
Tile t2 = new Tile(Shape.Shuriken, Color.Red);
tiles.Add(t);
tiles.Add(t2);*/
TilesBag = tiles.AsReadOnly(); TilesBag = tiles.AsReadOnly();
} }

@ -23,5 +23,10 @@ namespace QwirkleConsoleApp
{ {
Console.WriteLine("The tile [" + args.tile.ToString() + "] " + args.reason); Console.WriteLine("The tile [" + args.tile.ToString() + "] " + args.reason);
} }
public void NotificationEndOfGame(object? sender, EndOfGameNotifiedEventArgs args)
{
Console.WriteLine("This end of game ! The last player is " + args.player.NameTag + " !");
}
} }
} }

@ -1,5 +1,8 @@
using QwirkleClassLibrary; using QwirkleClassLibrary;
using QwirkleConsoleApp; using QwirkleConsoleApp;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics.Metrics;
using System.Net.Quic; using System.Net.Quic;
using System.Text; using System.Text;
using System.Transactions; using System.Transactions;
@ -60,21 +63,46 @@ static void AddTile(Game game)
Tile? tile = null; Tile? tile = null;
Write("Enter the number of the tile you want to place : "); Write("Enter the number of the tile you want to place : ");
int no = Convert.ToInt32(ReadLine());
while (no is < 1 or > 6) int no = -1;
int x = -1;
int y = -1;
try
{ {
Write("ERROR : Enter a number between 1 and 6 ! : "); while (no is < 1 or > 6)
no = Convert.ToInt32(ReadLine()); {
Write("ERROR : Enter a number between 1 and 6 ! : ");
no = Convert.ToInt32(ReadLine());
}
} }
catch
{
WriteLine("ERROR : You must type. Please retry : ");
}
tile = game.TileOfPlayerWithPos(no - 1); tile = game.TileOfPlayerWithPos(no - 1);
Write("Enter the x of the cell: "); Write("Enter the x of the cell: ");
int x = Convert.ToInt32(ReadLine()); try
Write("Enter the y of the cell : "); {
int y = Convert.ToInt32(ReadLine()); 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
{
WriteLine("ERROR : You must type. Please retry : ");
}
game.PlaceTile(game.GetPlayingPlayer(), tile, x, y); game.PlaceTile(game.GetPlayingPlayer(), tile, x, y);
game.PlaceTileNotified -= nc.NotificationAddTile; game.PlaceTileNotified -= nc.NotificationAddTile;
} }
@ -133,7 +161,8 @@ static void MenuSwitch(Game game)
{ {
enter = Convert.ToInt32(ReadLine()); enter = Convert.ToInt32(ReadLine());
} }
catch { catch
{
WriteLine("ERROR : You must type (1 / 2 / 3). Please retry : "); WriteLine("ERROR : You must type (1 / 2 / 3). Please retry : ");
} }
@ -150,6 +179,7 @@ static void MenuSwitch(Game game)
case 3: case 3:
WriteLine("Your score on this turn : " + game.GetPlayerScore(game.GetPlayingPlayer(), game.CellsUsed, game.GetBoard())); WriteLine("Your score on this turn : " + game.GetPlayerScore(game.GetPlayingPlayer(), game.CellsUsed, game.GetBoard()));
game.EmptyCellUsed(); game.EmptyCellUsed();
game.DrawTiles(game.GetPlayingPlayer());
return; return;
} }
} }
@ -159,11 +189,11 @@ static void ShowBoard(Game game)
{ {
Board board = game.GetBoard(); Board board = game.GetBoard();
for(int i = 0; i<board.Rows; i++) for (int i = 0; i < board.Rows; i++)
{ {
for(int y = 0; y<board.Columns; y++) for (int y = 0; y < board.Columns; y++)
{ {
if(board.GetCell(y, i)!.IsFree == false) if (board.GetCell(y, i)!.IsFree == false)
{ {
Tile? tile = board.GetCell(y, i)?.GetTile; Tile? tile = board.GetCell(y, i)?.GetTile;
if (tile != null) if (tile != null)
@ -180,6 +210,18 @@ static void ShowBoard(Game game)
} }
} }
static void ShowScoreBoard(Game g)
{
int i = 0;
foreach (KeyValuePair<Player, int> pair in g.ScoreBoard)
{
i++;
WriteLine("[" + i + "] " + pair.Key.NameTag + " with " + pair.Value.ToString() + " points.");
}
}
static void MainMenu(Game game) static void MainMenu(Game game)
{ {
game.GiveTilesToPlayers(); game.GiveTilesToPlayers();
@ -192,33 +234,70 @@ static void MainMenu(Game game)
{ {
NotificationClass nc = new NotificationClass(); NotificationClass nc = new NotificationClass();
game.NextPlayerNotified += nc.NotificationNextPlayer; game.NextPlayerNotified += nc.NotificationNextPlayer;
game.EndOfGameNotified += nc.NotificationEndOfGame;
game.SetNextPlayer(); game.SetNextPlayer();
WriteLine(" --------------------- GAME ! ------------------------"); WriteLine(" --------------------- GAME ! ------------------------");
game.DrawTiles(game.GetPlayingPlayer());
MenuSwitch(game); MenuSwitch(game);
game.NextPlayerNotified -= nc.NotificationNextPlayer; game.NextPlayerNotified -= nc.NotificationNextPlayer;
} while (game.IsGameOver()); } while (game.GameRunning);
WriteLine(" --------------------- END OF GAME ! ------------------------");
WriteLine(" --------------------- THE BOARD : --------------------------");
ShowBoard(game);
WriteLine(" --------------------- THE SCORE BOARD : ---------------------");
ShowScoreBoard(game);
} }
static void MainGame() static void MainGame()
{ {
Game game = new Game();
Console.ForegroundColor = ConsoleColor.DarkGray; Console.ForegroundColor = ConsoleColor.DarkGray;
WriteLine(" ===================== WELCOME TO QWIRKLE ! ====================="); WriteLine(" ===================== WELCOME TO QWIRKLE ! =====================");
WriteLine("Enter the players' nametags (2 to 4 players) : ");
Console.ResetColor(); Console.ResetColor();
AddPlayers(game); int enter = 0;
while (enter != 3)
{
WriteLine("[1] Create game");
WriteLine("[2] Show leaderboard");
WriteLine("[3] Exit");
Write("Enter your choice : ");
try
{
enter = Convert.ToInt32(ReadLine());
}
catch
{
WriteLine("ERROR : You must type (1 / 2 / 3). Please retry : ");
}
game.StartGame();
MainMenu(game); switch (enter)
{
case 1:
WriteLine("Enter minimun 2 player and max 4 player !");
Game game = new Game();
AddPlayers(game);
game.StartGame();
MainMenu(game);
break;
case 2:
//ShowLeaderboard();
break;
case 3:
return;
}
}
} }
MainGame(); MainGame();

Loading…
Cancel
Save