2017-02-08 5 views
0

私はCodi​​nGame hypersonicボットを書いています。データ宣言を別のデータに含めることはできますか?

は、私が解析入力のための作業コード

type Position = (Int,Int) 

data Entity = Player { pId :: Int, pPos :: Position, pBombStock :: Int, pBombRange :: Int } 
      | Bomb { bOwnerId :: Int, bPos :: Position, bTimeLeft :: Int , bBombRange :: Int} 
    deriving Show 

-- The entityType will be: 
-- For players: 0. 
-- For bombs: 1. 
-- The owner will be: 
-- For players: id of the player (0 or 1). 
-- For bombs: id of the bomb's owner. 
-- The param1 will be: 
-- For players: number of bombs the player can still place. 
-- For bombs: number of rounds left until the bomb explodes. 
-- The param2 is not useful for the current league, and will always be: 
-- For players: explosion range of the player's bombs (= 3). 
-- For bombs: explosion range of the bomb (= 3). 
mkEnt :: Int -> Int -> Int -> Int -> Int -> Int -> Entity 
mkEnt 0 o x y p1 p2 = Player { pId = o, pPos = (x,y), pBombStock = p1, pBombRange = p2} 
mkEnt 1 o x y p1 p2 = Bomb { bOwnerId = o, bPos = (x,y), bTimeLeft = p1, bBombRange = p2} 
mkEnt _ _ _ _ _ _ = error "invalid entity type" 

main :: IO() 
main = do 
    print $ mkEnt 0 1 0 0 0 3 
    print $ mkEnt 1 1 0 0 0 3 

を持っているしかし、私はコードがerror: Multiple declarations of ‘Player’

でコンパイルされませんEntity

data Entity = Player | Bomb 
    deriving Show 

data Player = Player { pId :: Int, pPos :: Position, pBombStock :: Int, pBombRange :: Int } 
    deriving Show 

data Bomb = Bomb { bOwnerId :: Int, bPos :: Position, bTimeLeft :: Int , bBombRange :: Int} 
    deriving Show 

をリファクタリングしようとすると、任意のが必要ですこの作業を行うための言語拡張、または(設計によって)実行することはできません。

答えて

2

同じモジュールにPlayerという2つのコンストラクタがあり、Bombと同じものがあります。あなたはそれらのいずれかにコンストラクタを明確にするために、このような接頭辞を与えることができる:

data Entity = EPlayer | EBomb 
    deriving Show 

をしかし、私はあなたのEntityは、あなたがそれをやりたいかなり何やっているとは思いません。 Entityのこれらの2つのコンストラクタはデータを持たず、PlayerBombのデータ型とは関係しません。あなたはEntityコンストラクタがデータを運ぶしたい場合は、あなたがそのように定義することができます。

data Entity = EPlayer Player | EBomb Bomb 
    deriving Show 

しかし、それまでには、あなたが開始したのと同じ位置に一種のです。このリファクタリングによって正確に何を達成しようとしていますか?

+0

私は、「エンティティ」はコンストラクタノイズが少ないために読みやすいと思います。しかし、これは私の古い作業コードよりも 'Entity'のパターンマッチングを難しくしますか?その場合、私は働いているものを使用します。 – wizzup

+1

別々の 'Entity'型に移動すると、ペイロードを取得するためにどこでも' EPlayer'と 'Player'にパターンマッチする必要があります。 –

関連する問題