interface-based programming c++ cpp file Problem Set 4: Interface-based Programm
ID: 3838857 • Letter: I
Question
interface-based programming c++
cpp file
Problem Set 4: Interface-based Programming In problem set 3, you developed a console-based version of the paper and pencil game Bulls and Cows. The solution consisted of one class BullsAndcows and a main function defining the game loop. The approach works nicely, but did not follow the Model-View-Controller paradigm The task for this problem set is to develop an interface-based solution for Bulls and Cows. In particular, you are asked to implement a console view for the application that satisfies the following specification pragma once includeExplanation / Answer
bundle cargame;
import java.awt.*; import java.awt.geom.*; import java.util.*;
open conceptual class Vehicle {/Member factors ensured Point2D.Float position = new Point2D.Float(); secured Point2D.Float introduction = new Point2D.Float(); ensured Point2D.Float side = new Point2D.Float(); secured Point2D.Float speed = new Point2D.Float(); secured Point2D.Float guiding = new Point2D.Float(); secured skim mass; ensured glide maxSpeed; ensured coast maxSteering;/List of practices ensured ArrayList practices = new ArrayList(10);/Getters and setters open Point2D.Float getPosition() { return position; } open void updatePosition(Point2D.Float p) { position.x = p.x; position.y = p.y; } open void updatePosition(float x, drift y) { position.x = x; position.y = y; } open Point2D.Float getOrientation() { return introduction; } open void updateOrientation(Point2D.Float o) { orientation.x = o.x; orientation.y = o.y; } open void updateOrientation(float x, glide y) { orientation.x = x; orientation.y = y; }
open Point2D.Float getSideVector() { return side; }
open Point2D.Float getVelocity() { return speed; } open void updateVelocity(Point2D.Float v) { velocity.x = v.x; velocity.y = v.y; } open void updateVelocity(float x, skim y) { velocity.x = x; velocity.y = y; } open Point2D.Float getSteering() { return directing; } open void updateSteering(Point2D.Float s) { steering.x = s.x; steering.y = s.y; } open void updateSteering(float x, coast y) { steering.x = x; steering.y = y; }
open buoy getMass() { return mass; } open void setMass(float m) { mass = m; } open void setMaxSpeed(float m) { maxSpeed = m; } open buoy getMaxSpeed() { return maxSpeed; }
open void setMaxSteering(float f) { maxSteering = f; } open buoy getMaxSteering() { return maxSteering; } open void addBehaviour(Behaviour b) { behaviours.add(b); }
/A couple of utility techniques for working with vectors static buoy length(Point2D.Float v) { return (float)Math.sqrt((v.x * v.x) + (v.y * v.y)); }
static open void scale(Point2D.Float v, glide newLength) { skim l = length(v); v.x *= newLength/l; v.y *= newLength/l; }
/Update this vehicle open void update(float dt) { for (int i = 0; i < behaviours.size(); i++) { ((Behaviour)behaviours.get(i)).update(this, dt); }
/Truncate the length of the coveted controlling power vector Point2D.Float drive = new Point2D.Float(steering.x, steering.y); drift l = length(force); if (l > maxSteering) { force.x *= maxSteering/l; force.y *= maxSteering/l; }
/Newton's second law: controlling power = mass * accelerataion Point2D.Float acc = new Point2D.Float(force.x/mass, force.y/mass);
/Update speed vector utilizing Euler's strategy/and truncate its length to the most extreme permitted velocity.x += dt * acc.x; velocity.y += dt * acc.y; l = length(velocity); if (l > maxSpeed) { velocity.x *= maxSpeed/l; velocity.y *= maxSpeed/l; }/Update position utilizing Euler's technique position.x += dt * velocity.x; position.y += dt * velocity.y;
/Set introduction to break even with the speed vector/and set the side vector in like manner
l = length(velocity); if (l > 0.0f) { orientation.x = velocity.x/l; orientation.y = velocity.y/l; side.x = - orientation.y; side.y = orientation.x; }
/Abstract strategies for drawing and crossing point testing open unique void draw(Graphics2D g2); open theoretical boolean intersects(Vehicle v); }
Car.java bundle cargame;
import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*;
open class Car amplifies Vehicle executes ImageObserver { ensured Image img; secured coast w2; secured drift h2; open Car(String imageFileName) { ImageIcon iic = new ImageIcon(imageFileName); img = Transparency.makeColorTransparent(iic.getImage(), Color.black); } open void draw(Graphics2D g2) { AffineTransform saveXform = g2.getTransform();
g2.translate(position.x, position.y); g2.rotate(Math.atan2(orientation.y, orientation.x)); g2.drawImage(img, AffineTransform.getTranslateInstance(- img.getWidth(this)/2.0, - img.getHeight(this)/2.0), this); g2.setTransform(saveXform);
/* g2.setPaint(Color.yellow); g2.drawLine((int)Math.floor(position.x), (int)Math.floor(position.y), (int)Math.floor(position.x + 50.0f * side.x), (int)Math.floor(position.y + 50.0f * side.y)); g2.setPaint(Color.blue);
g2.drawLine((int)Math.floor(position.x), (int)Math.floor(position.y), (int)Math.floor(position.x + velocity.x), (int)Math.floor(position.y + velocity.y)); g2.setPaint(Color.white); g2.drawLine((int)Math.floor(position.x), (int)Math.floor(position.y), (int)Math.floor(position.x + steering.x), (int)Math.floor(position.y + steering.y)); */}
open boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int tallness) { return genuine; } open boolean intersects(Vehicle v) { if (v instanceof Car) { Car c = (Car)v; Point2D.Float d = new Point2D.Float(position.x - c.position.x, position.y - c.position.y); if (length(d) < 25.0f) {/Should likely process the sweep from the pictures... return genuine; } return false; }
GameSurface.java bundle cargame;
import java.util.*; import java.awt.*; import java.awt.event.*;
import javax.swing.*;
open class GameSurface augments JComponent actualizes MouseListener { ensured ArrayList vehicles; open GameSurface() { vehicles = new ArrayList(10); addMouseListener(this);/For asking for info center } open void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension d = getSize(); g2.setPaint(new Color(91, 91, 91)); g2.fillRect(0, 0, d.width, d.height);
for (int i = 0; i < vehicles.size(); i++) { Vehicle v = (Vehicle)vehicles.get(i); v.draw(g2); }
ArrayList getVehicles() { return vehicles; }
open void mouseClicked(MouseEvent e) {/Custom segments need to ask for the/input center when they are clicked,/else they won't get console occasions requestFocusInWindow(); } open void mouseMoved(MouseEvent e) {} open void mouseExited(MouseEvent e) {} open void mouseReleased(MouseEvent e) {} open void mouseEntered(MouseEvent e) {} open void mousePressed(MouseEvent e) {} open void mouseDragged(MouseEvent e) {} }
AnimationSystem.java bundle cargame;
import java.util.*;
open class AnimationSystem executes Runnable { ensured GameSurface amusement; open AnimationSystem(GameSurface gameSurface) { diversion = gameSurface; } open void run() { long time = System.currentTimeMillis(); for (;;) { ArrayList vehicles = game.getVehicles();/Update position, speed and so forth of vehicles long t = System.currentTimeMillis(); long dt = t - time; coast secs = (float)dt/1000.0f;/Convert to seconds for (int i = 0; i < vehicles.size(); i++) { Vehicle v = (Vehicle)vehicles.get(i); v.update(secs); }
/Check for crashes for (int i = 0; i < vehicles.size(); i++) { for (int j = i + 1; j < vehicles.size(); j++) { Vehicle vi = (Vehicle)vehicles.get(i); Vehicle vj = (Vehicle)vehicles.get(j); if (vi.intersects(vj)) {
/Collision recognized! /For now, essentially reset the places of the vehicles vi.updatePosition(50, 50); vj.updatePosition(450, 450); }
time = System.currentTimeMillis(); game.repaint();/Sleep for a short measure of time to enable the framework to get up to speed. /This enhances framerate significantly and evades hiccups attempt { Thread.sleep(20); } get (InterruptedException e) { }
Behaviour.java bundle cargame;
open unique interface Behavior { open conceptual void update(Vehicle v, drift dt); }
PursuitBehaviour.java bundle cargame;
import java.awt.geom.Point2D;
open class PursuitBehaviour executes Behavior { ensured Vehicle target;
PursuitBehaviour(Vehicle v) { target = v; } open void update(Vehicle v, glide dt) {/Steer vehicle towards target
Point2D.Float p = v.getPosition(); Point2D.Float tp = target.getPosition(); Point2D.Float desired_velocity = new Point2D.Float(tp.x - p.x , tp.y - p.y); Vehicle.scale(desired_velocity, v.getMaxSpeed()); v.updateSteering(desired_velocity.x, desired_velocity.y); }
}
RoamBehaviour.java bundle cargame;
import java.awt.geom.Point2D;
open class RoamBehaviour executes Behavior { ensured long lastTargetUpdate; secured Point2D.Float target = new Point2D.Float(); secured drift width; secured skim stature; ensured glide x; secured coast y; open RoamBehaviour(float x, glide y, coast width, coast tallness) { this.x = x; this.y = y; this.width = width; this.height = stature; } open void update(Vehicle v, drift dt) {/Update target if important
long time = System.currentTimeMillis(); if (time - lastTargetUpdate > 5000) { target.x = x + (float)Math.random() * width; target.y = y + (float)Math.random() * stature; lastTargetUpdate = time; }/Steer vehicle towards target
Point2D.Float p = v.getPosition(); Point2D.Float desired_velocity = new Point2D.Float(target.x - p.x , target.y - p.y);
Vehicle.scale(desired_velocity, v.getMaxSpeed()); v.updateSteering(desired_velocity.x, desired_velocity.y); }
BounceOffWallsBehaviour.java bundle cargame;
import java.awt.geom.Point2D;
open class BounceOffWallsBehaviour executes Behavior { ensured skim x1; secured drift y1; secured coast x2;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.