0

だから私は、Google Apps エンジンで実行される戦艦バックエンド システムを設計しています...昨日始めて、ゲームのフレームワークを設計しました。

残念ながら、私は Python であまりコードを書いていないので、言語の詳細にあまり詳しくありません。プログラムを実行しようとすると、次のエラーが発生し続けます。

 File "C:\Users\Shlomo\Desktop\eclipse\plugins\org.python.pydev_2.7.5.2013052819\pysrc\pydev_runfiles.py", line 432, in __get_module_from_str
mod = __import__(modname)
File "C:\Users\Shlomo\workspace\battleship\Battleship.py", line 222, in <module>
battleship = BattleshipGame("Shlomo",1,1,4,1,1,2,5,2,1,3,3,3,1,4,2,4,1,5,5,5,"John",1,1,4,1,1,2,5,2,1,3,3,3,1,4,2,4,1,5,5,5)
File "C:\Users\Shlomo\workspace\battleship\Battleship.py", line 210, in __init__
field = self.player1_field.getField()
AttributeError: 'NoneType' object has no attribute 'getField'
ERROR: Module: Battleship could not be imported (file: C:\Users\Shlomo\workspace\battleship\Battleship.py).

変数フィールドが PlayerField オブジェクトで初期化されていないため、このエラーを翻訳しました...

これが私のコードです:

import random

class Player:
  player_name = None
  player_id = None
  game_id = None
  your_turn = False
  player_field = None
  hit_field = None
  opponent = None

def __init__(self,name,player_field,hit_field,opponent):
    self.player_name = name
    self.player_field = player_field
    self.hit_field = hit_field
    self.opponent = opponent

def getPlayerField(self):
    return self.player_field

def performHit(self,x,y):
    mark = None
    hit = self.opponent.getPlayerField.hitAt(x,y)
    if hit:
        mark = 'X'
    else:
        mark = 'O'
    self.hit_field.markHitField(x,y,mark)


 class HitField:
    hit_field = None

    def __init__(self):
        hit_field = [[0 for i in xrange(10)] for i in xrange(10)]

    def markHitField(self,x,y,mark):
        self.hitfield[x][y] = mark

 class PlayerField:
    player_field = [[0 for i in xrange(10)] for i in xrange(10)]
    shipsOnField = []
    goodToGo = False
    def __init__(self,battleship,aircraft,destroyer,submarine,patrol):
        if self.validPlacement(battleship)and self.validPlacement(aircraft)and self.validPlacement(destroyer) and self.validPlacement(submarine) and self.validPlacement(patrol):
            self.placeShip(battleship)
            self.placeShip(aircraft)
            self.placeShip(destroyer)
            self.placeShip(submarine)
            self.placeShip(patrol)
            self.shipsOnField.append(battleship)
            self.shipsOnField.append(aircraft)
            self.shipsOnField.append(destroyer)
            self.shipsOnField.append(submarine)
            self.shipsOnField.append(patrol)

            self.goodToGo = True
        else:      
            print "some pieces have been placed incorrectly"

def validPlacement(self,ship):
    hx = ship.getHeadX;
    hy = ship.getHeadY;
    tx = ship.getTailX;
    ty = ship.getTailY;

    if not hx > 0 and not hx < 11:
        return False
    return True

def placeShip(self,ship):
    hx = ship.getHeadX();
    hy = ship.getHeadY();
    tx = ship.getTailX();
    ty = ship.getTailY();
    for y in range(ty,hy):
        for x in range(tx,hx):
            self.player_field[x][y] = ship.getShipID


def hitAt(self,x,y):
    hitPos = self.player_field[x][y]
    if not hitPos == 0 and not hitPos == 'X':
        self.getShipByID(hitPos).removeHealth
        self.player_field[x][y] = 'X'
        return True

def getShipByID(self,ID):
    for ship in self.shipsOnField:
        if ship.getShipID == ID:
            return ship 

def getField(self):
    return self.player_field

 class Ship(object):
    ship_id = None
    ship_name = None
    max_health = None
    remaining_health = None
    head_pos_x = None
    head_pos_y = None
    tail_pos_x = None
    tail_pos_y = None

def __init__(self,id,name,max,hx,hy,tx,ty):
    self.ship_id = id
    self.max_health = max
    self.remaining_health = max
    self.ship_name = name
    self.head_pos_x = hx
    self.head_pos_y = hy
    self.tail_pos_x = tx
    self.tail_pos_y = ty
    self.remaining_health = max

def removeHealth(self):
    self.remaining_health -= 1

def getHeadX(self):
    return self.head_pos_x

def getHeadY(self):
    return self.head_pos_y

def getTailX(self):
    return self.tail_pos_x

def getTailY(self):
    return self.tail_pos_y

def getRemainingHealth(self):
    return self.remaining_health

def getShipID(self):
    return self.ship_id

 class Battleship(Ship):
    def __init__(self,hx,hy,tx,ty):
        Ship.__init__(self,1,"Battle Ship",4,hx,hy,tx,ty)

 class AircraftCarrier(Ship):
    def __init__(self,hx,hy,tx,ty):
        Ship.__init__(self,2,"Aircraft Carrier",5,hx,hy,tx,ty)

 class Destroyer(Ship):
    def __init__(self,hx,hy,tx,ty):
        Ship.__init__(self,3,"Destroyer",3,hx,hy,tx,ty)

 class Submarine(Ship):
    def __init__(self,hx,hy,tx,ty):
        Ship.__init__(self,4,"Submarine",3,hx,hy,tx,ty)

 class PatrolBoat(Ship):
    def __init__(self,hx,hy,tx,ty):
        Ship.__init__(self,5,"Patrol Boat",2,hx,hy,tx,ty)

 class BattleshipGame:

current_turn = None 

player1 = None
player2 = None

player1_field = None
player1_opponent = player2


player2_field = None
player2_opponent = player1

def firstTurn(self):
    rand = random.randint(1,2) 
    if rand==1:
        return self.player1
    else:
        return self.player2

def printGameBoard(self):
    field = self.player1_field.getField() 
    for y in range(1,10):
        for x in range(1,10):
            print field[x][y]     
        print '\n'

def __init__(self,p1name,p1bshx,p1bshy,p1bstx,p1bsty
                ,p1dhx,p1dhy,p1dtx,p1dty
                ,p1shx,p1shy,p1stx,p1sty
                ,p1pbhx,p1pbhy,p1pbtx,p1pbty
                ,p1achx,p1achy,p1actx,p1acty
                ,p2name,p2bshx,p2bshy,p2bstx,p2bsty
                ,p2dhx,p2dhy,p2dtx,p2dty
                ,p2shx,p2shy,p2stx,p2sty
                ,p2pbhx,p2pbhy,p2pbtx,p2pbty
                ,p2achx,p2achy,p2actx,p2acty):


    player1_field = PlayerField(Battleship(p1bshx,p1bshy,p1bstx,p1bsty),
                                AircraftCarrier(p1achx,p1achy,p1actx,p1acty),
                                Destroyer(p1dhx,p1dhy,p1dtx,p1dty),
                                Submarine(p1shx,p1shy,p1stx,p1sty),
                                PatrolBoat(p1pbhx,p1pbhy,p1pbtx,p1pbty))

    player2_field = PlayerField(Battleship(p2bshx,p2bshy,p2bstx,p2bsty),
                                AircraftCarrier(p2achx,p2achy,p2actx,p2acty),
                                Destroyer(p2dhx,p2dhy,p2dtx,p2dty),
                                Submarine(p2shx,p2shy,p2stx,p2sty),
                                PatrolBoat(p2pbhx,p2pbhy,p2pbtx,p2pbty))


    player1 = Player(p1name,self.player1_field,HitField(),self.player2)
    player2 = Player(p2name,self.player2_field,HitField(),self.player1)

    self.current_turn = self.firstTurn()


 battleship = BattleshipGame("Player1",1,1,4,1,1,2,5,2,1,3,3,3,1,4,2,4,1,5,5,5,"Player2",1,1,4,1,1,2,5,2,1,3,3,3,1,4,2,4,1,5,5,5)
 battleship.printGameBoard()

構文については申し訳ありませんが、コード形式で貼り付けるのに苦労しました。このコードにはおそらくいくつかの問題がありますが、まだこの問題を解決できていません...

私は何を間違っていますか?

4

2 に答える 2

1

Python は Java ではなく、あなたが書こうとしている言語でもありません。これらの変更可能なクラス属性は、あなたが思っていることをする可能性は低いです。

 class PlayerField:
    player_field = [[0 for i in xrange(10)] for i in xrange(10)]
    shipsOnField = []
    goodToGo = False
于 2013-06-19T23:53:35.793 に答える
1

では、__init__インスタンス データではなく、ローカル変数 player1_field に割り当てています。それ以外の:

player1_field = PlayerField(...

あなたがしたい:

self.player1_field = PlayerField(...

それがなければ、クラスの player1_field の値を無視しようとしていますが、これは None です。

于 2013-06-19T23:45:31.713 に答える