You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sae201_qwirkle/Qwirkle/QwirkleConsoleApp/Program.cs

367 lines
9.2 KiB

using QwirkleClassLibrary.Boards;
using QwirkleClassLibrary.Events;
using QwirkleClassLibrary.Games;
using QwirkleClassLibrary.Players;
using QwirkleClassLibrary.Tiles;
using QwirkleConsoleApp;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics.Metrics;
using System.Net.Quic;
using System.Text;
using System.Transactions;
using static System.Console;
static void AddPlayers(Game game)
{
NotificationClass nc = new NotificationClass();
game.PlayerAddNotified += nc.NotificationPlayerAdd;
List<string> playerstag = [];
while (game.PlayerList.Count < 4)
{
Write("Enter player tag : ");
string? tag = ReadLine();
playerstag.Add(tag!);
Write("Do you want to add another player ? (y/n) : ");
string? answer = ReadLine();
if (answer == "n" && game.AddPlayerInGame(playerstag))
{
break;
}
}
}
static void ShowTiles(Game game)
{
WriteLine("\n --------------------- YOUR TILES ------------------------");
var currentPlayer = game.GetPlayingPlayer();
var stringBuilder = new StringBuilder();
for (int i = 0; i < currentPlayer.Tiles.Count; i++)
{
stringBuilder.Append("[" + (i + 1) + "] ");
stringBuilder.AppendLine(currentPlayer.Tiles[i].ToString());
}
Write(stringBuilder);
}
static void AddTile(Game game)
{
NotificationClass nc = new NotificationClass();
game.PlaceTileNotified += nc.NotificationAddTile;
Tile? tile = null;
WriteLine("Enter the number of the tile you want to place : ");
int no = -1;
int x = -1;
int y = -1;
try
{
while (no is < 1 or > 6)
{
no = Convert.ToInt32(ReadLine());
if (no is < 1 or > 6)
{
ForegroundColor = ConsoleColor.Red;
WriteLine();
WriteLine("ERROR : Enter a number between 1 and 6 ! : ");
ResetColor();
}
else
{
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();
}
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)
{
var nc = new NotificationClass();
game.SwapTilesNotified += nc.NotificationSwapTile;
var tilesToSwap = new List<Tile>();
bool continueSwap = true;
ShowTiles(game);
while (continueSwap)
{
Write("Enter the number of the tile you want to swap : ");
int no = Convert.ToInt32(ReadLine());
if (no is < 1 or > 6)
{
ForegroundColor = ConsoleColor.Red;
WriteLine();
WriteLine("ERROR : Enter a number between 1 and 6 !");
ResetColor();
}
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);
game.SwapTilesNotified -= nc.NotificationSwapTile;
}
static void MenuSwitch(Game game)
{
int enter = 0;
while (enter != 3)
{
ShowBoard(game);
ShowTiles(game);
ShowScoreBoard(game);
WriteLine("\n --------------------- CHOICES ------------------------");
WriteLine("[1] Place your tiles");
WriteLine("[2] Swap your tiles");
WriteLine("[3] End your turn / Skip your turn");
Write("Enter your choice : ");
try
{
enter = Convert.ToInt32(ReadLine());
}
catch
{
ForegroundColor = ConsoleColor.Red;
WriteLine();
WriteLine("ERROR : You must type (1 / 2 / 3). Please retry : ");
ResetColor();
}
switch (enter)
{
case 1:
AddTile(game);
break;
case 2:
SwapTile(game);
enter = 3;
break;
case 3:
WriteLine("Your score on this turn : " + game.GetPlayerScore(game.GetPlayingPlayer(), game.CellsUsed, game.Board));
game.EmptyCellUsed();
game.DrawTiles(game.GetPlayingPlayer());
game.CheckGameOver(game.GetPlayingPlayer());
return;
}
}
}
static void ShowBoard(Game game)
{
Board board = game.Board;
for (int i = 0; i < board.Rows; i++)
{
for (int y = 0; y < board.Columns; y++)
{
Cell? cell = board.GetCell(y, i);
if (cell != null && !cell.IsFree)
{
Tile? tile = cell.GetTile;
if (tile != null)
{
Console.Write("| " + tile.GetShape.ToString()[0] + tile.GetShape.ToString()[1] +
tile.GetColor.ToString()[0] + " |");
}
}
else
{
Console.Write("| |");
}
}
Console.WriteLine();
}
}
static void ShowScoreBoard(Game g)
{
WriteLine(" --------------------- THE SCORE BOARD : ---------------------");
int i = 0;
var sb = g.ScoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key.NameTag);
foreach (KeyValuePair<Player, int> pair in sb)
{
i++;
WriteLine("[" + i + "] " + pair.Key.NameTag + " with " + pair.Value.ToString() + " points.");
}
}
static void ShowLeaderboard(Leaderboard leaderboard)
{
WriteLine(" --------------------- THE LEADERBOARD : ---------------------");
WriteLine("Position : | PlayerTag : | Last Date : | Points : | Victories :");
for (int i=0; i<leaderboard.Lb.Count; i++)
{
WriteLine("[" + (i+1) + "] " + leaderboard.Lb[i].ToString());
}
WriteLine();
WriteLine();
}
static void MainMenu(Game game)
{
game.GiveTilesToPlayers();
Console.ForegroundColor = ConsoleColor.Green;
WriteLine("Game is starting !");
Console.ResetColor();
NotificationClass nc = new NotificationClass();
do
{
game.NextPlayerNotified += nc.NotificationNextPlayer;
game.EndOfGameNotified += nc.NotificationEndOfGame;
game.SetNextPlayer();
WriteLine(" --------------------- GAME ! ------------------------");
MenuSwitch(game);
game.NextPlayerNotified -= nc.NotificationNextPlayer;
game.EndOfGameNotified -= nc.NotificationEndOfGame;
} while (game.GameRunning);
WriteLine(" --------------------- END OF GAME ! ------------------------");
WriteLine(" --------------------- THE BOARD : --------------------------");
ShowBoard(game);
ShowScoreBoard(game);
}
static void MainGame()
{
Leaderboard leaderboard = new Leaderboard();
Console.ForegroundColor = ConsoleColor.DarkGray;
WriteLine(" ===================== WELCOME TO QWIRKLE ! =====================");
Console.ResetColor();
int enter = 0;
while (enter != 3)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
WriteLine("[1] Create game");
WriteLine("[2] Show leaderboard");
WriteLine("[3] Exit");
WriteLine();
ResetColor();
WriteLine("Enter your choice : ");
try
{
enter = Convert.ToInt32(ReadLine());
}
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();
MainMenu(game);
leaderboard.AddScoreInLead(game.ScoreBoard);
break;
case 2:
ShowLeaderboard(leaderboard);
break;
case 3:
return;
}
}
}
MainGame();