4 Replies - 650 Views - Last Post: 14 October 2012 - 02:48 PM Rate Topic: -----

#1 JDSFTW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-October 12

Implement while loop for Puzzlemania program (Duelist program)

Posted 14 October 2012 - 11:04 AM

Hello everyone,

I like many of you have been assigned the Puzzlemania problem for my Computer Science class.

I have been working on it for a couple days between work and school and I feel like I am getting close, but I am having trouble implementing the while loop.

Here is what I have so far:

import java.util.Random;

public class Duelist
{
    private String name;
    private double accuracy;
    private boolean isAlive = true;
    private int winCount;
    
    
    //Constructor
    public Duelist()
    {
        name = "";
        accuracy = 0.0;
        winCount = 0;
    }
    
    //Constructor
    public Duelist(String name, double accuracy)
    {
        this.name = name;
        this.accuracy = accuracy;
        
    }
    
    
    //getter
    public double getAccuracy()
    {
        return accuracy;
    }
    
    
    //mutator
    public void setAccuracy(double accuracy)
    {
        this.accuracy = accuracy;
    }
    
    //getter
    public String getName()
    {
        return name;
    }
    
    //To String method
    public String toString()
    {
        return("Duelist "+getAlive());
    }
    
    //mutator
    public void setName(String name)
    {
        this.name = name;
    }
    
    //Check to see if player is alive
    public boolean getAlive()
    {
        return isAlive;
    }
    
    //mutator
    public void setDead()
    {
        isAlive = false;
    }     
    
    //Shooting method
    public void ShootAtTarget(Duelist target)
    {
        //For Bob and Aaron shots
        double shotAccuracy = Math.random()*accuracy;
        if(shotAccuracy > accuracy)
        {
            target.setDead();
            //System.out.println(target + " dead");
            System.out.println(shotAccuracy);
        }
        //For Charlie's 100% marksman ability
        else if(accuracy ==1)
        {
            target.setDead();
            //System.out.println(target + "dead");
        }
    }
    
    public static void main(String[] args)
    {
        Random rand = new Random();
        int count = 0;
        int cWin = 0;
        int aWin = 0;
        int bWin = 0;
        Duelist aaron = new Duelist("Aaron", .33);
        Duelist bob = new Duelist("Bob", .5);
        Duelist charlie = new Duelist("Charlie", 1);
        
        for(int i = 0; i<10000; i++)
        {
        count++;
        
        
        //while((bob.getAlive() && charlie.getAlive()) || (bob.getAlive() && aaron.getAlive()) || (aaron.getAlive() && charlie.getAlive()))
        
        //Aaron's shot
        if(bob.getAccuracy() > charlie.getAccuracy())
        {
            aaron.ShootAtTarget(bob);
        }
        else if(bob.getAccuracy() < charlie.getAccuracy());
        {
            aaron.ShootAtTarget(charlie);
        }
        
        
        //Bob's Shot
        if(aaron.getAccuracy() > charlie.getAccuracy())
        {
            bob.ShootAtTarget(aaron);
        }
        else if(aaron.getAccuracy() < charlie.getAccuracy())
        {
            bob.ShootAtTarget(charlie);
        }
        
        //Charlie's shot
        if(aaron.getAccuracy() > bob.getAccuracy())
        {
            charlie.ShootAtTarget(aaron);
        }
        else if(aaron.getAccuracy() < bob.getAccuracy())
        {
            charlie.ShootAtTarget(bob);
        }
        //Record if charlie wins
        if((aaron.getAlive())==false && ((bob.getAlive()) == false))
        {
            cWin++;
        }
        //Record if bob wins
        if((aaron.getAlive())==false && ((charlie.getAlive()) == false))
        {
            bWin++;
        }
        //Record if aaron wins
        if((bob.getAlive())==false && ((charlie.getAlive()) == false))
        {
            aWin++;
        }
        
        
        }
        System.out.println(count);
        System.out.println(charlie.getAlive());
        System.out.println(bob.getAlive());
        System.out.println(aaron.getAlive());
        System.out.println(charlie.getAccuracy());
        System.out.println(bob.getAccuracy());
        System.out.println(aaron.getAccuracy());
        System.out.println(aWin);
        System.out.println(bWin);
        System.out.println(cWin);
    }
    
}


I commented out my while loop because with it in the program I would just get an infinte loop, which leads me to believe that no one is shooting at each other?

This is the output I get when I run this program:

10000
true
false
true
1.0
0.5
0.33
0
0
0


I think my brain is just not cooperating with me anymore :/

and I have seen a few of these topics on DIC with the solutions but copy/paste just isn't my style. I feel like I am close (or I'd like to hope so) but there are maybe a few things that are preventing me from getting my desired output.

I'd also like to keep the methods and such that I have. I know some of you guys are java gurus but I am not so I need my program to reflect my rudimentary skills. Again, I am not asking someone to do it for me, but I just can't pin point what is wrong!

Is This A Good Question/Topic? 0
  • +

Replies To: Implement while loop for Puzzlemania program (Duelist program)

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Implement while loop for Puzzlemania program (Duelist program)

Posted 14 October 2012 - 11:17 AM

I'm a tad confused >.<

Does each duelist get to take one shot, or do they keep shooting until someone is dead? If it is one shot then you're while loop is going at things the wrong way. First of all in your if statements to test which duelist the person should shoot. You should wrap it all in a if statement that tests if the person is alive. You don't want dead people taking turns.

You should have a boolean that states if you should keep playing the game. Then while this boolean is true loop through and have each duelist go at each other. Right before you test for the winner (which should be out of the while loop) You test if only one person is alive. If so then set that boolean to false ending the while loop. Then close the while loop.
Was This Post Helpful? 0
  • +
  • -

#3 JDSFTW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-October 12

Re: Implement while loop for Puzzlemania program (Duelist program)

Posted 14 October 2012 - 11:26 AM

Thank you for the response Kinaces,

Each duelist takes one shot in this order
Aaron,
Bob,
Charlie


They each fire till there is only one left.

With your advice I adjusted my shot to include the if statement to make sure they are alive

if(aaron.getAlive())
        {
            if(bob.getAccuracy() > charlie.getAccuracy())
        {
            aaron.ShootAtTarget(bob);
        }
        else if(bob.getAccuracy() < charlie.getAccuracy());
        {
            aaron.ShootAtTarget(charlie);
        }
        }
        
        
        //Bob's Shot
        if(bob.getAlive())
        {
        if(aaron.getAccuracy() > charlie.getAccuracy())
        {
            bob.ShootAtTarget(aaron);
        }
        else if(aaron.getAccuracy() < charlie.getAccuracy())
        {
            bob.ShootAtTarget(charlie);
        }
        }
        
        //Charlie's shot
        if(charlie.getAlive())
        {
        if(aaron.getAccuracy() > bob.getAccuracy())
        {
            charlie.ShootAtTarget(aaron);
        }
        else if(aaron.getAccuracy() < bob.getAccuracy())
        {
            charlie.ShootAtTarget(bob);
        }
        }


I'll implement your while loop in like 30 min I gotta get to work lol.

Thank you,
JDSFTW
Was This Post Helpful? 0
  • +
  • -

#4 JDSFTW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-October 12

Re: Implement while loop for Puzzlemania program (Duelist program)

Posted 14 October 2012 - 02:38 PM

Ok so I have built my Boolean keepPlaying() to return the condition if there is only one player left. But I get the error cannot find symbol in reference to each of their names. Also I get "non -static method keepPlaying() cannot be referenced from a static context"
Was This Post Helpful? 0
  • +
  • -

#5 JDSFTW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-October 12

Re: Implement while loop for Puzzlemania program (Duelist program)

Posted 14 October 2012 - 02:48 PM

I wish I could post the code but I'm at work and there is not Internet connection for my computer here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1