Attachment 'RockPaperScissors.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - RockPaperScissors
   4 
   5     @copyright: 2004 by Jos Yule <jos@theorganization.net>
   6     @license: GNU GPL, see COPYING for details.
   7 
   8 	Usage:
   9 	[[RockPaperScissors( pageName )]]
  10 
  11 	pageName is optional, works the same way that the MonthCalendar Macro does. You could have a game being played on page GameOne, and want to include that game on another page. Simply use the macro like so:
  12 	
  13 	[[RockPaperScissors(GameOne)]]
  14 
  15 
  16 """
  17 
  18 
  19 #import re, string, time
  20 #from MoinMoin import config, wikiutil
  21 from MoinMoin.Page import Page
  22 import re
  23 
  24 
  25 missingHomePage = """
  26 RockPaperScissors Info: You have entered the page <b>%s</b> as the root for this Rock Paper Scissors game. Unfortunatly, it can not be found. Please try a different page.
  27 """
  28 
  29 playerPagesMissing = """
  30 RockPaperScissors Info: You seem to be missing the player pages for the game on page %s. You need to create the subpages %s/PlayerOne and %s/PlayerTwo. As this game requries some secrecy, the two players might want to use the ACLs to make their respective pages private. 
  31 """
  32 
  33 noPlays = """
  34 RockPaperScissors Info: There are no valid moves entered yet. Make sure that on the two player pages (/PlayerOne and /PlayerTwo) you have a list of the form:
  35 <pre>
  36  * paper or rock or scissors</pre>
  37  Each line is a "play" of the game. Anything else will be ignored. The case is not important.
  38 """
  39 
  40 playMatrix = {'paper':{'scissors':-1,'rock':1,'paper':0},
  41 	'rock':{'scissors':1,'rock':0,'paper':-1},
  42 	'scissors':{'scissors':0,'rock':-1,'paper':1}}
  43 
  44 def execute(macro, args):
  45 	
  46 	if args == '':
  47 		RPShome = macro.formatter.page.page_name
  48 	else:
  49 		RPShome = args.split(',')[0]
  50 
  51 
  52 	RPShomePage = Page(RPShome)
  53 
  54 	if not RPShomePage.exists():
  55 		return missingHomePage % RPShome
  56 	
  57 	# get the 2 subpages
  58 
  59 	listOfAllPages = macro.request.getPageList()
  60 	pageToMatch = "^" + RPShome + "/PlayerOne"
  61 	cond = re.compile(pageToMatch).match 
  62 	playerOnePage = filter(cond, listOfAllPages)
  63 
  64 	if len(playerOnePage) == 0:
  65 		return playerPagesMissing % (RPShome, RPShome, RPShome)
  66 
  67 	listOfAllPages = macro.request.getPageList()
  68 	pageToMatch = "^" + RPShome + "/PlayerTwo"
  69 	cond = re.compile(pageToMatch).match 
  70 	playerTwoPage = filter(cond, listOfAllPages)
  71 
  72 	if len(playerTwoPage) == 0:
  73 		return playerPagesMissing % (RPShome, RPShome, RPShome)
  74 
  75 	playerOneText = Page(playerOnePage[0]).get_raw_body()
  76 	playerTwoText = Page(playerTwoPage[0]).get_raw_body()
  77 
  78 	moveRegex = re.compile(r' [*] (paper|rock|scissors)',re.I)
  79 	playerOneMoves = moveRegex.findall(playerOneText)
  80 	playerTwoMoves = moveRegex.findall(playerTwoText)
  81 
  82 	numberOfPlays = min(len(playerOneMoves),len(playerTwoMoves))
  83 
  84 	if not numberOfPlays > 0:
  85 		return noPlays
  86 	
  87 	playOutput = ''
  88 	score = [0,0]
  89 
  90 	for aPlay in range(numberOfPlays):
  91 		p1 = playerOneMoves[aPlay].lower()
  92 		p2 = playerTwoMoves[aPlay].lower()
  93 
  94 		if playMatrix[p1][p2] == -1:
  95 			#player one looses
  96 			score[1] += 1;
  97 			playOutput += '<br> Player One\'s <b>%s</b> looses to Player Two\'s <b>%s</b>.' % (p1.capitalize(),p2.capitalize())
  98 		elif playMatrix[p1][p2] == 0:
  99 			# draw
 100 			playOutput += '<br> Player One\'s <b>%s</b> draws against Player Two\'s <b>%s</b>.' % (p1.capitalize(),p2.capitalize())
 101 		elif playMatrix[p1][p2] == 1:
 102 			#player one wins
 103 			score[0] += 1;
 104 			playOutput += '<br> Player One\'s <b>%s</b> defeats Player Two\'s <b>%s</b>.' % (p1.capitalize(),p2.capitalize())
 105 
 106 	if score[0] > score[1]:
 107 		playOutput += '<br> The Score is %s to %s for Player One.' % (score[0],score[1])
 108 	elif score[1] > score[0]:
 109 		playOutput += '<br> The Score is %s to %s for Player Two.' % (score[1],score[0])
 110 	elif score[1] == score[0]:
 111 		playOutput += '<br> The Score is tied at %s to %s.' % (score[1],score[0])
 112 
 113 	return playOutput
 114 
 115 	
 116 	

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-05-23 12:50:09, 3.8 KB) [[attachment:RockPaperScissors.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.