This site requires JavaScript, please enable it in your browser!
Greenfoot back
shivzzz123
shivzzz123 wrote ...

2016/11/2

Is there a way to system.out.print in world rather than the terminal?

shivzzz123 shivzzz123

2016/11/2

#
i want to out put this to my world: import java.util.Arrays; import java.util.Scanner; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.HashMap; import java.util.*; import java.util.Scanner; import java.io.File; import java.io.BufferedReader; import java.io.FileReader; import java.awt.Color; import greenfoot.*; /** * Write a description of class halejadbsa here. * * @author (your name) * @version (a version number or a date) */ public class wordsss extends Actor { public static Scanner in = new Scanner(System.in); public wordsss(){ String word = getRandomWord(); String ga; int tries = 10; GreenfootImage textTwo = new GreenfootImage(30, 40); textTwo.drawString("" + word,30, 40); //String guess = ""; update(toDash(word)); ga = toDash(word); checkCorrect(word,7); GreenfootImage text = new GreenfootImage(30, 40); text.drawString("Your word was " + word,30, 40); //System.out.println("Your word was " + word); } public void act(){ String word = getRandomWord(); String ga; int tries = 10; GreenfootImage textTwo = new GreenfootImage(30, 40); textTwo.drawString("" + word,30, 40); //String guess = ""; update(toDash(word)); ga = toDash(word); checkCorrect(word,7); GreenfootImage text = new GreenfootImage(30, 40); text.drawString("Your word was " + word,30, 40); //System.out.println("Your word was " + word); } public static void main(Stringargs) { String word = getRandomWord(); String ga; int tries = 10; String guess = ""; update(toDash(word)); ga = toDash(word); checkCorrect(word,7); GreenfootImage text = new GreenfootImage(30, 40); text.drawString("Your word was " + word,30, 40); //System.out.println("Your word was " + word); } public static void update(String word) { for (int i = 0; i < word.length; i++) { GreenfootImage text = new GreenfootImage(30, 40); text.drawString(word + " ",30, 40); //setImage(text); // System.out.print(word + " "); } } public static String toDash(String word) { String a = new String; //String output = ""; GreenfootImage text = new GreenfootImage(30, 40); text.drawString("" ,30, 40); for (int i = 0; i < word.length(); i++) { a = "_"; } return a; } public static String checkCorrect(String word, int tries) { String guess; word = word.toUpperCase(); TreeSet<String> tree = new TreeSet<String>(); String copy = new String; int counter = 0; int numberTimes = 0; copy = toDash(word); while (!toString(copy).equals(word) && tries >= 0) { guess = in.nextLine(); guess = guess.toUpperCase(); for (String s : toArray(word)) { if (guess.equals(s)) { copy = s; numberTimes++; } counter++; } if (isAlreadyIn(guess, tree)) { GreenfootImage text = new GreenfootImage(30, 40); text.drawString("You've already guessed that before",30, 40); //System.out.println("You've already guessed that before"); } tree.add(guess); if (numberTimes == 0 || !isAlreadyIn(guess, tree)) { tries--; } //System.out.println("d: " + toString(copy) + "w: " + word + "B: " + numberTimes); GreenfootImage textTwo = new GreenfootImage(30, 40); textTwo.drawString("Tries left: " + tries,30, 40); //System.out.println("Tries left: " + tries); numberTimes = 0; counter = 0; update(copy); } return toString(copy); } public static String toString(String s) { String output = ""; for(String str: s) { output=output+str; } return output.toUpperCase(); } public static String toArray(String s) { s = s.toUpperCase(); String sa = s.split(""); return sa; } public static String getRandomWord(){ String randWord = "a"; try { while (randWord.length() <= 3) { ArrayList<String> a = new ArrayList<String>(); FileReader in = new FileReader("/Users/ShivamJanda/Desktop/verbs.txt"); BufferedReader br = new BufferedReader(in); String line; while ((line = br.readLine()) != null) { a.add(br.readLine()); } Random r = new Random(); randWord = a.get(r.nextInt(a.size()) + 1); in.close(); } } catch (IOException e) { System.out.println("Error occurred."); } return randWord; } public static boolean isAlreadyIn(String s, TreeSet<String> tree) { for (String str : tree) { if (str.equals(s)) { return true; } } return false; } }
danpost danpost

2016/11/2

#
You can always use:
1
getWorld().showText(< text to display >, < x-coordinate >, < y-coordinate >);
to put the text in the world.
shivzzz123 shivzzz123

2016/11/3

#
OK so i changed my code a bit import java.util.Arrays; import java.util.Scanner; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.HashMap; import java.util.*; import java.util.Scanner; import java.io.File; import java.io.BufferedReader; import java.io.FileReader; import java.awt.Color; import greenfoot.*; /** * Write a description of class halejadbsa here. * * @author (your name) * @version (a version number or a date) */ public class wordsss extends Actor { public static Scanner in = new Scanner(System.in); public wordsss(){ //System.out.println("Your word was " + word); } public void act(){ String word = getRandomWord(); String ga; int tries = 10; String guess = ""; display(toDash((word))); ga = toDash(word); checkCorrect(word,7); //GreenfootImage text = new GreenfootImage(100, 40); //text.drawString("Your word was " + word,200, 40); //setImage(text); getWorld().showText("Your word was " + word, 200 , 110) ; //System.out.println("Your word was " + word); } /**public static void main(Stringargs) { String word = getRandomWord(); String ga; int tries = 10; String guess = ""; update(toDash(word)); ga = toDash(word); checkCorrect(word,7); GreenfootImage text = new GreenfootImage(30, 40); text.drawString("Your word was " + word,30, 40); //System.out.println("Your word was " + word); } */ public void display(String word) { for (int i = 0; i < word.length; i++) { //GreenfootImage text = new GreenfootImage(30, 40); //text.drawString(word + " ",30, 40); getWorld().showText(word + " ", 30, 40); // System.out.print(word + " "); } } public String toDash(String word) { String a = new String; String output = ""; for (int i = 0; i < word.length(); i++) { a = "_"; } return a; } public String checkCorrect(String word, int tries) { String guess; word = word.toUpperCase(); TreeSet<String> tree = new TreeSet<String>(); String copy = new String; int counter = 0; int numberTimes = 0; copy = toDash(word); while (!toString(copy).equals(word) && tries >= 0) { guess = in.nextLine(); guess = guess.toUpperCase(); for (String s : toArray(word)) { if (guess.equals(s)) { copy = s; numberTimes++; } counter++; } if (isAlreadyIn(guess, tree)) { //GreenfootImage textb = new GreenfootImage(30, 40); //textb.drawString("You've already guessed that before",30, 40); //setImage(textb); getWorld().showText("You've already guessed that before",30, 40); //System.out.println("You've already guessed that before"); } tree.add(guess); if (numberTimes == 0 || !isAlreadyIn(guess, tree)) { tries--; } //System.out.println("d: " + toString(copy) + "w: " + word + "B: " + numberTimes); //^ dont need code //GreenfootImage textc = new GreenfootImage(30, 40); //textc.drawString("Tries left: " + tries,30, 40); //setImage(textc); getWorld().showText("Tries left: " + tries,30, 40); //System.out.println("Tries left: " + tries); numberTimes = 0; counter = 0; display(copy); } return toString(copy); } public String toString(String s) { String output = ""; for(String str: s) { output=output+str; } return output.toUpperCase(); } public String toArray(String s) { s = s.toUpperCase(); String sa = s.split(""); return sa; } public String getRandomWord(){ String randWord = "a"; try { while (randWord.length() <= 3) { ArrayList<String> a = new ArrayList<String>(); FileReader in = new FileReader("/Users/ShivamJanda/Desktop/verbs.txt"); BufferedReader br = new BufferedReader(in); String line; while ((line = br.readLine()) != null) { a.add(br.readLine()); } Random r = new Random(); randWord = a.get(r.nextInt(a.size()) + 1); in.close(); } } catch (IOException e) { System.out.println("Error occurred."); } return randWord; } public static boolean isAlreadyIn(String s, TreeSet<String> tree) { for (String str : tree) { if (str.equals(s)) { return true; } } return false; } } ///////////////////////////////////////////////////////////////// and here is my code for my world: i want what ever the code does up top to display in the world below how would i do that? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.util.Arrays; import java.util.Scanner; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.HashMap; import java.util.*; import java.util.Scanner; import java.io.File; import java.io.BufferedReader; import java.io.FileReader; import java.awt.Color; import greenfoot.*; /** * Write a description of class smt here. * * @author (your name) * @version (a version number or a date) */ public class gameScreen extends World { wordsss wrd = new wordsss(); Body body = new Body(); Head head = new Head(); ArmOne armOne = new ArmOne(); ArmTwo armTwo = new ArmTwo(); LegOne legOne = new LegOne(); LegTwo legTwo = new LegTwo(); /** * Constructor for objects of class smt. * */ public gameScreen() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); // for hanger Hanger hanger = new Hanger(); addObject(hanger, 110, 180); prepare(); // for person body parts addObject(body, 190, 255); addObject(head, 216, 165); addObject(armOne,135, 105); addObject(armTwo,255, 105); addObject(legOne,177, 205); addObject(legTwo,210, 205); } public void prepare(){ // wordsss wrd = new wordsss(); //addObject (wrd, 200, 250); } }
danpost danpost

2016/11/3

#
It looks like you have shown the same Actor subclass twice, instead of showing your world class. Also, please use code tags around your code (see the 'Posting code? read this!' link below the reply box). Wait, I see the world class at the end (after double posting the actor class
shivzzz123 shivzzz123

2016/11/3

#
OK so i changed my code a bit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import java.util.Arrays;
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
import java.util.*;
import java.util.Scanner;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.awt.Color;
import greenfoot.*;
/**
 * Write a description of class halejadbsa here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class wordsss extends Actor
{
    public static Scanner in = new Scanner(System.in);
    public wordsss(){
 
        //System.out.println("Your word was " + word);
    }
    public void act(){
        String word = getRandomWord();
        String ga;
        int tries = 10;
 
        String guess = "";
        display(toDash((word)));
        ga = toDash(word);
        checkCorrect(word,7);
 
        //GreenfootImage text = new GreenfootImage(100, 40);
        //text.drawString("Your word was " + word,200, 40);
        //setImage(text);
        getWorld().showText("Your word was " + word, 200 , 110) ;
 
       
        //System.out.println("Your word was " + word);
    }
 
    /**public static void main(Stringargs) {
    String word = getRandomWord();
    String ga;
    int tries = 10;
 
    String guess = "";
    update(toDash(word));
    ga = toDash(word);
    checkCorrect(word,7);
    GreenfootImage text = new GreenfootImage(30, 40);
 
    text.drawString("Your word was " + word,30, 40);
    //System.out.println("Your word was " + word);
    }
     */
    public  void  display(String word) {
 
        for (int i = 0; i < word.length; i++) {
            //GreenfootImage text = new GreenfootImage(30, 40);
            //text.drawString(word + " ",30, 40);
            getWorld().showText(word + " ", 30, 40);
            // System.out.print(word + " ");
 
        }
 
    }
 
    public String toDash(String word) {
        String a = new String;
        String output = "";
         
        for (int i = 0; i < word.length(); i++) {
 
            a = "_";
        }
        return a;
    }
 
    public  String checkCorrect(String word, int tries) {
        String guess;
        word = word.toUpperCase();
        TreeSet<String> tree = new TreeSet<String>();
        String copy = new String;
        int counter = 0;
        int numberTimes = 0;
        copy = toDash(word);
        while (!toString(copy).equals(word) &&  tries >= 0) {
            guess = in.nextLine();
            guess = guess.toUpperCase();
            for (String s : toArray(word)) {
                if (guess.equals(s)) {
                    copy = s;
                    numberTimes++;
                }
                counter++;
            }
 
            if (isAlreadyIn(guess, tree)) {
                //GreenfootImage textb = new GreenfootImage(30, 40);
 
                //textb.drawString("You've already guessed that before",30, 40);
                //setImage(textb);
                 
                getWorld().showText("You've already guessed that before",30, 40);
                //System.out.println("You've already guessed that before");
            }
            tree.add(guess);
 
            if (numberTimes == 0 || !isAlreadyIn(guess, tree)) {
                tries--;
            }
             
            //System.out.println("d: " + toString(copy) + "w: " + word + "B: " + numberTimes);
            //^ dont need code
             
            //GreenfootImage textc = new GreenfootImage(30, 40);
 
           //textc.drawString("Tries left: " + tries,30, 40);
            //setImage(textc);
            getWorld().showText("Tries left: " + tries,30, 40);
            //System.out.println("Tries left: " + tries);
            numberTimes = 0;
            counter = 0;
            display(copy);
        }
 
        return toString(copy);
    }  
 
    public  String toString(String s) {
        String output = "";
        for(String str: s) {
            output=output+str;
        }
        return output.toUpperCase();
    }
 
    public  String toArray(String s) {
        s = s.toUpperCase();
        String sa = s.split("");
        return sa;
    }
 
    public  String getRandomWord(){
        String randWord = "a";
        try {
            while (randWord.length() <= 3) {
                ArrayList<String> a = new ArrayList<String>();
                FileReader in = new FileReader("/Users/ShivamJanda/Desktop/verbs.txt");
                BufferedReader br = new BufferedReader(in);
                String line;
                while ((line = br.readLine()) != null) {
                    a.add(br.readLine());
                }
                Random r = new Random();
                randWord = a.get(r.nextInt(a.size()) + 1);
                in.close();
            }
        }
        catch (IOException e) {
            System.out.println("Error occurred.");
        }      
        return randWord;
    }
 
    public static boolean isAlreadyIn(String s, TreeSet<String> tree) {
        for (String str : tree) {
            if (str.equals(s)) {
                return true;
            }
        }
        return false;
    }
}
 
/////////////////////////////////////////////////////////////////
and here is my code for my world:
 
i want what ever the code does up top to display in the world below
 
how would i do that?
 
 
 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.Arrays;
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
import java.util.*;
import java.util.Scanner;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.awt.Color;
import greenfoot.*;
 
/**
 * Write a description of class smt here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class gameScreen extends World
{
    wordsss wrd = new wordsss();
    Body body = new Body();
    Head head = new Head();
    ArmOne armOne = new ArmOne();
    ArmTwo armTwo = new ArmTwo();
    LegOne legOne = new LegOne();
    LegTwo legTwo = new LegTwo();
    /**
     * Constructor for objects of class smt.
     *
     */
    public gameScreen()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        // for hanger
 
        Hanger hanger = new Hanger();
        addObject(hanger, 110, 180);
 
         
        prepare();
 
         
        // for person body parts
        addObject(body, 190, 255);
        addObject(head, 216, 165);
        addObject(armOne,135, 105);
        addObject(armTwo,255, 105);
        addObject(legOne,177, 205);
        addObject(legTwo,210, 205);
 
 
    }
 
    public void prepare(){
       // wordsss wrd = new wordsss();
        //addObject (wrd, 200, 250);
 
    }
}
danpost danpost

2016/11/3

#
It appears that this is your first time trying to use greenfoot, but you are fairly well instructed in java. With greenfoot, you do not create a 'public static void main(varArgs)' method. When you create a jar file or an applet from the project, greenfoot will add the method to create your initial world and add the controls to run it. Also, you can make use of the fact that Actor objects can have images that you can control. Your wordsss instance can display the random word and have it update itself each time a letter is guessed (using underscores for the letters not guessed yet). Then, you are not restrained to the font and size of that displayed by 'showText'.
shivzzz123 shivzzz123

2016/11/3

#
can you give me an example of how i would be able to display the word and update itself each time a letter is guessed
shivzzz123 shivzzz123

2016/11/3

#
like how would you change up my code so it would work?
danpost danpost

2016/11/3

#
shivzzz123 wrote...
can you give me an example of how i would be able to display the word and update itself each time a letter is guessed
By convention, class names begin with uppercase letters (I will use the name Word since it will display the word (with underscores -- or not). This is a brief summary of what is needed only to sow you an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import greenfoot.*;
 
public class Word extends Actor
{
    String word;
    String guesses = "";
    boolean done;
 
    public Word()
    {
        word = getRandomWord().toUpperCase();
        updateImage();
    }
     
    // updates image and checks for completed word
    private void updateImage()
    {
        done = true;
        GreenfootImage image = new GreenfootImage(word.length()*30, 30);
        for (int i=0; i<word.length(); i++)
        {
            String cStr = word.substring(i, i+1);
            if (!guesses.contains(cStr))
            {
                cStr = '_';
                done = false;
            }
            GreenfootImage cImage = new GreenfootImage(cStr, 24, null, null);
            image.drawImage(cImage, 15+30*i-cImage.getWidth()/2, 0);
        }
        setImage(image);
        if (done)
        {
            // show message (solved)
        }
    }
     
    public boolean isDone()
    {
        return done;
    }
}
I would simplify everything by using methods supplied by greenfoot for images and input. I do not think the scanner you are trying to use will work in greenfoot (greenfoot captures the keystrokes and the scanner is left empty-handed -- at least, I think that is what happens). A simple world class might be just this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import greenfoot.*;
 
public class GameScreen extends World // class names should begin with uppercase letter (by convention)
{
    private Word word;
    private Actor[] bodyPart =
    {
        new Head(),
        new Body();
        new ArmOne();
        new ArmTwo();
        new LegOne();
        new LegTwo();
    };
    private int[][] partLocation =
    {
        { 216, 165 },
        { 190, 255 ),
        { 135, 105 },
        { 255, 105 },
        { 177, 205 },
        { 210, 205 }
    };
         
    public GameScreen()
    {
        addObject(new Hanger(), 110, 180);
        word = new Word();
        addObject(word, 30, 20);
    }
     
    public void act()
    {
        if (word.isDone() && Greenfoot.isKeyDown("n")) Greenfoot.setWorld(new GameScreen());
    }
     
    public void showBodyPart(int which)
    {
        addObject(bodyPart[which], partLocation[which][0], partLocation[which][1]);
    }
}
You will notice that in straight java, you program the life of the program in your 'main' method. In greenfoot, the act methods of the World and Actor subclasses are programmed for what needs done at any instance in time. By greenfoot continuously cycling the execution of act methods, your first loop is created, breaking down time to instances of time, or moments. So, the act method of the Word class should first check for already done, which would mean nothing needs to be processed "at the moment". Then, it should check for any guesses and if none, no processing needs done. Finally, since the word is incomplete and we have a new guess, process that guess. The act method might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public void act()
{
    if (isDone()) return;
    String key = Greenfoot.getKey();
    if (key == null) return; // no guess (at this moment)
    key = key.toUpperCase();
    if (key.length != 1 || "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(key) < 0) return; // ignore invalid keystroke
    if (guesses.contains(key))
    {
        // show message (already guessed that letter)
        return;
    }
    guesses += key;
    int count = 0; // to count matching characters
    for (char c : word) if (c == key.charAt(0)) count++;
    // show message (there are 'count' 'key' characters in the word)
    updateImage();
    if (!done)
    {
        count = 0; // to count bad guesses
        for (char g : guesses) if (!word.contains(""+g)) count++;
        if (count > 0) ((GameScreen)getWorld().showBodyPart(count-1);
        if (count == 10)
        {
            done = true;
            // show message (failure to solve in required number of guesses)
        }
    }
}
Now, I do not know how your body parts currently work -- I presume that they are hidden until each is told to show (because you add all of them into the world at the beginning). The above is probably better (just adding the part into the world when a wrong guess is made), rather than messing with the image of the body part. However, there seems to be a conflict between number of body parts (6) and the number of tries allowed (10). If you do not mind all your messages showing at the same location (one replacing another), you could create a Message class and add it to the world. Then have the world keep a reference to it with a getter method so the Word actor to make use of it. If interested, just indicate it. If you noticed with the codes given above, I only used the one import statement bringing in the greenfoot package. Because of the methods provided in the various classes provided there, there is really no need for any of the others -- at least for the basic functioning of the project. You will still need a few that you had to read the word file in (those from the 'io' package). Also, I only used a minimal number of fields. Just enough to allow whatever checks need to be done to be possible. Oh, and after you commented out the main' method and added the 'act' method, you placed all the variables inside the act method. They cannot be retained there. Notice how I placed mine in the codes above. Variables declared within a method are lost once the method is done executing (for that instance of execution). Outside the method, these variables are called fields and are retained for as long as that instance of the class exists.
You need to login to post a reply.