Skip to content

A game involving a mouse and several kitties who're in a maze. You might have guessed what the objectives of each are.

Notifications You must be signed in to change notification settings

KanwarGill/MouseAndKittiesMazeGame

Repository files navigation

Cats and Mice!

Cats and Mice and Game A.I.s

May the best graph-based algorithm win!

The kitties are on the loose. What is a poor, hard-working mouse to do?

In this project, we take the role of both the mouse trying to find some lunch and get away before becoming lunch, and the kitties who are always hungry and looking for unsuspecting customers. We will be using a variety of graph based algorithms,

and be responsible for planning the movement of each agent in the maze.

Learning Goals for this project:

After completing this project, you should be able to:

*Explain the structure of heaps and how to use them to implement priority queues

*Store and manipulate graphs using adjacency matrices

*Implement Prim's MST and Dijkstra's shortest path algorithms

*Use graph and graph algorithms to implement a simple game of cat(s) and mouse

The above goals directly support your learning of material presented in the final weeks of the course. Additionally, this project is an opportunity to learn about:

*Path finding and path planning, fundamental topics in A.I.

*Interacting with the O/S from your C program

*Creating random mazes using trees

*Representing and manipulating the state of different agents in the game, state-based planning.

Be sure to review the material on graphs in the online notes.

Have FUN!

Basic setup:

The action takes place within a maze. The maze consists of a square grid with Gsize positions along both the x and y directions. Each location in the maze is represented as a node in a graph G(V,E), and the edges between neighboring nodes represent the connectivity of the maze. Note that because the graph represents a 2D maze, each node can be connected to at most 4 other nodes: its neighbours in the up, down, left, and right directions. Each grid location is given an index starting at zero for the top-left corner and going up to (Gsize*Gsize)-1 for the bottom-right corner

as shown below:

 

0 1 2 ...

Gsize-1

 

(2*Gsize)-1

Gsize

 

 

 

 

 

(Gsize*Gsize)-1

0

1

2

3

20

21

22

23

40

41

42

43

