Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .idea/generai.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

743 changes: 743 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

128 changes: 90 additions & 38 deletions game.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
#
import sys
import traceback

from helper import print_scoresheets

from generala import get_random_dice, valid_play, play_value
"""Library necessary to copy the list "scoresheets" to pass by value to the plugin"""
from copy import deepcopy
"""Libraries necessary for timing the plugin's response"""
import signal
"""Library for displaying the winner"""
import operator
"""Libraries for sandboxing"""
from sandboxing import getPlay#for the sandboxing
TIME_AVAILABLE = 1#time available to compute the plugin's response

class Timeout():
"""Timeout class using ALARM signal."""

class Timeout(Exception):
pass

def __init__(self, sec):
self.sec = sec

def __enter__(self):
signal.signal(signal.SIGALRM, self.raise_timeout)
signal.alarm(self.sec)

def __exit__(self, *args):
signal.alarm(0) # disable alarm

def raise_timeout(self, *args):
raise Timeout.Timeout()

class Player():#segundo comentario de prueba

class Player():#

def __init__(self, name):
self.name = name
Expand All @@ -31,18 +57,17 @@ class Game():
def __init__(self, nscoresheets):
self.nscoresheets = nscoresheets
self.nplayers = 0
self.players_plugins = {}
self.players = []
self.players_plugins = {} #the name of the file of the plugins
self.players = [] #the name() of the plugins
self.scoresheets = []
for i in range(nscoresheets):
self.scoresheets.append({})

def add_player(self, player):
name = player.name()
self.players_plugins[name] = player
self.players.append(name)
def add_player(self, namePlayer,plugin):
self.players_plugins[namePlayer] = plugin
self.players.append(namePlayer)
for i in range(self.nscoresheets):
self.scoresheets[i][name] = {}
self.scoresheets[i][namePlayer] = {}
self.nplayers = self.nplayers + 1

def start(self):
Expand All @@ -61,38 +86,65 @@ def start(self):

def results(self):
print_scoresheets(self.scoresheets)
"""calculating the winner"""
total = {}
for player in self.players:
total[player] = 0
for scoresheet in self.scoresheets:
for player in scoresheet:
for play, score in scoresheet[player].items():
total[player] += score
print "The winner is!:",max(total.iteritems(),key=operator.itemgetter(1))[0]

def turn(self, player):
plugin = self.players_plugins[player]
r = get_random_dice(5)
plugin = self.players_plugins[player]#name of the module
print "turn", plugin
r = get_random_dice(5)#obtain the dices as a list eg: [2,1,4,4,6]
nroll = 5
for i in range(3):
for i in range(3):#ith opportunity of the current player
try:
bonus = (nroll == 5)
roll, decision, scoresheet = plugin.play(i, r, bonus, self.players, self.scoresheets)
decision = decision.upper() if decision else None
nroll = len(roll)
if nroll > 0:
r0 = get_random_dice(nroll)
i = 0
for new_r in roll:
r[new_r] = r0[i]
i = i+1
else:
if not valid_play(decision):
raise Exception('Play [{0}] is invalid'.format(decision))
scoresheet = self.scoresheets[scoresheet][player]
if decision not in scoresheet:
scoresheet[decision] = play_value(decision, r, bonus)
else:
raise Exception('Decision [{0}] is already taken'.format(decision))
break


"""It executes the corresponding turn for the plugin with TIME_AVAILABLE seconds to decide"""
with Timeout(TIME_AVAILABLE):
bonus = (nroll == 5)
"""
The return parameters of the play function of the plugin should be:
roll: which dices should be re-rolled referencing the indexes of the r list
decision: the final decision of the turn as a string. The avalaible decisions are stated in helper.py
scorsheet: which scoresheet should the play be applied
"""
copyScoresheets = deepcopy(self.scoresheets)#make a copy of the scoresheets because we want to send them by value not by reference for safety
"""getPlay from the sandboxing.py with the parameters
plugin: name of the file's plugin eg: serial_dicer.py
i: ith opportunity to throw again the dice
r: the dices itself
bonus: if that throw contains a bonus
player: the current players playing the game
Scoresheets: the poins currently assigned
"""
roll, decision, scoresheet = getPlay(plugin,i, r, bonus, self.players, copyScoresheets)
#roll, decision, scoresheet = plugin.play(i, r, bonus, self.players, copyScoresheets)<--reemplazado
decision = decision.upper() if decision else None
nroll = len(roll)#how many dices should be re-roll
if nroll > 0:#the player has decided re-roll again at least one dice
r0 = get_random_dice(nroll)#it obtains "nroll" new dices
i = 0
for new_r in roll:#it updates the new dices list
r[new_r] = r0[i]
i = i+1
else:#the player has decided that no dice shoul be re-rolled
if not valid_play(decision):#the player has not stated a valid play in the format stated in generala.py
raise Exception('Play [{0}] is invalid'.format(decision))
scoresheet = self.scoresheets[scoresheet][player]#retrieve the plays that player has already played in that scoresheet
if decision not in scoresheet:
#The player adds a new play in that scoresheet
scoresheet[decision] = play_value(decision, r, bonus)
else:
raise Exception('Decision [{0}] is already taken'.format(decision))
break
except Timeout.Timeout:#Times up for the ith opportunity
print "Plugin Timeout",plugin
break
except Exception as e:
print(traceback.format_exc())
print("Error inesperadamente inesperado: {0}".format(e))
# NOTIFY



Binary file added game.pyc
Binary file not shown.
28 changes: 15 additions & 13 deletions generai.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# coding=utf-8
from os import listdir
from os.path import isfile, join
from importlib import import_module
from game import Game

