Avis aux courageux !

Résolu
Sandrine_87 Messages postés 5 Date d'inscription jeudi 9 juin 2005 Statut Membre Dernière intervention 17 juin 2005 - 9 juin 2005 à 01:21
Sandrine_87 Messages postés 5 Date d'inscription jeudi 9 juin 2005 Statut Membre Dernière intervention 17 juin 2005 - 9 juin 2005 à 18:12
Bonjour tout le monde !

Bon je vais faire ca bref. Je fais presentement un stage d'ete a
l'universite de montreal et je dois apprendre le Java. Cela fais donc
un gros 5 jours que j'ai commence a apprendre ce langage. Je comprends
pas comment faire la question suivante (qui est encore a propos d'un
bonhomme pendu !!!) et j'aimerais savoir si une gentille ame
voudrait m'aider ^^. (dsl si c'est dur a lire sans accent :S)



You will have to implement or modify some functionalities to the
actual version (que je vais inclure a la question) we give you.

a) The first part of this assignments is to initialize the applet and
to show the GUI components (text boxes and buttons). Methods to do this
job were already ceated in the Hangman.java file, you just have to call
them in the appropriate way.



Voici ce que le prof nous a
donne. (Vous remarquez qu'il manque des donnees dans certaines
fonctions, comme Graphics car c'est dans les questions b,c et d, que je
vous demanderez surment apres ! )



import java.io.*;



import java.awt.*;

import java.awt.event.*;



import java.applet.Applet;



import java.util.Date;

import java.util.Random;







/*

This GUI component serve has a drawing canvas to delimit the drawing region.

*/

class HangmanCanvas extends Canvas

{



// ---------------------------

// --- Class member fields ---

// ---------------------------



// User good answer counter.

private int goodCounter = 0;



// User error counter.

private int badCounter = 0;



// Flag set to true when the word is found by the user.

private boolean won = false;



// Flag set to true when the game is over (won or lost).

private boolean exit = false;



// Drawing canvas fixed size.

private Dimension canvasSize = new Dimension (400, 400);





// ---------------------------

// -- Class member methods ---

// ---------------------------





// Simple field access methods

public boolean getWon () { return won; }

public boolean getExit () { return exit; }

public int getBadCounter () { return badCounter; }

public int getGoodCounter () { return goodCounter; }





/*

Increments the good answer counter and returns the new value.

*/

public int good ()

{

return ++goodCounter;

}





/*

Increments the error counter (up to 6) and returns the new value.

*/

public int wrong ()

{

if (++badCounter > 6)

badCounter = 6;

return badCounter;

}





public void victory ()

{

won = true;

}





/*

Canvas drawing method. This method will be called when the applet is redrawn.

(see the refresh() method in the Hangman class)

** TODO **

You must draw the hangman here, in 6 parts (head, body, arms and legs).

The parts drawn depend on how many errors the user has made. When the

game is terminated, you must also draw a message

*/

public void paint (Graphics page)

{





}







// These two methods fix the canvas size.

public Dimension getMinimumSize () { return canvasSize; }

public Dimension getPreferredSize () { return canvasSize; }



}



/*

This is the main Applet class that implements the hangman game.

*/

public class Hangman

extends Applet

implements ActionListener

