} //Printing the board public Player PlayerName(int i, IManageScrabbleDb manageScrableDb) //OK Entering the names of players { Console.WriteLine(); string playerName = ""; int x = 0; while (x == 0) { Console.Write($"Enter player {i + 1} name: "); playerName = Console.ReadLine(); if (playerName.Length < 1) { Console.WriteLine("You havn't entered correct name"); } else { x = 1; } } if ((manageScrableDb.GetAllPlayers().Exists(p => p.PlayerName == playerName))) { Console.WriteLine($"Welcome {playerName} to the Scrabble again!"); } else { manageScrableDb.InsertPlayer(playerName); Console.WriteLine($"Welcome {playerName} to the Scrabble!"); } return(manageScrableDb.GetAllPlayers().FirstOrDefault(p => p.PlayerName == playerName)); }
public List <Player> PlayersNames(int howManyPlayers, IManageScrabbleDb manageScrableDb) //OK getting players names { List <Player> players = new List <Player>(); for (int i = 0; i < howManyPlayers; i++) { players.Add(PlayerName(i, manageScrableDb)); } return(players); }
public void Scrabble(IManageScrabbleDb manageScrableDb) { int roundNumber = 1; int gameId = manageScrableDb.GetLastGameId(); var howManyPlayers = HowManyPlayers(); //getting the number of players var players = PlayersNames(howManyPlayers, manageScrableDb); //getting the names of players List <Tile> bag = ShuffleBag(); //getting full shuffled bag with tiles List <PlayerHand> playersHands = new List <PlayerHand>(); List <int> playingPlayersId = new List <int>(); // the list for playing players foreach (var player in players) { var generatedNewPlayerHand = GenerateNewHand(bag); playersHands.Add(new PlayerHand(player.PlayerId, generatedNewPlayerHand, 0)); bag.RemoveRange(0, 7); playingPlayersId.Add(player.PlayerId); } //initiation hands and the starting points for players List <BoardCell> gameBoard = new List <BoardCell>(); foreach (var cell in BoardInitialData.DataSeed) { gameBoard.Add(new BoardCell(cell.BoardCellId, cell.HPosition, cell.VPosition, cell.SpecialValue, '.')); } //initiation of the board //'.' List <Tile> listOfUniqueTiles = new List <Tile>(); foreach (var tile in TileInitialData.DataSeed) { listOfUniqueTiles.Add(new Tile(tile.TileId, tile.Letter, tile.Value)); } //initiation of all possible tiles and their values PrintBoard(gameBoard, listOfUniqueTiles); while (skipedMoves.Count < 2) //the case when the game is over { for (int i = 0; i < howManyPlayers; i++) { var gameBoardAfterTurn = PlayersMove(roundNumber, playingPlayersId[i], manageScrableDb, playersHands, gameBoard, roundNumber, bag, listOfUniqueTiles, i, gameId); //IDpriskyrimas PrintBoard(gameBoardAfterTurn, listOfUniqueTiles); } roundNumber += 1; } int maxPoints = playersHands.Max(x => x.Points); var winner = playersHands.FirstOrDefault(y => y.Points == maxPoints).PlayerID; Console.WriteLine($"The winner of this game is {players.FirstOrDefault(p => p.PlayerId == winner)}"); manageScrableDb.InsertGame(players, DateTime.Now); }
public ScrabbleGame(IManageScrabbleDb manageScrabbleDb) { _manageScrabbleDb = manageScrabbleDb; }
public List <BoardCell> PlayersMove(int roundNUmber, int playerId, IManageScrabbleDb manageScrableDb, List <PlayerHand> playersHands, List <BoardCell> gameBoard, int roundNumber, List <Tile> bag, List <Tile> listOfUniqueTiles, int playerNumber, int gameId) { var playerName = manageScrableDb.GetAllPlayers().FirstOrDefault(p => p.PlayerId == playerId).PlayerName; var playerHand = GetPlayerHand(playerId, playersHands); Console.WriteLine($"Round nr.{roundNUmber}, player's {playerName} turn and letters on hand: {String.Join(' ',LettersFromHand(playerHand))}"); string wordToPlay = null; int horizontalPosition = 0; int verticalPosition = 0; char direction = ' '; var points = playersHands.FirstOrDefault(p => p.PlayerID == playerId).Points; int turnPoints = 0; bool correctPositioning = false; bool possibleToCreate = false; List <Tile> playedTiles = new List <Tile>(); while (correctPositioning is false || possibleToCreate is false) { Console.WriteLine("Enter the word to play"); wordToPlay = Console.ReadLine().ToUpper(); if (wordToPlay.Length == 0) { skipedMoves.Add(1); return(gameBoard); } else { skipedMoves.Clear(); } Console.WriteLine("Enter Horizontal position"); int.TryParse(Console.ReadLine(), out horizontalPosition); Console.WriteLine("Enter Vertical position"); int.TryParse(Console.ReadLine(), out verticalPosition); direction = Direction(); var chArrayOfWordToPlay = wordToPlay.ToArray(); correctPositioning = CheckTheWord(chArrayOfWordToPlay, horizontalPosition, verticalPosition, direction, gameBoard); //checking if it's possible to corectly place the word on board playedTiles = CheckIsItPossibleToCreateTheWord(wordToPlay.ToArray(), horizontalPosition, verticalPosition, direction, gameBoard, playerHand, roundNumber, listOfUniqueTiles); // cheking if there was any and returning it if the word was crossing other words on board if (playedTiles.Count > 0 || (roundNumber == 1 && playerNumber == 0)) //if it's first word on board it's not need to cross other word { possibleToCreate = true; } else { possibleToCreate = false; } } turnPoints = PointsOfPlacedWord(playedTiles, gameBoard, listOfUniqueTiles, wordToPlay.ToArray(), horizontalPosition, verticalPosition, direction); //points after turn Console.WriteLine($"Player {playerName} received {turnPoints} points"); points += turnPoints; Console.WriteLine($"Player {playerName} has total {points} points"); playersHands.FirstOrDefault(p => p.PlayerID == playerId).Points = points; Console.WriteLine(); manageScrableDb.InsertStatistic(playerId, gameId, playerHand, wordToPlay, horizontalPosition, verticalPosition, direction); //inserting statistic to DB var newPlayerHand = AddToHand(bag, playedTiles, playerHand); foreach (var player in playersHands) { if (player.PlayerID == playerId) { player.Hand = newPlayerHand; } } return(PlaceTheWordOnBoard(wordToPlay.ToArray(), horizontalPosition, verticalPosition, direction, gameBoard)); }