# Monkey And Banana AI Game

## Explain How Game Play :

• the monkey find four paths to banana .
• he go in every path and check is the banana there in the end or what
• if it there he win otherwise he go in the next path …..
• the goal or the location of banana is random .

## Implementation The Project (Some Important Code ) :-

### We Work With Flutter .

• At First : We use graph to design the Ui and we consider the game as group of nodes and edges then by using BreathFirst Algorithm We Search for The Random Goal (Banana) using Iterator in Dart .

• _BreadthFirstTree creates the BreadthFirstIterator. Also, both of these collections stores the Graph object to save the tree data structure itself.

ITreeIterator defines a common interface for all specific iterators of the tree collection:

• hasNext() – returns true if the iterator did not reach the end of the collection yet, otherwise false;
• getNext() – returns the next value of the collection;
• reset() – resets the iterator and sets the current position of it to the beginning.

### Graph Code In Dart

A class which stores the adjacency list of the graph. It is stored as a map data structure where the key represents the node’s (vertix) id and the value is a list of vertices (ids of other nodes) adjacent to the vertex of that id (key). Also, this class defines the addEdge() method to add an edge to the adjacency list.

``````class Graph {
final Map<int, Set<int>> adjacencyList = Map<int, Set<int>>();

void addEdge(int source, int target) {
} else {
}
}
}
``````

### Tree collections

• BreadthFirstTreeCollection – a tree collection class which stores the graph object and implements the createIterator() method to create an iterator which uses the breadth-first algorithm to traverse the graph.

``````class BreadthFirstTreeCollection implements ITreeCollection {
final Graph graph;

@override
ITreeIterator createIterator() {
}

@override
String getTitle() {
}
}
``````

• BreadthFirstIterator – a specific implementation of the tree iterator which traverses the tree collection by using the breadth-first algorithm. This algorithm uses the queue data structure to store vertices (nodes) which should be visited next using the getNext() method.

``````class BreadthFirstIterator implements ITreeIterator {
final Set<int> visitedNodes = <int>{};
final ListQueue<int> nodeQueue = ListQueue<int>();

final int _initialNode = 1;
int _currentNode;

_currentNode = _initialNode;
}

@override
bool hasNext() {
return nodeQueue.isNotEmpty;
}

@override
int getNext() {
if (!hasNext()) {
return null;
}

_currentNode = nodeQueue.removeFirst();

.where((n) => !visitedNodes.contains(n))) {
}
}

return _currentNode;
}

@override
void reset() {
_currentNode = _initialNode;
visitedNodes.clear();
nodeQueue.clear();
}
}
``````

### Grraph (Ndes & Edges) defenition :

``````  Graph _buildGraph() {
var graph = Graph();

....
....
....
....
return graph;
}

``````

View Github