programcs upgrade + units test

test_old_branch
Jérémy Mouyon 1 year ago
parent 60848f64f6
commit 867f542f3d

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -11,16 +12,18 @@ namespace QwirkleClassLibrary
public class Game public class Game
{ {
private TileBag bag; private TileBag bag;
private List<Player> players;
private bool gameRunning; private bool gameRunning;
private Board board; private Board board;
public ReadOnlyCollection<Player> PlayerList { get; private set; }
private readonly List<Player> players = new List<Player>();
public Game() public Game()
{ {
this.players = new List<Player>();
board = new Board(); board = new Board();
bag = new TileBag(3); bag = new TileBag(3);
gameRunning = false; gameRunning = false;
PlayerList = players.AsReadOnly();
} }
public bool AddPlayerInGame(string? PlayerTag) public bool AddPlayerInGame(string? PlayerTag)
@ -80,17 +83,15 @@ namespace QwirkleClassLibrary
get { return players.Count; } get { return players.Count; }
} }
public List<Tile> ListTilesBag // a passer en program cs
{
get { return bag.TilesInBag(); }
}
public string ShowTileOfPlayer(int posplayer) public string ShowTileOfPlayer(int posplayer)
{ {
List<Tile> tiles = players[posplayer].Tiles; List<Tile> tiles = players[posplayer].Tiles.ToList();
string r = ("Tile of " + posplayer + " : "); string r = ("Tile of " + posplayer + " : ");
StringBuilder stringBuilder = new StringBuilder(); // A UTILISER !!!
foreach (Tile tile in tiles) foreach (Tile tile in tiles)
{ {
r = (r + " " + tile.NameColorTile()); r = (r + " " + tile.NameColorTile());
@ -121,7 +122,7 @@ namespace QwirkleClassLibrary
{ {
for (int j = 0; j < 6; j++) for (int j = 0; j < 6; j++)
{ {
Tile tile = ListTilesBag[j]; Tile tile = bag.TilesBag[j];
players[i].AddTilePlayer(tile); players[i].AddTilePlayer(tile);
bag.RemoveTileInBag(tile); bag.RemoveTileInBag(tile);
} }

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,12 +10,18 @@ namespace QwirkleClassLibrary
public class Player public class Player
{ {
private string nameTag; private string nameTag;
private List<Tile> playerTiles; public ReadOnlyCollection<Tile> Tiles { get; private set; }
private readonly List<Tile> playerTiles = new List<Tile>();
public Player(string name) public Player(string name)
{ {
if(name==null || string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
nameTag = name; nameTag = name;
Tiles = new List<Tile>(); playerTiles = new List<Tile>();
Tiles = playerTiles.AsReadOnly();
} }
public string GetNameTag public string GetNameTag
@ -27,17 +34,13 @@ namespace QwirkleClassLibrary
public void AddTilePlayer(Tile tile) public void AddTilePlayer(Tile tile)
{ {
Tiles.Add(tile); playerTiles.Add(tile);
} }
public bool RemoveTilePlayer(Tile tile) public bool RemoveTilePlayer(Tile tile)
{ {
return Tiles.Remove(tile); return playerTiles.Remove(tile);
} }
public List<Tile> Tiles
{
get;
}
} }
} }

@ -31,5 +31,10 @@ namespace QwirkleClassLibrary
{ {
get { return this.color; } get { return this.color; }
} }
public override string ToString()
{
return shape.ToString() + color.ToString();
}
} }
} }

@ -9,7 +9,7 @@ namespace QwirkleClassLibrary
{ {
public class TileBag public class TileBag
{ {
public ReadOnlyCollection<Tile> Tiles { get ; private set; } public ReadOnlyCollection<Tile> TilesBag { get ; private set; }
private readonly List<Tile> tiles = new List<Tile>(); private readonly List<Tile> tiles = new List<Tile>();
public TileBag(int nbSet) public TileBag(int nbSet)
@ -25,6 +25,7 @@ namespace QwirkleClassLibrary
} }
} }
} }
TilesBag = tiles.AsReadOnly();
} }
public void RemoveTileInBag(Tile tile) public void RemoveTileInBag(Tile tile)
@ -37,11 +38,6 @@ namespace QwirkleClassLibrary
} }
} }
} }
public List<Tile> TilesInBag()
{
return tiles;
}
} }
} }

