Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5901cb5

Browse files
authored
Merge pull request MicrosoftDocs#1 from softchris/sample-code
adding sample code
2 parents 28f7e6c + 5e1f31b commit 5901cb5

File tree

5 files changed

+291
-0
lines changed

5 files changed

+291
-0
lines changed

rock-paper-scissor-0.py

Whitespace-only changes.

rock-paper-scissor-1.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from typing import ClassVar
2+
3+
4+
class Participant:
5+
def __init__(self, name):
6+
self.name = name
7+
self.points = 0
8+
self.choice = ""
9+
def choose(self):
10+
self.choice = input("{name}, select rock, paper or scissor: ".format(name= self.name))
11+
print("{name} selects {choice}".format(name=self.name, choice = self.choice))
12+
def toNumericalChoice(self):
13+
switcher = {
14+
"rock": 0,
15+
"paper": 1,
16+
"scissor": 2
17+
}
18+
return switcher[self.choice]
19+
20+
class GameRound:
21+
def __init__(self, p1, p2):
22+
self.rules = [
23+
[0, -1, 1],
24+
[1, 0, -1],
25+
[-1, 1, 0]
26+
]
27+
28+
p1.choose()
29+
p2.choose()
30+
result = self.compareChoices(p1,p2)
31+
print("Round resulted in a {result}".format(result = self.getResultAsString(result) ))
32+
def compareChoices(self, p1, p2):
33+
return self.rules[p1.toNumericalChoice()][p2.toNumericalChoice()]
34+
def awardPoints(self):
35+
print("implement")
36+
def getResultAsString(self, result):
37+
res = {
38+
0: "draw",
39+
1: "win",
40+
-1: "loss"
41+
}
42+
return res[result]
43+
44+
class Game:
45+
def __init__(self):
46+
self.endGame = False
47+
self.participant = Participant("Spock")
48+
self.secondParticipant = Participant("Kirk")
49+
def start(self):
50+
game_round = GameRound(self.participant, self.secondParticipant)
51+
52+
def checkEndCondition(self):
53+
print("implement")
54+
def determineWinner(self):
55+
print("implement")
56+
57+
game = Game()
58+
game.start()

rock-paper-scissor-2.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from typing import ClassVar
2+
3+
4+
class Participant:
5+
def __init__(self, name):
6+
self.name = name
7+
self.points = 0
8+
self.choice = ""
9+
def choose(self):
10+
self.choice = input("{name}, select rock, paper or scissor: ".format(name= self.name))
11+
print("{name} selects {choice}".format(name=self.name, choice = self.choice))
12+
def toNumericalChoice(self):
13+
switcher = {
14+
"rock": 0,
15+
"paper": 1,
16+
"scissor": 2
17+
}
18+
return switcher[self.choice]
19+
def incrementPoint(self):
20+
self.points += 1
21+
22+
class GameRound:
23+
def __init__(self, p1, p2):
24+
self.rules = [
25+
[0, -1, 1],
26+
[1, 0, -1],
27+
[-1, 1, 0]
28+
]
29+
30+
p1.choose()
31+
p2.choose()
32+
result = self.compareChoices(p1,p2)
33+
print("Round resulted in a {result}".format(result = self.getResultAsString(result) ))
34+
if result > 0:
35+
p1.incrementPoint()
36+
elif result < 0:
37+
p2.incrementPoint()
38+
39+
def compareChoices(self, p1, p2):
40+
return self.rules[p1.toNumericalChoice()][p2.toNumericalChoice()]
41+
def awardPoints(self):
42+
print("implement")
43+
def getResultAsString(self, result):
44+
res = {
45+
0: "draw",
46+
1: "win",
47+
-1: "loss"
48+
}
49+
return res[result]
50+
51+
class Game:
52+
def __init__(self):
53+
self.endGame = False
54+
self.participant = Participant("Spock")
55+
self.secondParticipant = Participant("Kirk")
56+
def start(self):
57+
game_round = GameRound(self.participant, self.secondParticipant)
58+
59+
def checkEndCondition(self):
60+
print("implement")
61+
def determineWinner(self):
62+
print("implement")
63+
64+
game = Game()
65+
game.start()

