Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dfs new #2

Open
wants to merge 3 commits into
base: dls
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion in/M1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ L
U
R
U
R
R
6 changes: 5 additions & 1 deletion include/threes_AI.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

/* dfs on board for move path */
std::vector<Node> dfs(Board &);
int greedy_search(Board &, int, int);
Direction greedy_search2(Board, int);
Direction greedy_search(Board, int);
// int greedy_search(Board &, int, int);
int a_star(Board, std::vector<std::string> &, int, int *);
int i_aStar(Board &, std::vector<std::string> &);

#endif
33 changes: 29 additions & 4 deletions include/threes_Mechanics.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,48 @@ class comparator {
/** Board data structure **/
typedef std::vector< std::vector<int> > Board;

typedef std::priority_queue<std::pair<int, Direction>, std::vector<std::pair<int, Direction> >, comparator> PQ;

typedef std::priority_queue<std::pair<int, Direction>, std::vector<std::pair<int, Direction> >, comparator> PQ;
/** Node for DFS tree **/
struct Node {
PQ poss_moves;
Node *parent;
Node *parent = nullptr;
// the direction moved to get from parent-->node
Direction moveMade;
Board b;
int depth;
int f;
int g;
int h;
int score;
// unique identifier for comparing nodes
int id;
std::string str;
bool isRoot;
};

class nodeComparator {
bool reverse;
public:
nodeComparator(const bool & revparam=false) {
reverse=revparam;
}

bool operator() (const Node &n1, const Node &n2) {
if (reverse) return n1.f < n2.f;
return n2.f < n1.f;
}
};

typedef std::priority_queue<Node, std::vector<Node>, nodeComparator> NodeQ;

/**
* data structure to represent shift.
*/
struct Shift {
int id; // describes the row/col that was shifted (id in [0-3])
Direction m; // determines if id is row or col (D/U: col, L/R: row)
// string representation of shift - allows lexicographic sorting of shifts
// representation of shift row/col - allows lexicographic sorting of shifts
std::vector<int> string_vec;

// constructor
Expand Down Expand Up @@ -117,7 +142,7 @@ int tileMove(int *, int *);
*/
void setShiftString(Board &, Shift &);

/* Get legal moves on board given input tile */
/* Get legal moves on board given tileNum to refer to tile in sequence */
std::vector<Direction> getPossibleMoves(const Board &, int);
PQ getPossibleMovesSorted(const Board &, int);

Expand Down
Binary file modified threes
Binary file not shown.
15 changes: 14 additions & 1 deletion threes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ int iterateMoves(Board &board,
switch (playType) {
case MAN: { // user defined input determines moves
std::cin >> move;
if (move == "Q") return -1;
while (move_parse.count(move) == 0) {
std::cout << "Move \"" << move
<< "\" invalid, please enter from {U, L, D, R}:\n";
std::cin >> move;
if (move == "Q") return -1;
if (std::cin.eof()) {
std::cout << "Read EOF for stdin.. Ending game.\n";
return -1;
Expand All @@ -57,6 +59,10 @@ int iterateMoves(Board &board,
m = poss_moves[rand_move];
break;
}
case AI:{
m = greedy_search2(board,tile_num);
break;
}
default: {
printf("Invalid playType used. Please use those defined in threes.h\n");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -137,6 +143,7 @@ int main(int argc, char *argv[]) {
std::srand(std::time(NULL));
int endGame = -1;


switch (playType) { // play the game how user wants to
case (MAN):
endGame = iterateMoves(board, move_sequence, MAN);
Expand All @@ -147,7 +154,13 @@ int main(int argc, char *argv[]) {
case (AI):
// not implemented yet, call to AI algorithm goes here,
// endGame = AI(); or something
return 1;
// endGame = dfs(board, move_sequence, 4);
// int maxDepth;
// endGame = a_star(board, move_sequence, &maxDepth);
// endGame = i_aStar(board, move_sequence);
endGame = iterateMoves(board, move_sequence, AI);
break;
// return 1;
default:
return help();
}
Expand Down
Loading