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

2012/5/27

Problem with comparing of two lists

1
2
Chaos_of_90 Chaos_of_90

2012/5/27

#
Hey! I have some troubles with comparing two lists. First heres the important part of my code:
if(player.size() == game.size()){ //prüft ob die Listengrößen übereinstimmen
                    if(game.equals (player)) //Wenn game und player Liste übereinstimmen ...
                    {
                        Spielende = false; //... bleibt das Spielende false ...
                        sequenz = runden + 1; /** Wird sequenz um 1 erhöht
                         * sorgt dafür das die anfangsschleife nicht
                         * unterbrochen und fortgeführt wird
                         */
                        beg = 0;
                    }
                    else {Spielende = true; //.. wenn nicht, tritt das Spielende ein
                    }
Problem: in round 1 it works, but in round 2, the game is setting the variable "Spielende" true, just when I pressed one color (so one value is setted in the list) without waiting on the second value. Its a Color Memory game. The computer set values (they stand for colors) in the list game and the player must repeat the order of the colors. The colors which the player press, will be wrote into the list player. I dont know why the game set "Spielende" true because, i just pressed one color. You know what I mean and can help me?
ttamasu ttamasu

2012/5/27

#
since it is a limited snipet, I can't really say whats wrong. my guess is after round 1 you are not clearing the values for player list (ie creating a new list)... but thats just a shot in the dark.
Chaos_of_90 Chaos_of_90

2012/5/28

#
No i clear the list, here the complete code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;  // Verarbeitung von Listen
import java.util.ArrayList;
import java.lang.Math;
import java.util.Arrays;

/**
 * Write a description of class Starter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Starter  extends Actor

{
    RedIn ri;
    BlueIn bi;
    YellowIn yi;
    GreenIn gi;

    MouseInfo m;

    int playerSize;
    int gamerSize;
    int runden = 0;
    int ende;
    int beg = 0;
    int sequenz = 1;
    int z = 60; //Zeitraum in der die Farbe sichtbar ist
    List<Integer> game = new ArrayList<Integer>();    // Game erstelle Liste
    List<Integer> player = new ArrayList<Integer>();    // Spieler Liste (zum vergleich)
    int c; //Farbvariable für die Listen
    boolean Spielende = false;

    /** Konstruktor
     * 
     */
    public Starter(RedIn r, BlueIn b, YellowIn y, GreenIn g) {
        ri = r;
        bi = b;
        yi = y;
        gi = g;
    }

    /**
     * Act - do whatever the Starter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Spiel(); //das eigentliche Spiel

    }    

    public void Spiel()
    {
        //         while(Spielende == false) //Solange das Spielende nicht eingetroffen ist, wird gespielt
        //         { 
        //         }
        ende = 0;
        game.clear();
        player.clear();

        if (beg < sequenz) //Beginn() wird so oft wie die Sequenz ausgeführt ausgeführt
        {
            Beginn();
        }

        m = Greenfoot.getMouseInfo();
        if (m != null){

            //if(Greenfoot.mouseClicked("RedIn.class")) { //Prüft ob auf RedIn geklickt wurde
            if(Greenfoot.mouseClicked(ri)) { //Prüft ob auf RedIn geklickt wurde
                //if(Greenfoot.mouseClicked(null)) { //Prüft ob auf RedIn geklickt wurde
                //if(m.getActor() == ri) { //Prüft ob auf RedIn geklickt wurde
                //if(m.getX() < 250) { //Prüft ob auf RedIn geklickt wurde
                player.add(1); //in die Player Liste wird "1" für Rot gesetzt
                setRed();
                beg = beg - 1; //beg wird um 1 reduziert, da vorher falsch addiert
                if(player.size() == game.size()){ //prüft ob die Listengrößen übereinstimmen
                    if(game.equals (player)) //Wenn game und player Liste übereinstimmen ...
                    {
                        Spielende = false; //... bleibt das Spielende false ...
                        sequenz = runden + 1; /** Wird sequenz um 1 erhöht
                         * sorgt dafür das die anfangsschleife nicht
                         * unterbrochen und fortgeführt wird
                         */
                        beg = 0;
                    }
                    else {Spielende = true; //.. wenn nicht, tritt das Spielende ein
                    }
                }
            }

            if(Greenfoot.mouseClicked(bi)) {
                player.add(2);
                setBlue();
                beg = beg - 1;

                if(player.size() == game.size()){
                    if(game.equals (player) && player.size() == runden)
                    {
                        Spielende = false;
                        sequenz = runden + 1;
                        beg = 0;
                    }
                    else {Spielende = true;}
                }
            }

            if(Greenfoot.mouseClicked(yi)) {
                player.add(3);
                setYellow();
                beg = beg - 1;

                if(player.size() == game.size()){
                    if(game.equals (player) && player.size() == runden)
                    {
                        Spielende = false;
                        sequenz = runden + 1;
                        beg = 0;
                    }
                    else { Spielende = true;}
                }
            }

            if(Greenfoot.mouseClicked(gi)) {
                player.add(0);
                setGreen();
                beg = beg - 1;

                if(player.size() == game.size()){
                    if(game.equals (player) && player.size() == runden)
                    {
                        Spielende = false;
                        sequenz = runden + 1;
                        beg = 0;
                    }
                    else {Spielende = true;}
                }
            }
        }

        if (Spielende == true)
        {
            ende = 1;
            setRed();
            setBlue();
            setYellow();
            setGreen();
        }
    }

    public void Beginn() //Wählt zufällig eine Farbe
    {
        int farbe;

        farbe = Greenfoot.getRandomNumber (4); //qqq
        //farbe = 1;

        if ( farbe == 1 )
        {
            c = 1;
            runden = runden + 1;
            setRed();
        }

        if ( farbe == 2 )
        {
            c = 2;
            runden = runden + 1;
            setBlue();
        }

        if ( farbe == 3 )
        {
            c = 3;
            runden = runden + 1;
            setYellow();
        }

        if ( farbe == 0 )
        {
            c = 0;
            runden = runden + 1;
            setGreen();
        } 

    }

    public void setRed()
    {  
        Red r = new Red();
        int xPos = 137;
        int yPos = 131;

        this.getWorld().addObject(r, xPos, yPos);
        if(ende == 0) {
            Greenfoot.delay(z); //farbe bleibt für den zeitraum z auf dem Feld
            this.getWorld().removeObject(r);
            Greenfoot.delay(5); //kleine pause vor der neuen farbwahl
            beg = beg + 1;
            game.add(c);
        }
    }

    public void setBlue()
    {   
        Blue b = new Blue();

        int xPos = 363;
        int yPos = 131;

        this.getWorld().addObject(b, xPos, yPos);
        if(ende == 0) {
            Greenfoot.delay(z);
            this.getWorld().removeObject(b);
            //Greenfoot.playSound("Blau.mp3");
            Greenfoot.delay(5);
            beg = beg + 1;
            game.add(c);
        }
    }

    public void setYellow()
    {   
        Yellow  y = new Yellow();
        int xPos = 138;
        int yPos = 366;

        this.getWorld().addObject(y, xPos, yPos);
        //Greenfoot.playSound("Gelb.mp3");
        if (ende == 0) {
            Greenfoot.delay(z);
            this.getWorld().removeObject(y);
            Greenfoot.delay(5);
            beg = beg + 1;
            game.add(c);
        }
    }

    public void setGreen()
    {   
        Green g = new Green();
        int xPos = 362;
        int yPos = 368;

        this.getWorld().addObject(g, xPos, yPos);
        //Greenfoot.playSound("Gruen.mp3");
        if (ende == 0) {
            Greenfoot.delay(z);
            this.getWorld().removeObject(g);
            Greenfoot.delay(5);
            beg = beg +1;
            game.add(c);
        }
    }
}
The problem is still in the second round. Two Colors pop up, I press one and the game set the true, but thats (in my opinion) impossible, because the lists arent equal big, so the game cant change the variable "spielende" Know what I mean?
davmac davmac

