Hi ich versuche gerade Schiffeversenken zu programmieren und habe schon folgendes geschafft:
ich krieg aber immer folgende Fehlermeldung:
hoffe mir kann jemand weiterhelfen vielen Dank im voraus
Code:
public class cSchiff
{
private int typ;
protected char[][] rumpf;
private int länge;
private int tiefe;
private int xKor;
private int yKor;
public cSchiff(int pTyp, int pLänge, int pTiefe)
{
typ = pTyp;
länge = pLänge;
tiefe = pTiefe;
}
public void setTyp(int pTyp)
{
this.typ = pTyp;
}
public int getTyp()
{
return typ;
}
public void setLänge(int pLänge)
{
this.länge = pLänge;
}
public int getLänge()
{
return länge;
}
public void setTiefe(int pTiefe)
{
this.tiefe = pTiefe;
}
public int getTiefe()
{
return tiefe;
}
public void setXKor(int pXKor)
{
this.xKor = pXKor;
}
public int getXKor()
{
return xKor;
}
public void setYKor(int pYKor)
{
this.yKor = pYKor;
}
public int getYKor()
{
return yKor;
}
public boolean istTreffer(int pX, int pY)
{
if(rumpf[pX][pY]=='s')
return true;
else
return false;
}
public void setzeTreffer(int pX, int pY)
{
if(this.istTreffer(pX,pY))
rumpf[pX][pY]='z';
}
public boolean versenkt()
{
int help = 0;
for(int lauf=0; lauf<länge-1; lauf++)
{
for(int lauf2=0; lauf2<tiefe-1; lauf2++)
{
if(rumpf[lauf][lauf2]=='s')
help = help+1;
}
}
if(help==0)
return true;
else
return false;
}
}
Code:
import java.io.*;
public class cMeer
{
private char[][] anzeige;
private cSchiff[] flotte;
private int xEin;
private int yEin;
public cMeer()
{
anzeige = new char[10][10];
for(int y=0; y<2; y++)
{
for(int x=0; x<2; x++)
{
anzeige[x][y]= 'w';
}
}
}
public void setzeSchiffe()
{
flotte[0]= new cBoje(0,0);
flotte[0]= new cTanker(5,5);
}
public void spielereingabe() throws IOException
{
BufferedReader Tastatur;
Tastatur = new BufferedReader(new InputStreamReader (System.in));
System.out.println("Geben Sie die Zielkoordinaten ein. Erst die x-Koordinate");
xEin = Integer.parseInt(Tastatur.readLine());
System.out.println("Und jetzt die y-Koordinate");
yEin = Integer.parseInt(Tastatur.readLine());
}
public void auswertung()
{
if(anzeige[xEin][yEin]=='w')
{
System.out.println("Daneben muhahaha");
}
if(anzeige[xEin][yEin]=='s')
{
System.out.println("Sie haben ein Schiff getroffen");
cSchiff.setzeTreffer(xEin,yEin);
}
}
public void Anzeige()
{
for(int y=0; y<10; y++)
{
for(int x=0; x<10; x++)
{
System.out.println(anzeige[x][y]);
}
}
}
}
Code:
Compiliere C:\Dokumente und Einstellungen\Dagmar\Eigene Dateien\marvin\Marvins Programme\Unterricht\Schiffeversenken\cMeer.java mit Java-Compiler
cMeer.java:51:14: non-static method setzeTreffer(int,int) cannot be referenced from a static context
cSchiff.setzeTreffer(xEin,yEin);
^
1 error