RE: Python lists and Math

  • From: "Brent Neal" <bneal@xxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 22 Oct 2008 15:14:44 -0600

Thank You for the help.
That is quick response.  This example will be very helpful.  That is what I 
need. 

>>> "Ken Perry" <whistler@xxxxxxxxxxxxx> 10/22/2008 3:06 PM >>>
Well I don't really need any time to answer this question.  Right after my
first Java class in college I was bored and wrote Yahtzee.  Below is my die
class and my FiveDiceGUI class.  One warning the GUI stuff might or might
not be the best way to do it I did it years ago.  It was accessible though.



---die.java---
import java.util.Random;

public class Die
{
  private int value;
    private int sides;
  private static Random rand;
  
    public Die ()
      {
    if (rand == null)
      rand = new java.util.Random ();
      
      //default regular dice
    value=1;
    sides=6;
  }


        public Die (int s,int v)
  {
    if (rand == null)
      rand = new java.util.Random ();
      
    value=v;
    sides=s;
  }

public   int roll ()
  {
    value= rand.nextInt (sides) + 1;
    
    return value;
  }

  public void setValue(int v)
  {
  value=v;
  }  
  
  public int getValue()
  {
    return value;
  }

  public void setSides (int s)
{
sides=s;
}

  public int getSides ()
  {
    return sides;
  }
  
  }


---end die.java

---FiveDiceGUI.java
   import javax.swing.*;
   import java.awt.event.*;
import java.awt.GridLayout;

public class FiveDiceGUI extends JPanel
                        implements ActionListener
                        {
                        
        JButton roll;// roll button for dice.
        private int rollCount;
        private SixSidedDieGUI []dice;
    private boolean marked;
    private int[] values;


public FiveDiceGUI(){
super(new GridLayout(6,1,2,2));  //3 rows, 1 column
rollCount=1;

         roll=new JButton();
roll.setText("Roll dice");

add (roll);
roll.addActionListener(this);
dice=new SixSidedDieGUI[5];
     values=new int[dice.length];
     for (int i=0;i<dice.length;i++){
     dice[i]=new SixSidedDieGUI();
add(dice[i]);
                         dice[i].roll();
                         dice[i].changeAccessibleName();
                                          dice[i].addActionListener(this);
    }
marked=true;




        }

   public void actionPerformed(ActionEvent e)
         {

     if(e.getSource() == roll)
     {

if (marked){
         for (int i=0;i<dice.length;i++)
                dice[i].setSelected(false);
        marked=false;
        rollCount=1;
                 for (int i=0;i<dice.length;i++)
                dice[i].roll();
roll.setText("Roll "+(rollCount)+" of 3");
}
else if (rollCount<3){
         for (int i=0;i<dice.length;i++)
                dice[i].roll();
        rollCount++;
roll.setText("Roll "+(rollCount)+" of 3");

marked=false;
                }
        

//    getAccessibleContext().setAccessibleDescription(roll.getText());

    
for (int i=0;i<dice.length;i++)
                              dice[i].changeAccessibleName();
    }
else if (e.getSource() == dice[0])
dice[0].changeAccessibleName();
else if (e.getSource() == dice[1])  
dice[1].changeAccessibleName();
else if (e.getSource() == dice[2])  
dice[2].changeAccessibleName();
else if (e.getSource() == dice[3])
dice[3].changeAccessibleName();
else if (e.getSource() == dice[4])  
dice[4].changeAccessibleName();
else if (e.getSource() == dice[5])  
dice[5].changeAccessibleName();


         }


public void setMarked(boolean m)
{
marked=m;
if (m==true)
roll.setText("Roll dice");

}

public boolean canMark(){
if (marked)
        return (false);
        else
                return (true);
}
}

---end FiveDiceGUI.java---
 

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Brent Neal
Sent: Wednesday, October 22, 2008 3:49 PM
To: programmingblind@xxxxxxxxxxxxx 
Subject: RE: Python lists and Math

Hi Ken,
If you get time would you mind doing something like this showing how you
would do it with Java?
I tried to do it from this one but cannot seem to get it. 

>>> "Ken Perry" <whistler@xxxxxxxxxxxxx> 10/19/2008 10:28 AM >>>

 
Ok the problem is your not thinking of this in an objective manor.  Your
class should be an object an object is something in the real world in this
case a Die.  so your first class should be a Die.  The die should have a
number of sides you can set when you make it.  It should have a roll method
and  depending on how you like to separate the game from the object you
could put a hold method in it.  You should also have a set method in it and
maybe a reset to set all the Die to 1 or something.  So a quick and dirty
Pseudo code of what you  should have in python would look something like
this.
 
class Die:
 
def  __init__ (self,sides):
self.value=1 #default value
def roll ():
 self.value=random
def setValue (self,v):
self.value=v
def getValue(self):
  return self.value)
 
 
Now like I said you can add a held boolean to your die class but that is
what the die should be able to do is just roll and return a value.  then you
should have a class that is your set of dice and you could make it generic
so if you had a game of two dice you could use it or 500 dice you could use
it.  so for example it would be something like
 
class dicebox:
def __init__(self,count,sides)
  self.dice=[]