2012/5/28

#
E.g. line 78, 79: player.add(1); //in die Player Liste wird "1" für Rot gesetzt setRed(); And 'setRed()' does, on line 204: game.add(c); So, both 'player' and 'game' have had a single item added, and are size 1. The lists are equal size.
Chaos_of_90 Chaos_of_90

2012/5/28

#
Yeah it works in the first round, but not in the second round. Two colors are poping up (so in list game are two values), i press the first color (one item must be in list player) but the game set Spielende true and the game is over. I have no chance to click the second color ...
ttamasu ttamasu

2012/5/28

#
on line 62 and 63 you are clearing the list every cycle (creating an empy list for both list) or the game list is always going to have one element and so if the player guess equals the random value this then it meets the "spielende" condition. I don't know your game algorthm but its very confusing to me what you are trying to do. My guess is that you would want to generate the random computer guess first and then let the player guess the colors. Then you would only clear the game list after the player makes a correct guess. I am assuming that you will eventually give hints on how close the guess was (like the mastermind game. Also liness 70 and 71 are not necessary.
davmac davmac

2012/5/28

#
You never reset Spielende (except in conditions which aren't met), so once it's true it will stay true. Furthermore, as ttamasu pointed out, you are constantly clearing the lists and so resetting them to 0 size.
Chaos_of_90 Chaos_of_90

2012/5/28

#
ok i wasnt sure about the position of the clear methods. I try to explain you the game. Its a color memory game. You have 4 colors, the coputer choose randomly colors (in round 1 one color, in 2 two colors, in 3 three ...). the player must repeat the sequen of the colors in the correct order. so the game and player listt must be cleared before the next round starts. but i have troubles to find the right place for these methods. Can you help me? maybe thats the solution of the problem ( could also uplad my game)
danpost danpost

2012/5/28

#
It would be much easier, if instead of arrays, you just used two strings and a level counter. 1) while computer string length < level, randomly choose a color, highlight the color, add the first letter in its color name to the computer string, then a slight delay, remove highlight. 2) while player string length < level, get player on a color and do the same with the player string. 3) Compare the strings. 4) Score the level, clear the strings, goto next level if successful else gameover
davmac davmac