from sandboxing import getName
# plugins

plugins = {}
plugins_list = []

plugins_list=[]
def load_plugins():
print("Loading plugins...")
plugin_files = [f for f in listdir('plugins') if isfile(join('plugins',
Expand All @@ -18,11 +18,13 @@ def load_plugins():
plugin_name = plugin_file[:-3]
print("Loading plugin {0}".format(plugin_name))
try:
m = import_module('plugins.{0}'.format(plugin_name))
plugins_loaded = plugins_loaded + 1
name = m.name()
plugins[name] = m
plugins_list.append(m)
name = getName(plugin_file)
if(name is not None):
plugins_loaded = plugins_loaded + 1
plugins[name] = plugin_file
plugins_list.append(name)
else:
raise Exception(name)
except Exception as e:
print("Plugin {0} was not loaded {1}".format(plugin_name, e))
invalid_plugins = invalid_plugins + 1
Expand All @@ -33,20 +35,20 @@ def list_plugins(title=True):
print("---------------- PLUGINS -----------------")
i = 0
for plugin in plugins_list:
print("{0}) {1}".format(i, plugin.name()))
print("{0}) {1}".format(i, plugin))
i = i + 1
print("\n")

def start_game():
print("---------------- GENERALA -----------------")
list_plugins(False)

players_input = input("Ingrese los jugadores (separándolos con ,): ")
players_input = raw_input("Ingrese los jugadores (separándolos con ,): ")
players = [int(x.strip()) for x in players_input.split(",")]
nscoresheets = int(input("Cuántas casillas? "))
nscoresheets = int(raw_input("Cuántas casillas? "))
game = Game(nscoresheets)
for player in players:
game.add_player(plugins_list[player])
game.add_player(plugins_list[player],plugins[plugins_list[player]])
game.start()
game.results()

Expand Down Expand Up @@ -78,7 +80,7 @@ def menu():

def process_option(opt):
for option in options:
if (isinstance(option["val"], list) and opt in option["val"]) or (opt == option["val"]):
if (isinstance(option["val"], list) and opt in option["val"]) or (str(opt) == option["val"]):
option["fn"]()

if __name__ == "__main__":
Expand Down
Binary file added generala.pyc
Binary file not shown.
Binary file added helper.pyc
Binary file not shown.
Binary file added plugins/__init__.pyc
Binary file not shown.
6 changes: 3 additions & 3 deletions plugins/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def name():
# this plugin uses console I/O to make plays

def roll_dice_eval(roll_dice):
return [int(x) - 1 for x in roll_dice if x in string.digits]
return [int(x) - 1 for x in list(roll_dice)]

def play(roll_no, dice, bonus, players, scoresheets):
print_scoresheets(scoresheets)
Expand All @@ -18,6 +18,6 @@ def play(roll_no, dice, bonus, players, scoresheets):
if roll_dice:
return roll_dice_eval(roll_dice), None, None
else:
decision = input("jugada? ")
scoresheet = int(input("casilla {0}? ".format(str(list(range(len(scoresheets)))))))
decision = raw_input("jugada? ")
scoresheet = int(raw_input("casilla {0}? ".format(str(list(range(len(scoresheets)))))))
return ([], decision, scoresheet)
Binary file added plugins/console.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion plugins/random_dicer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def name():
return "random_roshkero 1.0"

def play(dice, servido, players, scoresheets):
def play(turno, dice, servido, players, scoresheets):
print("MY TURN! -> {0} [{1}]".format(1, dice))
return (1, 2, 3)

Binary file added plugins/random_dicer.pyc
Binary file not shown.
26 changes: 26 additions & 0 deletions plugins/se1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

def name():
return "SERIAL 1"

# this plugin will play serially to the next available free space
# in its corresponding scoresheet
def play(roll_no, dice, bonus, players, scoresheets):
# where
i = 0
for ss in scoresheets:
my_ss = ss[name()]
if '4' not in my_ss:
return [], '4', i
elif '5' not in my_ss:
return [], '5', i
elif '6' not in my_ss:
return [], '6', i
elif 'ESCALERA' not in my_ss:
return [], 'ESCALERA', i
elif 'FULLHOUSE' not in my_ss:
return [], 'FULLHOUSE', i
elif 'POKER' not in my_ss:
return [], 'POKER', i
elif 'GENERALA' not in my_ss:
return [], 'GENERALA', i
i = i + 1
29 changes: 29 additions & 0 deletions plugins/se2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

def name():
return "SERIAL 2"


# this plugin will play serially to the next available free space
# in its corresponding scoresheet

def play(roll_no, dice, bonus, players, scoresheets):
# where

i = 0
for ss in scoresheets:
my_ss = ss[name()]
if '4' not in my_ss:
return [], '4', i
elif '5' not in my_ss:
return [], '5', i
elif '6' not in my_ss:
return [], '6', i
elif 'ESCALERA' not in my_ss:
return [], 'ESCALERA', i
elif 'FULLHOUSE' not in my_ss:
return [], 'FULLHOUSE', i
elif 'POKER' not in my_ss:
return [], 'POKER', i
elif 'GENERALA' not in my_ss:
return [], 'GENERALA', i
i = i + 1
Binary file added plugins/serial_dicer.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions plugins/while1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

def name():
return "while1"

# this plugin will play serially to the next available free space
# in its corresponding scoresheet
def play(roll_no, dice, bonus, players, scoresheets):
# where
while(1):
pass
Loading