@ -1,10 +1,10 @@
using QwirkleClassLibrary; using QwirkleClassLibrary;
using System.Text;
using static System.Console; using static System.Console;
static void testJeremy()
{
Game game = new Game();
static void addPlayer(Game game)
{
string? enterline = ""; string? enterline = "";
do do
@ -21,64 +21,93 @@ static void testJeremy()
} }
} while (enterline != "quit"); } while (enterline != "quit");
}
game.StartGame(); static void MainMenu(Game game)
{
game.TilsBagPlayer();
String TagPlayerPlay;
TagPlayerPlay = game.NextPlayer();
if (game.GameRunning == true) Write(" --------------------- GAME ! ------------------------ \n");
Write(TagPlayerPlay + " you have main now ! \n");
Write("\n --------------------- YOUR TILES ------------------------ \n");
int pos = game.GetPlayingPlayerPosition();
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < game.PlayerList[pos].Tiles.Count(); i++)
{ {
stringBuilder.AppendLine(game.PlayerList[pos].Tiles[i].ToString());
}
game.TilsBagPlayer(); Write(stringBuilder);
String TagPlayerPlay; Write("\n --------------------- CHOICES ------------------------ \n");
TagPlayerPlay = game.NextPlayer();
Write(TagPlayerPlay + " you have main now ! \n");
string s = game.ShowTileOfPlayer(game.GetPlayingPlayerPosition());
Write(s + "\n");
Write("[1] Place your tiles \n"); Write("[1] Place your tiles \n");
Write("[2] Swap your tiles \n"); Write("[2] Swap your tiles \n");
int enter = Convert.ToInt32(ReadLine()); int enter = Convert.ToInt32(ReadLine());
Tile tile = null; switch (enter)
{
case 1:
CaseOneAddTile(game);
break;
case 2:
break;
}
}
switch (enter) static void CaseOneAddTile(Game game)
{
Tile tile = null;
Write("Enter no tile : ");
int no = Convert.ToInt32(ReadLine());
if (no >= 0 && no <= 5)
{
tile = game.TileOfPlayerWithPos(no);
Write("Enter x : ");
int x = Convert.ToInt32(ReadLine());
Write("Enter y : ");
int y = Convert.ToInt32(ReadLine());
if (game.PlaceTileGame(tile, x, y) == true)
{
Write("ok ! your tile is placed \n");
}
else
{ {
case 1: Write("no no no \n");
// si good faire des boucles demain
Write("Enter no tile : ");
int no = Convert.ToInt32(ReadLine());
if (no >= 0 && no <= 5)
{
tile = game.TileOfPlayerWithPos(no);
Write("Enter x : ");
int x = Convert.ToInt32(ReadLine());
Write("Enter y : ");
int y = Convert.ToInt32(ReadLine());
if (game.PlaceTileGame(tile, x, y) == true)
{
Write("ok ! your tile is placed \n");
}
else
{
Write("no no no \n");
}
}
else
{
Write("No is out of range\n");
}
break;
case 2:
break;
} }
}
else
{
Write("No is out of range\n");
}
}
static void testJeremy()
{
Game game = new Game();
addPlayer(game);
game.StartGame();
if (game.GameRunning == true)
{
MainMenu(game);
} }

@ -2,14 +2,42 @@ using QwirkleClassLibrary;
namespace TestBase namespace TestBase
{ {
public class UnitTest1 public class TestPlayers
{ {
public void Test_PlayerRemoveTile(bool expectedResult) [Theory]
[InlineData(true, "Mathis")]
[InlineData(false, null)]
public void Test_CreatePlayer(bool isValid, string playertag)
{ {
Tile t = new Tile(Square, Red); if (!isValid)
Player p = new Player("Patrick"); {
bool result = p.RemoveTilePlayer(t); Assert.Throws<ArgumentNullException>(() => new Player(playertag));
Assert.True(result); return;
}
Player player = new Player(playertag);
Assert.Equal(playertag, player.GetNameTag);
}
[Fact]
public void Test_AddTilePlayer()
{
Player p = new Player("cobaye");
Tile tile = new Tile(Shape.Round, Color.Orange);
p.AddTilePlayer(tile);
bool r = false;
for(int i = 0; i < p.Tiles.Count; i++)
{
if (p.Tiles[i] == tile)
{
r = true;
}
}
Assert.True(r);
} }
} }
} }
Loading…
Cancel
Save