overskrift = new Font("SansSerif", Font.BOLD, 24); topLabel = new Label("SPIL: Gæt et tal mellem 0 og 10.000"); topLabel.setFont(overskrift); topPanel.add(topLabel); topLabel.setForeground(Color.white); topLabel.setBackground(Color.gray); topLabel.setBounds(5,0,800,75);
midtPanel.setLayout(null);
navnLabel = new Label("Spiller navn"); gætLabel = new Label ("Indtast Gæt"); svarLabel = new Label("Svar"); antalLabel = new Label("Antal gæt");
infoLabel1 = new Label("Vejledning"); infoLabel2 = new Label("Indtast navn, herefter klikkes på Nyt Spil"); infoLabel3 = new Label("Nu kan der indtastes et Gæt, når det er "); infoLabel4 = new Label("foretaget - klikkes på Nyt Gæt osv.");
public void run(){ try { ArrayList toptiliste; sock = new Socket(host, port); System.out.println("Spil client forbundet til " + sock.toString()); input = new ObjectInputStream( sock.getInputStream());
while (true) { try{ toptiliste = (ArrayList) input.readObject(); setChanged(); notifyObservers(toptiliste);} catch (Exception e){}; } } catch (UnknownHostException UnHoEx) { // new Socket System.err.println("Ukendt host: " + host); System.err.println(UnHoEx); } catch (NoRouteToHostException ex) { // new Socket System.err.println("Ingen forbindelse til host: " + host); System.err.println(ex); } catch (ConnectException ex) { // new Socket System.err.println("Kan ikke forbinde til " + host + " port " + port); System.err.println(ex); } catch (SocketException ex) { System.err.println("Serveren har pludselig afbrudt forbindelsen"); System.err.println(ex); } catch (IOException CoEx) { // new Socket System.err.println("Ukendt fejl ved skabelse af forbindelse"); System.err.println(CoEx); } finally { // udføres til sidst uanset exception eller ej try { if (input != null) input.close(); if (sock != null) sock.close(); } catch (IOException ex) { System.err.println("Fejl ved lukning af forbindelser"); System.err.println(ex); } } // finally }// run }// class
// //******************* DRIVERCLASS: class SpilClient ******************* //
public class SpilClient {
public static void main(String[] args){ TopTiConnection topticonnection = new TopTiConnection(1025); SpilConnection spilconnection = new SpilConnection(1026);
new GUISpilTester(topticonnection, spilconnection); } }
// //********* class spiller********************************************** //
class Spiller extends Observable implements Serializable{ private String navn; private int gæt; private int antal; private transient Spil spil; private transient TopTi topTi;
public String gæt(int tal){ antal++; switch (spil.forsøg(tal)){ case -1: return "for lavt"; case 1: return "for højt"; case 2: return "tal er gættet - prøv nyt spil"; case 0: {topTi.checkResultat(this); return "godt gættet";}; } return null; }
public String getNavn(){return navn;}; public int getAntal(){return antal;}; public void setAntal(int nytantal){antal=nytantal;}; public int getGæt(){return gæt;};
public void setGæt(int nytGæt){ gæt=nytGæt; setChanged(); notifyObservers(); }; public Spil getSpil(){return spil;}; }
c:\j2sdk1.4.1\myown\SpilClient.java:438: cannot resolve symbol symbol : class Spil location: class Spiller private transient Spil spil; ^ c:\j2sdk1.4.1\myown\SpilClient.java:439: cannot resolve symbol symbol : class TopTi location: class Spiller private transient TopTi topTi; ^ c:\j2sdk1.4.1\myown\SpilClient.java:441: cannot resolve symbol symbol : class Spil location: class Spiller public Spiller(String navn, Spil spil, TopTi topTi){ ^ c:\j2sdk1.4.1\myown\SpilClient.java:441: cannot resolve symbol symbol : class TopTi location: class Spiller public Spiller(String navn, Spil spil, TopTi topTi){ ^ c:\j2sdk1.4.1\myown\SpilClient.java:471: cannot resolve symbol symbol : class Spil location: class Spiller public Spil getSpil(){return spil;}; ^ 5 errors
public String gæt(int tal){ antal++; switch (spil.forsøg(tal)){ case -1: return "for lavt" + antal; case 1: return "for højt" + antal; case 2: return "tal er gættet - prøv nyt spil" + antal; case 0: {topTi.checkResultat(this); return "godt gættet" + antal;} } return "øv" + antal; }
public String getNavn(){return navn;}; public int getAntal(){return antal;}; public void setAntal(int nytantal){antal=nytantal;}; public int getGæt(){return gæt;};
public void setGæt(int nytGæt){ gæt=nytGæt; setChanged(); notifyObservers(); }; public Spil getSpil(){return spil;}; }
class Spil{ private int tal; private boolean gættet=true;
public int getTal(){return tal;}; public boolean getGættet(){return gættet;};
public void nytSpil(){ tal=(int)(Math.random()*10000); //genererer et tilfældigt tal mellem 1 og 10.000 System.out.println("Tallet...." + tal); gættet=false; }
public int forsøg(int gæt){ if (gættet==false){ if (gæt<tal) return -1; else if (gæt>tal) return 1; else { gættet=true; return 0; } } else return 2; } }
// //****************** class SpillerComperator****************************** //
class SpillerComparator implements Comparator { public int compare(Object o1, Object o2){ int antal1 = ((Spiller) o1).getAntal(); int antal2 = ((Spiller) o2).getAntal(); if (antal1 < antal2) return -1; else if (antal1 > antal2) return 1; else return 0; } }
class TopTi { private static int size=10; private ArrayList topTiListe = new ArrayList(size); private static SpillerComparator sc = new SpillerComparator();
public synchronized void nulstil(){ topTiListe.clear(); notifyAll(); }
public synchronized void checkResultat(Spiller spiller){ if (topTiListe.size()<size) { topTiListe.add(spiller); Collections.sort(topTiListe, sc); notifyAll(); } else if (((Spiller)(topTiListe.get(size))).getGæt()>spiller.getGæt()) { topTiListe.set(size, spiller); Collections.sort(topTiListe, sc); notifyAll(); } }
class TopTiService extends Thread{ private Socket connection; private TopTi topTi;
public TopTiService(Socket connect, TopTi topTi) {connection = connect; this.topTi=topTi;}
public void run(){ try { ObjectOutputStream output = new ObjectOutputStream(connection.getOutputStream()); output.writeObject(new ArrayList(topTi.getTopTiListe())); while(true){ output.writeObject(new ArrayList(topTi.nyTopTi())); }
} catch (SocketException SoEx) { System.err.println("Forbindelsen er gået ned "); System.err.println(SoEx);
} catch (IOException IOEx) { // new ServerSocket, accept m.fl. System.err.println("Ukendt fejl"); System.err.println(IOEx); } finally { // Udføres uanset om metoden afsluttes på normal vis // eller pga. exception, // så forbindelsen lukkes under alle omstændigheder. try { connection.close(); } catch (IOException ex) { // close System.err.println("Den er gal med close"); System.err.println(ex); } } } }
if (portnr == toptiportnr) { server = new ServerSocket(portnr); System.out.println("Topti server startet paa port " + portnr);
while (true) { connection = server.accept(); topTiService = new TopTiService(connection, topti); topTiService.start(); }//end_while } //end_if else { server = new ServerSocket(portnr); System.out.println("Spil server startet paa port " + portnr);
while (true) { connection = server.accept(); spilService = new SpilService(connection, topti); spilService.start(); } }//end_else } // end_try
catch (BindException BiEx) { // new ServerSocket System.err.println("Server kan ikke bindes til port " + portnr); }//end_catch
Man lipper for javac og java i udviklingen, men normalt skal programmet ikke kun køres fra IDE og så skal man stadig have styr på den slags.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.