#include <iostream>
#define MAX_CELL 64
#include <cmath>
#include <string>
using namespace std;
struct Cell {
int m_Row;
int m_Col;
};
int cMult;
int rMult;
float avgCost;
float CostSoFar = 0.0F;
class CellTracker
{
//Establish String to hold all coords, separated by |~|
std::string SeenCell = "|~|";
public:
bool seenCell(Cell tCell, bool storeCell)
{
std::string searchString(tCell.m_Row+","+tCell.m_Col);//store str as a string temporarily
if (SeenCell.find("|~|"+searchString+"|~|")!=std::string::npos)
//if the cell has been seen before, it will be stored, delimited by |~|
//this searches for the full cell with delimiters
//if it finds the cell, it returns true
{
return true;
}
else
{
//if the cell hasn't been seen before, it isn't stored,
// we need to store it then return false;
if(storeCell){SeenCell = SeenCell+searchString+"|~|";}//update string
return false;//return false
}
}
};
CellTracker visited;//stores visited cells
CellTracker badCells;//stores cells that I shouldn't visit again
// have a class object hold the following:
// *Cells to avoid
// *path taken
// *return if path crosses itself (if new point is already on path) and send it to cells to avoid
// *start cell
// *end cell
// *calculate average weight of cells
// *current weight
// *function to find most direct path, and add weights
class custPather
{
Cell start;
Cell end;
};
// cellCost represents a row-major 2d grid, each entry is cost of moving through that cell.
int bestPath(Cell dest[], int maxDestLength,
const Cell* start, const Cell* end,
float cellCost[MAX_CELL][MAX_CELL])
{
//float avgCost = avgWeight(cellCost);
}
float dPathCheck(float currWeight, Cell* currCell, Cell* tarCell)//check cost of new move before making
{
return 0;
}
bool moveVert(Cell currCell, Cell tarCell)
{
float cDiff = tarCell.m_Col-currCell.m_Col;
float rDiff = tarCell.m_Row-currCell.m_Row;
cMult = cDiff > 0? 1:-1;
rMult = rDiff > 0? 1:-1;
return((abs(cDiff)>abs(rDiff)));
//return true if there is more vertical distance
}
float returnCellWeight(Cell weightedCell, float cellCost[MAX_CELL][MAX_CELL])//TODO REMOVE CELL COST
{
return(cellCost[weightedCell.m_Row][weightedCell.m_Col]);
}
float shortestRoute(Cell currCell, Cell tarCell, float cellCost[MAX_CELL][MAX_CELL])//TODO REMOVE CELL
{
Cell testCell = {currCell.m_Row,currCell.m_Col};
float moves = 0;
while((testCell.m_Col != tarCell.m_Col)&&(testCell.m_Row != tarCell.m_Row))
{
if(moveVert(testCell, tarCell))
{
testCell.m_Col+= cMult;
}
else
{
testCell.m_Row+= rMult;
}
moves+=cellCost[testCell.m_Row][testCell.m_Col];
}
return moves;
}
Cell checkSurroundingCells(Cell curCell, Cell tarCell, float cellCost[MAX_CELL][MAX_CELL])
{
float smallestF;
float currentF;
Cell smallestC;
if((curCell.m_Col > 0)&&!(badCells.seenCell(Cell{curCell.m_Row,curCell.m_Col-1},false)))
{
currentF = smallestF = returnCellWeight(Cell{curCell.m_Row,curCell.m_Col-1}, cellCost)+CostSoFar+shortestRoute(curCell, tarCell, cellCost);
cout << smallestF << endl;
smallestC = {curCell.m_Row,curCell.m_Col-1};
}
if((curCell.m_Col < MAX_CELL)&&!(badCells.seenCell(Cell{curCell.m_Row,curCell.m_Col+1},false)))
{
currentF = returnCellWeight(Cell{curCell.m_Row,curCell.m_Col+1}, cellCost)+CostSoFar+shortestRoute(curCell, tarCell, cellCost);
cout << currentF << endl;
if(currentF<smallestF)
{
smallestF=currentF;smallestC={curCell.m_Row,curCell.m_Col+1};
}
}
if((curCell.m_Row > 0)&&!(badCells.seenCell(Cell{curCell.m_Row-1,curCell.m_Col},false)))
{
currentF = returnCellWeight(Cell{curCell.m_Row-1,curCell.m_Col}, cellCost)+CostSoFar+shortestRoute(curCell, tarCell, cellCost);
cout << currentF << endl;
if(currentF<smallestF)
{
smallestF=currentF;smallestC={curCell.m_Row-1,curCell.m_Col};
}
}
if((curCell.m_Row < MAX_CELL)&&!(badCells.seenCell(Cell{curCell.m_Row+1,curCell.m_Col}, false)))
{
currentF = returnCellWeight(Cell{curCell.m_Row+1,curCell.m_Col}, cellCost)+CostSoFar+shortestRoute(curCell, tarCell, cellCost);
cout << currentF << endl;
if(currentF<smallestF)
{
smallestF=currentF;smallestC={curCell.m_Row+1,curCell.m_Col};
}
}
if (visited.seenCell(smallestC,true)){badCells.seenCell(curCell,true);}
return smallestC;
}
float avgWeight(float cellCosts[MAX_CELL][MAX_CELL])//calculate average weight of cells
{
float totalCosts = 0.0F;
for(int i = 0; i < MAX_CELL; i++)// using MAX_CELL since it's already been established
{
for(int j = 0; j < MAX_CELL; j++)//for every cell in array, add weight
{
totalCosts += cellCosts[i][j];
}
}
return (totalCosts / (MAX_CELL*MAX_CELL));//using MAX_CELL since it's already been established
// we return the average weight of all cells;
}
int main()
{
//cout << "Hello World" << endl;
Cell sCell = {10,10};
Cell eCell = {32,10};
Cell nCell;
float L[MAX_CELL][MAX_CELL];
for(int i = 0; i < MAX_CELL; i++)
{
for(int j = 0; j < MAX_CELL; j++)
{
L[i][j]=(float)(rand()%100);
//cout << L[i][j] << endl;
}
}
avgCost = avgWeight(L);
cout << avgCost << endl;
//cout << moveVert(sCell,eCell) << endl;
//cout << rMult << endl;
//cout << cMult << endl;
//cout << shortestRoute(sCell,eCell) << endl;
nCell = checkSurroundingCells(sCell, eCell, L);
cout << "This move was to: ";
cout << nCell.m_Row << " " << nCell.m_Col << endl;
for (int i = 0; i < 5; i++)
{
nCell = checkSurroundingCells(nCell, eCell, L);
cout << "This move was to: ";
cout << nCell.m_Row << " " << nCell.m_Col << endl;
}
return 0;
}