0% found this document useful (0 votes)
34 views

Strategy Design Pattern

The document describes a class diagram and code for a Black Shadow game that uses the strategy pattern, with an AttackManager class that can set different AttackStrategy types (SlashAttack, StabAttack, SpecialAttack) and call their attack methods, taking damage and healing after each turn over 5 rounds. The main method runs the game, getting user input to select an attack and call methods on the AttackManager.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Strategy Design Pattern

The document describes a class diagram and code for a Black Shadow game that uses the strategy pattern, with an AttackManager class that can set different AttackStrategy types (SlashAttack, StabAttack, SpecialAttack) and call their attack methods, taking damage and healing after each turn over 5 rounds. The main method runs the game, getting user input to select an attack and call methods on the AttackManager.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

University of Engineering and Technology,

Taxila

SDA (THEORY#2)

Submitted to:
Ma’am Sidra Shafi

Submitted by:
Muhammad yousaf rana(21-SE-58)
AbdulAhad(21-SE-56)

Section:
OMEGA(Ω)
Title: Black shadow Game using strategy Pattern

Class Diagram:

Generated Pseudo Code Using Star UML:

<<Interface>>

package shadowBlade_Game;

public interface AttackStrategy {


public void attack();
}

Slash Attack Class:

package shadowBlade_Game;

public class SlashAttack implements AttackStrategy{

@Override
public void attack() {
System.out.println("Performing Slash Attack... ...");

Stab Attack Class:

package shadowBlade_Game;

public class StabAttack implements AttackStrategy{

@Override
public void attack() {
System.out.println("Performing StabAttack... ...");

Special Attack Class

package shadowBlade_Game;

public class SpecialAttack implements AttackStrategy{

@Override
public void attack() {
System.out.println("Performing Special Attack... ...");
}

Attack Manager Class:


package shadowBlade_Game;

public class AttackManager implements AttackStrategy{


private String name;
private int health;
AttackStrategy attackStrategy;
public AttackManager( String name, int health) {
this.name = name;
this.health = health;

}
public void setStrategy(AttackStrategy attackStrategy) {
this.attackStrategy = attackStrategy;
}
@Override
public void attack() {
this.attackStrategy.attack();
}

public void takedamage(int damage) {


this.health-=damage;
System.out.println("Name: "+this.name+" take "+damage+" ... ..." );
if(health<=0) {
System.out.println(this.name+" has been defeated... ...");
}
}

public void heal(int healing) {


if(this.health<100&&this.health>=0) {
this.health+=healing;
}
System.out.println(this.name+" healed for "+ healing+"
health ...🌡...");
}

Main Class:

package shadowBlade_Game;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int choice;
int count=0;
AttackManager attackManager = new AttackManager("Player", 100);
System.out.println(
"========== Wellome To Shadow Blade
Game ==========");
do {
System.out.println(
" 1. Slash Attack. \n"+
" 2. Stab Atack. \n"+
" 3. Special Attack. ");

System.out.println("Enter Your Choice... ...");


choice = sc.nextInt();
if(choice == 1) {
attackManager.setStrategy(new SlashAttack());
attackManager.takedamage(20);
attackManager.heal(2);
count+=1;
}else if(choice == 2) {
attackManager.setStrategy(new StabAttack());
attackManager.takedamage(30);
attackManager.heal(5);
count+=1;
}else if(choice == 3) {
attackManager.setStrategy(new SpecialAttack());
attackManager.takedamage(40);
attackManager.heal(7);
count++;
}else {
System.out.println("invalid Choice");
}

}while(count<=5);
sc.close();

Expected Output:

You might also like