Banker Algorithm Snake

Overview

This project aims to combine the banker algorithm with the snake game to achieve a comprehensive application through Java coding. The Banker algorithm will be used to simulate the rational allocation of resources to enhance the challenging and educational nature of the snake game.

The main goal of the project is to help students gain a deep understanding of how the banker algorithm works through practical coding challenges and apply it to snake games, thereby improving their computer science and programming skills.

Banker Algorithm implementation: We will learn and implement the Banker algorithm to ensure that the system can properly allocate resources to multiple processes.

Instruction

  • “W,A,S,D” to Move Snake #1
  • “Up, Down, Left, Right” to Move Snake #2
  • “1,2,3,4,5” to select snakes
  • “Space” to restart

Developer

ROLENAME
Main CodingReed Zhu
PrototypingLin Xiang
PaperYuge Xu
PresentationChen Li

Download

Developed by JAVA

* Use Command Line to run .jar


Design Progress

Introduction

The primary purpose of this game is to help players learn the concept of the Banker’s Algorithm in operating systems through an engaging and interactive experience. While studying this topic, I found it to be an incredibly fascinating concept. So, as a game designer, I decided to make my final project for this class a game based on the Banker’s Algorithm.

The Banker’s Algorithm is a resource allocation algorithm used to avoid deadlock in operating systems, proposed by Edsger W. Dijkstra in 1965. It is mainly used in multi-tasking operating systems to ensure that the system does not enter a deadlock state when allocating resources. The core idea of the Banker’s Algorithm is that the system, when allocating resources, will check if the resulting state is “safe.” If a resource allocation leads to an “unsafe state,” the allocation is not made. A state is considered safe if the system can schedule resources in such a way that all processes can complete successfully.

Many of the concepts in the Banker’s Algorithm can be naturally abstracted into game mechanics. For instance, resource requests can be adapted into a resource management or collection mechanism, while checking for deadlocks can be used as a victory or failure condition. With these ideas in mind, I started designing the gameplay for this project.

Method

My personal design approach leans towards drawing inspiration from classic designs rather than creating an entirely original framework from scratch. This method allows me to develop a fully functional prototype more quickly and avoid falling into a cycle of self-validation or self-doubt. Additionally, basing the design on classic games helps ensure that players are more likely to understand the game mechanics without confusion.

I ultimately decided to use the classic “Snake” game as the core of my game. The original Snake game already involves elements of resource management: the longer the snake gets, the more likely it is to collide with itself and fail, while the player’s score is tied to the amount of food consumed. As a variation, I reimagined the “snake” as the concept of a process or task in an operating system. Players take on the role of the operating system, controlling a process represented by the snake, and complete tasks managed by the Banker’s Algorithm.

Let me briefly explain a simple example of the Banker’s Algorithm. Suppose there are three processes and three resource types (A, B, C). The initial system state is:

Available resources: A=3, B=3, C=2

Maximum demand of each process:

Allocated resources:

If Process 2 now requests 1 unit of A, 0 units of B, and 2 units of C, the system will use the Banker’s Algorithm to determine whether granting this request would lead to an unsafe state.

In the game, the processes are represented by snakes, and the resource allocation process becomes the snake’s gameplay of collecting food. However, if there is only one snake, it would be difficult to illustrate the primary goal of the Banker’s Algorithm, which is managing multiple threads. This led me to the idea of having the player control two snakes simultaneously. This makes the gameplay more challenging and aligns more closely with the logic of the Banker’s Algorithm.

At this point, I had a prototype where the player controls two snakes, but it still felt somewhat disconnected from the Banker’s Algorithm. After a few iterations, I realized I had overlooked the aspect of resource management in my initial design. I changed the gameplay of the Snake game: instead of making the snake longer by consuming food, the player fills gaps in the snake’s body by eating food. In the Banker’s Algorithm, a process requires various resources, which must be allocated by the system. In the game, this allocation is handled by the player. This design makes players focus on filling the gaps in the snake’s body rather than simply making it longer. By completing the snake, the player essentially mimics the resource allocation process in an operating system. I also used Aseprite to design different colored food items and snake bodies.

Now, I had a prototype where two snakes with gaps collected food to fill their bodies. However, this felt too simple, and having only two snakes to represent processes might confuse players. Therefore, I decided to make the player complete multiple snakes. While the player controls two snakes at a time, the goal is to complete all the snakes in the game. I added a basic UI with a selection system where players choose which two snakes to control, using number keys, and then collect food accordingly.

With this, the basic game design was complete. I added some UI elements, such as displaying the remaining amount of each type of food, so players could make strategic decisions about which snake to complete. I also added a mechanic where the game ends if the snakes collide with each other—while not directly related to the Banker’s Algorithm, I felt it added a layer of challenge and fun.

Implementation

Finally, I implemented the game using Java. The gameplay loop is as follows: the player chooses two snakes out of five, collects food to fill in the gaps for those snakes, and once one snake is complete, they return to the selection screen to pick the next pair. This process continues until the player completes all five snakes. While the current version still has some bugs, combining an algorithm with a game was an exciting and novel challenge for me.