//get the coin pile with the least cost path public void GetBestCoin(Square[] sqrs, Coin[] coins, Square current, int direction, out List<StarNode> bestP, out Coin targetCoin) { AStar ast = new AStar(sqrs, mapSize); List<StarNode> bestPath = null; Coin target = null; int minCost = int.MaxValue; foreach(Coin coin in coins) { int index = (int)(coin.position.X+(coin.position.Y*mapSize)); if (sqrs[index].getSType() == 3) { continue; } List<StarNode> path = ast.calculatePath(current, sqrs[index], direction); int cost = path[path.Count - 1].cost; if ((cost < minCost) && ((cost * Util.Constants.Timeout) < coin.remainTime)) { bestPath = path; minCost = cost; target = coin; } } bestP = bestPath; targetCoin = target; //return this.generateCommandList(bestPath, direction); }
private void Evade() { bool hor = false; bool ver = false; int vDir = 1; int hDir = 1; bool success = true; Square nextSquare = null; foreach (Player player in players) { if (player.playerNumber == myNumber) continue; if (player.position.X == players[myNumber].position.X) { if ((player.position.Y < players[myNumber].position.Y) && (player.direction == 2)) { ver = true; vDir = 1; } else if ((player.position.Y > players[myNumber].position.Y) && (player.direction == 0)) { ver = true; vDir = -1; } } if (player.position.Y == players[myNumber].position.Y) { if ((player.position.X < players[myNumber].position.X) && (player.direction == 1)) { hor = true; hDir = 1; } else if ((player.position.X > players[myNumber].position.X) && (player.direction == 3)) { hor = true; hDir = -1; } } } //how to evade if (ver) { int x = (int)players[myNumber].position.X; int y = (int)players[myNumber].position.Y; if(x == 0){ while ((squares[GetIndex(x+1, y)].getSType() != 0) || (squares[GetIndex(x+1, y)].hasPlayer())) { if ((y == 0) || (y == mapSize - 1)) { success = false; break; } y = y + vDir; } if (success) { nextSquare = squares[GetIndex(x + 1, y)]; } } else if (x == mapSize - 1) { while ((squares[GetIndex(x-1, y)].getSType() != 0) || (squares[GetIndex(x-1, y)].hasPlayer())) { if ((y == 0) || (y == mapSize - 1)) { success = false; break; } y = y + vDir; } if (success) { nextSquare = squares[GetIndex(x - 1, y)]; } } else { while (((squares[GetIndex(x - 1, y)].getSType() != 0) || (squares[GetIndex(x - 1, y)].hasPlayer())) && ((squares[GetIndex(x + 1, y)].getSType() != 0) || (squares[GetIndex(x + 1, y)].hasPlayer()))) { if ((y == 0) || (y == mapSize - 1)) { success = false; break; } y = y + vDir; } if (success) { if ((squares[GetIndex(x - 1, y)].getSType() == 0) && (!squares[GetIndex(x - 1, y)].hasPlayer())) { nextSquare = squares[GetIndex(x - 1, y)]; } else if ((squares[GetIndex(x + 1, y)].getSType() == 0) && (!squares[GetIndex(x + 1, y)].hasPlayer())) { nextSquare = squares[GetIndex(x + 1, y)]; } } } } ////////////////// success = true; if (hor) { int x = (int)players[myNumber].position.X; int y = (int)players[myNumber].position.Y; if (y == 0) { while ((squares[GetIndex(x, y+1)].getSType() != 0) || (squares[GetIndex(x, y+1)].hasPlayer())) { if ((x == 0) || (x == mapSize - 1)) { success = false; break; } x = x + hDir; } if (success) { nextSquare = squares[GetIndex(x, y+1)]; } } else if (y == mapSize - 1) { while ((squares[GetIndex(x, y-1)].getSType() != 0) || (squares[GetIndex(x, y-1)].hasPlayer())) { if ((x == 0) || (x == mapSize - 1)) { success = false; break; } x = x + hDir; } if (success) { nextSquare = squares[GetIndex(x, y-1)]; } } else { while (((squares[GetIndex(x , y-1)].getSType() != 0) || (squares[GetIndex(x, y-1)].hasPlayer())) && ((squares[GetIndex(x, y+1)].getSType() != 0) || (squares[GetIndex(x, y+1)].hasPlayer()))) { if ((x == 0) || (x == mapSize - 1)) { success = false; break; } x = x + hDir; } if (success) { if ((squares[GetIndex(x, y - 1)].getSType() == 0) && (!squares[GetIndex(x, y - 1)].hasPlayer())) { nextSquare = squares[GetIndex(x, y - 1)]; } else if ((squares[GetIndex(x, y + 1)].getSType() == 0) && (!squares[GetIndex(x, y + 1)].hasPlayer())) { nextSquare = squares[GetIndex(x, y + 1)]; } } } } if (nextSquare != null) { Navigator navg = new Navigator(mapSize); AStar ast = new AStar(squares, mapSize); List<StarNode> path = ast.calculatePath(players[myNumber].currentSquare, nextSquare, players[myNumber].direction); foreach (StarNode node in path) { sqrPath.Add(node.mySqr); } commands = navg.generateCommandList(path, players[myNumber].direction); opMode = "evade"; } else { //call shoot method } }
//get the lifepack with the least cost path public void GetBestHealth(Square[] sqrs, LifePack[] packs, Square current, int direction, out List<StarNode> bestP, out LifePack targetPack) { AStar ast = new AStar(sqrs, mapSize); List<StarNode> bestPath = null; LifePack target = null; int minCost = int.MaxValue; foreach (LifePack pack in packs) { int index = (int)(pack.position.X + (pack.position.Y * mapSize)); List<StarNode> path = ast.calculatePath(current, sqrs[index], direction); int cost = path[path.Count - 1].cost; if ((cost < minCost) && ((cost * Util.Constants.Timeout) < pack.remainTime)) { bestPath = path; minCost = cost; target = pack; } } bestP = bestPath; targetPack = target; }
//move to the centermost cell private void MoveToCenter() { int x = mapSize/2; int y = mapSize/2; int index = x + ( y * mapSize ); Square center = squares[index]; int add = 1; bool succ = true; while (center.getSType() != 0) { x = x + add; add = -(add+1); if((x<0)||(x>mapSize-1)){ succ = false; break; } index = x + (y * mapSize); center = squares[index]; } if (!succ) { succ = true; while (center.getSType() != 0) { y = y + add; add = -(add + 1); if ((y < 0) || (y > mapSize - 1)) { succ = false; break; } index = x + (y * mapSize); center = squares[index]; } } if (succ) { AStar ast = new AStar(squares, mapSize); List<StarNode> path = ast.calculatePath(players[myNumber].currentSquare, center, players[myNumber].direction); foreach (StarNode node in path) { sqrPath.Add(node.mySqr); } Navigator navg = new Navigator(mapSize); commands = navg.generateCommandList(path, players[myNumber].direction); opMode = "center"; } }