The enlarged area shows how grid positions map to a corresponding node in the graph (I'm assuming here that Gsize=20). Connections between nodes encode maze connectivity. In

particular, absence of an edge between neighbouring nodes indicates the presence of a wall in the maze.

The weights of all edges in the maze graph are equal to 1.0

First Task:

Build the maze! The first thing we need to do is to set up the maze in which the agents will interact. Maze creation is an interesting topic by itself, and you may want to check the wiki page on it before proceeding.

The function initGraph() in the starter code sets up a graph structure for you. The initial graph is connected, and every node has a link to each of its neighbours (note that nodes along the border of the grid have fewer than 4 neighbours).

Initially, the weight of each edge is random. Based on the setup described in the previous page, the initial graph corresponds to a maze with no walls. Compile and run the starter code and verify that you indeed obtain an empty maze, note the program takes one command line parameter: the initial random seed. Your task is to select a set of edges to remove from this graph thereby introducing walls. However, doing this randomly will not result in a nice maze, so we have to do something smart.

Note that we can think of maze generation as the problem of selecting which subset of the edges in the initial graph to keep, and which to discard. We will impose the following conditions on the maze:

-It should be connected: There should be at least one path between any pair of grid locations.

-It should have at least a few loops (think about why that may be a good thing).

There are many ways to build a maze from a graph. Here, we will use Prim's algorithm to generate an MST, since the initial weights between nodes are random, this is equivalent to creating a new, connected graph with random paths between edge pairs.

Your task will be to implement Prim's algorithm (see graph_algorithms.c). Once you have completed Prim's algorithm, test to make sure you obtain a maze, and that the resulting maze is fully connected (if there are isolated grid locations or disconnected regions in the maze, you have a bug in Prim's method). The starter code will generate an image called 'maze.ppm' or 'maze.jpg' (more on that in a sec.) to ease your testing.

The starter code already has a function to introduce loops after running Prim's method, so you don't need to worry about that. You can select how many loops to introduce

by changing the appropriate value in GameAI.h.

For efficiency, you should use heaps and priority queues

Notes on Prim's algorithm

The MST function will replace the original graph (the one with the random edge weights) with the resulting MST. Note that the weight of all edges in the MST must be 1.0. We will not keep the random edge weights since they would be meaningless in terms of computing the length of the path between two maze locations.

Second Task:

Implement Dijkstra's shortest path algorithm. Your implementation should be able to compute the shortest path between any pair of nodes in the graph. The algorithm is short and straightforward, but you must know what you're doing on the graph.

Test your shortest path algorithm thoroughly since it will be used extensively. Once you have completed the shortest path function, the starter code will generate an image called 'path_0-0.ppm' (or 'path_0-0.jpg'), showing the shortest path from the top-left corner of the grid to the bottom-right corner. Make sure the path is correct, and test with different pairs of grid points until you're sure it works always.

There is a good reason why I can update my projected path at every turn. See the crunchy stuff...

Task 3: Game A.I.

Now that you have all the components of your graph working, it's time to implement the algorithms that will update the positions of the mouse and kitties in the maze. This is where you get to get to do path planning, one common task in Artificial Intelligence.

Mouse A.I. – Simple and fast

In a typical game, the mouse would be controlled by a user. Here, we will update the mouse position automatically. However, since the mouse represents the player, we will assume the mouse can 'see' the entire maze, including the positions of the cheese and gate as well as the kitties.

The strategy for the mouse updates is simple:

*The mouse will use the shortest path algorithm to get to wherever it's going.

*The mouse must eat the cheese, and then escape before being eaten.

Simple mouse A.I.

Implement the function mouseWalk(). This function computes and returns the shortest path between the mouse and its target (the cheese if it has not been eaten, or the gate if the mouse had his lunch).

If you completed this successfully, you should see the mouse moving toward the target in successive frames output by the program.

That's it for the mouse, however, see the mouseWalk() function for a crunchy problem that you may want to solve to give the mouse a better chance of not running directly Into a cat!

About Shortest Path and A.I.

The shortest path algorithm seems like is a reasonable choice for updating the

mouse's behaviour... or is it? One problem with our mouse update strategy is that

it does not consider at all the position and behaviour of the kitties. What would you do to incorporate information about the cats and their behaviour

into the mouse update function?

If you want to think about it, look at the crunchy bits in the starter code.

Kitty AI – Cats are smart, and they own the house...

Kitty AI is more interesting, the kitties are the agents that would be controlled by

the program in a typical game. The goal of the kitties is, of course, to eat the mouse. You will explore different ways to control cat behaviour. At the beginning of the program the user can select which mode of cat behaviour will be used.

The modes of cat behaviour you have to implement are:

Visual cats: They wander randomly through the maze, however, if they can 'see' the mouse, they will give chase. A kitty can see a mouse if both are aligned horizontally or vertically and there are no walls in between.

Scent tracking cats: They do not know exactly where the mouse is, but can tell whether a grid location next them is closer or farther from the mouse and move in the direction that puts them closer to the Mouse. Unfortunately, they tend to get stuck...

Smart scent tracking kitties: They use smell as described above, but if they get stuck due to a wall blocking their path, they can use the shortest path method to get to the other side of the wall. Once on the other side, they continue using smell.

Sound chasing kitties: These kitties use their ears. If the mouse steps within a specified distance from the cat, they will hear it and use the shortest path algorithm to get to where the mouse is making noise.

Evil Cats of Doom: These exist to show you what happens when the game designer allows the AI to gain knowledge it shouldn't have. In this case, you will provide the cats with the actual location of the mouse, and the kitties will use the shortest path method to get there.

Read the corresponding function header in the starter code for a description of the behaviour of each function.

Your task is to implement all of the

cat behaviours described above. You will need to enforce graph connectivity (even these kitties can not walk through walls), and you will explore how the choice of algorithm for cat behaviour influences the mouse's chance of success.

Test kitty A.I. thoroughly, and remember there are harder and easier mazes!

Building a Super Cat **CRUNCHY!**

The kitty update functions you have to implement offer a good glimpse into strategies you could use to find and chase a game character. However, you may be inclined to investigate how 'smart' you can make the kitties without actually cheating (i.e. without providing them with the location of the mouse).

One thing you can try, is to arm each kitty with all possible modes of mouse tracking: vision, smell, and sound. You would have to deal with the precedence of each sense in the overall cat behaviour, and determine how they interact to bring the kitties toward the mouse and hopefully get them some lunch.

If you're curious to try, the function superKitty() in the starter code is the place for you. see how smart you can make the cats and feel free to add your own ideas. Remember That you can not provide the kitties with any knowledge of the mouse, cheese location, or gate location that they can not acquire directly by exploring the maze.

Building a smarter Mouse **CRUNCHY!**

This is the most interesting (and most fun) part of this project. Here you can try more advanced A.I. techniques to try and make your mouse impossible to catch. The only constraints for you are these:

*The mouse can not escape until it has eaten the cheese

*The mouse can not teleport or move through walls

*The mouse can not see into the future (i.e. it should not see where the cats will move to in the next round)

You are free to use information about current cat location and even their mode of operation. You may want to look at search algorithms such as A* search, you could use the shortest path algorithm together with separately enforced constraints, you could use breadth first or depth first search, or you can even get ambitious and do a bit of simulation to try to predict where the cats will go.

See the function mouseEscape() and feel free to drop by my office if you want to discuss your strategy or simply talk a bit about A.I.

This should encourage you to think about what exactly makes an algorithm 'smart', and whether you can make your mouse smart enough to escape any cats!

I sense a roasted

This furball can't

mouse for dinner!

catch me!

 

About

A game involving a mouse and several kitties who're in a maze. You might have guessed what the objectives of each are.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published