2012/5/28

#
Sounds like you should only clear the game list once the player list and the game list are the same size.
davmac davmac

2012/5/28

#
danpost, do you mean '... instead of lists, ...'? I don't see any arrays. Personally I'd stick with lists at this point; there doesn't seem to be much to gain by using Strings instead (and, most importantly, it would mean changing a lot of code which is otherwise fine).
Chaos_of_90 Chaos_of_90

2012/5/28

#
the best way is to change so less as possible. If I change the most part of my code i could make mistakes and maybe I dont understand it any more ;-) The way with strings are two complicated for me, I dont know how I use them. I tried to put the clear methods in the inner if:
if(player.size() == game.size()){ //prüft ob die Listengrößen übereinstimmen
                    if(game.equals (player)) //Wenn game und player Liste übereinstimmen ...
                    {
                        Spielende = false; //... bleibt das Spielende false ...
                        sequenz = runden + 1; /** Wird sequenz um 1 erhöht
                         * sorgt dafür das die anfangsschleife nicht
                         * unterbrochen und fortgeführt wird
                         */
                        beg = 0;
                        game.clear();
                        player.clear();
                    }
                    else {Spielende = true; //.. wenn nicht, tritt das Spielende ein
                    }
                }
but that works worser than my first way -> just one round, round two wont start
danpost danpost

2012/5/28

#
Yes, I did mean '... instead of lists, ...'! And, you are correct, changing the code at this point would be ... well, pretty much point-less (sorry for making a point-less point).
danpost danpost

2012/5/28

#
Try:
if (sequenz == player.size() && player.size() == game.size())
Chaos_of_90 Chaos_of_90

2012/5/28

#
instead of?
There are more replies on the next page.
1
2