Порядок в коде игры
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
forgivingStrategy :: (Ord t, Num t) => [Char] -> [Char] -> t -> [Char]
|
||||
forgivingStrategy :: [Char] -> [Char] -> Int -> [Char]
|
||||
forgivingStrategy opponentMoves generatedMoves n
|
||||
| n > 100 || length opponentMoves == 1 = generatedMoves
|
||||
| n == 0 = forgivingStrategy opponentMoves (generatedMoves ++ ['С']) (n + 1)
|
||||
| head opponentMoves == 'С' = forgivingStrategy (tail opponentMoves) (generatedMoves ++ ['С']) (n + 1)
|
||||
| otherwise = forgivingStrategy (tail opponentMoves) (generatedMoves ++ ['П']) (n + 1)
|
||||
|
||||
| n == 0
|
||||
= forgivingStrategy opponentMoves (generatedMoves ++ ['С']) (n + 1)
|
||||
| head opponentMoves == 'С'
|
||||
= forgivingStrategy (tail opponentMoves) (generatedMoves ++ ['С']) (n + 1)
|
||||
| otherwise
|
||||
= forgivingStrategy (tail opponentMoves) (generatedMoves ++ ['П']) (n + 1)
|
||||
|
||||
indexOf :: (Eq t) => [t] -> t -> Int
|
||||
indexOf [] _ = -1
|
||||
@@ -12,11 +14,11 @@ indexOf (x : xs) target
|
||||
| x == target = 0
|
||||
| otherwise = 1 + indexOf xs target
|
||||
|
||||
nashStrategy :: (Num t, Ord t) => [a] -> [Char] -> t -> [Char]
|
||||
nashStrategy opponentMoves generatedMoves n =
|
||||
nashEquilibriumStrategy :: [a] -> [Char] -> Int -> [Char]
|
||||
nashEquilibriumStrategy opponentMoves generatedMoves n =
|
||||
if n <= 100 && length opponentMoves > 0
|
||||
then
|
||||
nashStrategy (tail opponentMoves) (generatedMoves ++ [nextStep]) (n + 1)
|
||||
nashEquilibriumStrategy (tail opponentMoves) (generatedMoves ++ [nextStep]) (n + 1)
|
||||
else generatedMoves
|
||||
where
|
||||
cases = [['П', 'С'], ['П', 'П']]
|
||||
@@ -38,16 +40,14 @@ getScore moves1 moves2 score1 score2 =
|
||||
newScore2 = score2 + results !! indexOf cases [head moves1, head moves2] !! 1
|
||||
|
||||
|
||||
game :: [Char] -> Int -> [Char]
|
||||
game :: [Char] -> ([Char] -> [Char] -> Int -> [Char]) -> [Char]
|
||||
game playerMoves gameStrategy
|
||||
| null playerMoves = []
|
||||
| gameStrategy == 0 = forgivingStrategy playerMoves [] 0
|
||||
| gameStrategy == 1 = nashStrategy playerMoves [] 0
|
||||
| otherwise = []
|
||||
| otherwise = gameStrategy playerMoves [] 0
|
||||
|
||||
|
||||
playerMoves = ['С', 'П', 'С', 'С', 'П', 'П', 'С', 'С', 'П', 'П']
|
||||
gameStrategy = 0
|
||||
gameStrategy = forgivingStrategy -- forgivingStrategy, nashEquilibriumStrategy
|
||||
computerMoves = game playerMoves gameStrategy
|
||||
(score1, score2) = getScore playerMoves computerMoves 0 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user