I am currently working on a project called Rectangle project in which I am supposed to do the following on Java:
Make the following methods:
setOrigin
area
move
Also make a method that determines if two rectangles intersect and returns a new intersection Rectangle. Test all your methods in the ObjectDemo program for the following rectangles:
A: Origin 0,0: width 10: height 20
B: Origin 5,5: width 15, height 15
C: Origin 20,12: width 10: height 20
What is the area of each? Test if each of them intersect with the other two and what is the intersection area. Move A by 5,5; B by -5,-5: and C by -20, 0. Now give the intersection area of each.
I need to finish this by Monday but I keep getting a ton of errors like unrecognized variables, etc., and I'm not sure how to fix them. Please let me know!
I have three files: Point, RectangleTest, and Rectangle.
Here are their codes:
Point code:
public class Point
{
//Class variables
private int xCoord; //Private (instead of Public) because we are going to use this class in the other file
//We don't want people changing the values unless we let them
private int yCoord; //Variables are not in a function so will maintain their value
//Constructor
Point()
{
xCoord = 0;
yCoord = 0;
}
//Constructor
Point(int startX, int startY)
{
xCoord = startX;
yCoord = startY;
}
public int getX()
{
return xCoord;
}
public int getY()
{
return yCoord;
}
public void setX(int newX)
{
xCoord = newX;
}
public void setY(int newY)
{
yCoord = newY;
}
public void move(int moveX, int moveY)
{
xCoord+=moveX;
yCoord+=moveY;
}
Point(Point p)
{
xCoord = p.getX();
yCoord = p.getY();
}
}
RectangleTest Code:
public class RectangleTest
{
public static void main(String [] args)
{
Rectangle A = new Rectangle(0,0,10,20);
Rectangle B = new Rectangle(5,5,15,15);
Rectangle C = new Rectangle(20,12,10,20);
//Move rectangles
A.moveby(5,10);
B.moveby(-5,-5);
C.moveby(-20,0);
int areaA = A.getarea;
System.out.println("The area of rectangle A is " +areaA);
int areaB = B.getarea;
System.out.println("The area of rectangle B is " +areaB);
int areaC = C.getarea;
System.out.println("The area of rectangle C is " +areaC);
Rectanlge iAB = A.intersect(B);
Rectangle iAC = A.intersect(C);
Rectangle iBC = B.intersect(C);
if(iab != null)
{
System.out.println("The area of intersection rectangle iab = " +iAB.area());
}
if(iac != null)
{
System.out.println("The area of intersection rectangle iac = " +iAC.area());
}
if (ibc != null)
{
System.out.println("The area of intersection area ibc = " +iBC.area());
}
}
}
Rectangle Code:
public class Rectangle
{
Point origin;
int height;
int width;
//Constructor for rectangle object
Public Rectangle(int startX, int startY, int startW, int startH)
{
origin = new Point (startX, startY);
width = startW;
height = startH;
}
//Set origin point for NEW rectangle origins
//FIX
public void setOrigin(int newX, int newY)
{
origin.setX(newX);
origin.setY(newY);
}
public int moveBy(int moveX, int moveY)
{
origin.move(moveX, moveY);
}
public int getArea()
{
int recArea = height*width;
return recArea;
}
public Rectangle intersect(Rectangle testR)
{
int meTRX = origin.getX() + width;
int meTRY = origin.getY() + height;
int testTRX = testR.origin.getX() + width;
int testTRY = testR.origin.getY() + height;
//Boolean to get iTRX
if(meTRX>testTRX)
{
int iTRX = testTRX;
}
else
{
int iTRX = meTRX;
}
//Boolean to get iTRY
if(meTRY>testTRY)
{
int iTRY = testTRY;
}
else
{
int iTRY = meTRY;
}
//Boolean to get iBLX
if(testBLX>meBLX)
{
int iBLX = testBLX;
}
else
{
int iBLX = meBLX;
}
//Boolean to get iBLY
if(testBLY>meBLY)
{
int iBLY = testBLY;
}
else
{
int iBLY = meBLY;
}
//Testing for whether or not there is an intersection rectangle
if(iTRX-iBLX<0 || iTRY-iBLY<0)
{
return null;
}
int iH = iTRY - iBLY;
int iW = iTRX - iBLX;
int intersectArea = iH * iW;
}
}
Please point out any problems! I'm rather new to programming, so I usually make a lot of simple mistakes. Also, I would appreciate if there are no newly introduced commands or anything because my teacher is pretty strict about doing it this way.
Thanks!
P.S. I would appreciate any extra knowledge or info on code improvement (just in general). Thanks!
Couple of Issues:
Java is case sensitive so Public is not same as public in your rectangle class.
When your method doesnt return anything you should use void as return type. So in your method public int moveBy(int moveX, int moveY), you should change it to public void moveBy(int moveX, int moveY)
You need to define variables before using them. So variables like testBLX, meBLX, testBLY, meBLY, iTRX, iTRY, iBLX, iBLY are undefined. I am not sure from where the values will get populated. But you could avoid the compilation error by defining them as int testBLX = 0; and similarly the others.
In your Rectangle class:
In the constructor your wrote Public Rectangle(int startX, int startY, int startW, int startH), but you actually want public Rectangle(int startX, int startY, int startW, int startH). In Java keywords start always with a lower case.
Your method for changing the origin of a rectangle public int moveBy(int moveX, int moveY) has int as a return type, so the compiler wants you to return an integer value. I suppose you did not want to return anything at all so you can change the return type to void.
In your intersect method public Rectangle intersect(Rectangle testR) you declare your variables (iTRX, iTRY, iBLX, iBLY) such as int iTRX = testTRX; only in the scope of your if/else statements which means that after every if/else statement these variables are not available anymore. To learn more about the different scopes of variables: Variable scopes
In your RectangleTest class:
You forgot a part of your task: What is the area of each? Test if each of them intersect with the other two and what is the intersection area.
Some general leads:
The use of more descriptive variable names improves the readability. For example the variable name meTRX does not have any meaning for me as person who did not work on your code or maybe for you if you review your code two months later.
Before you start coding, you could check if Java has built-in classes which you can use. In your case Java provides a Point class in the package java.awt.Point. You do not have to reinvent the wheel.
I would also recommend to read the Java Code Conventions Code Conventions which can bring you and others who read your code on a common denominator in the future.
Related
I am writing a platform game in Java in which there are various game objects such as platforms, the player, and (eventually) enemies. I describe the position of these game objects with rectangles: x position, y position, width, and height. However, I would also like to add in other variables to describe the position: left, top, right, and bottom. For the last two I know I will need to change them whenever x, y, width, or height gets modified, but as left and top are the same as x and y, I was wondering how I could get them to point to the same value as x and y. I think this can be done in C with the #define function, but sadly this is Java, not C.
How can I get two different variable names to point to the same value in Java such that if one changes, the other changes as well?
Edit: Here is the basics of my GameObject class:
public abstract class GameObject {
protected float x;
protected float y;
protected float right;
protected float bottom;
protected float width;
protected float height;
public float getX() {
return x;
}
public float getY() {
return y;
}
public void setX(float x) {
this.x = x;
right=this.x+width;
}
public void setY(float y) {
this.y = y;
bottom=this.y+height;
}
//A bunch of other getters/setters
//Subclasses must have render (for displaying graphics) and tick (mainly for updating position) functions.
public abstract void render(Graphics g);
public abstract void tick();
}
What I would like to do is add in two more variables:
protected float left = x;
protected float top = y;
and have these refer to the same primitive as x and y (not copy the primitives). This, apparently, does not seem possible. Thanks for your answers!
This can't be done in Java in the sense that you mean it. You can have two object references that point to the same object, but if you have a normal primitive variable, those are always distinct and don't refer to each other. You can make an object to hold a primitive, have two different references to that same object, and a change to the contents of that object through one reference will be reflected in the other, but you cannot have an int x and and int y where writing x = 10 will make y == 10.
If you really need something behaving like a reference to a primitive, you need to create an object holding that primitive value and then share the "holder" object.
However, I don't think this is the best way to tackle your problem.
As I understand your game objects, they can be entirely described with x, y, height and width variable. left, top, right and bottom on the other hand, are values that can derived from the four basic ones.
In fact, it would be possible to have a game object containing only x, y, height and width and to compute the other four values outside of the object whenever they are needed. Such an object could look like this :
public class GameObject {
private int x, y, height, width;
public int getX() { return x; }
public int getY() { return y; }
public int getHeight() { return height; }
public int getWidth() { return width; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public void setHeight(int height) { this.height = height; }
public void setwidth(int width) { this.width = width; }
}
Notice that the actual variables are private and only readable through getters. This helps to avoid the use of a variable reference later on.
Now, to have an easy and unique way to access to left, top, right and bottom, while you could add four variables and keep them in sync with the four other variable already present, I think it is not the best way to do it.
I suggest that you just compute them on the fly inside slightly smarter-than-average getters. It would look like that :
public class GameObject {
// getters and setters omitted for brevity
private int x, y, height, width;
public int getLeft() {
return x;
}
public int getRight() {
// assuming x increase toward the right
return x + width;
}
public int getTop() {
return y
}
public int getBottom() {
// assuming y increase toward the bottom
return y + height;
}
}
This will also help if you need to create some special update methods, like moveRight(int delta), scale(double factor) since you will only need to apply changes to the basic variables and not the values that are derived from them.
int/integer is passed by value, not by reference in java.
You can define a new class, For example,
public class Position {
int x;
int y;
}
Let two variables point to the same instance of Position.
In java there are two types of variables.
1.Primitive types
2.Reference types
You need to use reference type variable if you want to refer the same object through different variables.
In your case for gaming application those parameters might change frequently.
So if all the time values, left and top are same as x and y, you can use use one variable to represent the both parameters which saves memory.
e.g : for left and x use one variable.
Or else using java OOP concepts,
you can make those x, y, left, top parameters private and assign getters and setters for those.
public int setX(int x){
this.x =x;
}
public setLeft(int left){
this.left = left;
}
public int setY(int y){
this.y =y;
}
public setTop(int top){
this.top= top;
}
public setXnLeft(int xnleft){
setX(xnleft);
setleft(xnleft);
}
public setYnTop(int yntop){
setY(yntop);
setTop(yntop);
}
This can easily be done by creating a new Class. Java is object-oriented, so, basically, everything is about classes and it's instances. Take a look at an example below:
class GameObject
{
//Instance variables
//(all objects of this class will have their own)
int x;
int y;
int width;
int height;
//Constructor
//(you call a constructor when creating a new object)
GameObject(int x, int y, int width, int height)
{
//Here you are assigning the values received to the instance variables (marked with "this", that represents the current object)
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
//This method will return the ordinate ("y") of the object's top position (assuming the "y axis" points down)
int getTop() {
return (y - height/2);
}
}
After creating the class GameObject, you can create instances (objects) of it. Look below:
GameObject player = new GameObject(0, 0, 50, 50); //creates a new object with coordinates (0,0), width = 50 and height = 50
GameObject enemy1 = new GameObject(30, 50, 100, 100);
//Getting the "x" position of the player and the monster
System.out.println("Player X = " + player.x);
System.out.println("Enemy1 X = " + enemy1.x);
//Get the top position "y"
System.out.println("Player top = " + player.getTop());
//Change player's "y"
player.y = 10;
//Get player's top position again
System.out.println("Player top = " + player.getTop()); //it will have changed, since "y" was changed.
Oracle has a really good lesson about Object-Oriented Programming, you should check it out here. The C language is structured, while Java is Object-Oriented, these are two different paradigms. I hope this was of help. Cheers!
I have a Main class of game named BrickBreaker
public class BrickBreaker extends Activity {
// there is lot of other code but i am only pointing to the issue
class BreakoutView extends SurfaceView implements Runnable {
// The size of the screen in pixels
int screenX;
int screenY;
// Get a Display object to access screen details
Display display = getWindowManager().getDefaultDisplay();
// Load the resolution into a Point object
Point size = new Point();
display.getSize(size);
screenX = size.x;
screenY = size.y;
}}
And from another class (below) I want to access screenX from the Main class:
public class Paddle {
// This the the constructor method
// When we create an object from this class we will pass
// in the screen width and height
public Paddle(int screenX, int screenY){
// 130 pixels wide and 20 pixels high
length = 130;
height = 20;
// Start paddle in roughly the sceen centre
x = screenX / 2;
y = screenY - 20;
rect = new RectF(x, y, x + length, y + height);
// How fast is the paddle in pixels per second
paddleSpeed = 550;
}
public void update(long fps){
rect.left = x;
rect.right = x + length;
if (x<0){
x=0;
}
else if (x+length > screenX){
x = screenX-length;
}
}
}
How can I access screenX from the Paddle class?
In Paddle class modify field screenX
Java static variable
If you declare any variable as static, it is known static variable.
The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
The static variable gets memory only once in class area at the time of class loading.
Advantage of static variable
Please check this : STATIC
It makes your program memory efficient (i.e it saves memory).
public class Paddle {
public static int screenX;
// This the the constructor method
// When we create an object from this class we will pass
// in the screen width and height
public Paddle(int screenX, int screenY){
this.screenX = screenX;
// 130 pixels wide and 20 pixels high
length = 130;
height = 20;
// Start paddle in roughly the sceen centre
x = screenX / 2;
y = screenY - 20;
rect = new RectF(x, y, x + length, y + height);
// How fast is the paddle in pixels per second
paddleSpeed = 550;
}
public void update(long fps){
rect.left = x;
rect.right = x + length;
if (x<0){
x=0;
}
else if (x+length > screenX){
x = screenX-length;
}
}
}
And to acces in BrickBeaker you need to:
Paddle.screenX
Enjoy
You need to get access to BreakoutView's member screenX from your Paddle class.
For this, your Paddle class first needs to get access to the BreakoutView instance.
You could do this by passing it for example in the constructor:
public class Paddle {
BreakoutView view;
public Paddle (BreakoutView view) {
this.view = view;
}
public void update(long fps){
...
}
}
And where you create it:
BreakoutView view = new BreakoutView(.....);
Paddle paddle = new Paddle(view);
....
Next, you need to get access to the screenX member. There are two options:
First: make it public:
// The size of the screen in pixels
public int screenX;
public int screenY;
With this solution, you could access it in your Paddle class with this.view.screenX.
But this would allow other code, for example from inside your Paddle class, to modify these values, which is often not desired. Thus, the safer way is:
Second: Provide a getter.
In BreakoutView, add the following method:
public int getScreenX() {
return this.screenX;
}
and then, in your Paddle class, you could access it like this.view.getScreenX() instead of just screenX.
So, as the title reads I am trying to add offsets to my java game. I was given a tip by a friend that I need to minus the offset from where I render the tiles onto my screen.
So I created a random world generator and did the offset thing, but I ran into a problem.
My Code:
public void generateMap(Graphics g) {
block = seed.nextInt(2);
//Render Dirt
if(block == 0) {
g.drawImage(Assets.dirt, x - GameState.xOffset, y - GameState.yOffset, null);
x += 32;
}
//Render Grass
if(block == 1) {
g.drawImage(Assets.grass, x - GameState.xOffset, y - GameState.yOffset, null);
x += 32;
}
//Check Where the X is
if(x > xFinish) {
if(y < yFinish) {
x = xStart;
y += 32;
}
}
}
looks simple enough right? after I do that I create code to add one to the offset every time I loop around:
public void tick() {
xOffset += 1;
}
So after that is done I run it but it does this:
is there any simple way I can fix this so that it appears that the screen "scrolls" to the left?
Is there any simple way I can fix this...
Probably not. Games are complicated. Don't let that dissuade you.
You are generating your game world and drawing in the same methods - you don't want to do this. Separation of responsibility is very important - you don't want a whole bunch of code in one spot doing the same thing. In this case, the functionality to generate the world and the drawing code need to be split.
For the world generation, generate the game world once, and persist it to storage using whatever format you like. Keep this away from the drawing code - it has no place there.
For representing blocks in your world, consider something like this:
class Block {
public BlockType getType() {
return type;
}
public int getxPosition() {
return xPosition;
}
public int getyPosition() {
return yPosition;
}
// hashCode(), equals(), etc omitted, they should be implemented
public static enum BlockType {
Dirt(Assets.dirt),
Grass(Assets.grass);
private final BufferedImage image;
BlockType(BufferedImage image) {
this.image = image;
}
public BufferedImage getImage() {
return image;
}
}
private final BlockType type;
private final int xPosition;
private final int yPosition;
private Block(BlockType type, int xPosition, int yPosition) {
this.type = type;
this.xPosition = xPosition;
this.yPosition = yPosition;
}
public static Block getInstance(BlockType type, int xPosition, int yPosition) {
return new Block(type, xPosition, yPosition);
}
}
You can then use Block.getInstance() to generate a map once, like this:
class GameState {
private final int WORLD_SIZE = 1024;
private Block[][] _world = new Block[WORLD_SIZE][WORLD_SIZE];
private static Random seed = new Random();
public void generateMap() {
int blockTypeLength = Block.BlockType.values().length;
for (int x = 0; x < WORLD_SIZE; x++) {
for (int y = 0; y < WORLD_SIZE; y++) {
int blockType = seed.nextInt(blockTypeLength);
_world[x][y] = Block.getInstance(Block.BlockType.values()[blockType], x, y);
}
}
}
public Block[][] getMap() {
return _world; // not thread safe, shares internal state, all the usual warnings
}
This obviously isn't the only way to generate a world - you would probably generate a world and save, then load from disk in later games (unless it was a short lived game - I don't know, that's your call).
Once you've got the world sorted out, you'd move on to a different module that would handle drawing. Assume GameState has two fields playerX and playerY that represent the player's coordinates in the game world (note: direct fields like this are bad practice, but used to simplify this example):
public void paintComponent(Graphics g) {
super.paintComponent(g);
Block[][] screen = new Block[16][16]; // declare a screen buffer to draw
// Assumes player is in the center of the screen
int screenRadiusX = GameFrame.Assets.SCREENBOUNDS_X / 2 / blockSize;
int screenRadiusY = GameFrame.Assets.SCREENBOUNDS_Y / 2 / blockSize;
for (int x = state.playerX - 8, xS = 0; x < state.playerX + 8; x++, xS++) {
for (int y = state.playerY - 8, yS = 0; y < state.playerY + 8; y++, yS++) {
screen[xS][yS] = world[x][y];
}
}
for (int x = 0; x < screen.length; x++) {
for (int y = 0; y < screen.length; y++) {
Block current = screen[x][y];
g.drawImage(current.getType().getImage(),
x * blockSize, // blockSize is the pixel dimension of
y * blockSize,
null
);
}
}
}
If this helps, then great! I'm glad I was able to help. If not, or if some ideas are still unclear, then I would consider perhaps running through a tutorial or book that walks you through making a game. Don't forget to learn the platform you're coding on during such a process.
I looked up how to draw a star in Java, and I found the following code:
public void paint(Graphics g) {
drawStar(g,Color.BLACK,5,300,300,100,1…
drawStar(g,Color.RED,6,100,100,20,20);
drawStar(g,Color.BLUE,9,200,400,40,40)…
drawStar(g,Color.YELLOW,27,400,200,10,…
drawStar(g,Color.GREEN,400,300,300,250…
}
public double circleX(int sides, int angle) {
double coeff = (double)angle/(double)sides;
return Math.cos(2*coeff*Math.PI-halfPI);
}
public double circleY(int sides, int angle) {
double coeff = (double)angle/(double)sides;
return Math.sin(2*coeff*Math.PI-halfPI);
}
public void drawStar(Graphics g, Color c, int sides, int x, int y, int w, int h) {
Color colorSave = g.getColor();
g.setColor(c);
for(int i = 0; i < sides; i++) {
int x1 = (int)(circleX(sides,i) * (double)(w)) + x;
int y1 = (int)(circleY(sides,i) * (double)(h)) + y;
int x2 = (int)(circleX(sides,(i+2)%sides) * (double)(w)) + x;
int y2 = (int)(circleY(sides,(i+2)%sides) * (double)(h)) + y;
g.drawLine(x1,y1,x2,y2);
}
}
}
halfPI is defined as a private static variable outside the body
I don't quite get the logic behind these methods. Could someone offer an explanation?
You can follow the graphics object carefully line by line and see what happens to it. It looks like the writer's algorithm uses sine and cosine the evenly split the circle at the same sized angles depending on the number of sides. Then for each side, it draws the line. It is a good beginner program to test and make it work and don't worry if you can't make the basic math work, those are just rather easy trigonometric expressions depending on the arguments that are passed to the drawing method and the helper methods.
I want to make a 10x10 grid and put the robot in position (10,1) (bottom left). I want this robot to be able to move forward, turn left/right and to pick up/put objects in a grid. When put in any position, there should be number in a grid which shows how many objects is put in this position, just like this:
..........
...1......
..2.......
....3.....
..........
..........
......9...
.....4....
.........1
..........
We will not see the robot in a grid. I have two classes. Class Robot:
public class Robot {
private Area area;
private Robot rob;
public Robot(Area area){
this.area = area;
rob = new Robot(area);
}
public void Right(){
}
public void Left(){
}
public void Forward(){
}
public void Put(){
}
public void PickUp(){
}
public (?) getPosition(){ // should return robot's position
}
}
Class Area:
private int numberOfObjects;
private Robot robot;
private static final int X = 10;
private static final int Y = 10;
private Object [][] area; // grid
public Area(){ // defines a grid and robot
area = new Area[X][Y];
for(int a=0;a<X;a++){
for(int b=0;b<Y;b++)
area[a][b]=".";
}
numberOfObjects = 0; // grid is initially empty
Area ar = new Area();
robot = new Robot(ar);
}
public void Put(int x,int y){ // put the object to position (x,y)
area[x][y]=numberOfObjects++;
}
public void PickUp(int x,int y){ // pick up the object in position (x,y)
if(area[x][y]!=null){
area[x][y]=numberOfObjects--;
}
}
public void PrintAGrid(){
for(int r=0;r<X;r++){
for(int c=0;c<Y;c++)
System.out.print(area[r][c]+" ");
System.out.println();
}
System.out.println();
}
}
How can I put the robot in position (10,1)? How can I declare and set its orientation (i.e. on the right)? I guess it will be easy to write other methods, so I do not focus on it.
There are several issues with your code.
Why do you have an instance of Robot inside the class Robot? You have not used that instance at all!
private Object [][] area; should be int[][] area. You always save int in this, right?
If I understand your requirements correctly, Your implementation of pick and put is not correct.
Here is a help how you can solve the problems. I had to think several times if Robot should be in Grid or it should be the other way. I ended up with Grid in Robot.
May be Grid could be a singleton.
Here is our Grid
public class Grid {
private int[][] numberOfObjects = new int[10][10];
public void put(int x, int y) {
numberOfObjects[y][x]++;
}
public void pick(int x, int y) {
numberOfObjects[y][x]--;
}
}
You can replace parameters int x, int y with a Point.
And here is the robot
public class Robot {
private static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3;
private int direction;
private int x, y;
private Grid grid;
public Robot(Grid grid) {
this.x = 0;
this.y = 0;
this.grid = grid;
direction = NORTH;
}
public void right() {
direction++;
if (direction == 4) {
direction = 0;
}
}
public void left() {
direction--;
if (direction == -1) {
direction = 3;
}
}
public void forward() {
if (direction == NORTH) {
y--;
} else if (direction == SOUTH) {
y++;
} else if (direction == EAST) {
x++;
} else if (direction == WEST) {
x--;
}
}
public void put() {
grid.put(x, y);
}
public void pick() {
grid.pick(x, y);
}
}
You need to represent the curent location with a variable and initialize it to the 10 1 postion, though your array goes 0-9 and 0-9 so this may be 9,0. to store this position maybe try a Point object that contains a Point x,y.
If someone is interested in a JavaScript version, you can have a look at this repo right here. In general:
The robot must have a facing direction (left, up, right, down).
The are three possible commands: left, right, move.
With that being said, the algorithm is quite straightforward:
totalScore = 0
Foreach i in input
computeCurrentDirection()
if input != MOVE: continue
totalScore += i
return totalScore
There are several sweet-tricks that someone might do to optimize the functions. Take a look at switchDirection.
const directionArray = [Directions.RIGHT, Directions.DOWN, Directions.LEFT, Directions.UP];
const switchDirection = (currDirection, command) => {
if (command === Commands.MOVE) {
return currDirection
}
const currDirectionIndex = directionArray.indexOf(currDirection);
if (command === Commands.RIGHT) {
return directionArray[(currDirectionIndex + 1) % 4];
}
return directionArray[((currDirectionIndex - 1) + 4) % 4];
}
Instead of an exhaustive approach, someone might use an array to help compute the upcoming direction of the robot. This significantly reduces the amount of needed code.
Note this implementation can be easily expanded to accommodate any new requirements needed for project expansion. When faced with such questions, try to architect your codebase in a testable and expandable way, because it's usually the case where reviewers are interested in your coding organizational skills, rather than whether you are able to solve the problem or not.