简体   繁体   中英

Class , attribute and method in python

Hi i am new to python and doing this exercise online i am stuck.Here is the exercise and what I have already done someone can help me.

Define a Case class that contains a single occupy attribute.

This attribute will automatically take the value ' ' during instantiation.

Define in the Case class a play1 method which will give the value 'X' to the attribute occupied if the case is not occupied.

Define in the Case class a play2 method which will give the value 'O' to the attribute occupied if the case is not occupied.

Here is the first part of my code :

   class Case: 
def __init__(self, occupe):
    self.occupe = ' '
    
def jouer1(self):
    if self.occupe == ' ':
        self.occupe = 'X'
def jouer2(self):
    if self.occupe == ' ':
        self.occupe = 'O'

Define a Terrain class that has two attributes: grid and tower. The grid attribute is a 9-element list of type Case.

The turn attribute is an integer that equals 1 if it's player 1's turn to play and 2 if it's player 2's turn.

The lap attribute will be automatically initialized with the value 1. Define in the Terrain class the method str which will allow you to use the print function on objects of this class.

The print function should display in a first line the content of boxes 0 to 2, then in a second line the content of boxes 3 to 5 and finally in a third line the content of boxes 6 to 8.

The boxes will be separated by the character '|'and each line will end with the \\ n character that matches the end of line character.

Define in the class Terrain a method play which will take as parameter an integer ranging from 0 to 8. Depending on the player whose turn it is to play, this method will call the methods play1 or play2 of the cell corresponding to the integer passed in parameter.

It will then be necessary to modify the value of the turn attribute so that the next player can play.

 class Terrain():
def __init__(self, tour , grille = [0,1,2,3,4,5,6,7,8]):
    self.grille = grille
    self.tour = 1
def __str__(self):
     for i in range(3):
        return self (" | ".__str__() +str(grille[i+3]), end='')  
        print("     0)  1)  2)")
    for i in range(3,6):
        print(" | "+str(grille[i+3]), end='')  
        print("     0)  1)  2)")   
    for i in range(6,9):
        print(" | "+str(grille[i+3]), end='')  

and I'm blocking I don't understand the str method someone can help me.

str takes in an argument which is something like an integer or a float, and converts it into a String (word). To add a string and a number together, you need to convert that number into a string (a word) first.

The __str__ method is used to specify how your class will look like when it is treated as a string . For example, cionsider a class like this:

class myClass:
    def __init__(self, x, y) -> None:
        self.x = x
        self.y = y

When you call print(myClass(2, 3)) you will get something like:

< __main__ .myClass object at 0x00....>

But, if you have a class like this:

class myClass:
    def __init__(self, x, y) -> None:
        self.x = x
        self.y = y

    def __str__(self) -> str:
        return f"x: {self.x}\ny: {self.y}"

Then, when you call print(myClass(2, 3)) you will get

x: 2

y: 3

In your case, I think the code should look like this:

class Case:
    def __init__(self) -> None:
        self.occupy = ' '

    def play1(self) -> None:
        if self.occupy == ' ':
            self.occupy = 'X'

    def play2(self) -> None:
        if self.occupy == ' ':
            self.occupy = 'O'

class Terrain:
    def __init__(self, turn: int) -> None:
        self.grid = [Case(), Case(), Case(),
                     Case(), Case(), Case(),
                     Case(), Case(), Case()]
        self.turn = turn
        self.lap = 1

    def __str__(self) -> str:
        line1 = [x.occupy for x in self.grid[:3]]
        line2 = [x.occupy for x in self.grid[3:6]]
        line3 = [x.occupy for x in self.grid[6:]]
        final_string = '\n'.join(['|'.join(line) for line in [line1, line2, line3]])
        return final_string

Or, if you are like me, and love doing one-liners:

class Case:
    def __init__(self) -> None:
        self.occupy = ' '
    def play1(self) -> None:
        if self.occupy == ' ':
            self.occupy = 'X'
    def play2(self) -> None:
        if self.occupy == ' ':
            self.occupy = 'O'

class Terrain:
    def __init__(self, turn: int) -> None:
        self.grid = [Case() for _ in range(9)]
        self.turn = turn
        self.lap = 1
    def __str__(self) -> str:
        return '\n'.join(map(lambda x: '|'.join(map(lambda y: y.occupy, self.grid[3*x:3*x+3])), range(3)))

Also, remember that the value returned by __str__ must be a string.

and about what we can do

'Define, in the class Terrain, a method play which will take as parameter an integer ranging from 0 to 8. Depending on the player whose turn it is to play, this method will call the methods play1 or play2 of the cell corresponding to the integer passed in parameter.'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM