0

This is my program. [code inlined:]

import pygame, sys
import random

class Player1:
    def __init__(self, firstName, lastName, points):
        self.firstName = firstName
        self.lastName = lastName
        self.points = points

    def __repr__(self):
        return "%s %s" % (self.firstName, self.lastName)

    def __int__(self):
        return self.points

    def __add__(self, other):
        try:
            self.points += other
            return Person(points)

        except AttributeError:
            print "error"
            raw_input


class Player2:
    def __init__(self, firstName2, lastName2, points2):
        self.firstName2 = firstName2
        self.lastName2 = lastName2
        self.points2 = points2

    def __repr__(self):
        return "%s %s" % (self.firstName2, self.lastName2)

    def __int__(self):
        return self.points2

    def __add__(self, other):
        try:
            self.points += other
            return Person(points)

        except AttributeError:
            print "error"
            raw_input

def rollDie(sides):
    r = random.randint(1,sides)
    return r

def takeTurn(player):
    points = int(player)
    if int(player) < 100:
        keepRolling = 1
        print
        raw_input("%s <press enter to begin turn>" % (player))
        while keepRolling == 1:
            r = rollDie(6)
            print "%s rolled a " % (player) + str(r)
            print
            if r == 1:
                points = str(int(player))
                print "%s your points are now " % (player) + `points` + " because you rolled a 1." 
                keepRolling = 0
                print "Turn over"
                print
            else:
                points += r
                print "%s your points are now " % (player) + `points` 
                kR = input("Keep rolling?(0=no 1=yes): ")
                if kR == 1:
                    print
                    keepRolling = 1
                else:
                    keepRolling = 0
                    print "%s your points are now " % (player) + `points` 
                    print "turn over"
                    print


    else:
        print
        print "Congradulations %s you have won the Game of Thrones..... I mean pig. Yeah, pig." % (player)
        print
        raw_input("%s, press any key to exit the game.........") % (player)
        sys.exit()

    return points


def intro():
    print "This is the game of pig."
    print
    print "Each player has to roll a six-sided die"
    print "until they get a 1, reach 100, or want to stop."
    print "If they roll a 1, they get nothing."
    print "When prompted, press 1 to roll, 2 to skip."



def main():

    player11 = raw_input("What is Player 1's first name? -> ")
    player12 = raw_input("Last name? -> ")
    player21 = raw_input("Player 2's first name? -> ")
    player22 = raw_input("Last name? -> ")
    desiredT = input("How many turns do you want? ")

    player1 = Player1(player11, player12, 0) #I need the point from take turn to change these zeros
    player2 = Player2(player21, player22, 0)

    def correctInfo():
        verify = raw_input("Is this info correct? Player 1 is %s: Player2 is %s? [y,n] -> " % (player1, player2))
        if verify == "y":
               print

        elif verify == "n":
                print
                print
                main()

        else: 
            print "Not a valid input"
            print
            correctInfo()

    def turns(t):
        for i in range(t):
            takeTurn(player1) #how do I get the point from here to change the class?
            takeTurn(player2)
        if int(player1) > int(player2):
            print
            print "%s has won with %s points." % (player1, int(player1))
            print

        else:
            print
            print "%s has won with %s points." % (player2, int(player2))
            print
        raw_input("press any key to exit.............")

    correctInfo()
    intro()
    turns(desiredT)


main()

There are comments in the relevant areas. I just need to figure out how to get the

return points

at the end of takeTurn() to alter

player1 = Player1(firstName, lastName, 0)

in main() the zero is what needs to change

3
  • please post your code on codepad or something similar where we dont need to download it.. Commented Sep 18, 2012 at 22:58
  • Or, you know, put the code here? Commented Sep 18, 2012 at 23:03
  • I will do that in the future. Commented Sep 19, 2012 at 1:22

1 Answer 1

1

Besides missing the whole point of why and when to use classes, there are a number of issues with your code.

You only need one player class if they are going to share all functionality.

player1 = player(player11, player12, 0) # These instances,
player2 = player(player21, player22, 0) # use the same class.

Change this and everything you have should still work fine.

You want to construct a player with points from a turn the player needs to participate in? Your logic is wrong here.

If you want to change the players points, do it in the TakeTurn() function. You already have access to the player object here.

TakeTurn(player):
    # Instead of return points
    player.points = points
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ^ I am a complete novice, so it is appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.