rportet 12 months ago
commit a72794acfe

@ -125,7 +125,7 @@ WARN_LOGFILE =
# Configuration options related to the input files # Configuration options related to the input files
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
INPUT = Qwirkle INPUT = ../../Qwirkle
INPUT_ENCODING = UTF-8 INPUT_ENCODING = UTF-8
FILE_PATTERNS = *.c \ FILE_PATTERNS = *.c \
*.cc \ *.cc \

@ -9,7 +9,7 @@ using System.Security.Cryptography;
namespace QwirkleClassLibrary namespace QwirkleClassLibrary
{ {
public class Game : IPlayer public class Game : IPlayer, IRules
{ {
private TileBag bag; private TileBag bag;
public bool GameRunning { get; private set; } public bool GameRunning { get; private set; }
@ -20,8 +20,8 @@ namespace QwirkleClassLibrary
public Game() public Game()
{ {
board = new Board();
bag = new TileBag(3); bag = new TileBag(3);
board = CreateBoard();
} }
public bool AddPlayerInGame(string? playerTag) public bool AddPlayerInGame(string? playerTag)
@ -55,6 +55,12 @@ namespace QwirkleClassLibrary
return player; return player;
} }
public Board CreateBoard()
{
board = new Board();
return board;
}
public void StartGame() public void StartGame()
{ {
this.GameRunning = true; this.GameRunning = true;
@ -88,7 +94,7 @@ namespace QwirkleClassLibrary
{ {
for (int j = 0; j < 6; j++) 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]); p.AddTileToPlayer(bag.TilesBag[val]);
bag.RemoveTileInBag(bag.TilesBag[val]); bag.RemoveTileInBag(bag.TilesBag[val]);
@ -138,7 +144,7 @@ namespace QwirkleClassLibrary
return false; return false;
} }
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count); int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count + 1);
player.AddTileToPlayer(bag.TilesBag[val]); player.AddTileToPlayer(bag.TilesBag[val]);
bag.RemoveTileInBag(bag.TilesBag[val]); bag.RemoveTileInBag(bag.TilesBag[val]);
@ -147,9 +153,42 @@ namespace QwirkleClassLibrary
return true; return true;
} }
public bool SwapTiles(Player player) public bool SwapTiles(Player player, List<Tile> tilesToSwap)
{
if (tilesToSwap.Count == 0)
{ {
return false; 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 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 public interface IRules
{ {
Board CreateBoard(); Board CreateBoard();
bool isMoveCorrect(Tile t, Cell c); bool IsMoveCorrect(Tile t, Board b);
bool isGameOver(); bool IsGameOver();
} }
} }

@ -14,6 +14,11 @@ namespace QwirkleClassLibrary
public TileBag(int nbSet) public TileBag(int nbSet)
{ {
if (nbSet < 0 || nbSet < 3)
{
throw new ArgumentException(nbSet.ToString());
}
for (int i = 0; i < nbSet; i++) for (int i = 0; i < nbSet; i++)
{ {
foreach (Shape s in Enum.GetValues(typeof(Shape))) foreach (Shape s in Enum.GetValues(typeof(Shape)))
@ -28,20 +33,27 @@ namespace QwirkleClassLibrary
TilesBag = tiles.AsReadOnly(); TilesBag = tiles.AsReadOnly();
} }
public void AddTileInBag(Tile tile) public bool AddTileInBag(Tile tile)
{ {
if (tiles == null)
{
return false;
}
tiles.Add(tile); tiles.Add(tile);
return true;
} }
public void RemoveTileInBag(Tile tile) public bool RemoveTileInBag(Tile tile)
{ {
for (int i = 0; i < tiles.Count; i++) for (int i = 0; i < tiles.Count; i++)
{ {
if (tiles[i] == tile) if (tiles[i] == tile)
{ {
tiles.RemoveAt(i); tiles.RemoveAt(i);
return true;
} }
} }
return false;
} }
} }
} }

@ -58,7 +58,7 @@ static void AddTile(Game game)
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()); 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 ! : "); Write("ERROR : Enter a number between 1 and 6 ! : ");
no = Convert.ToInt32(ReadLine()); 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) static void MenuSwitch(Game game)
{ {
int enter = 0; int enter = 0;
@ -103,6 +136,8 @@ static void MenuSwitch(Game game)
AddTile(game); AddTile(game);
break; break;
case 2: case 2:
SwapTile(game);
enter = 3;
break; break;
case 3: case 3:
return; return;
@ -125,6 +160,7 @@ static void MainMenu(Game game)
WriteLine(" --------------------- GAME ! ------------------------"); WriteLine(" --------------------- GAME ! ------------------------");
WriteLine(tagPlayerPlay + "'s turn !"); WriteLine(tagPlayerPlay + "'s turn !");
game.DrawTiles(game.GetPlayingPlayer());
MenuSwitch(game); MenuSwitch(game);
} while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1); } while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1);

@ -0,0 +1,37 @@
using QwirkleClassLibrary;
namespace TestBase;
public class TestTileBag
{
[Theory]
[InlineData(false, 5)]
[InlineData(false, -5)]
[InlineData(true, 2)]
public void Test_TileBagConstructor(bool isValid, int nbset)
{
if (!isValid)
{
Assert.Throws<ArgumentException>(() => new TileBag(nbset));
return;
}
TileBag bag = new TileBag(nbset);
Assert.Equal(bag.TilesBag.Count, nbset);
}
[Fact]
public void Test_AddTileInBag()
{
Tile t = null;
Tile tok = new(Shape.Club, Color.Green);
TileBag bag = new TileBag(2);
if (bag.AddTileInBag(t) == false)
{
Assert.True(bag.AddTileInBag(tok));
}
}
}
Loading…
Cancel
Save