#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Help with classes and methods assignments


    Hello!
    I am in need of some explaining with my assignments. Programming doesn't come naturally to me so I'm trying read as much as I can and to ask as many people to explain as I can.
    The problem is that I don't know how to start solving the assignments, how to tackle them. We do have slides that explain theory but they only explain the theory not the assignments. So in the end I have these 'tools' so to speak but I have no idea how to use them.

    Here's one problem:
    Write a class UserPlayer that asks the user for moves: It should have
     a private attribute name (initialized to None)
     a constructor that takes 1 argument (the name) and uses it to define name
     a method getName returning name
     a method getMove that takes a board, asks the user for a move (by using input ), and returns it

    Our instructor posted something like this for 'taking a board' :

    Code:
    def init(self):       
             self.fields = [[0,0,0] for i in range (3)]        
             self.current = 1

    so I suppose I also start with that? Then what?
    It's also possible that I got it wrong...
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481

    Easy.


    Code:
    "I have these 'tools'"  "Uh huh, sure you do."
    
    'Write a class UserPlayer'
    class UserPlayer:
        'a class named UserPlayer'
    
    
    ################################################################
    
    '''
        Write a class UserPlayer: It should have
            a private attribute name (initialized to None)
            a constructor that takes 1 argument (the name) and uses it to define name
    '''
    class UserPlayer:
    
        def __init__(self, name = None):  # a constructor that takes 1 argument (the name) and uses it to define name
            self.name = name  # a private attribute name (initialized to None)
    
    ################################################################
    
    
    '''
        Write a class UserPlayer: It should have
         a private attribute name (initialized to None)
         a constructor that takes 1 argument (the name) and uses it to define name
         a method getName returning name
    '''
    
    class UserPlayer:
    
        def __init__(self, name = None):
            self.name = name
    
        def getName(self):               # a method getName
            return self.name             #                  returning name
    
    
    ################################################################
    
    
    
    
    '''
        Write a class UserPlayer that asks the user for moves: It should have
         a private attribute name (initialized to None)
         a constructor that takes 1 argument (the name) and uses it to define name
         a method getName returning name
         a method getMove that takes a board, asks the user for a move (by using input ), and returns it
    '''
    
    class UserPlayer:
    
        def __init__(self, name = None):
            self.name = name
    
        def getName(self):
            return self.name
    
        def getMove(self, board): # a method getMove that takes a board
            return input('your move? ') # asks the user for a move (by using input ), and returns it
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Thank you!

    Now I have this as an exercise

    Write a class RandomPlayer that plays randomly tic tac toe: It should have
    a method getName returning "random"
    a method getMove that takes a board and returns an arbitrary legal move (In this case, I'm checking if it is filled with an X or an O)

    Code:
    class RandomPlayer:      
         def getName (self):         
             return random     
    
         def getMove (self, board):        
                import random         
                i = random.randint (0,3)       
                j = ramdom.randint (0,3)        
               board[i][j] != 'X' and board[i][j] != 'O'       
                return board
    does it look ok?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Are you joking? Can you not try your programs in your own version of python? Are you programming without a computer?
    Code:
      File "s.py", line 9
        board[i][j] != 'X' and board[i][j] != 'O'       
                                                       ^
    IndentationError: unindent does not match any outer indentation level
    It does not look ok. Python doesn't like it either.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo