repl.it
@TonySiu/

rock paper scissors

Python

No description

fork
loading
Files
  • main.py
main.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
@ Clever Programmer
Rock paper scissors
"""
import random
import simplegui


# Global variables that all functions know about.
# DO NOT EDIT THESE GLOBAL VARIABLES
# OR YOUR GAME WILL BREAK.
COMPUTER_SCORE = 0
HUMAN_SCORE = 0
human_choice = ""
computer_choice = ""


def choice_to_number(choice):
    """Convert choice to number."""
    # If choice is 'rock', give me 0
    # If choice is 'paper', give me 1
    # If choice is 'scissors', give me 2
    if choice is 'rock':
        return 0
    elif choice is 'paper':
        return 1
    elif choice is 'scissors':
        return 2

#choice {

def number_to_choice(number):
    """Convert number to choice."""
    # If number is 0, give me 'rock'
    # If number is 1, give me 'paper'
    # If number is 2, give me 'scissors'
    if number is 0:
        return 'rock'
    elif number is 1:
        return 'paper'
    elif number is 2:
        return 'scissors'



def random_computer_choice():
    """Choose randomly for computer."""
    seq = ['rock', 'paper', 'scissors']
    return random.choice(seq)
    # lookup random.choice()

def choice_result(human_choice, computer_choice):
    """Return the result of who wins."""

        # DO NOT REMOVE THESE GLOBAL VARIABLE LINES.
    global COMPUTER_SCORE
    global HUMAN_SCORE

        # based on the given human_choice and computer_choice
        # determine who won and increment their score by 1.
        # if tie, then don't increment anyone's score.

        # example code
        # if human_choice == 'rock' and computer_choice == 'paper':
        #    COMPUTER_SCORE = COMPUTER_SCORE + 1
    if human_choice == 'rock' and computer_choice == 'scissors':
        HUMAN_SCORE += 1
    elif human_choice == 'paper' and computer_choice == 'rock':
        HUMAN_SCORE += 1
    elif human_choice == 'scissors' and computer_choice == 'paper':
        HUMAN_SCORE += 1
    elif computer_choice == 'rock' and human_choice == 'scissors':
        COMPUTER_SCORE += 1
    elif computer_choice == 'paper' and human_choice== 'rock':
        COMPUTER_SCORE+= 1
    elif computer_choice == 'scissors' and human_choice == 'paper':
        COMPUTER_SCORE+= 1
    elif human_choice == computer_choice:
        print('draw')
    return COMPUTER_SCORE, HUMAN_SCORE





# DO NOT REMOVE THESE TEST FUNCTIONS.
# They will test your code.
def test_choice_to_number():
    assert choice_to_number('rock') == 0
    assert choice_to_number('paper') == 1
    assert choice_to_number('scissors') == 2
    print('correct')
def test_number_to_choice():
    assert number_to_choice(0) == 'rock'
    assert number_to_choice(1) == 'paper'
    assert number_to_choice(2) == 'scissors'
    print('correct')
def test_all():
    test_choice_to_number()
    test_number_to_choice()

# Uncomment to test your functions.
test_all()


# # Handler for mouse click on rock button.
# # This code is for the GUI part of the game.
def rock():
    global human_choice, computer_choice
    global HUMAN_SCORE, COMPUTER_SCORE

    human_choice = 'rock'
    computer_choice = random_computer_choice()
    choice_result(computer_choice, human_choice)

def paper():
    global human_choice, computer_choice
    global HUMAN_SCORE, COMPUTER_SCORE

    human_choice = 'paper'
    computer_choice = random_computer_choice()
    choice_result(computer_choice, human_choice)

# # Handler for mouse click on paper button.
def scissors():
    global human_choice, computer_choice
    global HUMAN_SCORE, COMPUTER_SCORE

    human_choice = 'scissors'
    computer_choice = random_computer_choice()
    choice_result(computer_choice, human_choice)

# # Handler to draw on canvas
def draw(canvas):

    try:
        # Draw choices
        canvas.draw_text("You: " + human_choice, [10,40], 48, "Green")
        canvas.draw_text("Comp: " + computer_choice, [10,80], 48, "Red")

        # Draw scores
        canvas.draw_text("Human Score: " + str(HUMAN_SCORE), [10,150], 30, "Green")
        canvas.draw_text("Comp Score: " + str(COMPUTER_SCORE), [10,190], 30, "Red")

    except TypeError:
        pass


# # Create a frame and assign callbacks to event handlers
def play_rps():
    frame = simplegui.create_frame("Home", 300, 200)
    frame.add_button("Rock", rock)
    frame.add_button("Paper", paper)
    frame.add_button("Scissors", scissors)
    frame.set_draw_handler(draw)

    # Start the frame animation
    frame.start()

play_rps()

"""
[0, 1, 2]
[r, p, s]

rock vs scissors
(rock - scissors) % 3 == 1
(0 - 2) % 3 == 1
(-2) % 3 == 1
1 == 1
rock wins

paper vs scissors
(paper - paper) % 3 == 1
(1 - 2) % 3 == 1
2 == 1
scissor wins

paper vs rocks
(paper - rocks) % 3 == 1
(1 - 0) % 3 == 1
1 % 3 == 1
1 == 1
paper wins
"""