Java:
package faps.fau.vhbseibegleitprojekt.demo_rundenspiel;
import java.util.Random;
/**
* A Game Turn-based strategy games are games in
* which the actions of the players are carried out in individual rounds. This can mean
* that the players plan and execute their moves alternately or one after the other, such as in
* chess or mill, or that they plan their moves at the same time in rounds and the moves are then
* executed simultaneously or at least not in order of play. This is e.g. This is the case, for
* example, with the board game Diplomacy and sometimes with role-playing games
*/
public class LustigesSpiel {
public int ziel = -1;
public int aktuell = -1;
public final int zielMax = 100;
public final int zielMin = 21;
public int gewinner = -1;
public int[] letzteZuege = {-1, -1};
public boolean gesperrt = false;
public String nachricht = "";
public int amZug = 0;
public int[] moeglicheZuege = new int[3];
// TODO add java doc comments
public LustigesSpiel() {
nachricht = nachrichtWillkommen();
}
public boolean spielOffen() {
return (ziel >= 0 && aktuell >= 0 && gewinner < 0);
}
public boolean spielerAmZug() {
return (spielOffen() && !gesperrt);
}
public String nachricht() {
return nachricht;
}
public String nachrichtNichtAmZug() {
return "Sie sind gerade nicht am Zug!";
}
public String nachrichtKeinSpiel() {
return "Starten Sie ein neues Spiel, um zu spielen!";
}
public String nachrichtWillkommen() {
return "Willkommen! Starten Sie ein neues Spiel!\n" +
"Sie und Ihr Gegner versuchen sich einer unbekannten Zahl zwischen " + zielMin +
" und " + zielMax + " anzunähern. Dafür können Sie jede Runde eine von drei Zahlen "
+ "wählen, die zur " + "aktuellen Zahl addiert wird.\n";
}
public String nachrichtStart() {
return "Neues Spiel, neues Glück!";
}
public String nachrichtGewinn() {
if (gewinner < 0) return null;
String result = "Spiel beendet! Die gesuchte Zahl war " + ziel + ".\n";
if (gewinner == 0) {
result += "Glückwunsch! Sie haben gewonnen!\n";
if (aktuell > ziel) {
result += "Ihr Gegner hat um " + letzteZuege[1] + " auf " + aktuell + " erhöht.";
}
else {
result += "Sie haben die Zielzahl genau getroffen.";
}
}
else {
result += "Leider hat Ihr Gegner gewonnen.\n";
if (aktuell > ziel) {
result += "Sie haben um " + letzteZuege[0] + " auf " + aktuell + " erhöht.";
}
else {
result += "Ihr Gegner hat die Zielzahl genau getroffen.";
}
}
return result;
}
public String nachrichtZugSelbst() {
String result = "Sie sind am Zug.\n";
if (letzteZuege[1] >= 0) {
result += "Ihr Gegner hat " + letzteZuege[1] + " gewählt.\n";
}
result += " Wählen Sie eine der drei Zahlen aus.";
return result;
}
public String nachrichtZugGegner() {
return "Ihr Gegner ist am Zug. Er überlegt noch...";
}
public void starte() {
aktuell = 0;
ziel = zahlZwischen(zielMin, zielMax);
gewinner = -1;
int[] berechneteZuege = zugVorgabe();
for (int i = 0; i < 3; i++) {
moeglicheZuege[i] = berechneteZuege[i];
}
nachricht = nachrichtStart();
}
public int zahlZwischen(int von, int bis) {
Random rand = new Random();
return rand.nextInt(bis - von + 1) + von;
}
public int[] zugVorgabe() {
int[] result = new int[3];
result[0] = zahlZwischen(0, ziel - aktuell);
for (int i = 1; i < 3; i++) {
result[i] = zahlZwischen(0, zielMax - aktuell);
}
return result;
}
public void ziehe(int spieler, int zug) {
aktuell += zug;
letzteZuege[spieler] = zug;
if (aktuell > ziel) {
gewinner = (spieler == 1 ? 0 : 1);
}
else if (aktuell == ziel) {
gewinner = spieler;
}
}
public void zieheSelbst(int zugNr) {
int zug = moeglicheZuege[zugNr];
ziehe(0, zug);
amZug = 1;
if (gewinner < 0) {
nachricht = nachrichtZugGegner();
}
else {
nachricht = nachrichtGewinn();
}
}
public void zieheComputer() {
gesperrt = true;
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
gesperrt = false;
}
int[] moeglichkeiten = zugVorgabe();
ziehe(1, moeglichkeiten[zahlZwischen(0, 2)]);
amZug = 0;
gesperrt = false;
if (gewinner < 0) {
moeglicheZuege = zugVorgabe();
nachricht = nachrichtZugSelbst();
}
else {
nachricht = nachrichtGewinn();
}
}
}
Zuletzt bearbeitet von einem Moderator: