private bool checkTieWithOtherPlayer(Position posicionActual, Player player) { if (posicionActual.row == player.Trayectoria.LastOrDefault().row && posicionActual.column == player.Trayectoria.LastOrDefault().column) { return true; } return false; }
private bool checkCollisionWithOtherPlayerTrayectory(Position posicionActual, Player player) { foreach (Position posicionAnterior in player.Trayectoria) { if (posicionAnterior.row == posicionActual.row && posicionAnterior.column == posicionActual.column) { return true; } } return false; }
private bool checkRepeatedPositionInPlayer(Position posicionActual, Player player) { for (int i = 0; i < player.Trayectoria.Count - 1; i++) { if (player.Trayectoria[i].row == posicionActual.row && player.Trayectoria[i].column == posicionActual.column) { return true; } } return false; }
public GameLogic(List<Command> Commands, string playerTag1, string playerTag2, int rows, int columns) { Player1 = new Player { Tag = playerTag1 }; Player2 = new Player { Tag = playerTag2 }; Table = new int[rows, columns]; this.Commands = Commands; Player1.Trayectoria.Add(new Position {row = 0, column = 0 }); Player2.Trayectoria.Add(new Position {row = Table.GetLength(0)-1, column = Table.GetLength(1)-1 }); }
//-1 El juego continua //0 Colision //1 Uno de los players perdio private int checkPlayerAction(Player activePlayer, Player restingPlayer) { Position posicionActual = activePlayer.Trayectoria.LastOrDefault(); if (checkTieWithOtherPlayer(posicionActual, restingPlayer)) { return 0; } if (checkRepeatedPositionInPlayer(posicionActual, activePlayer)) { return 1; } if (checkCollisionWithOtherPlayerTrayectory(posicionActual, restingPlayer)) { return 1; } return -1; }
private string compileResult(Player activePlayer, Player restingPlayer, Command val, bool p1Active) { setDirection(val, activePlayer); if (validateTriggers(p1Active) == "Empate") { Console.WriteLine("Ha sido un empate en la " + activePlayer.Trayectoria.LastOrDefault().ToString()); return "END"; } else if (validateTriggers(p1Active) == String.Format("{0} perdio", activePlayer.Tag)) { Console.WriteLine("El jugador " + restingPlayer.Tag + " ha ganado!"); Console.WriteLine("El jugador " + activePlayer.Tag + " perdio en la " + activePlayer.Trayectoria.LastOrDefault().ToString()); return "END"; } return ""; }
public void setDirection(Command command, Player player) { Position lastvalue = player.Trayectoria.Last(); Position coordenada = new Position { row = lastvalue.row, column = lastvalue.column }; switch (command.Direction) { case "arriba": coordenada.row = coordenada.row != 0 ? coordenada.row - 1 : (Table.GetLength(0)-1); break; case "abajo": coordenada.row = coordenada.row != (Table.GetLength(0)-1) ? coordenada.row + 1 : 0; break; case "derecha": coordenada.column = coordenada.column != (Table.GetLength(1)-1) ? coordenada.column + 1 : 0; break; case "izquierda": coordenada.column = coordenada.column != 0 ? coordenada.column - 1 : (Table.GetLength(1) - 1); break; } player.Trayectoria.Add(coordenada); }
private string analyzeGameAction(Player loser, bool p1Active) { switch (checkTrayectory(Player2, Player1, p1Active)) { case -1: return "Continue"; case 0: return "Empate"; case 1: return String.Format("{0} perdio", loser.Tag); default: return ""; } }
private int checkTrayectory(Player player2, Player player1, bool p1Active) { if (p1Active) { return checkPlayerAction(Player1, Player2); } else { return checkPlayerAction(Player2, Player1); } }