Haskell Cardguess Game

前端 未结 2 1709
鱼传尺愫
鱼传尺愫 2021-01-27 04:17

I am writing a Cardguess Game, where user sets the answer and lets the PC guess. For my project, the input card number is 2.

Then, I need to write the cardguess algorit

相关标签:
2条回答
  • 2021-01-27 04:38

    As I don't understand your question, and you are a beginner, I am going to nitpick on some other areas of your post.

    1. You have

      initialGuess card_number 
           | card_number == 2   = ((Card Club R5):(Card Heart J),cardDeck)
      

      Accepting that you only need initialGuess 2 to work for now, you have the list syntax wrong. You mean either

      initialGuess card_number 
           | card_number == 2   = (Card Club R5 : Card Heart J : [], cardDeck)
      

      or

      initialGuess card_number 
           | card_number == 2   = ([Card Club R5, Card Heart J], cardDeck)
      

      They mean the same thing, but the latter is clearer.

    2. You have

      instance Ord Card where
          compare (Card s1 r1) (Card s2 r2) =
              let suitorder = compare s1 s2
                  in  if suitorder == EQ then compare r1 r2 else suitorder
      

      If you import Data.Monoid, you can write this more simply as

      instance Ord Card where
          compare (Card s1 r1) (Card s2 r2) = compare s1 s2 `mappend` compare r1 r2
      

      But better is to leave it out and ask the compiler to write it for you instead:

      data Card = Card {suit::Suit, rank::Rank}
            deriving (Eq, Ord, Bounded)
      

      The generated Ord instance uses lexicographical ordering, as you require.

    0 讨论(0)
  • 2021-01-27 04:47

    Hard to read your mind here, but maybe you want something like this:

    startGuessing guesses state = do
      feedBack <- readFeedback
      let (newGuess, newState) = nextGuess (guesses, state) feedback
      startGuessing (guesses ++ newGuess) newState
    
    main = do
      let (guesses, state) = initialGuess 2      
      startGuessing guesses state
    
    0 讨论(0)
提交回复
热议问题