forked from geebzter/game-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRPSPlayerExample.py
More file actions
115 lines (75 loc) · 3.17 KB
/
Copy pathRPSPlayerExample.py
File metadata and controls
115 lines (75 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
__author__ = 'geebzter'
# Example of how to implement an RPS player within the framework
import Player
import Message
class RPSPlayerExample(Player.Player):
def __init__(self):
# Call super class constructor
Player.Player.__init__(self)
self.reset()
def play(self):
return RpsPlayingStrategy.play(self.opponents_moves)
def reset(self):
self.opponents_moves = []
def get_name(self):
return "Minimizer"
def notify(self, msg):
# We use notifications to store opponent's moves in past rounds
# Process match-start and round-end messages
# At the start of the match, clear opponent moves history since a new match has started
# At the end of a round, append move to opponent's move history. Move history is used
# to compute the next move played.
if msg.is_match_start_message():
players = msg.get_players()
if players[0] == self or players[1] == self:
self.reset()
elif msg.is_round_end_message():
players = msg.get_players()
# Check if this message is for me and only then proceed
if (players[0] == self) or (players[1] == self):
# In this case, (by convention) the info is a tuple of the moves made and result e.g. ((1, 0), 1) which
# means player 1 played paper (1), the player 2 played rock(0) and the result was that
# player 1 won
moves, result = msg.get_info()
# RPS is a two person game; figure out which of the players is me
# and which one is the opponent
if players[0] == self:
opponent = 1
else:
opponent = 0
# Update opponent's past moves history
self.opponents_moves.append(moves[opponent])
# An implementation of a simple rps playing strategy
class RpsPlayingStrategy(object):
@staticmethod
def play(opponents_moves):
# Implements some way of predicting what the opponent might do next
# and play accordingly.
# For instance, assume he is going to play the move he has played the
# least.
# count number of rock, paper and scissors moves made in the past
count = [0, 0, 0]
for move in opponents_moves:
count[move] += 1
if count[0] < count[1]:
least = 0
else:
least = 1
if count[least] > count[2]:
least = 2
# Assuming that opponent's move is going to be the value of least, play to beat it
return (least + 1) % 3
# Test driver
# Run by typing "python3 RpsPlayerExample.py"
if __name__ == "__main__":
player = RPSPlayerExample()
opponent = RPSPlayerExample()
players = [opponent,player]
fakeinfo = ((0,1),1)
fakeresult = 1
fakemoves = (1,2)
player.notify(Message.Message.get_match_start_message(players))
player.notify(Message.Message.get_round_start_message(players))
move = player.play()
print ("Move played: ", move)
player.notify(Message.Message.get_round_end_message(players,fakemoves,fakeresult))