//la fox per mangiare si puo muovere in tutte le 8 direzioni, a distanza 2 private List <Move> IACalculateFoxValidEatingMoves(int r, int c) { List <Move> foxValidEatingMoves = new List <Move>(); for (int roundR = r - 2; roundR <= r + 2; roundR += 2) { for (int roundC = c - 2; roundC <= c + 2; roundC += 2) { if ( //la casella di destinazione deve essere nella board !MyUtility.IsPositionOutOfCross(roundR, roundC) && //la casella interpolata deve essere nella board !MyUtility.IsPositionOutOfCross((roundR + r) / 2, (roundC + c) / 2) && //non ci si può muovere verso la stessa casella di partenza !(roundR == r && roundC == c) && //le mosse dispari saltano le destinazioni dispari cioe saltano la diagonale !(((r + c) % 2 != 0) && ((roundR != r) && (roundC != c))) && //la casella di destinazione deve essere vuota board[roundR, roundC] == PawnType.None && //la casella interpolata deve avere una goose sotto board[(roundR + r) / 2, (roundC + c) / 2] == PawnType.Goose) { foxValidEatingMoves.Add(new Move(PawnType.Fox, r, c, roundR, roundC)); } } } return(foxValidEatingMoves); }
public List <Move> GetPossibleMovesOtherPlayer() { List <Move> allPossibleMoves = new List <Move>(); if (turn == PawnType.Fox) { for (int r = 0; r < 7; r++) { for (int c = 0; c < 7; c++) { if (board[r, c] == PawnType.Goose && !MyUtility.IsPositionOutOfCross(r, c)) { allPossibleMoves.AddRange(IACalculateGooseValidMoves(r, c)); } } } } else if (turn == PawnType.Goose) { Vector2 foxCoordinates = FindFoxCoordinates(); // nel vector2 la x è la rz e la y è la xc int r = (int)foxCoordinates.x; int c = (int)foxCoordinates.y; if (!MyUtility.IsPositionOutOfCross(r, c)) { //Console.WriteLine("Fox posC=" + c + " posR=" + r + " simulation=" + isSimulation); allPossibleMoves.AddRange(IACalculateFoxValidMoves(r, c)); allPossibleMoves.AddRange(IACalculateFoxValidEatingMoves(r, c)); } } return(allPossibleMoves); }
//date le coordinate della fox torna quante oche ci sono a distanza euclidea >2 private double GetNumberOfFarGoose(int rFox, int cFox) { int count = 0; for (int currentR = 0; currentR < 7; currentR++) { for (int currentC = 0; currentC < 7; currentC++) { if ( !MyUtility.IsPositionOutOfCross(currentR, currentC) && currentR != rFox - 1 && currentC != cFox - 1 && currentR != rFox && currentC != cFox && currentR != rFox + 1 && currentC != cFox + 1 && board[currentR, currentC] == PawnType.Goose ) { count++; } } } return(count); }
// Sezione aggiunta per la IA================================================= //la goose non puo andare in diagonale //non puo retrocedere di riga (anzi, cioò per gli indici avanzare) //restano solo tre mosse: avanti, dx, sx private List <Move> IACalculateGooseValidMoves(int r, int c) { List <Move> gooseValidMoves = new List <Move>(); if (!MyUtility.IsPositionOutOfCross(r - 1, c) && board[r - 1, c] == PawnType.None) { gooseValidMoves.Add(new Move(PawnType.Goose, r, c, r - 1, c)); } if (!MyUtility.IsPositionOutOfCross(r, c - 1) && board[r, c - 1] == PawnType.None) { gooseValidMoves.Add(new Move(PawnType.Goose, r, c, r, c - 1)); } if (!MyUtility.IsPositionOutOfCross(r, c + 1) && board[r, c + 1] == PawnType.None) { gooseValidMoves.Add(new Move(PawnType.Goose, r, c, r, c + 1)); } return(gooseValidMoves); }
//la fox si puo muovere in tutte le 8 direzioni, a distanza 1 private List <Move> IACalculateFoxValidMoves(int r, int c) { List <Move> foxValidMoves = new List <Move>(); //setto l'intorno del centro dato for (int roundR = r - 1; roundR <= r + 1; roundR++) { for (int roundC = c - 1; roundC <= c + 1; roundC++) { if (!MyUtility.IsPositionOutOfCross(roundR, roundC) && !(roundR == r && roundC == c) && //le mosse dispari saltano le destinazioni dispari cioe saltano la diagonale !((r + c) % 2 != 0 && (roundR + roundC) % 2 != 0) && board[roundR, roundC] == PawnType.None) { foxValidMoves.Add(new Move(PawnType.Fox, r, c, roundR, roundC)); } } } return(foxValidMoves); }
//ritorna se esiste una eatingMove da un certo centro private bool ExistsEatingMove(int r, int c) { //le mosse pari hanno la diagonale if ((r + c) % 2 == 0) { int roundR2 = r - 2; while (roundR2 <= r + 2) { int roundC = c - 2; while (roundC <= c + 2) { if (!MyUtility.IsPositionOutOfCross(roundR2, roundC)) { Move hypoteticMove = new Move(PawnType.Fox, r, c, roundR2, roundC); Vector2 interpolPawn = hypoteticMove.InterpolateEatenPawn(); // se è una mossa interpolabile if (interpolPawn.x != -1 && interpolPawn.y != -1) { int x = (int)interpolPawn.x; int y = (int)interpolPawn.y; //se sotto la mossa che tento di fare c'è un'oca non posso sovrappormi if (board[hypoteticMove.finalX, hypoteticMove.finalZ] == PawnType.None) { //se sotto l'interpolazione sta un'oca if (board[x, y] == PawnType.Goose) { return(true); } } } } roundC = roundC + 2; } roundR2 = roundR2 + 2; } } else { // le mosse dispari non hanno la diagonale for (int roundR = r - 2; roundR <= r + 2; roundR++) { if (!MyUtility.IsPositionOutOfCross(roundR, c)) { Move hypoteticMove = new Move(PawnType.Fox, r, c, roundR, c); Vector2 interpolPawn = hypoteticMove.InterpolateEatenPawn(); // se è una mossa interpolabile if (interpolPawn.x != -1 && interpolPawn.y != -1) { int x = (int)interpolPawn.x; int y = (int)interpolPawn.y; //se sotto la mossa che tento di fare c'è un'oca non posso sovrappormi if (board[hypoteticMove.finalX, hypoteticMove.finalZ] == PawnType.None) { //se sotto l'interpolazione sta un'oca if (board[x, y] == PawnType.Goose) { return(true); } } } } } for (int roundC = c - 2; roundC <= c + 2; roundC++) { if (!MyUtility.IsPositionOutOfCross(r, roundC)) { Move hypoteticMove = new Move(PawnType.Fox, r, c, r, roundC); Vector2 interpolPawn = hypoteticMove.InterpolateEatenPawn(); // se è una mossa interpolabile if (interpolPawn.x != -1 && interpolPawn.y != -1) { int x = (int)interpolPawn.x; int y = (int)interpolPawn.y; //se sotto la mossa che tento di fare c'è un'oca non posso sovrappormi if (board[hypoteticMove.finalX, hypoteticMove.finalZ] == PawnType.None) { //se sotto l'interpolazione sta un'oca if (board[x, y] == PawnType.Goose) { return(true); } } } } } } // se tutte le interpolazioni falliscono non esiste una eatingMove return(false); }