Skip to content

Instantly share code, notes, and snippets.

@Saafan
Created April 20, 2020 21:39
Show Gist options
  • Save Saafan/d3ae94a90faf58b5c72b0de2de1d2aa4 to your computer and use it in GitHub Desktop.
Save Saafan/d3ae94a90faf58b5c72b0de2de1d2aa4 to your computer and use it in GitHub Desktop.
package Gamecontroller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import engine.Game;
import engine.GameListener;
import exceptions.CannotAttackException;
import exceptions.FullFieldException;
import exceptions.FullHandException;
import exceptions.HeroPowerAlreadyUsedException;
import exceptions.InvalidTargetException;
import exceptions.NotEnoughManaException;
import exceptions.NotSummonedException;
import exceptions.NotYourTurnException;
import exceptions.TauntBypassException;
import model.cards.Card;
import model.cards.minions.Minion;
import model.cards.spells.Spell;
import model.heroes.Hero;
import model.heroes.Mage;
import model.heroes.Warlock;
import view.Gameview;
import javax.swing.*;
public class Controller implements ActionListener, GameListener {
private static Game model;
private static Gameview view;
private ArrayList<JButton> minions = new ArrayList<JButton>();
private ArrayList<JButton> spells = new ArrayList<JButton>();
private ArrayList<JButton> minionsfield = new ArrayList<JButton>();
private JButton selectedminion;
private JButton selectedopponentminion;
private static Controller cont;
public Controller() throws IOException, CloneNotSupportedException, FullHandException {
Warlock x = new Warlock();
Mage y = new Mage();
model = new Game(x, y);
model.setListener(this);
view = new Gameview();
}
public void UpdateScreen() {
view.getCHhand().removeAll();
view.getHk().removeAll();
view.getCHfield().removeAll();
view.getOpfield().removeAll();
minions.clear();
spells.clear();
minionsfield.clear();
for (int i = 0; i < model.getCurrentHero().getHand().size(); i++) {
JButton a = new JButton();
Card p1 = model.getCurrentHero().getHand().get(i);
if (p1 instanceof Minion) {
// the link below is for how to make a new line in JButton:
// https://www.roseindia.net/java/example/java/swing/MultilineLabelButton.shtml
a.setActionCommand("minion’s name:" + " " + p1.getName() + "\n" + "mana cost:" + " " + p1.getManaCost()
+ "\n" + "rarity:" + " " + p1.getRarity().toString() + "\n" + "attack points:" + " "
+ ((Minion) p1).getAttack() + "\n" + "current HP:" + " " + ((Minion) p1).getMaxHP() + "\n"
+ "taunt:" + " " + ((Minion) p1).isTaunt() + "\n" + "divine shield:" + " "
+ ((Minion) p1).isDivine() + "\n" + "charge:" + " " + ((Minion) p1).isSleeping() + "");
a.setText("<html>" + "minion’s name:" + " " + p1.getName() + "<br>" + "mana cost:" + " "
+ p1.getManaCost() + "<br>" + "rarity:" + " " + p1.getRarity().toString() + "<br>"
+ "attack points:" + " " + ((Minion) p1).getAttack() + "<br>" + "current HP:" + " "
+ ((Minion) p1).getMaxHP() + "<br>" + "taunt:" + " " + ((Minion) p1).isTaunt() + "<br>"
+ "divine shield:" + " " + ((Minion) p1).isDivine() + "<br>" + "charge:" + " "
+ ((Minion) p1).isSleeping() + "<html>");
a.addActionListener(this);
minions.add(a);
view.getCHhand().add(a);
} else {
a.setActionCommand("spell’s name:" + " " + p1.getName() + ", " + "mana cost:" + " " + p1.getManaCost()
+ ", " + "rarity:" + " " + p1.getRarity().toString());
a.setText("<html>" + "spell’s name:" + " " + p1.getName() + "<br>" + "mana cost:" + " "
+ p1.getManaCost() + "<br>" + "rarity:" + " " + p1.getRarity().toString() + "<html>");
a.addActionListener(this);
spells.add(a);
view.getCHhand().add(a);
}
}
for (int i = 0; i < model.getCurrentHero().getField().size(); i++) {
JButton a = new JButton();
Card p1 = model.getCurrentHero().getField().get(i);
a.setText("minion’s name:" + " " + p1.getName() + ", " + "mana cost:" + " " + p1.getManaCost() + ","
+ "rarity:" + " " + p1.getRarity().toString() + " ," + "attack points:" + " "
+ ((Minion) p1).getAttack() + ", " + "current HP:" + " " + ((Minion) p1).getMaxHP() + " ,"
+ "taunt:" + " " + ((Minion) p1).isTaunt() + " ," + "divine shield:" + " "
+ ((Minion) p1).isDivine() + " ," + "charge:" + " " + ((Minion) p1).isSleeping() + "");
a.addActionListener(this);
view.getCHfield().add(a);
minionsfield.add(a);
}
for (int i = 0; i < model.getOpponent().getField().size(); i++) {
JButton a = new JButton();
Card p1 = model.getOpponent().getField().get(i);
a.setText("minion’s name:" + " " + p1.getName() + ", " + "mana cost:" + " " + p1.getManaCost() + ","
+ "rarity:" + " " + p1.getRarity().toString() + " ," + "attack points:" + " "
+ ((Minion) p1).getAttack() + ", " + "current HP:" + " " + ((Minion) p1).getMaxHP() + " ,"
+ "taunt:" + " " + ((Minion) p1).isTaunt() + " ," + "divine shield:" + " "
+ ((Minion) p1).isDivine() + " ," + "charge:" + " " + ((Minion) p1).isSleeping() + "");
a.addActionListener(this);
view.getOpfield().add(a);
}
Hero p = model.getCurrentHero();
view.getInfoofCurrentHero()
.setText("Current Hero:\n------------------\n" + "name :" + " " + p.getName() + "\n" + "CurrentHp :"
+ " " + p.getCurrentHP() + "\n" + "Current Mana Crystals :" + " " + p.getCurrentManaCrystals()
+ "\n" + "Total mana Crystals :" + " " + p.getTotalManaCrystals() + "\n" + "Cards in Deck :"
+ " " + p.getDeck().size());
Hero k = model.getOpponent();
view.getInfoofOpponentHero()
.setText("Opponent:\n--------------\n" + "name :" + " " + k.getName() + "\n" + "CurrentHp :" + " "
+ k.getCurrentHP() + "\n" + "Current Mana Crystals :" + " " + k.getCurrentManaCrystals() + "\n"
+ "Total mana Crystals :" + " " + k.getTotalManaCrystals() + "\n" + "Cards in Deck :" + " "
+ k.getDeck().size() + "\n" + "Cards in Hand :" + " " + k.getHand().size());
JButton b = new JButton();
b.setActionCommand("Endturn");
b.setText("Endturn");
b.addActionListener(this);
JButton c = new JButton();
c.setActionCommand("use HeroPower");
c.setText("use HeroPower");
c.addActionListener(this);
JButton d = new JButton();
d.setActionCommand("Attack minion");
d.setText("Attack minion");
d.addActionListener(this);
JButton f = new JButton();
f.setActionCommand("Attack hero");
f.setText("Attack hero");
f.addActionListener(this);
JButton e = new JButton();
e.setActionCommand("play minion");
e.setText("play minion");
e.addActionListener(this);
view.getHk().add(f);
view.getHk().add(e);
view.getHk().add(d);
view.getHk().add(b);
view.getHk().add(c);
view.repaint();
view.revalidate();
}
public static void main(String[] args) throws FullHandException, IOException, CloneNotSupportedException {
cont = new Controller();
for (int i = 0; i < 8; i++)
model.getCurrentHero().endTurn();
cont.UpdateScreen();
}
@Override
public void onGameOver() {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
if (b.getActionCommand().equals("use HeroPower")) {
try {
model.getCurrentHero().useHeroPower();
} catch (NotEnoughManaException | HeroPowerAlreadyUsedException | NotYourTurnException | FullHandException
| FullFieldException | CloneNotSupportedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else if (b.getActionCommand().equals("Endturn")) {
try {
model.getCurrentHero().endTurn();
} catch (FullHandException | CloneNotSupportedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
cont.UpdateScreen();
} else if (b.getActionCommand().charAt(0) == 'm' && !minionsfield.contains(b)) {
int x = minions.indexOf(b);
JButton a = minions.get(x);
Minion d = (Minion) model.getCurrentHero().getHand().get(x);
try {
model.getCurrentHero().playMinion(d);
} catch (NotYourTurnException | NotEnoughManaException | FullFieldException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
addtofield(a);
cont.UpdateScreen();
} else if (b.getActionCommand().charAt(0) == 'm' && minionsfield.contains(b)) {
if (b.getActionCommand().equals("Attack minion") && selectedminion != null
&& selectedopponentminion != null) {
int x = minions.indexOf(selectedminion);
JButton a = minions.get(x);
Minion d = (Minion) model.getCurrentHero().getHand().get(x);
// try {
// model.getCurrentHero().attackWithMinion(d, );
// } catch (CannotAttackException | NotYourTurnException | TauntBypassException
// | InvalidTargetException
// | NotSummonedException e1) {
// TODO Auto-generated catch block
// e1.printStackTrace();
// }
}
} else if (b.getActionCommand().charAt(0) == 'm' && minionsfield.contains(b)) {
if (b.getActionCommand().equals("Attack hero") && selectedminion != null) {
int x = minions.indexOf(selectedminion);
JButton a = minions.get(x);
Minion d = (Minion) model.getCurrentHero().getHand().get(x);
try {
model.getCurrentHero().attackWithMinion(d, model.getOpponent());
} catch (CannotAttackException | NotYourTurnException | TauntBypassException | InvalidTargetException
| NotSummonedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
if (!b.getActionCommand().equals("Attack hero")) {
if (selectedminion == null) {
selectedminion = b;
}
} else {
if (b == selectedminion) {
selectedminion = null;
} else {
selectedminion = b;
}
}
}
}
}
public void mhf() {
}
public void addtofield(JButton x) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment