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