{

private static final int nbwordsmax = 100;

private static final int maxlength = 20;



// GUI components.

private Button ok;

private TextField input;

private TextField[] letters;

private TextArea saidArea;



// Drawing canvas.

private HangmanCanvas canvas;



// The word to find.

private String wordToFind;



// Contains already said characters by the user.

private StringBuffer charSaid;



// Word list read from "wordlist.txt".

private String[] wordlist;



// Word read counter.

private int nbwords;



private Random generator;





/*

This method is called once when the applet is loaded.

*/

public void init()

{



try

{

readWords ();

}

catch (IOException ex)

{

System.err.println ("A fatal error occured: " + ex);

ex.printStackTrace ();

System.exit (1);

}



System.out.println ("read " + wordlist.length + " words.");



// Random number generator initialization.

generator = new Random (new Date ().getTime ());

wordToFind = wordlist[Math.abs (generator.nextInt ()) % nbwords];





// **TODO**

// Here you must initialize the applet using the appropriate methods.

}





// This method is called each time the applet needs to be redrawn.

public void start ()

{

refresh ();

}





/*

This methods initializes the applet's components and member variables.

*/

private void makeApplet ()

{

charSaid = new StringBuffer ();



letters = new TextField[wordToFind.length ()];



for (int i = 0; i < wordToFind.length (); ++i)

{

letters[i] = new TextField (" ");

if (wordToFind.charAt (i) < 97 || wordToFind.charAt (i) > 122)

{

letters[i].setText ("" + wordToFind.charAt (i));

canvas.good ();

}

letters[i].setEditable (false);

}



ok = new Button("OK");

input = new TextField (1);

saidArea = new TextArea (10, 10);



// Here we create the drawing canvas previously defined.

canvas = new HangmanCanvas ();

}





/*

This method places the applet components in a "BorderLayout" layout type.

(Text boxes and the drawing canvas).

*/

private void placeObjects()

{

Panel center = new Panel();

Panel top = new Panel();



top.add (input);

top.add (ok);



// One TextBox for each letters.

for (int i = 0; i < wordToFind.length (); ++i)

center.add (letters[i]);



center.add (canvas);



setLayout (new BorderLayout());

add (top, BorderLayout.NORTH);

add (center, BorderLayout.CENTER);

add (saidArea, BorderLayout.SOUTH);

}





public void actionPerformed(ActionEvent event)

{

if (!canvas.getExit ())

{

if (event.getSource () == ok)

{

String ins = input.getText ();



// ** TODO **

// You must call some method here!



input.setText ("");

input.requestFocus ();

refresh ();

}

}

}





/*

This method reads a list of words from the file "wordlist.txt" and put

them in a array variable named "wordlist".

*/

private void readWords () throws IOException

{

String w;

BufferedReader infile;



infile = new BufferedReader (new FileReader ("wordlist.txt"));

String[] tmp = new String[nbwordsmax];



nbwords = 0;

w = infile.readLine ();



while (w != null && nbwords < nbwordsmax)

{

if (w.length () < maxlength)

tmp[nbwords++] = new String(w);

w = infile.readLine ();

}



if (nbwords == 0)

{

System.err.println ("No word read, exiting.");

System.exit (1);

}



wordlist = new String[nbwords];

for (int i = 0; i < nbwords; ++i)

wordlist[i] = new String (tmp[i]).toLowerCase ();





infile.close ();

}





/*

This method takes a character as parameter and check if the word to find

(the field String "wordToFind") contains at least one occurence of this character.

Also, the character is added to the "charSaid" StringBuffer, which contains already

tried characters by the user.

Then, if the character is contained within the word to find, the "goodCounter" is

incremented, otherwise the "wrongCounter" is incremented.

*/

private void trying (char thechar)

{

int index;

boolean said_flag = false;



// Check if the letter was already said

if (charSaid.toString ().indexOf (thechar) < 0)

{

said_flag = false;

charSaid.append (thechar);

saidArea.setText (charSaid.toString ());

}

else

{

said_flag = true;

System.out.println ("\'" + thechar + "\' was already said.");

}



// Validate input character

if ((index = wordToFind.indexOf (thechar)) < 0)

{

canvas.wrong ();

}

else

{



while (index >= 0)

{

if (!said_flag)

canvas.good ();

letters[index].setText ("" + wordToFind.charAt (index));

index = wordToFind.indexOf (thechar, index + 1);

}



if (canvas.getGoodCounter () == wordToFind.length ())

canvas.victory ();



}





}





public void refresh ()

{

if (canvas != null) canvas.repaint ();

repaint ();

}





}

2 réponses

cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 12
9 juin 2005 à 06:46
ca n a pas l air bien dur tu a les TO DO chaque foi que tu doi modifier le code

je pense que la tu doit juste appeller makeApplet et placeObject a l endroit du TO DO de la methode int() ...

j ai peu etre oublier une methode mais pas facil a lire comme ca.. ;o)

GL

++

GodConan
3
Sandrine_87 Messages postés 5 Date d'inscription jeudi 9 juin 2005 Statut Membre Dernière intervention 17 juin 2005
9 juin 2005 à 18:12
merci :) si je comprends bien, je dois faire un copier coller des
methodes ? Ou je suis carement dans le champs et c'est pas ca que tu
veux dire par appeler
0
Rejoignez-nous