public List <Piece> CheckPositions(Piece movedPiece, List <Player> players) { var ludoDbAccess = new LudoDbAccess(); List <Piece> updatedPieces = new List <Piece>(); List <List <Piece> > eachPlayersPieces = new List <List <Piece> >(); for (int i = 0; i < players.Count; i++) { eachPlayersPieces.Add(ludoDbAccess.GetCurrentPlayersPieces(players[i].Id)); } for (int i = 0; i < eachPlayersPieces.Count; i++) { foreach (var piece in eachPlayersPieces[i]) { // Checking for knuff if (piece.Position == movedPiece.Position && piece.PlayerId != movedPiece.PlayerId) { Console.WriteLine("KNUFF!".Rainbow()); piece.Position = GameBoard.nestPositions[i]; updatedPieces.Add(piece); Thread.Sleep(3000); } } } return(updatedPieces); }
public static List <Piece> GetPlayerPieces(Player player) { LudoDbAccess ludoDbAccess = new LudoDbAccess(); List <Piece> playersPieces = ludoDbAccess.GetCurrentPlayersPieces(player.Id); return(playersPieces); }
private static void CreatePieces(Game board, List <Player> player) { if (player is null) { throw new ArgumentNullException(nameof(player)); } // SET Piece Id, Position List <Piece> piece = new List <Piece>(); var counter = 0; for (int i = 0; i < player.Count; i++) { piece.Add(new Piece()); piece.Add(new Piece()); piece.Add(new Piece()); piece.Add(new Piece()); for (int x = 0; x < 4; x++) { piece[counter + x].Position = GameBoard.nestPositions[i]; piece[counter + x].IsActive = true; } counter += 4; } LudoDbAccess ludoDbAccess = new LudoDbAccess(); ludoDbAccess.SaveGame(board, player, piece); }
// End Of Gameloop, Update Player Turn public static void UpdatePlayerTurn(List <Piece> pieces, List <Player> currentPlayers, int diceValue) { LudoDbAccess ludoDbAccess = new LudoDbAccess(); int playercounter = currentPlayers.Count; ludoDbAccess.ChangeIsActive(pieces); try { int gameId = Convert.ToInt32(currentPlayers[0].GameId); var currentGame = ludoDbAccess.GetAllFinishedGames().Where(x => x.Id == gameId).Single(); // Checks If Game Is Completed // If True, End Game. if (currentGame.IsCompleted == true) { var runMenu = new UserInterface(); runMenu.MainMenu(); } } catch (Exception) { // If Game IsCompleted == False, continue next Gameloop } // Changing Player Turn if (diceValue != 6) { for (int i = 0; i < currentPlayers.Count; i++) { if (currentPlayers[i].PlayerTurn == true) { currentPlayers[i].PlayerTurn = false; if (i == playercounter - 1) { currentPlayers[0].PlayerTurn = true; ludoDbAccess.SavePositionsToDb(pieces, currentPlayers, diceValue); break; } else { currentPlayers[i + 1].PlayerTurn = true; ludoDbAccess.SavePositionsToDb(pieces, currentPlayers, diceValue); break; } } } } else // If diceValue == 6, Skip update player turn { ludoDbAccess.SavePositionsToDb(pieces, currentPlayers, diceValue); } }
// Loading Active Games To List, Player Can Choose What Game To Continue Playing public static void LoadGamesUI() { Console.Clear(); List <Game> games = new LudoDbAccess().GetAllUnfinishedGames(); List <Player> players = new LudoDbAccess().GetAllPlayers(); UserInterface userInterface = new UserInterface(); if (games.Count == 0) { Console.WriteLine("Sorry mate, no games to be found " + "\nPlease create a new game."); Thread.Sleep(2000); userInterface.MainMenu(); } while (isRunning) { Console.WriteLine("What game do you want to load?\n"); for (int i = 0; i < games.Count; i++) { playerCounter = 0; Console.WriteLine($"[{games[i].Id}]\nGame Id: {games[i].Id}\nLast Played: {games[i].LastTimePlayedDate}"); Console.Write($"Players:"); foreach (Player p in players) { if (p.GameId == games[i].Id) { Console.Write($"\nPlayer: {p.Name} | Color: {playerColors[playerCounter]}"); playerCounter++; } } Console.WriteLine($"\n---------------------------------------------------------------------------\n"); } int.TryParse(Console.ReadLine(), out userInput); foreach (var g in games) { if (userInput == g.Id) { LoadGame loadGame = new LoadGame(); Console.WriteLine($"Loading game id {g.Id}..."); loadGame.LoadAnyGame(g.Id); } } } }
public void When_Using_GetHigestPlayerId_Expecting_ShouldReturn_Name_Messi() { // Arange - lägg upp testet genom att ta in variabler osv. LudoDbAccess ludoDbAccess = new LudoDbAccess(); int expected = 8; // Act - Vad ska testet göra, en metod som får ut ett resultat? int result = ludoDbAccess.GetHighestPlayerId(); // Assert - Vad vill vi att resultatet ska bli? Assert.Equal(expected, result); }
// Loading Old Games to view in a List public static void LoadOldGamesUI() { Console.Clear(); List <Game> games = new LudoDbAccess().GetAllFinishedGames(); List <Player> players = new LudoDbAccess().GetAllPlayers(); UserInterface userInterface = new UserInterface(); if (games.Count == 0) { Console.WriteLine("Sorry mate, no games to be found " + "\nPlease create a new game."); Thread.Sleep(2000); userInterface.MainMenu(); } while (isRunning) { Console.WriteLine("Here are all your completed games! \n"); for (int i = 0; i < games.Count; i++) { playerCounter = 0; Console.WriteLine($"Game Id: {games[i].Id}\nGame Completed: {games[i].CompletedDate}\nWinner: {games[i].WinnerPlayerName}"); Console.Write($"Players:"); foreach (Player p in players) { if (p.GameId == games[i].Id) { Console.Write($"\nPlayer: {p.Name} | Color: {playerColors[playerCounter]}"); playerCounter++; } } Console.WriteLine($"\n---------------------------------------------------------------------------\n"); } Console.WriteLine("[1] Go To Main Menu"); do { int.TryParse(Console.ReadLine(), out userInput); } while (userInput != 1); userInterface.MainMenu(); } }
// Loading The Latest Game public void ContinueGame() { LudoDbAccess ludoDbAccess = new LudoDbAccess(); UserInterface userInterface = new UserInterface(); // GET latest game from board id var gameBoard = ludoDbAccess.GetHighestBoardId(); if (gameBoard == 0) { Console.WriteLine("No game found.. " + "\nCreate a new game.."); Thread.Sleep(2000); userInterface.MainMenu(); } LoadAnyGame(gameBoard); }
public void LoadAnyGame(int gameID) // Loading The Game Chosen In Loading Menu { LudoDbAccess ludoDbAccess = new LudoDbAccess(); // GET all players from game id var players = ludoDbAccess.GetPlayersWhenLoadingGame(gameID); // SET players Game Board GameBoard gameBoard = new GameBoard(); for (int i = 0; i < players.Count; i++) { players[i].PlayerBoard = gameBoard.PlayersGameBoards[i]; } // Run Game GameLoop gameLoop = new GameLoop(); gameLoop.RunGame(players); }
/// <summary> /// INITIALIZE GAME /// </summary> public void CreateNewGame() { // Creating a new board Game board = new Game(); GameLoop gameLoop = new GameLoop(); // Creating amount of players Console.WriteLine("How many players? (2-4)"); List <Player> player = new List <Player>(); LudoDbAccess ludoDbAccess = new LudoDbAccess(); int highestId = ludoDbAccess.GetHighestPlayerId(); GameBoard gameBoards = new GameBoard(); // Checking the amount of players while (isRunning) { int.TryParse(Console.ReadLine(), out userInput); if (userInput > 1 && userInput < 5) { for (int i = 0; i < userInput; i++) { player.Add(new Player() { Id = highestId + i + 1, PlayerBoard = gameBoards.PlayersGameBoards[i] }); } isRunning = false; } else { Console.WriteLine("Please enter a valid number."); } } CreatePlayer(board, player); gameLoop.RunGame(player); }
// Updates all the pieces positions on the game board public static List <string> PiecesOnGameBoardUpdate(List <Player> allActivePlayers) { LudoDbAccess ludoDbAccess = new LudoDbAccess(); List <string> gb = new GameBoard().CompleteGameBoard; // Get all player, and put their pieces to individual lists (up to 4 lists) // list[0] == player 1, list[1] == player 2... and so on. List <List <Piece> > eachPlayersPieces = new List <List <Piece> >(); for (int i = 0; i < allActivePlayers.Count; i++) { eachPlayersPieces.Add(ludoDbAccess.GetCurrentPlayersPieces(allActivePlayers[i].Id)); } // Checking each square on the game board for (int i = 0; i < gb.Count; i++) { // Set counter, (can only be 4 pieces on each square) int counter = 4; // Checking each player for (int x = 0; x < eachPlayersPieces.Count; x++) { int pieceCounter = 0; // Piece counter // This is to see what piece it is.. "1", "2", "3" or "4" foreach (var piece in eachPlayersPieces[x]) // Looping each player pieces (each player has x4 pieces) { pieceCounter += 1; if (piece.Position == i) { counter -= 1; if (x == 0) // Player 1 { if (pieceCounter == 1) // piece Id { gb[i] = gb[i] + "1".Red(); } else if (pieceCounter == 2) { gb[i] = gb[i] + "2".Red(); } else if (pieceCounter == 3) { gb[i] = gb[i] + "3".Red(); } else if (pieceCounter == 4) { gb[i] = gb[i] + "4".Red(); } } else if (x == 1) // Player 2 { if (pieceCounter == 1) // piece Id { gb[i] = gb[i] + "1".Blue(); } else if (pieceCounter == 2) { gb[i] = gb[i] + "2".Blue(); } else if (pieceCounter == 3) { gb[i] = gb[i] + "3".Blue(); } else if (pieceCounter == 4) { gb[i] = gb[i] + "4".Blue(); } } else if (x == 2) // Player 3 { if (pieceCounter == 1) // piece Id { gb[i] = gb[i] + "1".Yellow(); } else if (pieceCounter == 2) { gb[i] = gb[i] + "2".Yellow(); } else if (pieceCounter == 3) { gb[i] = gb[i] + "3".Yellow(); } else if (pieceCounter == 4) { gb[i] = gb[i] + "4".Yellow(); } } else if (x == 3) // Player 4 { if (pieceCounter == 1) // piece Id { gb[i] = gb[i] + "1".Green(); } else if (pieceCounter == 2) { gb[i] = gb[i] + "2".Green(); } else if (pieceCounter == 3) { gb[i] = gb[i] + "3".Green(); } else if (pieceCounter == 4) { gb[i] = gb[i] + "4".Green(); } } } } } // If there's no piece, fill square with blanks while (counter != 0) { gb[i] = gb[i] + " "; counter -= 1; } } return(gb); }