02 | Quarto 2.0: Jetzt wird's tierisch
Das Spiel Quarto soll erweitert werden: Chamäleon-Spielfiguren ändern bei jedem Spielerwechsel ihre Eigenschaften.
🟪 Analyse
👤 Erweitere dein Spiel nach den folgenden Vorgaben und spiele ein paar Runden.
-
Ergänze die Klasse
FigureChameleonColorim Packagemodel.package model; public class FigureChameleonColor extends Figure { private int round = 0; public FigureChameleonColor(int id, boolean circular, boolean filled, boolean small) { super(id, circular, filled, false, small); } public void setRound(int round) { this.round = round; } public boolean isWhite() { return round % 2 == 0; } } -
Ersetze bei der Instanziierung der Spielfiguren eine Spielfigur vom Typ
Figuredurch eine Spielfigur des TypsFigureChameleonColor. Tipp:💁 Hilfe
Also z. B.
passiveFigures[15] = new FigureChameleonColor(15, true, true, true); -
Ergänze am Ende der Methode
nextStateder KlasseQuartoGamedie Aktualisierung der Chamäleon-Figuren:// update chameleons for (int i = 0; i < figuresOnBoard.length; i++) { for (int j = 0; j < figuresOnBoard[i].length; j++) { if (figuresOnBoard[i][j] instanceof FigureChameleonColor) { ((FigureChameleonColor) figuresOnBoard[i][j]).setRound(playersTurn); } } } -
Ersetzte die Methode
drawFigurein der KlasseFigureDrawerdurch die folgende Methode:public void drawFigure(Figure figure, boolean isActive, int x, int y) { float xPos, yPos; int size; p.rectMode(3); if(isActive) { xPos = activeBoard.calculateCenterPoint(x, y)[0]; yPos = activeBoard.calculateCenterPoint(x, y)[1]; } else { xPos = passiveBoard.calculateCenterPoint(x, y)[0]; yPos = passiveBoard.calculateCenterPoint(x, y)[1]; } if (figure.isWhite()) p.fill(255); else p.fill(0); if(figure.isSmall()) size = 30; else size = 55; if(figure instanceof FigureChameleonColor) { p.strokeWeight(3); p.stroke(160, 32, 240); } else { p.strokeWeight(1); p.stroke(0); } if(figure.isCircular()) p.circle(xPos, yPos, size); else p.square(xPos, yPos, size); if(!figure.isFilled()) { p.fill(92, 64, 42); if (figure.isCircular()) p.circle(xPos, yPos, (float)size/2); else p.square(xPos, yPos, (float)size/2); } p.rectMode(0); p.strokeWeight(1); p.stroke(0); }
🟦 Erweiterung
👤 Erweitere das Projekt um andere Chamäleon-Klassen.
🟩 Vererbung in der Objektorientierung
👤 Ergänze die Lückentexte.