Welcome, guest | Sign In | My Account | Store | Cart

best of five against the computer

Python, 83 lines
 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
# The traditional paper scissors rock game
# best of five
import os
def clear():
   os.system("clear")
clear()
print "\n\nPaper, Rock, Scissors Game -(Best of five games)"
x = 0 ;  l = 0 ;  w = 0 ; d = 0 ; lt = 0 ; wt = 0 ; dt = 0
while x < 5:
  x = x + 1
  import random
  class Computer:
         pass
  comp_is = Computer()
  comp_is.opt = ('r','p','s')
  comp_is.rand = random.choice(comp_is.opt)

  if comp_is.rand == 'r':
            comp  = 'rock'
  elif comp_is.rand == 'p':
            comp  = 'paper'
  else:
        comp  = 'scissors'

  class Human:
       pass
  human_is = Human
  print
  human_is.player = raw_input(' Enter your choice of\n   r\
 for rock\n   p for paper or\n   s for scissors ... ')
  print
    
  class Result:
     pass
  Result_is = Result
  if comp_is.rand == human_is.player:
    print "draw - computer chose ",  comp
    print
    d = d + 1
    dt = dt + 1
  elif comp_is.rand == 'r' and human_is.player == 'p':
      print "  player beats computer -computer chose ",  comp
      print
      w = w + 1
      wt = wt + 1
  elif comp_is.rand == 'p' and human_is.player == 's':
      print "  computer chose ",  comp
      print "  player beats computer-because scissors cuts paper"
      print
      w = w + 1
      wt = wt + 1
  elif comp_is.rand == 's' and human_is.player == 'r':
     print " computer chose ", comp  
     print " player beats computer-because rock breaks scissors"
     w = w + 1
     wt = wt + 1
  else :
     print "   computer wins - computer chose  ", comp
     print
     l = l + 1
     lt = lt + 1
     
  if x == 5:
    print 
    print    
    print "  games  won ... ",  w
    print "  games lost ... ",  l
    print "  games drawn ... ",  d
    print 
    print "  Running total overall of games won ... ", wt
    print "  Running total overall of games lost ... ", lt
    print "  Running total overall of games drawn ... ", dt
    print 
    w = 0 ; l = 0 ; d = 0
    again = raw_input('Do you want to play again y for yes, n for no ..  ')
    if again == 'y':
       x = 0
    else:
      print 
      if lt > wt:
         print "You lost the game ha! ha!"
         print
         print 'finish'

2 comments

Canadian Programmer 13 years, 8 months ago  # | flag

Here's a way to create the same functionality with less code. Added the expansion "lizard, spock".

# The traditional paper scissors rock game w/ Lizard Spock Expansion
# best of five
import os
def clear():
os.system("clear")
clear()
print "\n\nPaper, Rock, Scissors, Lizard, Spock Game -(Best of five games)"
x = 0 ;  l = 0 ;  w = 0 ; d = 0 ; lt = 0 ; wt = 0 ; dt = 0
import random
if __name__ == "__main__":
    while x < 5:
        x = x + 1
        results = {('p','r'): ('Paper Covers Rock - Computer Wins', 0), ('r','p'): ('Paper Covers Rock - Player Wins', 1),
                ('s','p'): ('Scissors Cuts Paper - Computer Wins', 0), ('p','s'): ('Scissors Cuts Paper - Player Wins', 1),
                ('r','s'): ('Rock Smashes Scissors - Computer Wins', 0), ('s','r'): ('Rock Smashes Scissors - Player Wins', 1),
                ('l','p'): ('Lizard Eats Paper - Computer Wins', 0), ('p','l'): ('Lizard Eats Paper - Player Wins', 1),
                ('r','l'): ('Rock Squashes Lizard - Computer Wins', 0), ('l','r'): ('Rock Squashes Lizard - Player Wins', 1),
                ('s','l'): ('Scissors Decapitates Lizard - Computer Wins', 0), ('l','s'): ('Scissors Decapitates Lizard - Player Wins', 1),
                ('p','v'): ('Paper Disproves Spock - Computer Wins', 0), ('v','p'): ('Paper Disproves Spock - Player Wins', 1),
                ('l','v'): ('Lizard Poisons Spock - Computer Wins', 0), ('v','l'): ('Lizard Poisons Spock - Player Wins', 1),
                ('v','r'): ('Spock Vaporizes Rock - Computer Wins', 0), ('r','v'): ('Spock Vaporizes Rock - Player Wins', 1),
                ('v','s'): ('Spock Smashes Scissors - Computer Wins', 0), ('s','v'): ('Spock Smashes Scissors - Player Wins', 1)}

        comp_is = ('r','p','s','l','v')
        comp_is = random.choice(comp_is)

        human_is = raw_input(' Enter your choice of\n   r for rock\n   p for paper or\n   s for scissors or\n   l for lizard or\n   v for spock ... ')
        print
        if comp_is == human_is:
            print "Draw"
            d = d + 1
            dt = dt + 1
        elif results[comp_is,human_is][1]:
            print "Player Beats Computer: " + results[comp_is,human_is][0]
            print
            w = w + 1
            wt = wt + 1
        else:
            print "Computer Beats Player: " + results[comp_is,human_is][0]
            print
            l = l + 1
            lt = lt + 1
Canadian Programmer 13 years, 8 months ago  # | flag

cont...

 if x == 5:
    print 
    print    
    print "  games  won ... ",  w
    print "  games lost ... ",  l
    print "  games drawn ... ",  d
    print 
    print "  Running total overall of games won ... ", wt
    print "  Running total overall of games lost ... ", lt
    print "  Running total overall of games drawn ... ", dt
    print 
    w = 0 ; l = 0 ; d = 0
    again = raw_input('Do you want to play again y for yes, n for no ..  ')
    if again == 'y':
       x = 0
    else:
      print 
      if lt > wt:
         print "You lost the game ha! ha!"
         print
         print 'finish'