D
Dogy
Gast
--> --> Wir haben in der 5 Programmierstunde ein Programm vom Professor bekommen-
er hat uns das Programm(ist ein Spiel) ganz kurz erklärt, was keiner von meinen Mitstudenten so richtig verstandet hatte--> und am schluss der Vorlesung hatt er gesagt das wir das Programm ein bisschen umändern sollen
z.B: Desing- Farbe- das dass Programm halt ganz anders ausschaut !! aber ich kenne mich nocht nicht so gut aus
und brauch dringend HILFE !! wir müssen fas Programm dann in 1er Woche in der Vorlesungstunde abgeben!!
Hier ist das Programm Muehle mit java :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class Muehle extends JComponent implements MouseMotionListener, MouseListener, Runnable
{
public static final char SCHWARZ = 'S', SCHWARZ_W = 's';
public static final char WEISS = 'W', WEISS_W = 'w';
public static final char LEER = ' ';
public static final int AUSWAEHLEN = 0;
public static final int ZIEHEN = 1;
public static final int WEGNEHMEN = 2;
static final int[] koordX =
{
400, 700, 700, 700, 400, 100, 100, 100, // 0-7
400, 600, 600, 600, 400, 200, 200, 200, // 8-15
400, 500, 500, 500, 400, 300, 300, 300 // 16-23
};
static final int[] koordY =
{
100, 100, 400, 700, 700, 700, 400, 100, // 0-7
200, 200, 400, 600, 600, 600, 400, 200, // 8-15
300, 300, 400, 500, 500, 500, 400, 300 //16-23
};
static final char[] spielfeld =
{
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 0-7
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 8-15
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' //16-23
};
PrintStream ps;
Scanner scan;
// Noch zu setzende Spielsteine
int steineWeiss;
int steineSchwarz;
boolean spielModusSetzen;
boolean spielModusZiehen;
int ziehenZustand;
char naechsterSpieler = WEISS;
int gezogenerStein = -1;
int mousePosX, mousePosY;
char spieler;
public Muehle()
{
addMouseMotionListener(this);
addMouseListener(this);
steineWeiss = 9;
steineSchwarz = 9;
spielModusSetzen = true;
spieler = WEISS;
try
{
ServerSocket server = new ServerSocket(0);
System.out.println("Port: " + server.getLocalPort());
initVerbindung(server.accept());
}
catch (Exception e)
{
e.printStackTrace();
}
}
public Muehle(String inetAddr, int port)
{
addMouseMotionListener(this);
addMouseListener(this);
steineWeiss = 9;
steineSchwarz = 9;
spielModusSetzen = true;
spieler = SCHWARZ;
try
{
initVerbindung(new Socket(inetAddr, port));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
// Bildschirmhintergrund loeschen
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
// Spielfeld zeichnen
paintSpielfeld(g);
paintSteine(g);
if (spielModusSetzen)
{
paintStein(g, mousePosX, mousePosY, naechsterSpieler);
}
if (ziehenZustand == WEGNEHMEN)
{
paintSteinWegnehmen(g, mousePosX, mousePosY, naechsterSpieler);
}
paintNichtGesetzteSteine(g);
}
public void paintSteine(Graphics g)
{
for (int i = 0; i < spielfeld.length; i ++)
{
paintSteinPos(g, i, spielfeld);
}
}
public void paintSpielfeld(Graphics g)
{
((Graphics2D)g).setStroke(new BasicStroke(6));
g.setColor(Color.BLACK);
g.drawRect(100, 100, 600, 600);
g.drawRect(200, 200, 400, 400);
g.drawRect(300, 300, 200, 200);
g.drawLine(400, 100, 400, 300);
g.drawLine(100, 400, 300, 400);
g.drawLine(500, 400, 700, 400);
g.drawLine(400, 500, 400, 700);
}
public void paintSteinPos(Graphics g, int pos, char farbe)
{
paintStein(g, koordX[pos], koordY[pos], farbe);
}
public void paintSteinWegnehmen(Graphics g, int xPos, int yPos, char farbe)
{
paintStein(g, xPos, yPos, farbe);
g.setColor(Color.BLACK);
g.drawLine(xPos - 40, yPos - 40, xPos + 40, yPos + 40);
g.drawLine(xPos - 40, yPos + 40, xPos + 40, yPos - 40);
}
public void paintStein(Graphics g, int xPos, int yPos, char farbe)
{
if (farbe == SCHWARZ || farbe == SCHWARZ_W)
{
g.setColor(Color.BLACK);
g.fillOval(xPos - 30, yPos - 30, 60, 60);
}
else if (farbe == WEISS || farbe == WEISS_W)
{
g.setColor(Color.WHITE);
g.fillOval(xPos - 30, yPos - 30, 60, 60);
((Graphics2D)g).setStroke(new BasicStroke(3));
g.setColor(Color.BLACK);
g.drawOval(xPos - 30, yPos - 30, 60, 60);
}
else // Kein Stein gesetzt
{
g.setColor(Color.GRAY);
g.fillOval(xPos - 6, yPos - 6, 12, 12);
}
if (farbe == WEISS_W || farbe == SCHWARZ_W)
{
g.setColor(Color.BLUE);
g.fillOval(xPos - 10, yPos - 10, 20, 20);
}
}
public void paintNichtGesetzteSteine(Graphics g)
{
for (int i = 0; i < steineWeiss; i ++)
{
g.setColor(Color.WHITE);
g.fillOval(20, 100 + i * 60, 50, 50);
g.setColor(Color.BLACK);
g.drawOval(20, 100 + i * 60, 50, 50);
}
for (int i = 0; i < steineSchwarz; i ++)
{
g.fillOval(730, 100 + i * 60, 50, 50);
}
}
public void mouseMoved(MouseEvent evt)
{
if((naechsterSpieler == spieler) == (ziehenZustand != WEGNEHMEN))
{
mousePosX = evt.getX();
mousePosY = evt.getY();
ps.println("MOVE " + mousePosX + " " + mousePosY);
repaint();
}
}
public void mouseDragged(MouseEvent evt)
{
}
public void mouseClicked(MouseEvent e)
{
if((naechsterSpieler == spieler) == (ziehenZustand != WEGNEHMEN))
{
setzeSteinAuf(mousePosX = e.getX(), mousePosY = e.getY());
ps.println("CLICK " + mousePosX + " " + mousePosY);
}
}
public void setzeSteinAuf(int posx, int posy)
{
mousePosX = posx;
mousePosY = posy;
for (int i = 0; i < 24; i ++)
{
int x2 = koordX;
int y2 = koordY;
double diff = Math.sqrt(
(mousePosX - x2) * (mousePosX - x2) +
(mousePosY - y2) * (mousePosY - y2));
if (diff < 30)
{
if (ziehenZustand == WEGNEHMEN)
{
if (spielfeld == naechsterSpieler)
{
spielfeld = LEER;
ziehenZustand = AUSWAEHLEN;
repaint();
}
}
else if (spielModusSetzen)
{
if (spielfeld == LEER)
{
spielfeld = naechsterSpieler;
if (naechsterSpieler == WEISS)
{
steineWeiss --;
naechsterSpieler = SCHWARZ;
}
else
{
steineSchwarz --;
naechsterSpieler = WEISS;
}
if (steineWeiss == 0 && steineSchwarz == 0)
{
spielModusSetzen = false;
spielModusZiehen = true;
}
ueberpruefeMuehle(i);
repaint();
}
}
else if (spielModusZiehen)
{
if (ziehenZustand == AUSWAEHLEN)
{
if (spielfeld == naechsterSpieler)
{
spielfeld = (char)(spielfeld + ('a' -'A'));
gezogenerStein = i;
ziehenZustand = ZIEHEN;
}
}
else if (ziehenZustand == ZIEHEN)
{
if (spielfeld == LEER)
{
spielfeld = naechsterSpieler;
spielfeld[gezogenerStein] = LEER;
gezogenerStein = -1;
if (naechsterSpieler == WEISS)
{
naechsterSpieler = SCHWARZ;
}
else
{
naechsterSpieler = WEISS;
}
ziehenZustand = AUSWAEHLEN;
ueberpruefeMuehle(i);
}
}
repaint();
}
}
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void ueberpruefeMuehle(int pos)
{
int spieler = spielfeld[pos];
if (pos % 2 == 0) //Gerade Stellen (keine Ecken)
{
if (pos % 8 == 0)
{
if ((spielfeld[pos + 7] == spieler) && (spielfeld[pos + 1] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
else if ((spielfeld[pos - 1] == spieler) && (spielfeld[pos + 1] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
pos = pos % 8;
if ((spielfeld[pos] == spieler) && (spielfeld[pos + 8] == spieler) && (spielfeld[pos + 16] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
else //Ungerade Stellen (Ecken)
{
if (pos == 7 || pos == 15 || pos == 23)
{
if ((spielfeld[pos - 7] == spieler) && (spielfeld[pos - 6] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
else
{
if ((spielfeld[pos + 1] == spieler) && (spielfeld[pos + 2] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
if (pos == 1 || pos == 9 || pos == 17)
{
if ((spielfeld[pos - 1] == spieler) && (spielfeld[pos + 6] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
else
{
if ((spielfeld[pos - 1] == spieler) && (spielfeld[pos - 2] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
}
}
public void initVerbindung(Socket socket)
{
try
{
ps = new PrintStream(socket.getOutputStream());
scan = new Scanner(socket.getInputStream());
}
catch (Exception e)
{
e.printStackTrace();
}
new Thread(this).start();
}
public void run()
{
while (scan.hasNext())
{
String cmd = scan.next();
int xPos = scan.nextInt();
int yPos = scan.nextInt();
if (cmd.equals("MOVE"))
{
mousePosX = xPos;
mousePosY = yPos;
repaint();
}
else if (cmd.equals("CLICK"))
{
setzeSteinAuf(xPos, yPos);
}
}
}
}
er hat uns das Programm(ist ein Spiel) ganz kurz erklärt, was keiner von meinen Mitstudenten so richtig verstandet hatte--> und am schluss der Vorlesung hatt er gesagt das wir das Programm ein bisschen umändern sollen
z.B: Desing- Farbe- das dass Programm halt ganz anders ausschaut !! aber ich kenne mich nocht nicht so gut aus
und brauch dringend HILFE !! wir müssen fas Programm dann in 1er Woche in der Vorlesungstunde abgeben!!
Hier ist das Programm Muehle mit java :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class Muehle extends JComponent implements MouseMotionListener, MouseListener, Runnable
{
public static final char SCHWARZ = 'S', SCHWARZ_W = 's';
public static final char WEISS = 'W', WEISS_W = 'w';
public static final char LEER = ' ';
public static final int AUSWAEHLEN = 0;
public static final int ZIEHEN = 1;
public static final int WEGNEHMEN = 2;
static final int[] koordX =
{
400, 700, 700, 700, 400, 100, 100, 100, // 0-7
400, 600, 600, 600, 400, 200, 200, 200, // 8-15
400, 500, 500, 500, 400, 300, 300, 300 // 16-23
};
static final int[] koordY =
{
100, 100, 400, 700, 700, 700, 400, 100, // 0-7
200, 200, 400, 600, 600, 600, 400, 200, // 8-15
300, 300, 400, 500, 500, 500, 400, 300 //16-23
};
static final char[] spielfeld =
{
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 0-7
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 8-15
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' //16-23
};
PrintStream ps;
Scanner scan;
// Noch zu setzende Spielsteine
int steineWeiss;
int steineSchwarz;
boolean spielModusSetzen;
boolean spielModusZiehen;
int ziehenZustand;
char naechsterSpieler = WEISS;
int gezogenerStein = -1;
int mousePosX, mousePosY;
char spieler;
public Muehle()
{
addMouseMotionListener(this);
addMouseListener(this);
steineWeiss = 9;
steineSchwarz = 9;
spielModusSetzen = true;
spieler = WEISS;
try
{
ServerSocket server = new ServerSocket(0);
System.out.println("Port: " + server.getLocalPort());
initVerbindung(server.accept());
}
catch (Exception e)
{
e.printStackTrace();
}
}
public Muehle(String inetAddr, int port)
{
addMouseMotionListener(this);
addMouseListener(this);
steineWeiss = 9;
steineSchwarz = 9;
spielModusSetzen = true;
spieler = SCHWARZ;
try
{
initVerbindung(new Socket(inetAddr, port));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
// Bildschirmhintergrund loeschen
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
// Spielfeld zeichnen
paintSpielfeld(g);
paintSteine(g);
if (spielModusSetzen)
{
paintStein(g, mousePosX, mousePosY, naechsterSpieler);
}
if (ziehenZustand == WEGNEHMEN)
{
paintSteinWegnehmen(g, mousePosX, mousePosY, naechsterSpieler);
}
paintNichtGesetzteSteine(g);
}
public void paintSteine(Graphics g)
{
for (int i = 0; i < spielfeld.length; i ++)
{
paintSteinPos(g, i, spielfeld);
}
}
public void paintSpielfeld(Graphics g)
{
((Graphics2D)g).setStroke(new BasicStroke(6));
g.setColor(Color.BLACK);
g.drawRect(100, 100, 600, 600);
g.drawRect(200, 200, 400, 400);
g.drawRect(300, 300, 200, 200);
g.drawLine(400, 100, 400, 300);
g.drawLine(100, 400, 300, 400);
g.drawLine(500, 400, 700, 400);
g.drawLine(400, 500, 400, 700);
}
public void paintSteinPos(Graphics g, int pos, char farbe)
{
paintStein(g, koordX[pos], koordY[pos], farbe);
}
public void paintSteinWegnehmen(Graphics g, int xPos, int yPos, char farbe)
{
paintStein(g, xPos, yPos, farbe);
g.setColor(Color.BLACK);
g.drawLine(xPos - 40, yPos - 40, xPos + 40, yPos + 40);
g.drawLine(xPos - 40, yPos + 40, xPos + 40, yPos - 40);
}
public void paintStein(Graphics g, int xPos, int yPos, char farbe)
{
if (farbe == SCHWARZ || farbe == SCHWARZ_W)
{
g.setColor(Color.BLACK);
g.fillOval(xPos - 30, yPos - 30, 60, 60);
}
else if (farbe == WEISS || farbe == WEISS_W)
{
g.setColor(Color.WHITE);
g.fillOval(xPos - 30, yPos - 30, 60, 60);
((Graphics2D)g).setStroke(new BasicStroke(3));
g.setColor(Color.BLACK);
g.drawOval(xPos - 30, yPos - 30, 60, 60);
}
else // Kein Stein gesetzt
{
g.setColor(Color.GRAY);
g.fillOval(xPos - 6, yPos - 6, 12, 12);
}
if (farbe == WEISS_W || farbe == SCHWARZ_W)
{
g.setColor(Color.BLUE);
g.fillOval(xPos - 10, yPos - 10, 20, 20);
}
}
public void paintNichtGesetzteSteine(Graphics g)
{
for (int i = 0; i < steineWeiss; i ++)
{
g.setColor(Color.WHITE);
g.fillOval(20, 100 + i * 60, 50, 50);
g.setColor(Color.BLACK);
g.drawOval(20, 100 + i * 60, 50, 50);
}
for (int i = 0; i < steineSchwarz; i ++)
{
g.fillOval(730, 100 + i * 60, 50, 50);
}
}
public void mouseMoved(MouseEvent evt)
{
if((naechsterSpieler == spieler) == (ziehenZustand != WEGNEHMEN))
{
mousePosX = evt.getX();
mousePosY = evt.getY();
ps.println("MOVE " + mousePosX + " " + mousePosY);
repaint();
}
}
public void mouseDragged(MouseEvent evt)
{
}
public void mouseClicked(MouseEvent e)
{
if((naechsterSpieler == spieler) == (ziehenZustand != WEGNEHMEN))
{
setzeSteinAuf(mousePosX = e.getX(), mousePosY = e.getY());
ps.println("CLICK " + mousePosX + " " + mousePosY);
}
}
public void setzeSteinAuf(int posx, int posy)
{
mousePosX = posx;
mousePosY = posy;
for (int i = 0; i < 24; i ++)
{
int x2 = koordX;
int y2 = koordY;
double diff = Math.sqrt(
(mousePosX - x2) * (mousePosX - x2) +
(mousePosY - y2) * (mousePosY - y2));
if (diff < 30)
{
if (ziehenZustand == WEGNEHMEN)
{
if (spielfeld == naechsterSpieler)
{
spielfeld = LEER;
ziehenZustand = AUSWAEHLEN;
repaint();
}
}
else if (spielModusSetzen)
{
if (spielfeld == LEER)
{
spielfeld = naechsterSpieler;
if (naechsterSpieler == WEISS)
{
steineWeiss --;
naechsterSpieler = SCHWARZ;
}
else
{
steineSchwarz --;
naechsterSpieler = WEISS;
}
if (steineWeiss == 0 && steineSchwarz == 0)
{
spielModusSetzen = false;
spielModusZiehen = true;
}
ueberpruefeMuehle(i);
repaint();
}
}
else if (spielModusZiehen)
{
if (ziehenZustand == AUSWAEHLEN)
{
if (spielfeld == naechsterSpieler)
{
spielfeld = (char)(spielfeld + ('a' -'A'));
gezogenerStein = i;
ziehenZustand = ZIEHEN;
}
}
else if (ziehenZustand == ZIEHEN)
{
if (spielfeld == LEER)
{
spielfeld = naechsterSpieler;
spielfeld[gezogenerStein] = LEER;
gezogenerStein = -1;
if (naechsterSpieler == WEISS)
{
naechsterSpieler = SCHWARZ;
}
else
{
naechsterSpieler = WEISS;
}
ziehenZustand = AUSWAEHLEN;
ueberpruefeMuehle(i);
}
}
repaint();
}
}
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void ueberpruefeMuehle(int pos)
{
int spieler = spielfeld[pos];
if (pos % 2 == 0) //Gerade Stellen (keine Ecken)
{
if (pos % 8 == 0)
{
if ((spielfeld[pos + 7] == spieler) && (spielfeld[pos + 1] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
else if ((spielfeld[pos - 1] == spieler) && (spielfeld[pos + 1] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
pos = pos % 8;
if ((spielfeld[pos] == spieler) && (spielfeld[pos + 8] == spieler) && (spielfeld[pos + 16] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
else //Ungerade Stellen (Ecken)
{
if (pos == 7 || pos == 15 || pos == 23)
{
if ((spielfeld[pos - 7] == spieler) && (spielfeld[pos - 6] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
else
{
if ((spielfeld[pos + 1] == spieler) && (spielfeld[pos + 2] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
if (pos == 1 || pos == 9 || pos == 17)
{
if ((spielfeld[pos - 1] == spieler) && (spielfeld[pos + 6] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
else
{
if ((spielfeld[pos - 1] == spieler) && (spielfeld[pos - 2] == spieler))
{
ziehenZustand = WEGNEHMEN; //Muehle!
}
}
}
}
public void initVerbindung(Socket socket)
{
try
{
ps = new PrintStream(socket.getOutputStream());
scan = new Scanner(socket.getInputStream());
}
catch (Exception e)
{
e.printStackTrace();
}
new Thread(this).start();
}
public void run()
{
while (scan.hasNext())
{
String cmd = scan.next();
int xPos = scan.nextInt();
int yPos = scan.nextInt();
if (cmd.equals("MOVE"))
{
mousePosX = xPos;
mousePosY = yPos;
repaint();
}
else if (cmd.equals("CLICK"))
{
setzeSteinAuf(xPos, yPos);
}
}
}
}