public Cords(Cords newCords) { x = newCords.x; y = newCords.y; score = newCords.score; }
void GiveValues(Cords pos, bool isGoal) { int count; if (isGoal) { grid[playerPos.x, playerPos.y].visited = true; count = 7; } else { count = -15; } int x = pos.x; int y = pos.y; bool isFinished = false; Stack <Cords> move, temp; move = new Stack <Cords>(); temp = new Stack <Cords>(); grid[x, y].value = count; grid[x, y].visited = true; move.Push(new Cords(x, y)); while (!isFinished) { if (isGoal) { count--; } else { count++; } while (!(move.Count == 0)) { x = move.Peek().x; y = move.Peek().y; if (x != 0 && CheckSpot(x - 1, y)) //check left { grid[x - 1, y].visited = true; grid[x - 1, y].value = count; temp.Push(new Cords(x - 1, y)); } if (x != 6 && CheckSpot(x + 1, y)) //check right { grid[x + 1, y].visited = true; grid[x + 1, y].value = count; temp.Push(new Cords(x + 1, y)); } if (y != 6 && CheckSpot(x, y + 1)) //check up { grid[x, y + 1].visited = true; grid[x, y + 1].value = count; temp.Push(new Cords(x, y + 1)); } if (y != 0 && CheckSpot(x, y - 1)) //check down { grid[x, y - 1].visited = true; grid[x, y - 1].value = count; temp.Push(new Cords(x, y - 1)); } move.Pop(); } if (temp.Count == 0 && move.Count == 0) //if no blocks were found { isFinished = true; } while (!(temp.Count == 0)) { move.Push(temp.Peek()); temp.Pop(); } temp.Clear(); //ensure temp is empty } }
/* FindBestPath * Searches for the best path for AI's chasing state * * Takes a goal point (gp) and paths to that * * Returns the list that is the best path */ List <Cords> FindBestPath(Cords gp) { CleanGrid(); //clean the grid before we use it int count = 0; if (aiPath.Count == 0) { aiPath.Add(new Cords(aiPos.x, aiPos.y, 0)); } int x = aiPath[0].x; //assume aiPath[0] will always contain the current position of the AI int y = aiPath[0].y; bool isFinished = false; Stack <Cords> check, move, temp; check = new Stack <Cords>(); move = new Stack <Cords>(); temp = new Stack <Cords>(); Cords StartPoint = aiPath[0]; StartPoint.score = 0; List <Cords> fullPath = new List <Cords>(); List <Cords> bestPath = new List <Cords>(); //the list to be returned grid[x, y].visited = true; move.Push(new Cords(x, y, count)); fullPath.Add(new Cords(x, y, count)); while (!isFinished) { count++; while (!(move.Count == 0)) { x = move.Peek().x; y = move.Peek().y; if (x != 0 && CheckSpot(x - 1, y)) //check left { temp.Push(new Cords(x - 1, y, count)); check.Push(new Cords(x - 1, y, count)); fullPath.Add(new Cords(x - 1, y, count)); } if (x != 6 && CheckSpot(x + 1, y)) //check right { temp.Push(new Cords(x + 1, y, count)); check.Push(new Cords(x + 1, y, count)); fullPath.Add(new Cords(x + 1, y, count)); } if (y != 6 && CheckSpot(x, y + 1)) //check up { temp.Push(new Cords(x, y + 1, count)); check.Push(new Cords(x, y + 1, count)); fullPath.Add(new Cords(x, y + 1, count)); } if (y != 0 && CheckSpot(x, y - 1)) //check down { temp.Push(new Cords(x, y - 1, count)); check.Push(new Cords(x, y - 1, count)); fullPath.Add(new Cords(x, y - 1, count)); } move.Pop(); } while (!(check.Count == 0)) { if (check.Peek().x == gp.x && check.Peek().y == gp.y) { isFinished = true; gp.score = count; } check.Pop(); } while (!(temp.Count == 0)) { move.Push(temp.Peek()); temp.Pop(); } temp.Clear(); } //backwards trace to the AI to get a path List <Cords> newFullPath = new List <Cords>(); //new list to ensure we don't search through old values Cords comp; //comparison value to be returned int index = gp.score - 1; //set our search index to the next one down from the end point bestPath.Add(gp); //put goal point into comp = new Cords(gp); //comparison value isFinished = false; //reuse isFinished while (!isFinished) { foreach (Cords tile in fullPath) { if (tile.score == index) { if (isAdjacent(comp, tile)) { comp = tile; //make the new value the next comparion value bestPath.Add(tile); break; } } else //strip away the higher level indexes { newFullPath.Add(tile); } } //clean up and prepare fullPath for the next iteration fullPath.Clear(); foreach (Cords tile in newFullPath) { fullPath.Add(tile); } newFullPath.Clear(); index--; //search for the next lowest scores if (index < 0) //once we've gone below zero we've reached the goal point { isFinished = true; } } /* * The following lines switch the order of the list (using a stack) to make the path in the correct order * Before, the path is gp -> intermediate pos(int) 3 -> int 2 -> int 1 -> AI position * Afterwards, the path is AI position -> int 1 -> int 2 -> int 3 -> gp */ Stack <Cords> s = new Stack <Cords>(); //create a stack to reverse our path foreach (Cords tile in bestPath) { s.Push(tile); } bestPath.Clear(); //clean out the best path before we dump the correct order into it while (!(s.Count == 0)) { bestPath.Add(s.Peek()); s.Pop(); } return(bestPath); }
Cords AIAvoidMove() { CleanGridValues(); GiveValues(goalPos, true); GiveValues(playerPos, false); int x = aiPos.x; int y = aiPos.y; Cords move = new Cords(0, 0, -100); //make the score rediculously low so it'll always find something better List <Cords> aiOptions = new List <Cords>(); if (x != 0 && grid[x - 1, y].getOpen()) //check left { aiOptions.Add(new Cords(x - 1, y, grid[x - 1, y].value)); } if (x != 6 && grid[x + 1, y].getOpen()) //check right { aiOptions.Add(new Cords(x + 1, y, grid[x + 1, y].value)); } if (y != 6 && grid[x, y + 1].getOpen()) //check up { aiOptions.Add(new Cords(x, y + 1, grid[x, y + 1].value)); } if (y != 0 && grid[x, y - 1].getOpen()) //check down { aiOptions.Add(new Cords(x, y - 1, grid[x, y - 1].value)); } List <Cords> moveChoices = new List <Cords>(); foreach (Cords tile in aiOptions) { if (move.score < tile.score) { move = new Cords(tile); } } moveChoices.Add(move); foreach (Cords tile in aiOptions) { if (move.score == tile.score) { moveChoices.Add(tile); } } int rand; if (moveChoices.Count > 1) { rand = UnityEngine.Random.Range(0, moveChoices.Count); move = moveChoices[rand]; } else { move = moveChoices[0]; } return(move); }
//placing a ship square in the received Vector 2 if ship does not exist in that location yet. public bool Set_Loc(Vector2 vc) { Debug.Log("in set loc"); bool s = true; bool res_val = false; if ((cur_loc != ship_size) && (!structure_complete)) { for (int i = 0; i < cur_loc; i++) { if ((ship_conf[i].GetCordLoc().x == vc.x) && (ship_conf[i].GetCordLoc().y == vc.y)) { s = false; Debug.Log("ship in loc already exists"); res_val = false; } } if (s == true) { ship_conf[cur_loc] = new Cords(vc); cur_loc++; if (cur_loc == ship_size) { structure_complete = true; } //ship complete Debug.Log("new loc added to ship"); res_val = true; } } else { Debug.Log("ship already complete"); res_val = false; } return res_val; }
public bool Equals(Cords f, Cords s) { return(f.x == s.x && f.y == s.y); }
//placing a ship square in the received Vector 2 if ship does not exist in that location yet. public bool Set_Loc(Vector2 vc) { // Debug.Log("in set loc"); bool s = true; bool res_val = false; bool check_res = false; if ((cur_loc != ship_size) && (!structure_complete)) { for (int i = 0; i < cur_loc; i++) { if ((ship_conf[i].GetCordLoc().x == vc.x) && (ship_conf[i].GetCordLoc().y == vc.y)) { s = false; // Debug.Log("ship in loc already exists"); res_val = false; } } if (s == true) { if (input_validator.Get_used() == false) { input_validator.InitFirstVctr(vc); ship_conf[cur_loc] = new Cords(vc); cur_loc++; if (cur_loc == ship_size) { structure_complete = true; } //ship complete // Debug.Log("new loc added to ship"); res_val = true; } else { check_res = input_validator.CheclVectorValues(vc); if (check_res == true) { ship_conf[cur_loc] = new Cords(vc); cur_loc++; if (cur_loc == ship_size) { structure_complete = true; } //ship complete // Debug.Log("new loc added to ship"); res_val = true; } else { // Debug.Log("loc out of bounds"); res_val = false; } } } } else { // Debug.Log("ship already complete"); res_val = false; } return res_val; }
public PointBlip(int objectSize, Cords cords) { Size = objectSize; Cords = cords; }