public Grid Play(Grid currentState, Move move) { //0-Game already finished, no more move if (currentState.CurrentMessage.Id == (int)ClassicMessage.GameFinished) { return currentState; } //1-Not Your turn if (move.PlayerNumber != currentState.NextPlayerToPlay) { currentState.CurrentMessage = Message.GetMessage(ClassicMessage.NotYourTurn); return currentState; } var choosenX = move.Positions[0].Coord.x; var indice = maxPawnInColumns - currentState.PawnLocations.Where(o => o.Coord.x == choosenX).Count(); //2-Column full if (indice == 0) { currentState.CurrentMessage = Message.GetMessage(ConnectFourMessage.ColumnFull); return currentState; } //3-Case Ok, var PawnToAdd = new Pawn(move.PlayerNumber == 1 ? "R" : "Y", choosenX, indice); currentState.PawnLocations.Add(PawnToAdd); currentState.LastMove = move; if (IsFinished(currentState, PawnToAdd)) { //3-1 Case Ok + finished currentState.NextPlayerToPlay = 0; currentState.CurrentMessage = Message.GetMessage(ClassicMessage.GameFinished); } else { //3-1 Case Ok + Not finished currentState.NextPlayerToPlay = (currentState.NextPlayerToPlay == 1) ? 2 : 1; currentState.CurrentMessage = Message.GetMessage((currentState.NextPlayerToPlay == 2) ? ConnectFourMessage.J2 : ConnectFourMessage.J1); } return currentState; }
public Grid Play(Grid currentState, Move move) { //0-Game already finished, no more move if (currentState.CurrentMessage.Id == (int)ClassicMessage.GameFinished) { return currentState; } //1-Not Your turn if (move.PlayerNumber != currentState.NextPlayerToPlay) { currentState.CurrentMessage = Message.GetMessage(ClassicMessage.NotYourTurn); return currentState; } var choosenX = move.Positions[0].Coord.x; var choosenY = move.Positions[0].Coord.y; //2-Case already taken if (currentState.PawnLocations.Where(o => o.Coord.x == choosenX && o.Coord.y == choosenY).Any()) { currentState.CurrentMessage = Message.GetMessage(ClassicMessage.NotEmptyPosition); return currentState; } //3-Case Ok, var PawnToAdd = new Pawn(move.PlayerNumber == 1 ? "B" : "W", choosenX, choosenY); currentState.PawnLocations.Add(PawnToAdd); currentState.LastMove = move; if (IsFinished(currentState, PawnToAdd)) { //3-1 Case Ok + finished currentState.NextPlayerToPlay = 0; currentState.CurrentMessage = Message.GetMessage(ClassicMessage.GameFinished); } else { //3-1 Case Ok + Not finished currentState.NextPlayerToPlay = (currentState.NextPlayerToPlay == 1) ? 2 : 1; currentState.CurrentMessage = Message.GetMessage((currentState.NextPlayerToPlay == 2) ? MorpionMessage.J2 : MorpionMessage.J1); } return currentState; }
public Grid Play(PlayerInBattle playerInBattle, Move move) { // 1- We play the move var currentBattle = playerInBattle.Battle; var game = typeMapping.GetGame(currentBattle.GameType); var oldState = currentBattle.CurrentState.NextPlayerToPlay; var newState = game.Play(currentBattle.CurrentState, move); // 2- If the grid has change, we persit the new state currentBattle.CurrentState = newState; if (newState.DeservePersistence(oldState)) { //See what we want to do.... repoPlayerInBattle.Persist(playerInBattle); } // 3- We return the new state to the client return newState; }
private GridExtented Play(string sessionId, List<Tuple<int, int>> allPositions) { HackJsonp(); try { // 1-Check valid session var session = sessionManager.GetSession(sessionId, true); var positions = new List<Pawn>(); foreach (var item in allPositions) { positions.Add(new Pawn() { Coord = new Coord() { x = item.Item1, y = item.Item2 }, Name = "?", Player = session.PlayerInBattle.Player }); } Move move = new Move() { PlayerNumber = session.PlayerInBattle.PlayerNumber, Positions = positions }; var newState = gameManager.Play(session.PlayerInBattle, move); // 2-Return result return new GridExtented() { SessionId = session.Id, State = newState }; } catch (Exception) { throw; } }