rock-paper-scissor-3.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from typing import ClassVar
2+
3+
4+
class Participant:
5+
def __init__(self, name):
6+
self.name = name
7+
self.points = 0
8+
self.choice = ""
9+
def choose(self):
10+
self.choice = input("{name}, select rock, paper or scissor: ".format(name= self.name))
11+
print("{name} selects {choice}".format(name=self.name, choice = self.choice))
12+
def toNumericalChoice(self):
13+
switcher = {
14+
"rock": 0,
15+
"paper": 1,
16+
"scissor": 2
17+
}
18+
return switcher[self.choice]
19+
def incrementPoint(self):
20+
self.points += 1
21+
22+
class GameRound:
23+
def __init__(self, p1, p2):
24+
self.rules = [
25+
[0, -1, 1],
26+
[1, 0, -1],
27+
[-1, 1, 0]
28+
]
29+
30+
p1.choose()
31+
p2.choose()
32+
result = self.compareChoices(p1,p2)
33+
print("Round resulted in a {result}".format(result = self.getResultAsString(result) ))
34+
if result > 0:
35+
p1.incrementPoint()
36+
elif result < 0:
37+
p2.incrementPoint()
38+
else:
39+
print("No points for anybody")
40+
41+
def compareChoices(self, p1, p2):
42+
return self.rules[p1.toNumericalChoice()][p2.toNumericalChoice()]
43+
def awardPoints(self):
44+
print("implement")
45+
def getResultAsString(self, result):
46+
res = {
47+
0: "draw",
48+
1: "win",
49+
-1: "loss"
50+
}
51+
return res[result]
52+
53+
class Game:
54+
def __init__(self):
55+
self.endGame = False
56+
self.participant = Participant("Spock")
57+
self.secondParticipant = Participant("Kirk")
58+
def start(self):
59+
while not self.endGame:
60+
GameRound(self.participant, self.secondParticipant)
61+
self.checkEndCondition()
62+
63+
def checkEndCondition(self):
64+
answer = input("Continue game y/n: ")
65+
if answer == 'y':
66+
GameRound(self.participant, self.secondParticipant)
67+
self.checkEndCondition()
68+
else:
69+
print("Game ended, {p1name} has {p1points}, and {p2name} has {p2points}".format(p1name = self.participant.name, p1points= self.participant.points, p2name=self.secondParticipant.name, p2points=self.secondParticipant.points))
70+
self.determineWinner()
71+
self.endGame = True
72+
def determineWinner(self):
73+
resultString = "It's a Draw"
74+
if self.participant.points > self.secondParticipant.points:
75+
resultString = "Winner is {name}".format(name=self.participant.name)
76+
elif self.participant.points < self.secondParticipant.points:
77+
resultString = "Winner is {name}".format(name=self.secondParticipant.name)
78+
79+
print(resultString)
80+
81+
game = Game()
82+
game.start()

rock-paper-scissor-4.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from typing import ClassVar
2+
3+
4+
class Participant:
5+
def __init__(self, name):
6+
self.name = name
7+
self.points = 0
8+
self.choice = ""
9+
def choose(self):
10+
self.choice = input("{name}, select rock, paper, scissor, lizard or spock: ".format(name= self.name))
11+
print("{name} selects {choice}".format(name=self.name, choice = self.choice))
12+
def toNumericalChoice(self):
13+
switcher = {
14+
"rock": 0,
15+
"paper": 1,
16+
"scissor": 2,
17+
"lizard": 3,
18+
"spock": 4
19+
}
20+
return switcher[self.choice]
21+
def incrementPoint(self):
22+
self.points += 1
23+
24+
class GameRound:
25+
def __init__(self, p1, p2):
26+
self.rules = [
27+
[0, -1, 1, 1, -1],
28+
[1, 0, -1, -1, 1],
29+
[-1, 1, 0, 1, -1],
30+
[-1, 1, -1, 0, 1],
31+
[1, -1, 1, -1, 0]
32+
]
33+
34+
p1.choose()
35+
p2.choose()
36+
result = self.compareChoices(p1,p2)
37+
print("Round resulted in a {result}".format(result = self.getResultAsString(result) ))
38+
if result > 0:
39+
p1.incrementPoint()
40+
elif result < 0:
41+
p2.incrementPoint()
42+
else:
43+
print("No points for anybody")
44+
45+
def compareChoices(self, p1, p2):
46+
return self.rules[p1.toNumericalChoice()][p2.toNumericalChoice()]
47+
def awardPoints(self):
48+
print("implement")
49+
def getResultAsString(self, result):
50+
res = {
51+
0: "draw",
52+
1: "win",
53+
-1: "loss"
54+
}
55+
return res[result]
56+
57+
class Game:
58+
def __init__(self):
59+
self.endGame = False
60+
self.participant = Participant("Spock")
61+
self.secondParticipant = Participant("Kirk")
62+
def start(self):
63+
while not self.endGame:
64+
GameRound(self.participant, self.secondParticipant)
65+
self.checkEndCondition()
66+
67+
def checkEndCondition(self):
68+
answer = input("Continue game y/n: ")
69+
if answer == 'y':
70+
GameRound(self.participant, self.secondParticipant)
71+
self.checkEndCondition()
72+
else:
73+
print("Game ended, {p1name} has {p1points}, and {p2name} has {p2points}".format(p1name = self.participant.name, p1points= self.participant.points, p2name=self.secondParticipant.name, p2points=self.secondParticipant.points))
74+
self.determineWinner()
75+
self.endGame = True
76+
def determineWinner(self):
77+
resultString = "It's a Draw"
78+
if self.participant.points > self.secondParticipant.points:
79+
resultString = "Winner is {name}".format(name=self.participant.name)
80+
elif self.participant.points < self.secondParticipant.points:
81+
resultString = "Winner is {name}".format(name=self.secondParticipant.name)
82+
83+
print(resultString)
84+
85+
game = Game()
86+
game.start()

0 commit comments

Comments
 (0)