public void StartGameWihFourPlayer() { var game = new LudoEngine(4, "ppp"); Assert.Equal(4, game.PlayersList.Count); }
private static void Play(LudoEngine game) { bool gameHasWinner = false; while (!gameHasWinner) { Console.Clear(); DrawBoard(game); int moves = LetPlayerRollDice(game.CurrentPlayer, game); Console.WriteLine($"{game.CurrentPlayer.Name} got a {moves}!"); Console.WriteLine(); var moveablePiece = game.GetMoveablePieces(moves); if (moveablePiece.Count > 0) { var pieceToMove = LetPlayerChoosePiece(moveablePiece, game); if (pieceToMove != null) { bool moved = MovePiece(moves, pieceToMove, game); while (!moved) { pieceToMove = LetPlayerChoosePiece(moveablePiece, game); moved = MovePiece(moves, pieceToMove, game); } } } gameHasWinner = game.FindWinner(); game.SwitchPlayer(); } Console.WriteLine(); Console.WriteLine($"{game.Winner.Name} won the game!"); }
private static void ShowAllGames() { var allGames = LudoEngine.GetAllGames(dbContext); foreach (var game in allGames) { Console.WriteLine($"{game.Name} - Winner: {game.Winner.Name}\n"); } }
public void ExpectNumber1to6WhenRollDice() { var contextMock = new Mock <LudoDbContext>(); List <Game> gameToReturn = new List <Game>(); contextMock.Setup(x => x.Games).ReturnsDbSet(gameToReturn); LudoEngine game = new LudoEngine(contextMock.Object, "testgame1"); var diceRoll = game.ThrowDice(); Assert.True(diceRoll >= 1 && diceRoll <= 6); }
public void TestPieceInFinalStretch() { var game = new LudoEngine(2, "yyy"); game.PlayersList[0].Pieces[0].InNest = false; game.PlayersList[0].Pieces[0].Movement = 42; var question = game.MovePiece(0); var answer = new string[] { "Blue", "Piece has moved" }; Assert.Equal(question, answer); }
public void TestPieceMoveOnGameBoard() { var game = new LudoEngine(2, "xxx"); game.PlayersList[0].Pieces[0].InNest = false; game.PlayersList[0].Pieces[0].Movement = 14; var question = game.MovePiece(0); var answer = new string[] { "Blue", "Piece has moved" }; Assert.Equal(question, answer); }
private static LudoEngine SetupNewGame() { string gameName = AskForNewGameName(); LudoEngine game = new LudoEngine(dbContext, gameName); int numberOfPlayers = AskForNumberOfPlayers(); AddPlayers(numberOfPlayers, game); return(game); }
static public string[] LoadSavedGame(string gameName) { foreach (var item in GameList) { if (item.GameName == gameName) { game = item; } } string[] returnString = new string[2]; return(returnString = game.NextTurn()); }
static void Main(string[] args) { int loadGame = ReadInt("1: New game?\n2: Load game?"); LudoEngine game; if (loadGame == 2) { HackermanLudoApi.Models.GamSesssion.ShowSavedGames(); for (int i = 0; i < HackermanLudoApi.Models.GamSesssion.GameList.Count; i++) { Console.WriteLine(i + ": " + HackermanLudoApi.Models.GamSesssion.GameList[i].GameName); } int gameToLoad = ReadInt("What game to load?"); game = HackermanLudoApi.Models.GamSesssion.GameList[gameToLoad]; } else { int players = ReadInt("How many players?"); game = new LudoEngine(players, ReadString("GameName?")); } while (true) { var result = game.NextTurn(); PrintOutPieces(game, result[0]); var choice = ReadInt($"Player is: {result[0]}\nDice shows: {result[1]}\nWhat do You want to do?\n1: Move Piece?\n2: Pass turn?"); if (choice == 1) { int pieceNr = ReadInt("Wich piece do U wanna move?"); var actionFromEngine = game.MovePiece(pieceNr - 1); Console.WriteLine(actionFromEngine[0] + actionFromEngine[1]); } else if (choice == 2) { game.PassTurn(); } if (1 == ReadInt("SaveGame?\n1:Yes\n2:No")) { HackermanLudoApi.Models.GamSesssion.SaveGame(game); } } }
public void StartGameWihTwoPlayer() { var game = new LudoEngine(2, "zzz"); Assert.Equal(2, game.PlayersList.Count); Assert.Equal(40, game.TileList.Count); Assert.Equal(5, game.FinalStretch.Count); Assert.Equal(4, game.PlayersList[0].Pieces.Count); }
static void PrintOutPieces(LudoEngine game, string playerColor) { foreach (var player in game.PlayersList) { if (player.Color == playerColor) { for (int i = 0; i < 4; i++) { if (player.Pieces[i].InNest == true) { Console.WriteLine($"Piece nr {i + 1} is in the nest"); } else if (player.Pieces[i].Score) { } else { Console.WriteLine($"Piece nr {i + 1} have moved {player.Pieces[i].Movement}"); } } } } foreach (var item in game.TileList) { if (item.PieceList.Count > 0) { Console.Write("On tile position " + item.TilePosition + " "); for (int i = 0; i < item.PieceList.Count; i++) { Console.Write(item.PieceList[i].PlayerColor + " "); Console.Write(item.PieceList[i].PieceName + ", "); } Console.WriteLine(); } } foreach (var item in game.FinalStretch) { if (item.PieceList.Count > 0) { Console.Write("On final tile position " + item.TilePosition + " "); for (int i = 0; i < item.PieceList.Count; i++) { Console.Write(item.PieceList[i].PlayerColor + " "); Console.Write(item.PieceList[i].PieceName + ", "); } Console.WriteLine(); } } }
public void WhenAddingNewPlayer_ExpectItToHave4Pieces() { var contextMock = new Mock <LudoDbContext>(); List <Game> gameToReturn = new List <Game>(); List <User> usersToReturn = new List <User>(); contextMock.Setup(x => x.Games).ReturnsDbSet(gameToReturn); contextMock.Setup(x => x.Users).ReturnsDbSet(usersToReturn); LudoEngine game = new LudoEngine(contextMock.Object, "testgame1"); game.AddPlayer(typeof(RedPiece), "playerName"); Assert.Equal(4, game.Players[0].Pieces.Count); }
public void GivenAPlayerHasAllPiecesInNest_WhenDiceRollResultsInLessThan6_Expect0MoveablePieces() { var contextMock = new Mock <LudoDbContext>(); List <Game> gameToReturn = new List <Game>(); List <User> usersToReturn = new List <User>(); contextMock.Setup(x => x.Games).ReturnsDbSet(gameToReturn); contextMock.Setup(x => x.Users).ReturnsDbSet(usersToReturn); LudoEngine game = new LudoEngine(contextMock.Object, "testgame1"); game.AddPlayer(typeof(RedPiece), "playerName"); var moveablePieces = game.GetMoveablePieces(5); Assert.Empty(moveablePieces); }
private static void ShowOrPlayLoadedGame(LudoEngine game) { if (game != null) { if (game.Winner == null) { Play(game); } else { Console.WriteLine($"{game.Winner.Name} won this game!"); } } else { Console.WriteLine("Sorry, couldn't find game"); } }
static public void SaveGame(LudoEngine gameToSave) { bool found = false; ShowSavedGames(); for (int i = 0; i < GameList.Count && !found; i++) { if (GameList[i].GameName == gameToSave.GameName) { GameList.RemoveAt(i); found = true; } } GameList.Add(gameToSave); var json = JsonConvert.SerializeObject(GameList.ToArray()); File.WriteAllText(@"c:/windows/temp/ludo.json", json); }
private static string AskForNewGameName() { bool gameExists = true; string input; do { Console.Write("What should the game be called? "); input = Console.ReadLine(); gameExists = LudoEngine.GameExists(input, dbContext); if (gameExists) { Console.Clear(); Console.WriteLine("Sorry, name already taken."); } } while (gameExists); return(input); }
public void WhenAddingNewPlayer_ExpectItToHavePiecesOfTheCorrectType() { var contextMock = new Mock <LudoDbContext>(); List <Game> gameToReturn = new List <Game>(); List <User> usersToReturn = new List <User>(); contextMock.Setup(x => x.Games).ReturnsDbSet(gameToReturn); contextMock.Setup(x => x.Users).ReturnsDbSet(usersToReturn); LudoEngine game = new LudoEngine(contextMock.Object, "testgame1"); game.AddPlayer(typeof(RedPiece), "playerName"); var pieces = game.Players[0].Pieces; Assert.IsType <RedPiece>(pieces[0]); Assert.IsType <RedPiece>(pieces[1]); Assert.IsType <RedPiece>(pieces[2]); Assert.IsType <RedPiece>(pieces[3]); }
private static void DrawBoard(LudoEngine game) { int bluesInNest = 0; int greensInNest = 0; int redsInNest = 0; int yellowsInNest = 0; foreach (var player in game.Players) { if (player.Pieces[0].GetType() == typeof(BluePiece)) { bluesInNest = game.PlayerPiecesInNest(player).Count; } if (player.Pieces[0].GetType() == typeof(GreenPiece)) { greensInNest = game.PlayerPiecesInNest(player).Count; } if (player.Pieces[0].GetType() == typeof(RedPiece)) { redsInNest = game.PlayerPiecesInNest(player).Count; } if (player.Pieces[0].GetType() == typeof(YellowPiece)) { yellowsInNest = game.PlayerPiecesInNest(player).Count; } } FillZones(); FillZonesWithPieces(game); Console.WriteLine($" {commonZone[39]} {commonZone[40]} {commonZone[1]}"); Console.WriteLine($" Y:{yellowsInNest} {commonZone[38]} {blueSafeZone[1]} {commonZone[2]} B:{bluesInNest}"); Console.WriteLine($" {commonZone[37]} {blueSafeZone[2]} {commonZone[3]}"); Console.WriteLine($" {commonZone[36]} {blueSafeZone[3]} {commonZone[4]}"); Console.WriteLine($" {commonZone[31]} {commonZone[32]} {commonZone[33]} {commonZone[34]} {commonZone[35]} {blueSafeZone[4]} {commonZone[5]} {commonZone[6]} {commonZone[7]} {commonZone[8]} {commonZone[9]}"); Console.WriteLine($" {commonZone[30]} {yellowSafeZone[1]} {yellowSafeZone[2]} {yellowSafeZone[3]} {yellowSafeZone[4]} {greenSafeZone[4]} {greenSafeZone[3]} {greenSafeZone[2]} {greenSafeZone[1]} {commonZone[10]}"); Console.WriteLine($" {commonZone[29]} {commonZone[28]} {commonZone[27]} {commonZone[26]} {commonZone[25]} {redSafeZone[4]} {commonZone[15]} {commonZone[14]} {commonZone[13]} {commonZone[12]} {commonZone[11]}"); Console.WriteLine($" {commonZone[24]} {redSafeZone[3]} {commonZone[16]}"); Console.WriteLine($" {commonZone[23]} {redSafeZone[2]} {commonZone[17]}"); Console.WriteLine($" R:{redsInNest} {commonZone[22]} {redSafeZone[1]} {commonZone[18]} G:{greensInNest}"); Console.WriteLine($" {commonZone[21]} {commonZone[20]} {commonZone[19]}"); }
static void Main(string[] args) { dbContext = new LudoDbContext(); int choice; do { choice = ChooseFromMainMenu(); switch (choice) { case 1: // Create new game var newGame = SetupNewGame(); Play(newGame); break; case 2: // Load game from database string gameToLoad = AskForGameNameToLoad(); var game = LudoEngine.Load(gameToLoad, dbContext); ShowOrPlayLoadedGame(game); break; case 3: // Show user statistics var name = AskForUsername(); var user = LudoEngine.GetUserByName(name, dbContext); ShowStatistics(user); break; case 4: // Show info about all games in database ShowAllGames(); break; default: break; } }while (choice != 9); }
private static void FillZonesWithPieces(LudoEngine game) { foreach (var player in game.Players) { if (player.Pieces[0].GetType() == typeof(BluePiece)) { DrawPlayerSafeZone(blueSafeZone, player.Pieces); } else if (player.Pieces[0].GetType() == typeof(GreenPiece)) { DrawPlayerSafeZone(greenSafeZone, player.Pieces); } else if (player.Pieces[0].GetType() == typeof(RedPiece)) { DrawPlayerSafeZone(redSafeZone, player.Pieces); } if (player.Pieces[0].GetType() == typeof(YellowPiece)) { DrawPlayerSafeZone(yellowSafeZone, player.Pieces); } } for (int i = 1; i < commonZone.Length; i++) { commonZone[i] = "O"; foreach (var player in game.Players) { foreach (var piece in player.Pieces) { if (piece.AbsoluteBoardPosition == i && !game.PieceIsInSafeZone(piece)) { commonZone[i] = piece.GetType().Name.Remove(1); } } } } }
static public string[] NewGame(int numbersOfPlayers, string gameName) { game = new LudoEngine(numbersOfPlayers, gameName); string[] returnString = new string[2]; return(returnString = game.NextTurn()); }