public static LudoEngine Load(string name, LudoDbContext context) { try { // Find the game in the database using name var gameEntity = context.Games.Include(g => g.NextToRollDice).Include(g => g.Winner).Where(g => g.Name == name).Single(); LudoEngine game = CreateGameFromEntity(gameEntity, context); // Get all the players in the game var gameMemberEntities = context.GameMembers.Include(gm => gm.Piece).Where(gm => gm.GameId == gameEntity.GameId).ToList(); // Setup a player object for each member in the game foreach (var gameMemberEntity in gameMemberEntities) { var pieceEntity = context.Pieces.Where(p => p.PieceId == gameMemberEntity.Piece.PieceId).Single(); var pieceType = GetPieceTypeFromColor(pieceEntity.Color); var user = CreatePlayerFromGameMember(gameMemberEntity, pieceType, gameEntity, context); game.Players.Add(user); } return(game); }catch { return(null); } }
public static List <String> AddCorrespondingNames(List <String> Values, string CurrentName) { int Games = 0; int Wins = 0; using var context = new LudoDbContext(); List <Player> Players = context.players.ToList(); if (!Values.Contains(CurrentName)) { for (int j = 0; j < Players.Count; j++) { if (CurrentName == Players[j].Name) { Games++; if (Players[j].Won == true) { Wins++; } } } Values.Add(CurrentName); Values[0] = Games.ToString(); Values[1] = Wins.ToString(); } return(Values); }
public static List <string> PlayerCreate(LudoDbContext context, Game game, List <string> CurrentColors, int PlayerAmount) { Console.WriteLine("Whats your name player?"); var CurrentName = Console.ReadLine(); Console.Clear(); Console.WriteLine("Welcome " + CurrentName + " Whats your preferred color?"); var SelectedColor = MenuNavigator.Menu.ShowMenu(CurrentColors); CurrentColors = Banner.ListBan(SelectedColor, CurrentColors); Console.WriteLine("You have successfully chosen the color " + SelectedColor + "!"); var gamer = PlayerFactory.Create(CurrentName, SelectedColor, false); game.Players.Add(gamer); context.SaveChanges(); PlayerAmount--; if (0 < PlayerAmount) { CurrentColors = PlayerCreate(context, game, CurrentColors, PlayerAmount); } return(CurrentColors); }
/* * [Name] * Gamename * (Turn) * 12 * (Players) * player1: 3/4 * player2: 2/4 * player3: 2/4 * player4: 0/4 * (LastCheckpoint) * 2020-03-20 13:55 */ public static LudoDbContext Loading() { var context = new LudoDbContext(); var gamesInProgress = context.games.Where(g => g.Complete == false).Include(g => g.Players).ThenInclude(p => p.Pawns).ToList(); var gamenames = new List <string>(); foreach (var game in gamesInProgress) { gamenames.Add(game.Name); Console.WriteLine($"[Name]\n {game.Name}\n (Turn)\n{game.Turn}\n (Players)"); foreach (var player in game.Players) { Console.WriteLine(player.Name); } Console.WriteLine($"(LastCheckpoint)\n{game.LastCheckpointTime}\n"); } var chosenName = MenuNavigator.Menu.ShowMenu(gamenames); var chosenGame = gamesInProgress.FirstOrDefault(p => p.Name == chosenName); chosenGame.InProgress = true; context.SaveChanges(); return(context); }
public static IQueryable <Pawn> PawnsInPlayer(int PlayerId) { var Context = new LudoDbContext(); var Result = Context.players.First(p => p.Id == PlayerId).Pawns.AsQueryable(); return(Result); }
public static IQueryable <Player> PlayersInGame(int GameId) { var Context = new LudoDbContext(); var Result = Context.games.First(g => g.Id == GameId).Players.AsQueryable(); return(Result); }
public static void ResumeGame() { using var context = new LudoDbContext(); if (context.Board.Where(g => g.ID >= 0).Any()) { var game = context.Board.Where(g => g.ID >= 0 && g.GameEnded == null).OrderBy(i => i.ID).Last(); var players = context.Player.Where(p => p.BoardID == game.ID).ToList(); var moves = context.Move.Where(m => m.BoardID == game.ID).ToList(); Console.WriteLine($"Resuming game({game.ID})..."); foreach (var player in players) { player.Pieces = Setup.Pieces(player.Color); } foreach (var move in moves) { move.Player = players.Where(p => p.ID == move.PlayerID).Single(); } game.Players = players; game.Moves = moves; Clear(); RenderGame(game); } else { Console.WriteLine("Can't find any games in the database."); Clear(); } }
public static void CreateScoreboard() { List <string> Values = new List <string>(); Values.Add("1"); Values.Add("0"); using var context = new LudoDbContext(); List <Player> Players = context.players.ToList(); var results = Players.GroupBy(p => p.Name) .Select(grp => grp.First()) .ToList(); for (int i = 0; i < results.Count; i++) { string CurrentName = results[i].Name; AddCorrespondingNames(Values, CurrentName); var Wins = Int32.Parse(Values[1]); var Games = Int32.Parse(Values[0]); var Losses = Math.Max((Games - Wins), 1); decimal WinRate; WinRate = (decimal)Wins / Games; WinRate = decimal.Round(WinRate, 2); Values[1] = "0"; Values[0] = "0"; Console.WriteLine(i + 1 + "."); Console.WriteLine("Name:" + CurrentName); Console.WriteLine("Games:" + Games); Console.WriteLine("Wins:" + Wins); Console.WriteLine("Losses:" + Losses); Console.WriteLine("WinRate:" + WinRate); } }
public static void LoadGame() { bool isRunning = true; do { using var context = new LudoDbContext(); var gameOptions = context.Board.Where(g => g.GameEnded == null).ToList(); foreach (var gameOption in gameOptions) { Console.WriteLine($"{gameOption.ID}. {gameOption.GameStarted}"); } Console.Write("\nEnter the Game ID you would like to start :\t"); var userInput = Console.ReadLine(); var success = Int32.TryParse(userInput, out int result); if (success && context.Board.Where(g => g.GameEnded == null && g.ID == result).Any()) { var game = context.Board.Where(g => g.ID == result).Single(); var players = context.Player.Where(p => p.BoardID == game.ID).ToList(); var moves = context.Move.Where(m => m.BoardID == game.ID).ToList(); Console.Clear(); Console.WriteLine($"Loading game({game.ID})..."); foreach (var player in players) { player.Pieces = Setup.Pieces(player.Color); } foreach (var move in moves) { move.Player = players.Where(p => p.ID == move.PlayerID).Single(); } game.Players = players; game.Moves = moves; Clear(); RenderGame(game); isRunning = false; } else if (success) { Console.WriteLine("Couldn't find the game you're looking for."); Clear(); } else { Console.WriteLine("Couldn't parse the value you entered."); Clear(); } Console.Clear(); } while (isRunning); }
private static void CreatePlayer(Game board, List <Player> player) { // Slumpar fram vem som startar. Random randomStartPlayer = new Random(); int playerId = randomStartPlayer.Next(1, player.Count); List <string> dbColors = new List <string>() { "Red", "Blue", "Yellow", "Green" }; using (var context = new LudoDbContext()) { for (int i = 0; i < player.Count; i++) { // SET Player Color player[i].PlayerColor = dbColors[i]; // SET Player Name bool isRunning = true; while (isRunning) { Console.Write($"\nPlayer {i + 1} Name: "); player[i].Name = Console.ReadLine().ToString(); bool containsInt = player[i].Name.Any(char.IsDigit); if (containsInt == true) { Console.Write("No numbers as a name, try again."); } else if (player[i].Name == String.Empty) { Console.WriteLine("Please enter a name.."); } else { Console.WriteLine($"Added Player {i + 1} | Name: {player[i].Name} | Color: {player[i].PlayerColor} |"); isRunning = false; } } if (playerId == i + 1) { player[i].PlayerTurn = true; Console.WriteLine($"{player[i].Name} starts."); } } } CreatePieces(board, player); Console.WriteLine("\nThe game starts in 3 seconds"); Thread.Sleep(3000); }
private static LudoEngine CreateGameFromEntity(Game gameEntity, LudoDbContext context) { // Instantiate a new game and set the needed properties to values from the gameEntity LudoEngine game = new LudoEngine(context); game.gameName = gameEntity.Name; game.CurrentPlayer = gameEntity.NextToRollDice; game.game = gameEntity; game.Winner = gameEntity.Winner; return(game); }
public static bool GameExists(string name, LudoDbContext context) { if (name == "") { return(true); } try { Game game = context.Games.Where(g => g.Name.ToLower() == name.ToLower()).Single(); return(game != null); } catch { return(false); } }
public static void Main(string[] args) { using var context = new LudoDbContext(); bool isRunning = true; do { var userInput = PrintMenu(); var success = Int32.TryParse(userInput, out int result); Console.Clear(); if (success) { switch (result) { case 1: StartGame(); break; case 2: ResumeGame(); break; case 3: LoadGame(); break; case 4: GameHistory(); break; case 0: isRunning = false; Console.WriteLine("Already quitting this ludo game? Press any key to exit... Bye!"); break; default: Console.WriteLine("Couldn't find your value in the menu."); break; } } else { Console.WriteLine("Couldn't parse the value you entered."); } Clear(); } while (isRunning); }
public LudoEngine(LudoDbContext dbContext, string gameName) { context = dbContext; if (!GameExists(gameName, context)) { this.gameName = gameName; } else { throw new ArgumentException("Make sure to check the game name is available before instantiating."); } random = new Random(); Players = new List <User>(); }
public static void CreateBots(LudoDbContext context, Game game, List <string> currentColors) { List <string> BotNames = new List <string>(); BotNames.AddRange(new string[] { "Lion", "Panda", "Tiger" }); var BotAmount = currentColors.Count; for (int i = 0; i < BotAmount; i++) { var Name = Randomizer.ListRandomizer(BotNames); BotNames = Banner.ListBan(Name, BotNames); var Color = Randomizer.ListRandomizer(currentColors); currentColors = Banner.ListBan(Color, currentColors); var bot = PlayerFactory.Create(Name, Color, true); game.Players.Add(bot); } }
public static LudoDbContext GameCreate() { Console.WriteLine("What do you want your session to be called?"); var GameName = Console.ReadLine(); var context = new LudoDbContext(); var game = new Game() { Name = GameName, CreationTime = DateTime.Now, Complete = false, InProgress = true, Turn = 1, }; context.games.Add(game); context.SaveChanges(); Console.WriteLine("How many players are there?"); var PlayerAmountList = new List <string>(); PlayerAmountList.AddRange(new String[] { "1", "2", "3", "4" }); var PlayerAmount = MenuNavigator.Menu.ShowMenu(PlayerAmountList); var CurrentColors = new List <string>(); CurrentColors.AddRange(new string[] { "Red", "Green", "Blue", "Yellow" }); CurrentColors = PlayerCreation.PlayerCreate(context, game, CurrentColors, Int32.Parse(PlayerAmount)); if (PlayerAmount != "4") { var BotAmount = BotDialogue(int.Parse(PlayerAmount)); if (BotAmount > 0) { BotCreation.CreateBots(context, game, CurrentColors); } } context.SaveChanges(); return(context); }
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); }
public static LudoDbContext Menu() { List <string> MenuAlternatives = new List <string>(); MenuAlternatives.AddRange(new string[] { "New Game", "Load Game", "Scoreboard", "Exit" }); var Choice = MenuNavigator.Menu.ShowMenu(MenuAlternatives); var context = new LudoDbContext(); switch (Choice) { case "New Game": //var game = GameCreation.GameCreate(); //return game; context = GameCreation.GameCreate(); return(context); case "Load Game": //game = LoadGame.Loading(); //return game; context = LoadGame.Loading(); return(context); case "Scoreboard": Scoreboard.CreateScoreboard(); Console.WriteLine("Press any key to continue"); Console.ReadLine(); Menu(); break; case "Exit": System.Environment.Exit(0); break; } return(null); }
public static void CreatePawn(string GameName) { var context = new LudoDbContext(); var game = context.games.FirstOrDefault(p => p.Name == GameName); var Colors = new List <string>(); Colors.AddRange(new string[] { "Red", "Green", "Blue", "Yellow" }); for (var i = 0; i < game.Players.Count(); i++) { var Color = Colors[i]; for (var j = 0; j < Colors.Count; j++) { Pawn pawn = new Pawn() { Position = 1 + 10 * j, PawnState = Pawn.State.Base }; context.pawns.Add(pawn); context.SaveChanges(); } } }
public Pawn RollDiceNextPlayer(LudoDbContext context, Game game, string color) { var currentPlayer = game.Players.First(p => p.Color == color); var Bot = false; if (currentPlayer.Bot) { Bot = true; } var pawn = new Pawn(); Console.WriteLine("Press any key for next dice roll!"); var RandomNumber = new Random(); var r = 0; if (Bot) { r = RandomNumber.Next(1, 7); } else { r = VisualWidgets.MainFunction(); if (r == 0) { r = 1; } } Thread.Sleep(500); Console.WriteLine($"You got {r}"); var currentPawns = currentPlayer.Pawns; var currentPawnsInBase = currentPawns.Where(p => p.PawnState == Pawn.State.Base); var CurrentPawnsInPlay = currentPawns.Where(p => p.PawnState == Pawn.State.Playing); if (r == 6 || r == 1 && currentPawnsInBase.Any()) { List <string> Options = new List <string>(); var Choice = ""; if (r == 1) { if (!CurrentPawnsInPlay.Any()) { MovePawnOutOfNest(currentPawns); } else { Options.AddRange(new string[] { "Move one pawn out of the nest", "Move an existing pawn" }); if (Bot) { Choice = Randomizer.ListRandomizer(Options); Console.WriteLine(Choice); } else { Console.WriteLine("The Bot will: \n" + Choice); Choice = MenuNavigator.Menu.ShowMenu(Options); } } } else { if (currentPawnsInBase.Any()) { Options.Add("Move one pawn out of the nest and 6 steps"); if (currentPawnsInBase.Count() > 1) { Options.Add("Move two pawns out of the nest"); } } if (CurrentPawnsInPlay.Any()) { Options.Add("Move an existing pawn"); } if (Bot) { Choice = Randomizer.ListRandomizer(Options); Console.WriteLine("The Bot will: \n" + Choice); } else { Console.WriteLine("What Would you like to do?"); Choice = MenuNavigator.Menu.ShowMenu(Options); } } switch (Choice) { case "Move two pawns out of the nest": MovePawnOutOfNest(currentPawns); pawn = MovePawnOutOfNest(currentPawns); break; case "Move one pawn out of the nest and 6 steps": pawn = MovePawnOutOfNest(currentPawns); MovePlayingPawn(pawn, r); break; case "Move an existing pawn": DeterminePawn(currentPawns); break; case "Move one pawn out of the nest": MovePawnOutOfNest(currentPawns); break; } } else if (CurrentPawnsInPlay.Any()) { DeterminePawn(currentPawns); } Pawn MovePawnOutOfNest(IEnumerable <Pawn> pawns) { pawn = pawns.First(p => p.PawnState == Pawn.State.Base); pawn.PawnState = Pawn.State.Playing; pawn.Position = 0; return(pawn); } Pawn DeterminePawn(IEnumerable <Pawn> pawns) { var pawn = new Pawn(); var playingPawns = pawns.Where(p => p.PawnState == Pawn.State.Playing); if (playingPawns.Count() == 1) { pawn = pawns.First(p => p.PawnState == Pawn.State.Playing); } else { var pawnsPlaying = playingPawns.ToList(); List <string> pawnPositions = new List <string>(); for (int i = 0; playingPawns.Count() > i; i++) { pawnPositions.Add(pawnsPlaying[i].Position.ToString()); } var choice = ""; if (Bot) { choice = Randomizer.ListRandomizer(pawnPositions); Console.WriteLine("The bot will move the pawn at position: " + choice); } else { Console.WriteLine("Which Pawn do you want to move?"); choice = MenuNavigator.Menu.ShowMenu(pawnPositions); } pawn = pawns.First(p => p.Position == Int32.Parse(choice)); } return(MovePlayingPawn(pawn, r)); } Console.ReadLine(); return(pawn); }
private static User CreatePlayerFromGameMember(GameMember gameMemberEntity, Type pieceType, Game gameEntity, LudoDbContext context) { var user = context.Users.Where(u => u.UserId == gameMemberEntity.UserId).Single(); user.Pieces = new List <IPiece>(); // Get all the player piece positions var gamePositions = context.GamePositions.Where(gp => gp.Game == gameEntity && gp.User == user).ToList(); // For each piece that the player has in the game, create a piece object and add it to the player Pieces-list foreach (var gamePosition in gamePositions) { IPiece piece = (IPiece)Activator.CreateInstance(pieceType); piece.Position = gamePosition.Position; user.Pieces.Add(piece); } return(user); }
public static User GetUserByName(string name, LudoDbContext context) { try { return(context.Users.Where(u => u.Name.ToLower() == name.ToLower()).Single()); } catch { return(null); } }
public static void RenderGame(Board game) { bool fool = true; Setup.Lists(game); foreach (var move in game.Moves) { Dice.Value = move.DiceValue; game.MovePiece(move); } bool gameRunning = true; do { foreach (var player in game.Players) { fool = false; game.PrintLudoBoard(); int pieceId = 1; Setup.StringColor(player.Color); if (player.AI == false) { Console.WriteLine($"It's {player.Name} turn. Press any key to roll dice!"); Clear(); Dice.Roll(); bool success = false; do { game.PrintLudoBoard(); Setup.StringColor(player.Color); Console.Write($"{player.Name} rolled a {Dice.Value}."); if (!player.Pieces[0].AbleToMakeMove() && !player.Pieces[1].AbleToMakeMove() && !player.Pieces[2].AbleToMakeMove() && !player.Pieces[3].AbleToMakeMove()) { Console.WriteLine(" You can't move any piece."); fool = true; success = true; Clear(); } else { Console.WriteLine($" Which piece do you want to move? "); var playerInput = Console.ReadLine(); success = Int32.TryParse(playerInput, out pieceId); Console.Clear(); if (success) { switch (pieceId) { case 1: case 2: case 3: case 4: if (player.Pieces[pieceId - 1].AbleToMakeMove()) { success = true; } else { success = false; game.PrintLudoBoard(); Setup.StringColor(player.Color); Console.WriteLine("You're not able to move this piece. Try again..."); Clear(); } break; default: game.PrintLudoBoard(); Setup.StringColor(player.Color); Console.WriteLine("Wrong piece value. Try again..."); success = false; Clear(); break; } } else { game.PrintLudoBoard(); Setup.StringColor(player.Color); Console.WriteLine("Couldn't parse piece value. Try again..."); Clear(); } } } while (!success); } else if (player.AI == true) { Console.WriteLine($"It's {player.Name} turn. Rolling the dice"); player.Thinking(); Console.Clear(); Dice.Roll(); game.PrintLudoBoard(); Setup.StringColor(player.Color); if (!player.Pieces[0].AbleToMakeMove() && !player.Pieces[1].AbleToMakeMove() && !player.Pieces[2].AbleToMakeMove() && !player.Pieces[3].AbleToMakeMove()) { fool = true; Console.WriteLine($"{player.Name} can't move any piece."); player.Thinking(); } else { Console.WriteLine($"{player.Name} rolled a {Dice.Value}. Choosing which piece to move "); player.Thinking(); var rnd = new Random(); pieceId = rnd.Next(1, 5); if (!player.Pieces[pieceId - 1].AbleToMakeMove()) { for (int i = 0; i < player.Pieces.Length; i++) { if (player.Pieces[i].AbleToMakeMove()) { pieceId = i + 1; } } } } } if (!fool) { Move currentMove = new Move(player, pieceId, Dice.Value, player.ID, game.ID); game.MovePiece(currentMove); game.Moves.Add(currentMove); using var movecontext = new LudoDbContext(); movecontext.Move.Add(currentMove); movecontext.SaveChanges(); } Console.Clear(); if (game.Ended(player)) { game.GameEnded = DateTime.Now; using var context = new LudoDbContext(); context.Board.Update(game); context.SaveChanges(); gameRunning = false; game.PrintLudoBoard(); Setup.StringColor(player.Color); Console.WriteLine($"{player.Name} won!"); } } } while (gameRunning); Clear(); }
public static void GameHistory() { bool isRunning = true; Board game = new Board(); do { using var context = new LudoDbContext(); var gameOptions = context.Board.Where(g => g.GameEnded != null).ToList(); foreach (var gameOption in gameOptions) { Console.WriteLine($"{gameOption.ID}. {gameOption.GameEnded}"); } Console.Write("\nEnter the Game ID you would like to watch : \t"); var userInput = Console.ReadLine(); var success = Int32.TryParse(userInput, out int result); if (success && context.Board.Where(g => g.GameEnded != null && g.ID == result).Any()) { game = context.Board.Where(g => g.ID == result).Single(); var players = context.Player.Where(p => p.BoardID == game.ID).ToList(); var moves = context.Move.Where(m => m.BoardID == game.ID).ToList(); Console.Clear(); foreach (var player in players) { player.Pieces = Setup.Pieces(player.Color); } foreach (var move in moves) { move.Player = players.Where(p => p.ID == move.PlayerID).Single(); } game.Players = players; game.Moves = moves; isRunning = false; } else if (success) { Console.WriteLine("Couldn't find the game you're looking for."); Clear(); } else { Console.WriteLine("Couldn't parse the value you entered."); Clear(); } } while (isRunning); Setup.Lists(game); int numberOfMoves = 0; foreach (var move in game.Moves) { Dice.Value = move.DiceValue; game.MovePiece(move); numberOfMoves++; } game.PrintLudoBoard(); var duration = (game.GameEnded - game.GameStarted).GetValueOrDefault(); var totalMinutes = (int)duration.TotalMinutes; Console.WriteLine($"This game took {numberOfMoves} moves to finish and lasted for {totalMinutes}minutes"); Clear(); }
public static List <Game> GetAllGames(LudoDbContext context) { return(context.Games.Where(g => !g.Active).Include(g => g.Winner).OrderBy(g => g.GameId).ToList()); }
public static void StartGame() { var players = new List <Player>(); var moves = new List <Move>(); for (int i = 0; i < 4; i++) { var currentColor = Enum.GetName(typeof(Colors), i); Colors colorType = (Colors)Enum.Parse(typeof(Colors), currentColor); Console.WriteLine("1. Add New Player"); Console.WriteLine("2. Add AI-Player"); Console.Write($"\n{currentColor} Player :\t"); var userInput = Console.ReadLine(); var success = Int32.TryParse(userInput, out int result); Console.Clear(); if (success) { switch (result) { case 1: Console.Write("Enter player name: "); var name = Console.ReadLine(); var newPlayer = new Player(name, colorType, false); players.Add(newPlayer); Console.Clear(); Console.WriteLine($"Adding new {newPlayer.Name}({currentColor})..."); break; case 2: Console.Write("Enter player name: "); name = Console.ReadLine(); players.Add(new Player(name, colorType, true)); Console.Clear(); Console.WriteLine($"Adding new {name} AI({currentColor})..."); break; default: Console.WriteLine("Couldn't find your value in the menu."); i--; break; } } else { Console.WriteLine("Couldn't parse the value you entered."); i--; } Clear(); } using var context = new LudoDbContext(); var game = new Board(players, moves, DateTime.Now); context.Board.Add(game); context.SaveChanges(); foreach (var player in players) { player.BoardID = game.ID; context.Player.Add(player); } context.SaveChanges(); RenderGame(game); }
private LudoEngine(LudoDbContext dbContext) { context = dbContext; random = new Random(); Players = new List <User>(); }