for dice in range(0,count):
 self.dice.append(Die(sides)
 
def roll(self)
 for i in range (0,count):
  self.dice[i].roll()
 
def getDieValue(i):
  return self.dice[i].getValue()
 
def getDiceSum():
sum=0
  for i in range(0,count):
 sum+=self.dice[i]
return sum
 
 
You could also add to your class a way to return some kind of text for
example in my dice box class I return for the getDieText something like "1
held" or "1 not held"
 
Now once you have a class set up like this you could do something like
 
myDiceSet=DiceBox(5,6)
myDiceSet.roll()
 
print "The third dice value is "+myDiceSet.getDieValue(2) print "the sum of
all dice is" +myDiceSet.getDiceSum()
 
 
Then you could create a class that controls how many times you roll it ha
class for the score card that has player information and be able to pass the
dicebox around.
 
Well I wouldn't try compiling anything above because I just wrote it as
pseudo code so you could get an idea of what I am talking about but this is
how you should develop with objects.
 
Ken
 
 
 

  _____  

From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of BlueScale
Sent: Sunday, October 19, 2008 10:51 AM
To: programmingblind@xxxxxxxxxxxxx 
Subject: RE: Python lists and Math


Thanks, it works.  I put the [0] in the wrong place before.  I didn't quite
understand what you said is wrong with my dice class.  "the way you have
done it is going to make it difficult to get results or lock dice if you
need to so they don't roll" If I didn't need the dice to roll wouldn't I
just reduce the number of dice rolled?  Also, the line of code you helped me
with gets the results and adds them to the score?  I must be missing
something somewhere *grin* I would like to make a good dice class that won't
come back and cause a lot of problems later when I start writing some games.
The first one is going to be a dice game called pig where the object is to
be the first to get a score of 100.  It uses 1 six-sided die.  It should be
simple enough, but I want to be able to use the same dice class in other
games with more dice, and to be able to use the same class for something
like a D'N'D game too.
So, I am planning on adding set and get sides methods.
Thanks for the help.

On Sun, 2008-10-19 at 10:23 -0500, Ken Perry wrote: 

Well this is not the way I would code it.  In fact the way I did it was to
create a die class that did everything a single die should do.  Then I put
that class in a DiceBox or DiceSet class that would create how many dice you
want.  the way you have done it is going to make it difficult to get results
or lock dice if you need to so they don't roll.  With that said I was write
there was really only one line that was wrong in your code.  You are
returning a list of die or in this case a list of 1 die so you have to
access it as you would a list.  The line you need to change is. 

score += int(cast)



It will work if you do 

score +=cast[0] 

Then if you later change the amount of die your rolling to lets say 6 then
you could do something like 

for i in range (0 ,7): 

  score+=cast[i] 

To get the total. 

Ken 

Ken 




  _____  


From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of BlueScale
Sent: Sunday, October 19, 2008 8:37 AM
To: programmingblind@xxxxxxxxxxxxx 
Subject: RE: Python lists and Math





Hi,
I didn't get it to work.  Classes and OO in general have always given me
problems.  After working with Python a little, I have gotten somewhat better
but not much.  I did this in two files to get some practise with import.
Import works fine, but I may have gone about the class thing completely
wrong.  My code has changed slightly from what I posted earlier.  So, here's
my code for each file:
#dice.py
class dice:
def __init__(self,numDice):
  self.numDice = numDice

def roll(self):
  import random
  die = []
  x = 0
  while x < self.numDice:
   die.append(random.randint(1,6))
   x += 1
  return die

#dice-test.py
#!/usr/bin/python
import dice
score = 0
roll = dice.dice(1)
cast = roll.roll()
score += int(cast)
print score





        

On Sun, 2008-10-19 at 08:15 -0500, Ken Perry wrote: 


You didn't exactly send enough of the code for someone to answer this if
they had not ran into the problem.  I actually just wrote a dice class
recently for a project I am working on.  I am betting because of the error
message you got the roll is a list if this is the case its probably a list
of your dice so what you should have had was
score=roll[i].roll(6)
i being the number of the die you want to roll for example if you have a
list of 5 dice for a game like Yahtzee you could do something like for i in
range (0,5): 
  score+=roll[i].roll(6)
If you can't get it to work let me know I can take your code and fix it
probably in a bout 2 minutes or less.  
Ken
y 


  _____  



From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of BlueScale
Sent: Sunday, October 19, 2008 7:40 AM
To: programmingblind@xxxxxxxxxxxxx 
Subject: Python lists and Math



Hi,
In my effort to learn Python, I have written a dice class.  I have a roll
method that accepts one argument, the number of dice to roll.  It returns a
list of the results.  In the program I wrote to test the class, I have
something like:
score = roll.roll(1)
This rolls 1 six sided die.  The problem I run in to is when I try to do:
score += roll.roll(1)
I get an error about the += operand not supporting list and int.  So my next
move was to try:
score += int(roll.roll())
Which Python says can't be done because the number is stored in a list, not
a string.  So, my question is, how do I keep track of the score?  IS there
some function to convert lists to int?
Thanks for the help



__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind 

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind 

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: