1

Haskell を使用して Paper Scissors Stone のゲームを作成し、理解を深めようとしています。

残念ながら、以下のソース コードは望ましくない回答を提供します。

例えば:

>play pStone pRandom 1
1 games were played. Player 1 won 1 and player 2 won 1, making the match a draw.

1 ゲームをプレイした場合、1 勝か 0 勝しかないはずです。

>play pStone pCopy 100
100 games were played. Player 1 won 1 and player 2 won 1, making the match a draw.

100 ゲームがプレイされ、(最初のラウンドの後) 両方が同じ手でプレイされた場合、1 勝または 0 勝のいずれかしかないはずです。

>play pCopy pAntiCopy 100
100 games were played. Player 1 won 31 and player 2 won 37, making player 2 the overall winner.

pCopy と pAntiCopy の意図した定義により、pAntiCopy は 99 または 100 のいずれかを獲得し、pCopy は 0 または 1 を獲得する必要があります。

この動作の最も可能性の高い理由は、乱数が最後に評価されるためだと思います。つまり、同じ乱数に依存するはずの 2 つの値が、2 つの別々の乱数に依存することになります。上記で正しいですか?

私が正しければ、これをどのように修正すればよいか教えていただけますか? 間違っている場合は、何が問題で、どのように解決すればよいか教えていただけますか?

乱数のリストを生成してからそれらを使用し、メイン関数から関連する関数に引数として渡すことを提案する別の問題の解決策を読みました。ただし、必要な乱数の数は、使用されている計画に応じて 0 から 2*numRounds の範囲になる可能性があるため (これが機能している場合は、より高度な計画を作成するつもりです)、コードの可読性はさらに低下します。

私は Haskell と関数型プログラミング全般に不慣れなので、以下のソース コードのスタイルについてお詫び申し上げます。それを改善する方法について何か提案があれば、それも大歓迎です。

import System.Random

data Move= Paper|Scissors|Stone deriving Show
type Plan=([IO Move]->[IO Move]->IO Move)

play :: Plan -> Plan -> Integer -> IO ()
play plan1 plan2 numRounds=do p1intwins<-p1wins;p2intwins<-p2wins;putStr(show numRounds ++ " games were played. Player 1 won " ++ show p1intwins ++ " and player 2 won " ++ show p2intwins ++ ", making " ++ (if p1intwins > p2intwins then "player 1 the overall winner." else (if p1intwins < p2intwins then "player 2 the overall winner." else "the match a draw."))) where (_, _, _, _, _, _, p1wins, p2wins)=(playRound (plan1, plan2, numRounds,[],[], 0, return 0, return 0))

playRound :: (Plan, Plan, Integer, [IO Move], [IO Move], Integer, IO Integer, IO Integer) -> (Plan, Plan, Integer, [IO Move], [IO Move], Integer, IO Integer, IO Integer)
playRound (plan1, plan2, numRounds, p1moves, p2moves, elapsedRounds, p1wins, p2wins)=if elapsedRounds==numRounds then (plan1, plan2, numRounds, p1moves, p2moves, elapsedRounds, p1wins, p2wins) else (playRound (plan1, plan2, numRounds, p1moves++[p1move], p2moves++[p2move], elapsedRounds+1, do p1win<-beatsCaller p1move p2move;p1intwins<-p1wins;return (p1intwins+if p1win then 1 else 0), do p2win<-beatsCaller p2move p1move;p2intwins<-p2wins;return(p2intwins+(if p2win then 1 else 0)) )) where p1move=plan1 p1moves p2moves; p2move=plan2 p2moves p1moves

beatsCaller :: IO Move -> IO Move -> IO Bool
beatsCaller iom1 iom2=do m1<-iom1;m2<-iom2;return(beats m1 m2)

beats :: Move -> Move -> Bool
beats Scissors Paper=True
beats Stone Scissors=True
beats Paper Stone=True
beats _ _=False

--                                           ###############Plans###################
pStone :: Plan
pStone _ _ = return Stone

pScissors :: Plan
pScissors _ _ = return Scissors

pPaper :: Plan
pPaper _ _ = return Paper

pUScissors :: Plan
pUScissors [] _ = randomMove
pUScissors _ _ = return Scissors

pCopy :: Plan
pCopy _ []= randomMove
pCopy _ theirMoves= last theirMoves

pRandom :: Plan
pRandom _ _=randomMove

pAntiCopy :: Plan
pAntiCopy [] _ = randomMove
pAntiCopy ourMoves _ = do ourMove<-last ourMoves;return(beaterOf ourMove)

--                                           ##############Plan Logic###############

beaterOf ::  Move -> Move
beaterOf Scissors = Stone
beaterOf Paper = Scissors
beaterOf Stone = Paper

randomMove :: IO Move
randomMove=do x<-genRandom;return (doRMove x)

doRMove:: Int -> Move
doRMove rand
    |rand==1 =Paper
    |rand==2 =Scissors
    |rand==3 =Stone

genRandom :: IO Int
genRandom =getStdRandom (randomR (1,3))
4

2 に答える 2

6

ソースファイルを少し再フォーマットし、注釈を付けました。重要な点は、以下で説明するPlanように、 の型を に変更する必要があるということです。[Move] -> [Move] -> IO Move

import System.Random

data Move = Paper | Scissors | Stone
  deriving (Show, Eq, Enum, Bounded)

-- Make Move an instance of Random, so that we can use randomIO
-- in order to pick a random Move rather than a hand-written
-- function. We use the derived Enum instance and integer random
-- numbers to do the hard work for us.
instance Random Move where
  randomR (l, u) g = (toEnum n, g')
    where (n, g') = randomR (fromEnum l, fromEnum u) g
  random = randomR (minBound, maxBound)

-- Unchanged, just realigned.
beaterOf :: Move -> Move
beaterOf Scissors = Stone
beaterOf Paper    = Scissors
beaterOf Stone    = Paper

-- Reimplemented in terms of beaterOf, to avoid unnecessary
-- duplication of error-prone information.
beats :: Move -> Move -> Bool
beats x y = x == beaterOf y

-- Most important change. A plan is a strategy that takes two
-- lists of past moves. These are of type Move, not IO Move, as
-- they have already happened. We then have to determine a new
-- one. Here, to choose the next, we allow IO (such as picking
-- a random number, or asking a human player). I also reverse
-- the order of moves, so that the most recent moves are first,
-- because most strategies access the most recent move, and
-- accessing the head is more efficient in a list.
type Plan =  [Move]  -- my moves, latest move first
          -> [Move]  -- opponent's moves, latest move first
          -> IO Move -- my next move, may involve IO

--
-- Specific plans (slightly renamed, otherwise unchanged):
--

-- Always plays a particular move.
always :: Move -> Plan
always m _ _ = return m

-- Copies the latest move of opponent.
copy :: Plan
copy _ []           = randomIO
copy _ (latest : _) = return latest

randomly :: Plan
randomly _ _ = randomIO

-- Moves to the beater of our previous move.
antiCopy :: Plan
antiCopy []           _ = randomIO
antiCopy (latest : _) _ = return (beaterOf latest)

uScissors :: Plan
uScissors [] _ = randomIO
uScissors _  _ = return Scissors

-- Play wrapper. Interface as before.
play :: Plan    -- my plan
     -> Plan    -- opponent's plan
     -> Integer -- number of rounds to be played
     -> IO ()   -- output is printed as text
play myPlan opPlan rounds = do
  (myWins, opWins) <- playRounds
                        myPlan opPlan rounds
                        [] [] -- initialize with empty move lists
                        0 0   -- and 0 wins each
  -- print statistics
  let overallStatus | myWins > opWins = "Player 1 the overall winner"
                    | opWins > myWins = "Player 2 the overall winner"
                    | otherwise       = "the match a draw"
  putStrLn $ show rounds ++ " games were played. "
          ++ "Player 1 won " ++ show myWins ++ " and "
          ++ "Player 2 won " ++ show opWins ++ ", making "
          ++ overallStatus ++ "."

-- Does all the hard work.
playRounds :: Plan    -- my plan
           -> Plan    -- opponent's plan
           -> Integer -- number of rounds still to be played
           -> [Move]  -- our moves so far, latest first
           -> [Move]  -- opponent's moves so far, latest first
           -> Int     -- my wins
           -> Int     -- opponent's wins
           -> IO (Int, Int)  -- final wins
playRounds _      _      0      _       _       myWins opWins =
  return (myWins, opWins) -- if no rounds are left to play, return the final statistics
playRound myPlan opPlan rounds myMoves opMoves myWins opWins = do
  myMove <- myPlan myMoves opMoves -- here's where a random number might get chosen
  opMove <- opPlan opMoves myMoves -- and here again (but nowhere else)
  let myWin = myMove `beats` opMove -- this works on the already chosen Move, not on IO Move
      opWin = opMove `beats` myMove -- dito
  playRound
    myPlan opPlan      -- as before
    (rounds - 1)       -- one round is played
    (myMove : myMoves) -- adding moves in front of the list is easier
    (opMove : opMoves)
    (fromEnum myWin + myWins) -- update win count, turning True to 1 and False to 0
    (fromEnum opWin + opWins)
于 2012-12-11T19:43:08.207 に答える
4

この動作の最も可能性の高い理由は、乱数が最後に評価されるためだと思います。つまり、同じ乱数に依存するはずの 2 つの値が、2 つの別々の乱数に依存することになります。上記で正しいですか?

あなたは。反復中に実際にプレイしているのではなく、アクションを渡すIOため、戦略には構築するプレーンな s (前のラウンドの結果) はありませんがMove、 を生成するためのレシピMove(場合によっては実行genRandomして取得する必要があります) があります。 Move。_ そのたびに

ourMove<-last ourMoves

last ourMovesが含まれているgenRandom場合、新しい (疑似) 乱数が生成されます。多くの場合、以前の を生成したものとは異なりMoveます。

私が正しければ、これをどのように修正すればよいか教えていただけますか?

IOその use の周りにアクションとIO-actionsのリストを渡さないでくださいgenRandom。戦略を決定するには、純粋な値が必要です (pRandomと と の最初の選択pCopyを除くpAntiCopy)。

必要に応じて各ステップで疑似乱数生成を実行し、Move得られた純粋な s を次の反復に渡します。


また、短い行を使用し、空白を使用し、レイアウトを使用してコードを読みやすくします。

より慣用的なスタイルで書き直しますが、私はタイピングが遅いので、時間がかかる場合があります。

元の出力とロジックの多くを保持しているため、まだ少し雑然としていますが、読みやすく、従うのがはるかに簡単で、期待される出力が生成されます。

module Rocks where

import System.Random

data Move
    = Paper
    | Scissors
    | Stone
      deriving Show

type Plan = [Move] -> [Move] -> IO Move

play :: Plan -> Plan -> Int -> IO ()
play plan1 plan2 numRounds = do
    (wins1, wins2) <- playRounds plan1 plan2 numRounds [] [] 0 0
    putStrLn $ show numRounds ++ " games were played. Player 1 won "
                ++ show wins1 ++ " and player 2 won " ++ show wins2
                ++ ", making " ++ result wins1 wins2
  where
    result a b
        | a == b    = "the match a draw."
        | otherwise = "player " ++ (if a > b then "1" else "2") ++ " the overall winner."

playRounds :: Plan -> Plan -> Int -> [Move] -> [Move] -> Int -> Int -> IO (Int, Int)
playRounds _     _     0      _      _      wins1 wins2 = return (wins1,wins2)
playRounds plan1 plan2 rounds moves1 moves2 wins1 wins2 = do
    choice1 <- plan1 moves1 moves2
    choice2 <- plan2 moves2 moves1
    let (w1, w2)
            | beats choice1 choice2 = (wins1+1 ,wins2)
            | beats choice2 choice1 = (wins1, wins2+1)
            | otherwise             = (wins1, wins2)
    playRounds plan1 plan2 (rounds-1) (moves1 ++ [choice1]) (moves2 ++ [choice2]) w1 w2

beats :: Move -> Move -> Bool
beats Scissors  Paper       = True
beats Stone     Scissors    = True
beats Paper     Stone       = True
beats _         _           = False

--       ###############Plans###################

pStone :: Plan
pStone _ _ = return Stone

pScissors :: Plan
pScissors _ _ = return Scissors

pPaper :: Plan
pPaper _ _ = return Paper

pUScissors :: Plan
pUScissors [] _ = randomMove
pUScissors _  _ = return Scissors

pCopy :: Plan
pCopy _ []         = randomMove
pCopy _ theirMoves = return $ last theirMoves

pRandom :: Plan
pRandom _ _ = randomMove

pAntiCopy :: Plan
pAntiCopy []       _ = randomMove
pAntiCopy ourMoves _ = return (beaterOf $ last ourMoves)

--       ##############Plan Logic###############

beaterOf ::  Move -> Move
beaterOf Scissors   = Stone
beaterOf Paper      = Scissors
beaterOf Stone      = Paper

randomMove :: IO Move
randomMove = fmap doRMove genRandom

doRMove:: Int -> Move
doRMove rand
    | rand == 1 = Paper
    | rand == 2 = Scissors
    | rand == 3 = Stone
    | otherwise = error "oops"

genRandom :: IO Int
genRandom = getStdRandom (randomR (1,3))

*Rocks> play pCopy pAntiCopy 100
100 games were played. Player 1 won 0 and player 2 won 99, making player 2 the overall winner.
*Rocks> play pStone pRandom 100
100 games were played. Player 1 won 33 and player 2 won 34, making player 2 the overall winner.
*Rocks> play pStone pCopy 100
100 games were played. Player 1 won 1 and player 2 won 0, making player 1 the overall winner.
*Rocks> play pStone pAntiCopy 100
100 games were played. Player 1 won 33 and player 2 won 33, making the match a draw.
于 2012-12-11T19:08:35.823 に答える