Enhanced PlaceTile method, added SwapTiles method, began to implement IRules.cs into Game.cs

test_old_branch
Jules LASCRET 12 months ago
parent b4f18de867
commit 5c66fd76f9

@ -9,7 +9,7 @@ using System.Security.Cryptography;
namespace QwirkleClassLibrary
{
public class Game : IPlayer
public class Game : IPlayer, IRules
{
private TileBag bag;
public bool GameRunning { get; private set; }
@ -20,8 +20,8 @@ namespace QwirkleClassLibrary
public Game()
{
board = new Board();
bag = new TileBag(3);
board = CreateBoard();
}
public bool AddPlayerInGame(string? playerTag)
@ -55,6 +55,12 @@ namespace QwirkleClassLibrary
return player;
}
public Board CreateBoard()
{
board = new Board();
return board;
}
public void StartGame()
{
this.GameRunning = true;
@ -88,7 +94,7 @@ namespace QwirkleClassLibrary
{
for (int j = 0; j < 6; j++)
{
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count);
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count + 1);
p.AddTileToPlayer(bag.TilesBag[val]);
bag.RemoveTileInBag(bag.TilesBag[val]);
@ -138,7 +144,7 @@ namespace QwirkleClassLibrary
return false;
}
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count);
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count + 1);
player.AddTileToPlayer(bag.TilesBag[val]);
bag.RemoveTileInBag(bag.TilesBag[val]);
@ -146,10 +152,43 @@ namespace QwirkleClassLibrary
return true;
}
public bool SwapTiles(Player player)
public bool SwapTiles(Player player, List<Tile> tilesToSwap)
{
if (tilesToSwap.Count == 0)
{
return false;
}
foreach (var t in tilesToSwap)
{
if (player.RemoveTileToPlayer(t) == false)
{
return false;
}
}
if (DrawTiles(player) == false)
{
return false;
}
foreach (var t in tilesToSwap)
{
bag.AddTileInBag(t);
}
return true;
}
public bool IsMoveCorrect(Tile t, Board b)
{
return false;
}
public bool IsGameOver()
{
throw new NotImplementedException();
}
}
}

@ -10,5 +10,5 @@ public interface IPlayer
public bool DrawTiles(Player player);
public bool SwapTiles(Player player);
public bool SwapTiles(Player player, List<Tile> tilesToSwap);
}

@ -9,8 +9,7 @@ namespace QwirkleClassLibrary
public interface IRules
{
Board CreateBoard();
bool isMoveCorrect(Tile t, Cell c);
bool isGameOver();
bool IsMoveCorrect(Tile t, Board b);
bool IsGameOver();
}
}

@ -58,7 +58,7 @@ static void AddTile(Game game)
Write("Enter the number of the tile you want to place : ");
int no = Convert.ToInt32(ReadLine());
while (no < 1 || no > 6)
while (no is < 1 or > 6)
{
Write("ERROR : Enter a number between 1 and 6 ! : ");
no = Convert.ToInt32(ReadLine());
@ -81,6 +81,39 @@ static void AddTile(Game game)
}
}
static void SwapTile(Game game)
{
var tilesToSwap = new List<Tile>();
bool continueSwap = true;
ShowTiles(game);
while (continueSwap == true)
{
Write("Enter the number of the tile you want to swap : ");
int no = Convert.ToInt32(ReadLine());
if (no is < 1 or > 6)
{
WriteLine("ERROR : Enter a number between 1 and 6 !");
}
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);
}
static void MenuSwitch(Game game)
{
int enter = 0;
@ -103,6 +136,8 @@ static void MenuSwitch(Game game)
AddTile(game);
break;
case 2:
SwapTile(game);
enter = 3;
break;
case 3:
return;
@ -125,6 +160,7 @@ static void MainMenu(Game game)
WriteLine(" --------------------- GAME ! ------------------------");
WriteLine(tagPlayerPlay + "'s turn !");
game.DrawTiles(game.GetPlayingPlayer());
MenuSwitch(game);
} while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1);
@ -146,4 +182,4 @@ static void MainGame()
MainMenu(game);
}
MainGame();
MainGame();
Loading…
Cancel
Save