#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# filename: rpslc.py
# rev. date/time: 07-13-14-12:30 hrs.
# by Mr. Cantlin.
# modified from Cody Allen Haynes Python program
# ****************************************************
# Line 1 above is traditionally called a "shebang" line in Python.
# The #! symbols are the reason for the name.
# The # does not start a comment line in this first line.
# There should be no white space in the shebang line.
# The shebang tells the OS where to find the Python files.
# /usr/bin/python3 is where Python 3 is installed
# on the Bulldogmath.com server.
# It is not needed when you are running programs from IDLE.
# ****************************************************
# **************************************************************
# Install i.e., import CGI, math and random module support.
# The Python script is actually run as a .CGI program.
# It is changed from .PY to .CGI and put in the CGI folder.
# CGI=Common Gateway Interface, used for many scripting languages.
# Most Linux servers have a /cgi-bin/ folder for these programs.
# The Python interpreter on the server is smart enough to
# still know it is a Python program.
# Python needs to install i.e., import several CGI related modules.
# **************************************************************
# **************************************************************
# Install modules Python needs for this program. Don't forget "math".
import math
import random
import cgi
import cgitb
cgitb.enable() #enable debugging
random.seed() #randomize the random Class a bit using the system clock.
# **************************************************************
# **************************************************************
# Define and initialize variables
win = 0
lose = 0
draw = 0
result = 0
total_Games = 0
ratio = 0.0
user_Choice = ""
comp_Choice = ""
# **************************************************************
# ************HTML SECTION 1 of 3********************************
# Print to HTML requires this setup - it has THREE sections.
# SECTION 1: Two Lines. Separate this from Section 2 by printing a blank line.
print("Content-Type: text/html") # HTML is following
print() # blank line, end of headers
# **************************************************************
# ************HTML SECTION 2 of 3*******************************
# SECTION 2: Start Main HTML Output i.e. html framework
print("
")
print("Paper, Rock, Scissors, Lizard, Cody")
print("")
# **************************************************************
# **************************************************************
# Read the "Form Input" to get the user_Choice or done choice (y/n).
# The user clicks on a picture of Rock, Paper,... for the input.
# The "submit" type is an image which allows this input.
# Use cgi.escape to stip any html characters = improved security.
# The "quote=true" attribute means quotes will also be escaped.
form = cgi.FieldStorage()
user_Choice = cgi.escape(form['user_Choice'].value, quote=True)
# **************************************************************
# define function to print results.
def print_result(result, x):
if result == 't':
print("The computer chose " + x)
print("You and the computer tied.")
elif result == 'w':
print("The computer chose " + x)
print("You beat the computer!")
elif result == 'l':
print("The computer chose " + x)
print("You lost against the computer")
else:
print("Results: There was an error...sorry.
")
# The computer makes a random selection of Rock, Paper, ...
comp_Choice = random.choice(["rock","paper","scissors","lizard","cody"])
# Determine whether the user wins, loses, or draws.
if user_Choice == "":
print("Results: There was an error...sorry.
")
if user_Choice == comp_Choice:
result = 't'
print_result(result, comp_Choice)
draw += 1
elif user_Choice == 'paper':
if comp_Choice == 'rock':
result = 'w'
print_result(result, comp_Choice)
win += 1
elif comp_Choice == 'scissors':
result = 'l'
print_result(result, comp_Choice)
lose += 1
elif comp_Choice == 'lizard':
result = 'l'
print_result(result, comp_Choice)
lose += 1
elif comp_Choice == 'cody':
result = 'w'
print_result(result, comp_Choice)
win += 1
else:
print ("Results: There was an error...sorry.
")
elif user_Choice == 'rock':
if comp_Choice == 'paper':
result = 'l'
print_result(result, comp_Choice)
lose += 1
elif comp_Choice == 'scissors':
result = 'w'
print_result(result, comp_Choice)
win += 1
elif comp_Choice == 'lizard':
result = 'w'
print_result(result, comp_Choice)
win += 1
elif comp_Choice == 'cody':
result = 'l'
print_result(result, comp_Choice)
lose += 1
else:
print("Results: There was an error...sorry.
")
elif user_Choice == 'scissors':
if comp_Choice == 'rock':
result = 'l'
print_result(result, comp_Choice)
lose += 1
elif comp_Choice == 'paper':
result = 'w'
print_result(result, comp_Choice)
win += 1
elif comp_Choice == 'lizard':
result = 'w'
print_result(result, comp_Choice)
win += 1
elif comp_Choice == 'cody':
result = 'l'
print_result(result, comp_Choice)
lose += 1
else:
print("Results: There was an error...sorry.
")
elif user_Choice == 'lizard':
if comp_Choice == 'rock':
result = 'l'
print_result(result, comp_Choice)
lose += 1
elif comp_Choice == 'scissors':
result = 'l'
print_result(result, comp_Choice)
lose += 1
elif comp_Choice == 'paper':
result = 'w'
print_result(result, comp_Choice)
win += 1
elif comp_Choice == 'cody':
result = 'w'
print_result(result, comp_Choice)
win += 1
else:
print("Results: There was an error...sorry.
")
elif user_Choice == 'cody':
if comp_Choice == 'rock':
result = 'w'
print_result(result, comp_Choice)
win += 1
elif comp_Choice == 'scissors':
result = 'w'
print_result(result, comp_Choice)
win += 1
elif comp_Choice == 'lizard':
result = 'l'
print_result(result, comp_Choice)
lose += 1
elif comp_Choice == 'paper':
result = 'l'
print_result(result, comp_Choice)
lose += 1
else:
print("Results: There was an error...sorry.
")
else:
print("Results: There was an error...sorry.
")
print("Thanks for playing!.
")
print("Make a Selection For Another Play!
")
str1 = ''''''
print(str1+str2+str(win)+str3+str(lose)+str4+str(draw)+str5+str(total_Games)+str6+str(ratio)+str7+"
")
# Calculate and show game data.
total_Games = win + lose + draw
ratio = (float(win) / total_Games) * 100.0
print("How Are You Doing?
")
print ("Wins:" + str(win)+ " ")
print ("Losses:" + str(lose)+ " ")
print ("Ties:" + str(draw)+ "
")
print ("Total Games Played:" + str(total_Games)+ " ")
print ("Your win ratio:" + str(ratio) + "%")
# Exit if done.
str8 = ''''''
print(str8 + str9 + str10)
# ************HTML SECTION 3 of 3********************************
# SECTION 3: End Main HTML Output i.e. html framework
# End of the HTML section.
print ("")