From eb7c2f48b86fce68bbbe9289a3f53c5524fc6637 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 10:51:56 -0800 Subject: [PATCH 01/27] new file fsdf --- projects/lagmayj/James | 1 + 1 file changed, 1 insertion(+) create mode 100644 projects/lagmayj/James diff --git a/projects/lagmayj/James b/projects/lagmayj/James new file mode 100644 index 0000000..dc065e0 --- /dev/null +++ b/projects/lagmayj/James @@ -0,0 +1 @@ +james From 096973e6e3139ef79799931f79749c75f3d9404e Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 10:57:41 -0800 Subject: [PATCH 02/27] Rename projects/roberwen/dominion/Dominion.py to projects/lagmayj/dominion/Dominion.py --- projects/{roberwen => lagmayj}/dominion/Dominion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename projects/{roberwen => lagmayj}/dominion/Dominion.py (99%) diff --git a/projects/roberwen/dominion/Dominion.py b/projects/lagmayj/dominion/Dominion.py similarity index 99% rename from projects/roberwen/dominion/Dominion.py rename to projects/lagmayj/dominion/Dominion.py index 6a0a534..d8deecf 100644 --- a/projects/roberwen/dominion/Dominion.py +++ b/projects/lagmayj/dominion/Dominion.py @@ -759,4 +759,4 @@ def countcards(cards,form): def rankcards(form,vector): od = OrderedDict(zip(form,vector)) sd = OrderedDict(sorted(od.items(),key = itemgetter(1),reverse=True)) - return list(sd.keys()) \ No newline at end of file + return list(sd.keys()) From 54125d4940d3154eebe3ccaadbfda6a1801c402e Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 10:58:53 -0800 Subject: [PATCH 03/27] Rename projects/lagmayj/dominion/Dominion.py to projects/roberwen/dominion/Dominion.py --- projects/{lagmayj => roberwen}/dominion/Dominion.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename projects/{lagmayj => roberwen}/dominion/Dominion.py (100%) diff --git a/projects/lagmayj/dominion/Dominion.py b/projects/roberwen/dominion/Dominion.py similarity index 100% rename from projects/lagmayj/dominion/Dominion.py rename to projects/roberwen/dominion/Dominion.py From cf8a061b105e7c7507dd0b7356875f6db71dc6f7 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 11:02:36 -0800 Subject: [PATCH 04/27] Add files via upload --- projects/lagmayj/Dominion.py.html | 5771 +++++++++++++++++++++++++++++ projects/lagmayj/README.md.html | 1214 ++++++ 2 files changed, 6985 insertions(+) create mode 100644 projects/lagmayj/Dominion.py.html create mode 100644 projects/lagmayj/README.md.html diff --git a/projects/lagmayj/Dominion.py.html b/projects/lagmayj/Dominion.py.html new file mode 100644 index 0000000..1e92ba0 --- /dev/null +++ b/projects/lagmayj/Dominion.py.html @@ -0,0 +1,5771 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CS362-W2020/Dominion.py at master · jslgmy/CS362-W2020 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Skip to content + + + + + + + + + + + + + +
+ +
+ + +
+ +
+ + + +
+
+
+ + + + + + + + + + + + + + +
+
+ +
    + + + + +
  • + +
    + +
    + + + Watch + + +
    + Notifications +
    +
    + + + + + + + +
    +
    +
    + +
    +
  • + +
  • +
    +
    + + +
    +
    + + +
    + +
  • + +
  • +
    + + Fork + +
    + +

    Fork CS362-W2020

    +
    +
    + +
    +

    If this dialog fails to load, you can visit the fork page directly.

    +
    +
    +
    +
    + + +
  • +
+ +

+ + /CS362-W2020 + + + + forked from CS362-Roberts/CS362-W2020 + +

+ +
+ + + + + + +
+
+
+ + + + + + + + + Permalink + + + + +
+ + +
+ + Branch: + master + + + + + + + +
+ +
+ + Find file + + + Copy path + +
+
+ + +
+ + Find file + + + Copy path + +
+
+ + + + + + +
+ + +
+
+ + 2 contributors + + +
+ +

+ Users who have contributed to this file +

+
+ +
+
+ + + @jslgmy + + @CS362-W2019 + + + +
+
+ + + + + +
+ +
+
+ + 762 lines (695 sloc) + + 26.7 KB +
+ +
+ +
+ Raw + Blame + History +
+ + +
+ + + + +
+ +
+
+ +
+
+
+ + + +
+
+ + You're using code navigation to jump to definitions or references. +
+ +
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 13 15:26:55 2015
@author: tfleck
"""
import random
from collections import Counter, OrderedDict
from operator import itemgetter
import re
import pandas
+
class Card():
def __init__(self,name,category,cost,buypower,vpoints):
self.name = name
self.category = category
self.cost = cost
self.buypower = buypower
self.vpoints = vpoints
def react(self,player):
return False
+
class Coin_card(Card):
def __init__(self,name,cost,buypower):
Card.__init__(self,name,"coin",cost,buypower,0)
+
class Copper(Coin_card):
def __init__(self):
Coin_card.__init__(self,"Copper",0,1)
class Silver(Coin_card):
def __init__(self):
Coin_card.__init__(self,"Silver",3,2)
class Gold(Coin_card):
def __init__(self):
Coin_card.__init__(self,"Gold",6,3)
+
class Victory_card(Card):
def __init__(self,name,cost,vpoints):
Card.__init__(self,name,"victory",cost,0,vpoints)
+
class Estate(Victory_card):
def __init__(self):
Victory_card.__init__(self,"Estate",2,1)
class Duchy(Victory_card):
def __init__(self):
Victory_card.__init__(self,"Duchy",5,3)
class Province(Victory_card):
def __init__(self):
Victory_card.__init__(self,"Province",8,6)
+
class Gardens(Victory_card):
def __init__(self):
Victory_card.__init__(self,"Gardens",4,0)
+
class Curse(Card):
def __init__(self):
Card.__init__(self,"Curse","curse",0,0,-1)
+
class Action_card(Card):
def __init__(self,name,cost,actions,cards,buys,coins):
Card.__init__(self,name,"action",cost,0,0)
self.actions = actions
self.cards = cards
self.buys = buys
self.coins = coins
def use(self,player,trash):
player.played.append(self)
player.hand.remove(self)
def augment(self,player):
player.actions+=self.actions
player.buys+=self.buys
player.purse+=self.coins
for i in range(self.cards):
player.draw()
def play(self,player,players,supply,trash):
pass
class Woodcutter(Action_card):
def __init__(self):
Action_card.__init__(self,"Woodcutter",3,0,0,1,2)
+
class Smithy(Action_card):
def __init__(self):
Action_card.__init__(self,"Smithy",4,0,3,0,0)
+
class Laboratory(Action_card):
def __init__(self):
Action_card.__init__(self,"Laboratory",5,1,2,0,0)
+
class Village(Action_card):
def __init__(self):
Action_card.__init__(self,"Village",3,2,1,0,0)
+
class Festival(Action_card):
def __init__(self):
Action_card.__init__(self,"Festival",5,2,0,1,2)
+
class Market(Action_card):
def __init__(self):
Action_card.__init__(self,"Market",5,1,1,1,1)
+
class Chancellor(Action_card):
def __init__(self):
Action_card.__init__(self,"Chancellor",3,0,0,0,2)
def play(self,player,players,supply,trash):
if player.yesnoinput('Would you like to discard your entire deck?'):
player.discard = player.discard + player.deck
player.deck = []
+
class Workshop(Action_card):
def __init__(self):
Action_card.__init__(self,"Workshop",3,0,0,0,0)
def play(self,player,players,supply,trash):
player.gaincard(supply,4)
+
class Moneylender(Action_card):
def __init__(self):
Action_card.__init__(self,"Moneylender",4,0,0,0,0)
def play(self,player,players,supply,trash):
c = getcard("Copper",supply,player.hand,"your hand")
if c:
trash.append(c)
player.hand.remove(c)
player.purse += 3
+
class Chapel(Action_card):
def __init__(self):
Action_card.__init__(self,"Chapel",2,0,0,0,0)
def play(self,player,players,supply,trash):
trashed=0
while trashed<4 and len(player.hand)>0:
trashcard = input("Choose a card from your hand to trash: ")
if not trashcard:
break
c = getcard(trashcard,supply,player.hand,"your hand")
if c:
trash.append(c)
player.hand.remove(c)
trashed+=1
+
class Cellar(Action_card):
def __init__(self):
Action_card.__init__(self,"Cellar",2,1,0,0,0)
def play(self,player,players,supply,trash):
discarded=0
while len(player.hand)>0:
dis_card=input("Choose a card from your hand to discard: ")
if not dis_card:
break
c = getcard(dis_card,supply,player.hand,"your hand")
if c:
player.discard.append(c)
player.hand.remove(c)
discarded+=1
for j in range(discarded):
player.draw()
+
class Remodel(Action_card):
def __init__(self):
Action_card.__init__(self,"Remodel",4,0,0,0,0)
def play(self,player,players,supply,trash):
while len(player.hand)>0:
this_card = input("Choose a card from your hand to remodel: ")
c = getcard(this_card,supply,player.hand,"your hand")
if c:
trash.append(c)
player.hand.remove(c)
player.gaincard(supply,c.cost+2)
break
+
class Adventurer(Action_card):
def __init__(self):
Action_card.__init__(self,"Adventurer",6,0,0,0,0)
def play(self,player,players,supply,trash):
coins_added = 0
while (player.deck or player.discard) and coins_added <2:
player.draw()
if player.hand[-1].category == "coin":
coins_added +=1
else:
player.aside.append(player.hand.pop())
+
class Feast(Action_card):
def __init__(self):
Action_card.__init__(self,"Feast",4,0,0,0,0)
def use(self,player,trash):
trash.append(self)
player.hand.remove(self)
def play(self,player,players,supply,trash):
player.gaincard(supply,5)
+
class Mine(Action_card):
def __init__(self):
Action_card.__init__(self,"Mine",5,0,0,0,0)
def play(self,player,players,supply,trash):
while "coin" in catinlist(player.hand):
this_card = input("Choose a coin from your hand to upgrade: ")
c = getcard(this_card,supply,player.hand,"your hand",["coin"])
if c:
trash.append(c)
player.hand.remove(c)
while c.cost>2 or len(supply["Copper"])>0 or len(supply["Silver"])>0:
pick = input("Choose a coin to gain. Any coin up to " + str(c.cost+3)+": ")
g = getcard(pick,supply,categories=["coin"],upto=c.cost+3)
if g:
player.hand.append(g)
supply[pick].remove(g)
break
break
+
class Library(Action_card):
def __init__(self):
Action_card.__init__(self,"Library",5,0,0,0,0)
def play(self,player,players,supply,trash):
while (player.deck or player.discard) and len(player.hand) <7:
player.draw(player.hold)
if (player.hold[-1].category == "action" and not player.yesnoinput("You drew " + player.hold[-1].name + ". Would you like to keep it?",", add to my hand", ", set it aside")):
player.aside.append(player.hold.pop())
else:
player.hand.append(player.hold.pop())
+
class Moat(Action_card):
def __init__(self):
Action_card.__init__(self,"Moat",2,0,2,0,0)
def react(self,player):
player.show()
return player.yesnoinput(player.name + ", you have a " + self.name +
" in your hand. Do you want to block the attack?")
+
class Council_Room(Action_card):
def __init__(self):
Action_card.__init__(self,"Council Room",5,0,3,1,0)
def play(self,this_player,players,supply,trash):
for player in players:
player.draw()
+
class Witch(Action_card):
def __init__(self):
Action_card.__init__(self,"Witch",5,0,2,0,0)
def play(self,this_player,players,supply,trash):
if len(supply["Curse"])>0:
for player in players:
if player==this_player:
pass
else:
for c in player.hand:
if c.react(player):
break
else:
if len(supply["Curse"])>0:
player.discard.append(supply["Curse"].pop())
+
class Bureaucrat(Action_card):
def __init__(self):
Action_card.__init__(self,"Bureaucrat",4,0,0,0,0)
def play(self,this_player,players,supply,trash):
if len(supply["Silver"])>0:
this_player.deck.insert(0,supply["Silver"].pop())
for player in players:
if (not player==this_player) and ("victory" in catinlist(player.hand)):
for c in player.hand:
if c.react(player):
break
else:
player.show()
while True:
putback = player.choose_discard(player.name + ", which victory card" +
" do you want to put on top of your deck?\n--> ")
c = getcard(putback,supply,player.hand,"your hand",["victory"])
if c:
player.hand.remove(c)
player.deck.insert(0,c)
break
class Militia(Action_card):
def __init__(self):
Action_card.__init__(self,"Militia",4,0,0,0,2)
def play(self,this_player,players,supply,trash):
for player in players:
if (not player==this_player) and len(player.hand)>3:
for c in player.hand:
if c.react(player):
break
else:
player.show()
while len(player.hand)>3:
dis_card=player.choose_discard(player.name + ", choose a card from your hand to discard: ")
if dis_card:
c = getcard(dis_card,supply,player.hand,"your hand")
if c:
player.hand.remove(c)
player.discard.append(c)
class Spy(Action_card):
def __init__(self):
Action_card.__init__(self,"Spy",4,1,1,0,0)
def play(self,this_player,players,supply,trash):
for player in players:
for c in player.hand:
if c.react(player):
break
else:
b = player.draw([])
if not b:
continue
else:
print("The first card in the deck of " + player.name + " is " + b.name)
if this_player.yesnoinput(this_player.name + ", do you want " + player.name + " to discard this?",
", discard",", keep"):
player.discard.append(b)
else:
player.deck.insert(0,b)
#check logic of this structure
+
class Thief(Action_card):
def __init__(self):
Action_card.__init__(self,"Thief",4,0,0,0,0)
def play(self,this_player,players,supply,trash):
for player in players:
if player == this_player:
continue
for c in player.hand:
if c.react(player):
break
else:
for i in range(2):
player.draw(player.hold)
print( player.name, namesinlist(player.hold))
if "coin" in catinlist(player.hold):
while True:
burn = input("Which card would you like " + player.name + " to trash?\n-->")
c = getcard(burn,supply,player.hold," the top 2 cards",["coin"])
if c:
player.hold.remove(c)
break
if this_player.yesnoinput("Do you want to steal it?",", it's mine!",", leave it in the trash."):
this_player.discard.append(c)
else:
trash.append(c)
player.discard=player.discard+player.hold
player.hold = []
+
class Throne_Room(Action_card):
def __init__(self):
Action_card.__init__(self,"Throne Room",4,0,0,0,0)
def play(self,player,players,supply,trash):
if "action" in catinlist(player.hand):
while True:
double = input("What card would you like to double?\n--> ")
c = getcard(double,supply,player.hand," your hand",["action"])
if c:
c.use(player,trash)
c.augment(player)
c.play(player,players,supply,trash)
player.show()
c.augment(player)
c.play(player,players,supply,trash)
break
+
class Player():
def __init__(self,name):
self.name = name
self.hand = []
self.deck = [Copper()]*7 + [Estate()]*3
random.shuffle(self.deck)
self.played = []
self.discard = []
self.aside = []
self.hold = []
for i in range(5):
self.draw()
+
def other(self):
return self.played+self.discard+self.hold+self.aside
def stack(self):
return (self.deck + self.hand + self.played + self.discard + self.aside + self.hold)
def draw(self,dest=None):
#defualt destination is player's hand
if dest==None:
dest = self.hand
#Replenish deck if necessary.
if len(self.deck)==0:
self.deck = self.discard
self.discard = []
random.shuffle(self.deck)
#If deck has cards, add card to destination list
if len(self.deck)>0:
c = self.deck.pop(0)
dest.append(c)
return c
def turn(self,players,supply,trash):
self.show()
self.actions = 1
self.buys = 1
self.purse = 0
#action phase
while self.actions>0 and 'action' in catinlist(self.hand):
playthis = input("Which card would you like to play? You have " +
str(self.actions) + " action(s). \n-Hit enter for no play. --> ")
if playthis:
c = getcard(playthis,supply,self.hand,"your hand",['action'])
if c:
self.actions = self.actions - 1
c.use(self,trash)
c.augment(self)
c.play(self,players,supply,trash)
self.show()
else:
self.actions=0
#buy phase
for c in self.hand:
self.purse += c.buypower
while self.buys>0:
buy_string = "Buying power is " + str(self.purse) + ". You have " + str(self.buys) + " buy"
if self.buys>1:
buy_string += "s"
buy_string += "."
print( buy_string)
purchase = input("What would you like to purchase? -Hit enter for no purchase.-\n--> ")
if not purchase:
break
else:
c = getcard(purchase,supply,upto=self.purse)
if c:
self.discard.append(supply[purchase].pop())
self.buys = self.buys -1
self.purse = self.purse - c.cost
#cleanup phase
self.discard = self.discard + self.played + self.hand + self.aside
self.played = []
self.hand = []
self.aside = []
for i in range(5):
self.draw()
+
def gaincard(self,supply,upto):
while True:
gain = input("What would you like to get? Any card up to " + str(upto) + "\n--> ")
c = getcard(gain,supply,upto=upto)
if c:
self.discard.append(c)
supply[gain].remove(c)
break
def yesnoinput(self,prompt,yesstring='',nostring=''):
print(prompt + "\n1 - Yes" + yesstring + "\n0 - No" + nostring)
while True:
r = input("--> ")
if r == "0":
return False
if r == "1":
return True
def choose_discard(self,prompt):
return input(prompt)
+
def show(self):
print (self.name)
print ("hand:", ", ".join(sorted(namesinlist(self.hand))))
if len(self.deck)>0:
print ("deck (alphabetical order):", ", ".join(sorted(namesinlist(self.deck))))
if len(self.discard)>0:
print ("discard:", ", ".join(sorted(namesinlist(self.discard))))
if len(self.played)>0:
print ("played:", ", ".join(sorted(namesinlist(self.played))))
if len(self.aside)>0:
print ("aside:", ", ".join(sorted(namesinlist(self.aside))))
print ("\r")
def action_balance(self):
balance = 0
for c in self.stack():
if c.category == "action":
balance = balance - 1 + c.actions
return 70*balance / len(self.stack())
+
def cardsummary(self):
summary = {}
for c in self.stack():
if c.name in summary:
summary[c.name] += 1
else:
summary[c.name] = 1
summary['VICTORY POINTS']=self.calcpoints()
return summary
+
def calcpoints(self):
tally = 0
gardens = 0
n = 0
for c in self.stack():
tally += c.vpoints
n += 1
if c.name == "Gardens":
gardens+=1
return tally + n//10 * gardens
+
class ComputerPlayer(Player):
def __init__(self,name):
Player.__init__(self,name)
self.index = 0
self.buygaintable = []
#beginning and middle of game
self.buygaintable1 = ["Province","Gold","Laboratory","Festival","Witch",
"Council Room","Market","Militia","Adventurer","Smithy","Bureaucrat","Silver","Moat",""]
#end of game
self.buygaintable2 = ["Province","Gardens","Duchy","Estate","Gold","Silver",""]
#beginning and middle of the game, too many action cards
self.buygaintable3 = ["Province","Gold","Festival","Laboratory","Market","Village",
"Silver",""]
self.playtable1 = ["Village","Festival","Market","Laboratory","Witch",
"Council Room","Militia","Adventurer","Smithy","Bureaucrat","Moat",""]
self.discardtable1 = ["Gardens","Duchy","Province","Estate","Curse","Copper",
"Village","Bureaucrat","Silver","Militia","Smithy","Council Room","Witch",
"Festival","Market","Adventurer","Laboratory","Gold","Moat"]
def turn(self,players,supply,trash):
self.show()
self.actions = 1
self.buys = 1
self.purse = 0
#action phase
self.index = 0
while self.actions>0 and 'action' in catinlist(self.hand):
playthis = self.playtable1[self.index]
if playthis:
c = self.getcard(playthis,supply,self.hand,"your hand",['action'])
if c:
print (self.name + " plays " + c.name)
self.actions = self.actions - 1
c.use(self,trash)
self.index=0
c.augment(self)
c.play(self,players,supply,trash)
self.show()
else:
self.index += 1
else:
self.actions=0
#buy phase
if len(supply["Province"])>len(players)+totalbuypower(self.deck)/8:
if self.action_balance()<-10:
self.buygaintable = self.buygaintable3
else:
self.buygaintable = self.buygaintable1
else:
self.buygaintable = self.buygaintable2
self.index = 0
for c in self.hand:
self.purse += c.buypower
while self.buys>0:
purchase = self.buygaintable[self.index]
if not purchase:
break
else:
c = self.getcard(purchase,supply,upto=self.purse)
if c:
self.discard.append(supply[purchase].pop())
self.index = 0
self.buys = self.buys -1
self.purse = self.purse - c.cost
print (self.name + " bought " + c.name)
else:
self.index += 1
#cleanup phase
self.discard = self.discard + self.played + self.hand + self.aside
self.played = []
self.hand = []
self.aside = []
for i in range(5):
self.draw()
def getcard(self,name,supply,target_list=None,target_name= "the supply anymore",categories=['action','coin','curse','victory'],upto=100):
if not name in supply:
#print name + " is not in this game."
return None
if not target_list:
target_list = supply[name]
nameslist = namesinlist(target_list)
if not name in nameslist:
#print "There is no " + name + " in " + target_name
return None
i = nameslist.index(name)
c = target_list[i]
if c.category not in categories:
catstring = categories[0]
for i in categories[1:]:
catstring = catstring + " or " + categories[i]
#print name + " is not a " + catstring + " card."
return None
if c.cost > upto:
#print name + " costs " + str(c.cost)
return None
return c
def choose_discard(self,prompt):
index = 0
while True:
dis_card = self.discardtable1[index]
c = self.getcard(dis_card,[dis_card],self.hand)
if c:
return c.name
else:
index+=1
def yesnoinput(self,prompt,yesstring='',nostring=''):
return True
+
def show(self):
pass
+
class TablePlayer(ComputerPlayer):
def __init__(self,name):
ComputerPlayer.__init__(self,name)
self.index=0
self.buygaintable=[]
q=re.match(r'([a-zA-Z]+)(\d+)',name)
if q:
self.number = q.group(2)
else:
print(name)
self.playtable1 = ["Village","Festival","Market","Laboratory","Witch",
"Council Room","Militia","Adventurer","Smithy","Bureaucrat","Moat",""]
self.discardtable1 = ["Gardens","Duchy","Province","Estate","Curse","Copper",
"Village","Bureaucrat","Silver","Militia","Smithy","Council Room","Witch",
"Festival","Market","Adventurer","Laboratory","Gold","Moat"]
def turn(self,players,supply,trash):
self.show()
self.actions = 1
self.buys = 1
self.purse = 0
#action phase
self.index = 0
while self.actions>0 and 'action' in catinlist(self.hand):
playthis = self.playtable1[self.index]
if playthis:
c = self.getcard(playthis,supply,self.hand,"your hand",['action'])
if c:
#print (self.name + " plays " + c.name)
self.actions = self.actions - 1
c.use(self,trash)
self.index=0
c.augment(self)
c.play(self,players,supply,trash)
self.show()
else:
self.index += 1
else:
self.actions=0
#buy phase
v= self.number
csvstring = r"Dominionbuy" + str(v) + ".csv"
buydf=pandas.read_csv(csvstring,na_filter=False)
sortedbuydf=buydf.sort_values("Buyvalues",ascending=False)
ranktable = sortedbuydf.Cardname
self.index = 0
for c in self.hand:
self.purse += c.buypower
while self.buys>0:
purchase = ranktable.iloc[self.index]
if not purchase:
break
else:
c = self.getcard(purchase,supply,upto=self.purse)
if c:
self.discard.append(supply[purchase].pop())
self.index = 0
self.buys = self.buys -1
self.purse = self.purse - c.cost
else:
self.index += 1
#cleanup phase
self.discard = self.discard + self.played + self.hand + self.aside
self.played = []
self.hand = []
self.aside = []
for i in range(5):
self.draw()
def gameover(supply):
if len(supply["Province"])==0:
return True
out = 0
for stack in supply:
if len(supply[stack])==0:
out+=1
if out>=3:
return True
return False
+
def namesinlist(cardlist):
namelist = []
for c in cardlist:
namelist.append(c.name)
return namelist
+
def catinlist(cardlist):
catlist = []
for c in cardlist:
catlist.append(c.category)
return catlist
+
def getcard(name,supply,target_list=None,target_name= "the supply anymore",categories=['action','coin','curse','victory'],upto=100):
if not name in supply:
print( name + " is not in this game.")
return None
if not target_list:
target_list = supply[name]
nameslist = namesinlist(target_list)
if not name in nameslist:
print ("There is no " + name + " in " + target_name)
return None
i = nameslist.index(name)
c = target_list[i]
if c.category not in categories:
catstring = categories[0]
for i in categories[1:]:
catstring = catstring + " or " + categories[i]
print (name + " is not a " + catstring + " card.")
return None
if c.cost > upto:
print (name + " costs " + str(c.cost))
return None
return c
+
def totalbuypower(cardlist):
TBP = 0
for c in cardlist:
TBP += c.buypower
if c.category == "action":
TBP += c.coins
return TBP
+
def cardsummaries(players):
cardsums={}
for player in players:
cardsums[player.name]=player.cardsummary()
cardsdf = pandas.DataFrame(cardsums).fillna(0).sort_index()
vp=cardsdf.loc[['VICTORY POINTS']]
cardsdf.drop(['VICTORY POINTS'],inplace=True)
return pandas.concat([cardsdf,vp],axis=0).fillna(0).astype(int)
+
def countsupply(supply,form):
return [len(supply[a]) for a in form]
+
def countcards(cards,form):
dcount = Counter(namesinlist(cards))
return [dcount[a] for a in form]
+
def rankcards(form,vector):
od = OrderedDict(zip(form,vector))
sd = OrderedDict(sorted(od.items(),key = itemgetter(1),reverse=True))
return list(sd.keys())
+ + + +
+ +
+ + + +
+ + +
+ + +
+
+ + + + + +
+
+ +
+
+ + +
+ + + + + + +
+ + + You can’t perform that action at this time. +
+ + + + + + + + + + + + + + +
+ + + + diff --git a/projects/lagmayj/README.md.html b/projects/lagmayj/README.md.html new file mode 100644 index 0000000..a493e95 --- /dev/null +++ b/projects/lagmayj/README.md.html @@ -0,0 +1,1214 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CS362-W2020/README.md at master · jslgmy/CS362-W2020 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Skip to content + + + + + + + + + + + + + +
+ +
+ + +
+ +
+ + + +
+
+
+ + + + + + + + + + + + + + +
+
+ +
    + + + + +
  • + +
    + +
    + + + Watch + + +
    + Notifications +
    +
    + + + + + + + +
    +
    +
    + +
    +
  • + +
  • +
    +
    + + +
    +
    + + +
    + +
  • + +
  • +
    + + Fork + +
    + +

    Fork CS362-W2020

    +
    +
    + +
    +

    If this dialog fails to load, you can visit the fork page directly.

    +
    +
    +
    +
    + + +
  • +
+ +

+ + /CS362-W2020 + + + + forked from CS362-Roberts/CS362-W2020 + +

+ +
+ + + + + + +
+
+
+ + + + + + + + + Permalink + + + + +
+ + +
+ + Branch: + master + + + + + + + +
+ +
+ + Find file + + + Copy path + +
+
+ + +
+ + Find file + + + Copy path + +
+
+ + + + + + +
+
+ + @CS362-W2019 + CS362-W2019 + + Initial Check in for CS362 W2020 + + + + d9e1353 + Jan 1, 2020 + +
+ +
+
+ + 1 contributor + + +
+ +

+ Users who have contributed to this file +

+
+ +
+
+
+
+ + + + + +
+ +
+
+ + 10 lines (6 sloc) + + 928 Bytes +
+ +
+ +
+ Raw + Blame + History +
+ + +
+ + + + +
+ +
+
+ +
+
+
+ + + + + +
+

Dominion

+

complete Dominion game in python, text-only

+

Dominion is a popular deck-building game, for 2-4 players. Each card in the deck gives a player points or money, or lets a player perform a certain action when played. Each player tries to get the most points before the end of the game. As of July 2016, I have only programmed the base version.

+

There are two versions of the game--one that uses a module and a main page, and one that includes all the code in one main page, useful for sharing with a friend who doesn't have python. (It can be executed in an online REPL, hence the name.)

+

The list of players is hard-coded, so edit to play. Add a * at the beginning of the name for a computer player.

+

The computer (currently) plays a very simple direct strategy. Its main advantage is that is has a perfect memory of every transaction, and an accurate, dispassionate calculation of when the game is likely to end.

+
+
+ +
+ + + +
+ + +
+ + +
+
+ + + + + +
+
+ +
+
+ + +
+ + + + + + +
+ + + You can’t perform that action at this time. +
+ + + + + + + + + + + + + + +
+ + + + From 6eb5261c1d92b7d57322c965804e6b34d4bc4744 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 11:03:45 -0800 Subject: [PATCH 05/27] Delete Dominion.py.html --- projects/lagmayj/Dominion.py.html | 5771 ----------------------------- 1 file changed, 5771 deletions(-) delete mode 100644 projects/lagmayj/Dominion.py.html diff --git a/projects/lagmayj/Dominion.py.html b/projects/lagmayj/Dominion.py.html deleted file mode 100644 index 1e92ba0..0000000 --- a/projects/lagmayj/Dominion.py.html +++ /dev/null @@ -1,5771 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CS362-W2020/Dominion.py at master · jslgmy/CS362-W2020 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Skip to content - - - - - - - - - - - - - -
- -
- - -
- -
- - - -
-
-
- - - - - - - - - - - - - - -
-
- -
    - - - - -
  • - -
    - -
    - - - Watch - - -
    - Notifications -
    -
    - - - - - - - -
    -
    -
    - -
    -
  • - -
  • -
    -
    - - -
    -
    - - -
    - -
  • - -
  • -
    - - Fork - -
    - -

    Fork CS362-W2020

    -
    -
    - -
    -

    If this dialog fails to load, you can visit the fork page directly.

    -
    -
    -
    -
    - - -
  • -
- -

- - /CS362-W2020 - - - - forked from CS362-Roberts/CS362-W2020 - -

- -
- - - - - - -
-
-
- - - - - - - - - Permalink - - - - -
- - -
- - Branch: - master - - - - - - - -
- -
- - Find file - - - Copy path - -
-
- - -
- - Find file - - - Copy path - -
-
- - - - - - -
- - -
-
- - 2 contributors - - -
- -

- Users who have contributed to this file -

-
- -
-
- - - @jslgmy - - @CS362-W2019 - - - -
-
- - - - - -
- -
-
- - 762 lines (695 sloc) - - 26.7 KB -
- -
- -
- Raw - Blame - History -
- - -
- - - - -
- -
-
- -
-
-
- - - -
-
- - You're using code navigation to jump to definitions or references. -
- -
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 13 15:26:55 2015
@author: tfleck
"""
import random
from collections import Counter, OrderedDict
from operator import itemgetter
import re
import pandas
-
class Card():
def __init__(self,name,category,cost,buypower,vpoints):
self.name = name
self.category = category
self.cost = cost
self.buypower = buypower
self.vpoints = vpoints
def react(self,player):
return False
-
class Coin_card(Card):
def __init__(self,name,cost,buypower):
Card.__init__(self,name,"coin",cost,buypower,0)
-
class Copper(Coin_card):
def __init__(self):
Coin_card.__init__(self,"Copper",0,1)
class Silver(Coin_card):
def __init__(self):
Coin_card.__init__(self,"Silver",3,2)
class Gold(Coin_card):
def __init__(self):
Coin_card.__init__(self,"Gold",6,3)
-
class Victory_card(Card):
def __init__(self,name,cost,vpoints):
Card.__init__(self,name,"victory",cost,0,vpoints)
-
class Estate(Victory_card):
def __init__(self):
Victory_card.__init__(self,"Estate",2,1)
class Duchy(Victory_card):
def __init__(self):
Victory_card.__init__(self,"Duchy",5,3)
class Province(Victory_card):
def __init__(self):
Victory_card.__init__(self,"Province",8,6)
-
class Gardens(Victory_card):
def __init__(self):
Victory_card.__init__(self,"Gardens",4,0)
-
class Curse(Card):
def __init__(self):
Card.__init__(self,"Curse","curse",0,0,-1)
-
class Action_card(Card):
def __init__(self,name,cost,actions,cards,buys,coins):
Card.__init__(self,name,"action",cost,0,0)
self.actions = actions
self.cards = cards
self.buys = buys
self.coins = coins
def use(self,player,trash):
player.played.append(self)
player.hand.remove(self)
def augment(self,player):
player.actions+=self.actions
player.buys+=self.buys
player.purse+=self.coins
for i in range(self.cards):
player.draw()
def play(self,player,players,supply,trash):
pass
class Woodcutter(Action_card):
def __init__(self):
Action_card.__init__(self,"Woodcutter",3,0,0,1,2)
-
class Smithy(Action_card):
def __init__(self):
Action_card.__init__(self,"Smithy",4,0,3,0,0)
-
class Laboratory(Action_card):
def __init__(self):
Action_card.__init__(self,"Laboratory",5,1,2,0,0)
-
class Village(Action_card):
def __init__(self):
Action_card.__init__(self,"Village",3,2,1,0,0)
-
class Festival(Action_card):
def __init__(self):
Action_card.__init__(self,"Festival",5,2,0,1,2)
-
class Market(Action_card):
def __init__(self):
Action_card.__init__(self,"Market",5,1,1,1,1)
-
class Chancellor(Action_card):
def __init__(self):
Action_card.__init__(self,"Chancellor",3,0,0,0,2)
def play(self,player,players,supply,trash):
if player.yesnoinput('Would you like to discard your entire deck?'):
player.discard = player.discard + player.deck
player.deck = []
-
class Workshop(Action_card):
def __init__(self):
Action_card.__init__(self,"Workshop",3,0,0,0,0)
def play(self,player,players,supply,trash):
player.gaincard(supply,4)
-
class Moneylender(Action_card):
def __init__(self):
Action_card.__init__(self,"Moneylender",4,0,0,0,0)
def play(self,player,players,supply,trash):
c = getcard("Copper",supply,player.hand,"your hand")
if c:
trash.append(c)
player.hand.remove(c)
player.purse += 3
-
class Chapel(Action_card):
def __init__(self):
Action_card.__init__(self,"Chapel",2,0,0,0,0)
def play(self,player,players,supply,trash):
trashed=0
while trashed<4 and len(player.hand)>0:
trashcard = input("Choose a card from your hand to trash: ")
if not trashcard:
break
c = getcard(trashcard,supply,player.hand,"your hand")
if c:
trash.append(c)
player.hand.remove(c)
trashed+=1
-
class Cellar(Action_card):
def __init__(self):
Action_card.__init__(self,"Cellar",2,1,0,0,0)
def play(self,player,players,supply,trash):
discarded=0
while len(player.hand)>0:
dis_card=input("Choose a card from your hand to discard: ")
if not dis_card:
break
c = getcard(dis_card,supply,player.hand,"your hand")
if c:
player.discard.append(c)
player.hand.remove(c)
discarded+=1
for j in range(discarded):
player.draw()
-
class Remodel(Action_card):
def __init__(self):
Action_card.__init__(self,"Remodel",4,0,0,0,0)
def play(self,player,players,supply,trash):
while len(player.hand)>0:
this_card = input("Choose a card from your hand to remodel: ")
c = getcard(this_card,supply,player.hand,"your hand")
if c:
trash.append(c)
player.hand.remove(c)
player.gaincard(supply,c.cost+2)
break
-
class Adventurer(Action_card):
def __init__(self):
Action_card.__init__(self,"Adventurer",6,0,0,0,0)
def play(self,player,players,supply,trash):
coins_added = 0
while (player.deck or player.discard) and coins_added <2:
player.draw()
if player.hand[-1].category == "coin":
coins_added +=1
else:
player.aside.append(player.hand.pop())
-
class Feast(Action_card):
def __init__(self):
Action_card.__init__(self,"Feast",4,0,0,0,0)
def use(self,player,trash):
trash.append(self)
player.hand.remove(self)
def play(self,player,players,supply,trash):
player.gaincard(supply,5)
-
class Mine(Action_card):
def __init__(self):
Action_card.__init__(self,"Mine",5,0,0,0,0)
def play(self,player,players,supply,trash):
while "coin" in catinlist(player.hand):
this_card = input("Choose a coin from your hand to upgrade: ")
c = getcard(this_card,supply,player.hand,"your hand",["coin"])
if c:
trash.append(c)
player.hand.remove(c)
while c.cost>2 or len(supply["Copper"])>0 or len(supply["Silver"])>0:
pick = input("Choose a coin to gain. Any coin up to " + str(c.cost+3)+": ")
g = getcard(pick,supply,categories=["coin"],upto=c.cost+3)
if g:
player.hand.append(g)
supply[pick].remove(g)
break
break
-
class Library(Action_card):
def __init__(self):
Action_card.__init__(self,"Library",5,0,0,0,0)
def play(self,player,players,supply,trash):
while (player.deck or player.discard) and len(player.hand) <7:
player.draw(player.hold)
if (player.hold[-1].category == "action" and not player.yesnoinput("You drew " + player.hold[-1].name + ". Would you like to keep it?",", add to my hand", ", set it aside")):
player.aside.append(player.hold.pop())
else:
player.hand.append(player.hold.pop())
-
class Moat(Action_card):
def __init__(self):
Action_card.__init__(self,"Moat",2,0,2,0,0)
def react(self,player):
player.show()
return player.yesnoinput(player.name + ", you have a " + self.name +
" in your hand. Do you want to block the attack?")
-
class Council_Room(Action_card):
def __init__(self):
Action_card.__init__(self,"Council Room",5,0,3,1,0)
def play(self,this_player,players,supply,trash):
for player in players:
player.draw()
-
class Witch(Action_card):
def __init__(self):
Action_card.__init__(self,"Witch",5,0,2,0,0)
def play(self,this_player,players,supply,trash):
if len(supply["Curse"])>0:
for player in players:
if player==this_player:
pass
else:
for c in player.hand:
if c.react(player):
break
else:
if len(supply["Curse"])>0:
player.discard.append(supply["Curse"].pop())
-
class Bureaucrat(Action_card):
def __init__(self):
Action_card.__init__(self,"Bureaucrat",4,0,0,0,0)
def play(self,this_player,players,supply,trash):
if len(supply["Silver"])>0:
this_player.deck.insert(0,supply["Silver"].pop())
for player in players:
if (not player==this_player) and ("victory" in catinlist(player.hand)):
for c in player.hand:
if c.react(player):
break
else:
player.show()
while True:
putback = player.choose_discard(player.name + ", which victory card" +
" do you want to put on top of your deck?\n--> ")
c = getcard(putback,supply,player.hand,"your hand",["victory"])
if c:
player.hand.remove(c)
player.deck.insert(0,c)
break
class Militia(Action_card):
def __init__(self):
Action_card.__init__(self,"Militia",4,0,0,0,2)
def play(self,this_player,players,supply,trash):
for player in players:
if (not player==this_player) and len(player.hand)>3:
for c in player.hand:
if c.react(player):
break
else:
player.show()
while len(player.hand)>3:
dis_card=player.choose_discard(player.name + ", choose a card from your hand to discard: ")
if dis_card:
c = getcard(dis_card,supply,player.hand,"your hand")
if c:
player.hand.remove(c)
player.discard.append(c)
class Spy(Action_card):
def __init__(self):
Action_card.__init__(self,"Spy",4,1,1,0,0)
def play(self,this_player,players,supply,trash):
for player in players:
for c in player.hand:
if c.react(player):
break
else:
b = player.draw([])
if not b:
continue
else:
print("The first card in the deck of " + player.name + " is " + b.name)
if this_player.yesnoinput(this_player.name + ", do you want " + player.name + " to discard this?",
", discard",", keep"):
player.discard.append(b)
else:
player.deck.insert(0,b)
#check logic of this structure
-
class Thief(Action_card):
def __init__(self):
Action_card.__init__(self,"Thief",4,0,0,0,0)
def play(self,this_player,players,supply,trash):
for player in players:
if player == this_player:
continue
for c in player.hand:
if c.react(player):
break
else:
for i in range(2):
player.draw(player.hold)
print( player.name, namesinlist(player.hold))
if "coin" in catinlist(player.hold):
while True:
burn = input("Which card would you like " + player.name + " to trash?\n-->")
c = getcard(burn,supply,player.hold," the top 2 cards",["coin"])
if c:
player.hold.remove(c)
break
if this_player.yesnoinput("Do you want to steal it?",", it's mine!",", leave it in the trash."):
this_player.discard.append(c)
else:
trash.append(c)
player.discard=player.discard+player.hold
player.hold = []
-
class Throne_Room(Action_card):
def __init__(self):
Action_card.__init__(self,"Throne Room",4,0,0,0,0)
def play(self,player,players,supply,trash):
if "action" in catinlist(player.hand):
while True:
double = input("What card would you like to double?\n--> ")
c = getcard(double,supply,player.hand," your hand",["action"])
if c:
c.use(player,trash)
c.augment(player)
c.play(player,players,supply,trash)
player.show()
c.augment(player)
c.play(player,players,supply,trash)
break
-
class Player():
def __init__(self,name):
self.name = name
self.hand = []
self.deck = [Copper()]*7 + [Estate()]*3
random.shuffle(self.deck)
self.played = []
self.discard = []
self.aside = []
self.hold = []
for i in range(5):
self.draw()
-
def other(self):
return self.played+self.discard+self.hold+self.aside
def stack(self):
return (self.deck + self.hand + self.played + self.discard + self.aside + self.hold)
def draw(self,dest=None):
#defualt destination is player's hand
if dest==None:
dest = self.hand
#Replenish deck if necessary.
if len(self.deck)==0:
self.deck = self.discard
self.discard = []
random.shuffle(self.deck)
#If deck has cards, add card to destination list
if len(self.deck)>0:
c = self.deck.pop(0)
dest.append(c)
return c
def turn(self,players,supply,trash):
self.show()
self.actions = 1
self.buys = 1
self.purse = 0
#action phase
while self.actions>0 and 'action' in catinlist(self.hand):
playthis = input("Which card would you like to play? You have " +
str(self.actions) + " action(s). \n-Hit enter for no play. --> ")
if playthis:
c = getcard(playthis,supply,self.hand,"your hand",['action'])
if c:
self.actions = self.actions - 1
c.use(self,trash)
c.augment(self)
c.play(self,players,supply,trash)
self.show()
else:
self.actions=0
#buy phase
for c in self.hand:
self.purse += c.buypower
while self.buys>0:
buy_string = "Buying power is " + str(self.purse) + ". You have " + str(self.buys) + " buy"
if self.buys>1:
buy_string += "s"
buy_string += "."
print( buy_string)
purchase = input("What would you like to purchase? -Hit enter for no purchase.-\n--> ")
if not purchase:
break
else:
c = getcard(purchase,supply,upto=self.purse)
if c:
self.discard.append(supply[purchase].pop())
self.buys = self.buys -1
self.purse = self.purse - c.cost
#cleanup phase
self.discard = self.discard + self.played + self.hand + self.aside
self.played = []
self.hand = []
self.aside = []
for i in range(5):
self.draw()
-
def gaincard(self,supply,upto):
while True:
gain = input("What would you like to get? Any card up to " + str(upto) + "\n--> ")
c = getcard(gain,supply,upto=upto)
if c:
self.discard.append(c)
supply[gain].remove(c)
break
def yesnoinput(self,prompt,yesstring='',nostring=''):
print(prompt + "\n1 - Yes" + yesstring + "\n0 - No" + nostring)
while True:
r = input("--> ")
if r == "0":
return False
if r == "1":
return True
def choose_discard(self,prompt):
return input(prompt)
-
def show(self):
print (self.name)
print ("hand:", ", ".join(sorted(namesinlist(self.hand))))
if len(self.deck)>0:
print ("deck (alphabetical order):", ", ".join(sorted(namesinlist(self.deck))))
if len(self.discard)>0:
print ("discard:", ", ".join(sorted(namesinlist(self.discard))))
if len(self.played)>0:
print ("played:", ", ".join(sorted(namesinlist(self.played))))
if len(self.aside)>0:
print ("aside:", ", ".join(sorted(namesinlist(self.aside))))
print ("\r")
def action_balance(self):
balance = 0
for c in self.stack():
if c.category == "action":
balance = balance - 1 + c.actions
return 70*balance / len(self.stack())
-
def cardsummary(self):
summary = {}
for c in self.stack():
if c.name in summary:
summary[c.name] += 1
else:
summary[c.name] = 1
summary['VICTORY POINTS']=self.calcpoints()
return summary
-
def calcpoints(self):
tally = 0
gardens = 0
n = 0
for c in self.stack():
tally += c.vpoints
n += 1
if c.name == "Gardens":
gardens+=1
return tally + n//10 * gardens
-
class ComputerPlayer(Player):
def __init__(self,name):
Player.__init__(self,name)
self.index = 0
self.buygaintable = []
#beginning and middle of game
self.buygaintable1 = ["Province","Gold","Laboratory","Festival","Witch",
"Council Room","Market","Militia","Adventurer","Smithy","Bureaucrat","Silver","Moat",""]
#end of game
self.buygaintable2 = ["Province","Gardens","Duchy","Estate","Gold","Silver",""]
#beginning and middle of the game, too many action cards
self.buygaintable3 = ["Province","Gold","Festival","Laboratory","Market","Village",
"Silver",""]
self.playtable1 = ["Village","Festival","Market","Laboratory","Witch",
"Council Room","Militia","Adventurer","Smithy","Bureaucrat","Moat",""]
self.discardtable1 = ["Gardens","Duchy","Province","Estate","Curse","Copper",
"Village","Bureaucrat","Silver","Militia","Smithy","Council Room","Witch",
"Festival","Market","Adventurer","Laboratory","Gold","Moat"]
def turn(self,players,supply,trash):
self.show()
self.actions = 1
self.buys = 1
self.purse = 0
#action phase
self.index = 0
while self.actions>0 and 'action' in catinlist(self.hand):
playthis = self.playtable1[self.index]
if playthis:
c = self.getcard(playthis,supply,self.hand,"your hand",['action'])
if c:
print (self.name + " plays " + c.name)
self.actions = self.actions - 1
c.use(self,trash)
self.index=0
c.augment(self)
c.play(self,players,supply,trash)
self.show()
else:
self.index += 1
else:
self.actions=0
#buy phase
if len(supply["Province"])>len(players)+totalbuypower(self.deck)/8:
if self.action_balance()<-10:
self.buygaintable = self.buygaintable3
else:
self.buygaintable = self.buygaintable1
else:
self.buygaintable = self.buygaintable2
self.index = 0
for c in self.hand:
self.purse += c.buypower
while self.buys>0:
purchase = self.buygaintable[self.index]
if not purchase:
break
else:
c = self.getcard(purchase,supply,upto=self.purse)
if c:
self.discard.append(supply[purchase].pop())
self.index = 0
self.buys = self.buys -1
self.purse = self.purse - c.cost
print (self.name + " bought " + c.name)
else:
self.index += 1
#cleanup phase
self.discard = self.discard + self.played + self.hand + self.aside
self.played = []
self.hand = []
self.aside = []
for i in range(5):
self.draw()
def getcard(self,name,supply,target_list=None,target_name= "the supply anymore",categories=['action','coin','curse','victory'],upto=100):
if not name in supply:
#print name + " is not in this game."
return None
if not target_list:
target_list = supply[name]
nameslist = namesinlist(target_list)
if not name in nameslist:
#print "There is no " + name + " in " + target_name
return None
i = nameslist.index(name)
c = target_list[i]
if c.category not in categories:
catstring = categories[0]
for i in categories[1:]:
catstring = catstring + " or " + categories[i]
#print name + " is not a " + catstring + " card."
return None
if c.cost > upto:
#print name + " costs " + str(c.cost)
return None
return c
def choose_discard(self,prompt):
index = 0
while True:
dis_card = self.discardtable1[index]
c = self.getcard(dis_card,[dis_card],self.hand)
if c:
return c.name
else:
index+=1
def yesnoinput(self,prompt,yesstring='',nostring=''):
return True
-
def show(self):
pass
-
class TablePlayer(ComputerPlayer):
def __init__(self,name):
ComputerPlayer.__init__(self,name)
self.index=0
self.buygaintable=[]
q=re.match(r'([a-zA-Z]+)(\d+)',name)
if q:
self.number = q.group(2)
else:
print(name)
self.playtable1 = ["Village","Festival","Market","Laboratory","Witch",
"Council Room","Militia","Adventurer","Smithy","Bureaucrat","Moat",""]
self.discardtable1 = ["Gardens","Duchy","Province","Estate","Curse","Copper",
"Village","Bureaucrat","Silver","Militia","Smithy","Council Room","Witch",
"Festival","Market","Adventurer","Laboratory","Gold","Moat"]
def turn(self,players,supply,trash):
self.show()
self.actions = 1
self.buys = 1
self.purse = 0
#action phase
self.index = 0
while self.actions>0 and 'action' in catinlist(self.hand):
playthis = self.playtable1[self.index]
if playthis:
c = self.getcard(playthis,supply,self.hand,"your hand",['action'])
if c:
#print (self.name + " plays " + c.name)
self.actions = self.actions - 1
c.use(self,trash)
self.index=0
c.augment(self)
c.play(self,players,supply,trash)
self.show()
else:
self.index += 1
else:
self.actions=0
#buy phase
v= self.number
csvstring = r"Dominionbuy" + str(v) + ".csv"
buydf=pandas.read_csv(csvstring,na_filter=False)
sortedbuydf=buydf.sort_values("Buyvalues",ascending=False)
ranktable = sortedbuydf.Cardname
self.index = 0
for c in self.hand:
self.purse += c.buypower
while self.buys>0:
purchase = ranktable.iloc[self.index]
if not purchase:
break
else:
c = self.getcard(purchase,supply,upto=self.purse)
if c:
self.discard.append(supply[purchase].pop())
self.index = 0
self.buys = self.buys -1
self.purse = self.purse - c.cost
else:
self.index += 1
#cleanup phase
self.discard = self.discard + self.played + self.hand + self.aside
self.played = []
self.hand = []
self.aside = []
for i in range(5):
self.draw()
def gameover(supply):
if len(supply["Province"])==0:
return True
out = 0
for stack in supply:
if len(supply[stack])==0:
out+=1
if out>=3:
return True
return False
-
def namesinlist(cardlist):
namelist = []
for c in cardlist:
namelist.append(c.name)
return namelist
-
def catinlist(cardlist):
catlist = []
for c in cardlist:
catlist.append(c.category)
return catlist
-
def getcard(name,supply,target_list=None,target_name= "the supply anymore",categories=['action','coin','curse','victory'],upto=100):
if not name in supply:
print( name + " is not in this game.")
return None
if not target_list:
target_list = supply[name]
nameslist = namesinlist(target_list)
if not name in nameslist:
print ("There is no " + name + " in " + target_name)
return None
i = nameslist.index(name)
c = target_list[i]
if c.category not in categories:
catstring = categories[0]
for i in categories[1:]:
catstring = catstring + " or " + categories[i]
print (name + " is not a " + catstring + " card.")
return None
if c.cost > upto:
print (name + " costs " + str(c.cost))
return None
return c
-
def totalbuypower(cardlist):
TBP = 0
for c in cardlist:
TBP += c.buypower
if c.category == "action":
TBP += c.coins
return TBP
-
def cardsummaries(players):
cardsums={}
for player in players:
cardsums[player.name]=player.cardsummary()
cardsdf = pandas.DataFrame(cardsums).fillna(0).sort_index()
vp=cardsdf.loc[['VICTORY POINTS']]
cardsdf.drop(['VICTORY POINTS'],inplace=True)
return pandas.concat([cardsdf,vp],axis=0).fillna(0).astype(int)
-
def countsupply(supply,form):
return [len(supply[a]) for a in form]
-
def countcards(cards,form):
dcount = Counter(namesinlist(cards))
return [dcount[a] for a in form]
-
def rankcards(form,vector):
od = OrderedDict(zip(form,vector))
sd = OrderedDict(sorted(od.items(),key = itemgetter(1),reverse=True))
return list(sd.keys())
- - - -
- -
- - - -
- - -
- - -
-
- - - - - -
-
- -
-
- - -
- - - - - - -
- - - You can’t perform that action at this time. -
- - - - - - - - - - - - - - -
- - - - From 93142eb5cb8e7fd67b018b8b4154b14c61195bbb Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 11:03:53 -0800 Subject: [PATCH 06/27] Delete README.md.html --- projects/lagmayj/README.md.html | 1214 ------------------------------- 1 file changed, 1214 deletions(-) delete mode 100644 projects/lagmayj/README.md.html diff --git a/projects/lagmayj/README.md.html b/projects/lagmayj/README.md.html deleted file mode 100644 index a493e95..0000000 --- a/projects/lagmayj/README.md.html +++ /dev/null @@ -1,1214 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CS362-W2020/README.md at master · jslgmy/CS362-W2020 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Skip to content - - - - - - - - - - - - - -
- -
- - -
- -
- - - -
-
-
- - - - - - - - - - - - - - -
-
- -
    - - - - -
  • - -
    - -
    - - - Watch - - -
    - Notifications -
    -
    - - - - - - - -
    -
    -
    - -
    -
  • - -
  • -
    -
    - - -
    -
    - - -
    - -
  • - -
  • -
    - - Fork - -
    - -

    Fork CS362-W2020

    -
    -
    - -
    -

    If this dialog fails to load, you can visit the fork page directly.

    -
    -
    -
    -
    - - -
  • -
- -

- - /CS362-W2020 - - - - forked from CS362-Roberts/CS362-W2020 - -

- -
- - - - - - -
-
-
- - - - - - - - - Permalink - - - - -
- - -
- - Branch: - master - - - - - - - -
- -
- - Find file - - - Copy path - -
-
- - -
- - Find file - - - Copy path - -
-
- - - - - - -
-
- - @CS362-W2019 - CS362-W2019 - - Initial Check in for CS362 W2020 - - - - d9e1353 - Jan 1, 2020 - -
- -
-
- - 1 contributor - - -
- -

- Users who have contributed to this file -

-
- -
-
-
-
- - - - - -
- -
-
- - 10 lines (6 sloc) - - 928 Bytes -
- -
- -
- Raw - Blame - History -
- - -
- - - - -
- -
-
- -
-
-
- - - - - -
-

Dominion

-

complete Dominion game in python, text-only

-

Dominion is a popular deck-building game, for 2-4 players. Each card in the deck gives a player points or money, or lets a player perform a certain action when played. Each player tries to get the most points before the end of the game. As of July 2016, I have only programmed the base version.

-

There are two versions of the game--one that uses a module and a main page, and one that includes all the code in one main page, useful for sharing with a friend who doesn't have python. (It can be executed in an online REPL, hence the name.)

-

The list of players is hard-coded, so edit to play. Add a * at the beginning of the name for a computer player.

-

The computer (currently) plays a very simple direct strategy. Its main advantage is that is has a perfect memory of every transaction, and an accurate, dispassionate calculation of when the game is likely to end.

-
-
- -
- - - -
- - -
- - -
-
- - - - - -
-
- -
-
- - -
- - - - - - -
- - - You can’t perform that action at this time. -
- - - - - - - - - - - - - - -
- - - - From 0c370308b8dc0f34dd266d85c3e87c5067db485d Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:47:26 -0800 Subject: [PATCH 07/27] dominion --- projects/lagmayj/Dominion.py | 762 +++++++++++++++++++++++++++ projects/lagmayj/README.md | 10 + projects/lagmayj/REPLdominion.py | 853 +++++++++++++++++++++++++++++++ projects/lagmayj/playDominion.py | 119 +++++ 4 files changed, 1744 insertions(+) create mode 100644 projects/lagmayj/Dominion.py create mode 100644 projects/lagmayj/README.md create mode 100644 projects/lagmayj/REPLdominion.py create mode 100644 projects/lagmayj/playDominion.py diff --git a/projects/lagmayj/Dominion.py b/projects/lagmayj/Dominion.py new file mode 100644 index 0000000..d8deecf --- /dev/null +++ b/projects/lagmayj/Dominion.py @@ -0,0 +1,762 @@ +# -*- coding: utf-8 -*- +""" +Created on Tue Oct 13 15:26:55 2015 + +@author: tfleck +""" +import random +from collections import Counter, OrderedDict +from operator import itemgetter +import re +import pandas + +class Card(): + def __init__(self,name,category,cost,buypower,vpoints): + self.name = name + self.category = category + self.cost = cost + self.buypower = buypower + self.vpoints = vpoints + def react(self,player): + return False + +class Coin_card(Card): + def __init__(self,name,cost,buypower): + Card.__init__(self,name,"coin",cost,buypower,0) + +class Copper(Coin_card): + def __init__(self): + Coin_card.__init__(self,"Copper",0,1) + +class Silver(Coin_card): + def __init__(self): + Coin_card.__init__(self,"Silver",3,2) + +class Gold(Coin_card): + def __init__(self): + Coin_card.__init__(self,"Gold",6,3) + +class Victory_card(Card): + def __init__(self,name,cost,vpoints): + Card.__init__(self,name,"victory",cost,0,vpoints) + +class Estate(Victory_card): + def __init__(self): + Victory_card.__init__(self,"Estate",2,1) + +class Duchy(Victory_card): + def __init__(self): + Victory_card.__init__(self,"Duchy",5,3) + +class Province(Victory_card): + def __init__(self): + Victory_card.__init__(self,"Province",8,6) + +class Gardens(Victory_card): + def __init__(self): + Victory_card.__init__(self,"Gardens",4,0) + +class Curse(Card): + def __init__(self): + Card.__init__(self,"Curse","curse",0,0,-1) + +class Action_card(Card): + def __init__(self,name,cost,actions,cards,buys,coins): + Card.__init__(self,name,"action",cost,0,0) + self.actions = actions + self.cards = cards + self.buys = buys + self.coins = coins + def use(self,player,trash): + player.played.append(self) + player.hand.remove(self) + def augment(self,player): + player.actions+=self.actions + player.buys+=self.buys + player.purse+=self.coins + for i in range(self.cards): + player.draw() + def play(self,player,players,supply,trash): + pass + +class Woodcutter(Action_card): + def __init__(self): + Action_card.__init__(self,"Woodcutter",3,0,0,1,2) + +class Smithy(Action_card): + def __init__(self): + Action_card.__init__(self,"Smithy",4,0,3,0,0) + +class Laboratory(Action_card): + def __init__(self): + Action_card.__init__(self,"Laboratory",5,1,2,0,0) + +class Village(Action_card): + def __init__(self): + Action_card.__init__(self,"Village",3,2,1,0,0) + +class Festival(Action_card): + def __init__(self): + Action_card.__init__(self,"Festival",5,2,0,1,2) + +class Market(Action_card): + def __init__(self): + Action_card.__init__(self,"Market",5,1,1,1,1) + +class Chancellor(Action_card): + def __init__(self): + Action_card.__init__(self,"Chancellor",3,0,0,0,2) + def play(self,player,players,supply,trash): + if player.yesnoinput('Would you like to discard your entire deck?'): + player.discard = player.discard + player.deck + player.deck = [] + +class Workshop(Action_card): + def __init__(self): + Action_card.__init__(self,"Workshop",3,0,0,0,0) + def play(self,player,players,supply,trash): + player.gaincard(supply,4) + +class Moneylender(Action_card): + def __init__(self): + Action_card.__init__(self,"Moneylender",4,0,0,0,0) + def play(self,player,players,supply,trash): + c = getcard("Copper",supply,player.hand,"your hand") + if c: + trash.append(c) + player.hand.remove(c) + player.purse += 3 + +class Chapel(Action_card): + def __init__(self): + Action_card.__init__(self,"Chapel",2,0,0,0,0) + def play(self,player,players,supply,trash): + trashed=0 + while trashed<4 and len(player.hand)>0: + trashcard = input("Choose a card from your hand to trash: ") + if not trashcard: + break + c = getcard(trashcard,supply,player.hand,"your hand") + if c: + trash.append(c) + player.hand.remove(c) + trashed+=1 + +class Cellar(Action_card): + def __init__(self): + Action_card.__init__(self,"Cellar",2,1,0,0,0) + def play(self,player,players,supply,trash): + discarded=0 + while len(player.hand)>0: + dis_card=input("Choose a card from your hand to discard: ") + if not dis_card: + break + c = getcard(dis_card,supply,player.hand,"your hand") + if c: + player.discard.append(c) + player.hand.remove(c) + discarded+=1 + for j in range(discarded): + player.draw() + +class Remodel(Action_card): + def __init__(self): + Action_card.__init__(self,"Remodel",4,0,0,0,0) + def play(self,player,players,supply,trash): + while len(player.hand)>0: + this_card = input("Choose a card from your hand to remodel: ") + c = getcard(this_card,supply,player.hand,"your hand") + if c: + trash.append(c) + player.hand.remove(c) + player.gaincard(supply,c.cost+2) + break + +class Adventurer(Action_card): + def __init__(self): + Action_card.__init__(self,"Adventurer",6,0,0,0,0) + def play(self,player,players,supply,trash): + coins_added = 0 + while (player.deck or player.discard) and coins_added <2: + player.draw() + if player.hand[-1].category == "coin": + coins_added +=1 + else: + player.aside.append(player.hand.pop()) + +class Feast(Action_card): + def __init__(self): + Action_card.__init__(self,"Feast",4,0,0,0,0) + def use(self,player,trash): + trash.append(self) + player.hand.remove(self) + def play(self,player,players,supply,trash): + player.gaincard(supply,5) + +class Mine(Action_card): + def __init__(self): + Action_card.__init__(self,"Mine",5,0,0,0,0) + def play(self,player,players,supply,trash): + while "coin" in catinlist(player.hand): + this_card = input("Choose a coin from your hand to upgrade: ") + c = getcard(this_card,supply,player.hand,"your hand",["coin"]) + if c: + trash.append(c) + player.hand.remove(c) + while c.cost>2 or len(supply["Copper"])>0 or len(supply["Silver"])>0: + pick = input("Choose a coin to gain. Any coin up to " + str(c.cost+3)+": ") + g = getcard(pick,supply,categories=["coin"],upto=c.cost+3) + if g: + player.hand.append(g) + supply[pick].remove(g) + break + break + +class Library(Action_card): + def __init__(self): + Action_card.__init__(self,"Library",5,0,0,0,0) + def play(self,player,players,supply,trash): + while (player.deck or player.discard) and len(player.hand) <7: + player.draw(player.hold) + if (player.hold[-1].category == "action" and not player.yesnoinput("You drew " + player.hold[-1].name + ". Would you like to keep it?",", add to my hand", ", set it aside")): + player.aside.append(player.hold.pop()) + else: + player.hand.append(player.hold.pop()) + +class Moat(Action_card): + def __init__(self): + Action_card.__init__(self,"Moat",2,0,2,0,0) + def react(self,player): + player.show() + return player.yesnoinput(player.name + ", you have a " + self.name + + " in your hand. Do you want to block the attack?") + +class Council_Room(Action_card): + def __init__(self): + Action_card.__init__(self,"Council Room",5,0,3,1,0) + def play(self,this_player,players,supply,trash): + for player in players: + player.draw() + +class Witch(Action_card): + def __init__(self): + Action_card.__init__(self,"Witch",5,0,2,0,0) + def play(self,this_player,players,supply,trash): + if len(supply["Curse"])>0: + for player in players: + if player==this_player: + pass + else: + for c in player.hand: + if c.react(player): + break + else: + if len(supply["Curse"])>0: + player.discard.append(supply["Curse"].pop()) + +class Bureaucrat(Action_card): + def __init__(self): + Action_card.__init__(self,"Bureaucrat",4,0,0,0,0) + def play(self,this_player,players,supply,trash): + if len(supply["Silver"])>0: + this_player.deck.insert(0,supply["Silver"].pop()) + for player in players: + if (not player==this_player) and ("victory" in catinlist(player.hand)): + for c in player.hand: + if c.react(player): + break + else: + player.show() + while True: + putback = player.choose_discard(player.name + ", which victory card" + + " do you want to put on top of your deck?\n--> ") + c = getcard(putback,supply,player.hand,"your hand",["victory"]) + if c: + player.hand.remove(c) + player.deck.insert(0,c) + break + +class Militia(Action_card): + def __init__(self): + Action_card.__init__(self,"Militia",4,0,0,0,2) + def play(self,this_player,players,supply,trash): + for player in players: + if (not player==this_player) and len(player.hand)>3: + for c in player.hand: + if c.react(player): + break + else: + player.show() + while len(player.hand)>3: + dis_card=player.choose_discard(player.name + ", choose a card from your hand to discard: ") + if dis_card: + c = getcard(dis_card,supply,player.hand,"your hand") + if c: + player.hand.remove(c) + player.discard.append(c) + +class Spy(Action_card): + def __init__(self): + Action_card.__init__(self,"Spy",4,1,1,0,0) + def play(self,this_player,players,supply,trash): + for player in players: + for c in player.hand: + if c.react(player): + break + else: + b = player.draw([]) + if not b: + continue + else: + print("The first card in the deck of " + player.name + " is " + b.name) + if this_player.yesnoinput(this_player.name + ", do you want " + player.name + " to discard this?", + ", discard",", keep"): + player.discard.append(b) + else: + player.deck.insert(0,b) + #check logic of this structure + +class Thief(Action_card): + def __init__(self): + Action_card.__init__(self,"Thief",4,0,0,0,0) + def play(self,this_player,players,supply,trash): + for player in players: + if player == this_player: + continue + for c in player.hand: + if c.react(player): + break + else: + for i in range(2): + player.draw(player.hold) + print( player.name, namesinlist(player.hold)) + if "coin" in catinlist(player.hold): + while True: + burn = input("Which card would you like " + player.name + " to trash?\n-->") + c = getcard(burn,supply,player.hold," the top 2 cards",["coin"]) + if c: + player.hold.remove(c) + break + if this_player.yesnoinput("Do you want to steal it?",", it's mine!",", leave it in the trash."): + this_player.discard.append(c) + else: + trash.append(c) + player.discard=player.discard+player.hold + player.hold = [] + +class Throne_Room(Action_card): + def __init__(self): + Action_card.__init__(self,"Throne Room",4,0,0,0,0) + def play(self,player,players,supply,trash): + if "action" in catinlist(player.hand): + while True: + double = input("What card would you like to double?\n--> ") + c = getcard(double,supply,player.hand," your hand",["action"]) + if c: + c.use(player,trash) + c.augment(player) + c.play(player,players,supply,trash) + player.show() + c.augment(player) + c.play(player,players,supply,trash) + break + + +class Player(): + def __init__(self,name): + self.name = name + self.hand = [] + self.deck = [Copper()]*7 + [Estate()]*3 + random.shuffle(self.deck) + self.played = [] + self.discard = [] + self.aside = [] + self.hold = [] + for i in range(5): + self.draw() + + def other(self): + return self.played+self.discard+self.hold+self.aside + def stack(self): + return (self.deck + self.hand + self.played + self.discard + self.aside + self.hold) + def draw(self,dest=None): + #defualt destination is player's hand + if dest==None: + dest = self.hand + #Replenish deck if necessary. + if len(self.deck)==0: + self.deck = self.discard + self.discard = [] + random.shuffle(self.deck) + #If deck has cards, add card to destination list + if len(self.deck)>0: + c = self.deck.pop(0) + dest.append(c) + return c + def turn(self,players,supply,trash): + self.show() + self.actions = 1 + self.buys = 1 + self.purse = 0 + #action phase + while self.actions>0 and 'action' in catinlist(self.hand): + playthis = input("Which card would you like to play? You have " + + str(self.actions) + " action(s). \n-Hit enter for no play. --> ") + if playthis: + c = getcard(playthis,supply,self.hand,"your hand",['action']) + if c: + self.actions = self.actions - 1 + c.use(self,trash) + c.augment(self) + c.play(self,players,supply,trash) + self.show() + else: + self.actions=0 + #buy phase + for c in self.hand: + self.purse += c.buypower + while self.buys>0: + buy_string = "Buying power is " + str(self.purse) + ". You have " + str(self.buys) + " buy" + if self.buys>1: + buy_string += "s" + buy_string += "." + print( buy_string) + purchase = input("What would you like to purchase? -Hit enter for no purchase.-\n--> ") + if not purchase: + break + else: + c = getcard(purchase,supply,upto=self.purse) + if c: + self.discard.append(supply[purchase].pop()) + self.buys = self.buys -1 + self.purse = self.purse - c.cost + + #cleanup phase + self.discard = self.discard + self.played + self.hand + self.aside + self.played = [] + self.hand = [] + self.aside = [] + for i in range(5): + self.draw() + + def gaincard(self,supply,upto): + while True: + gain = input("What would you like to get? Any card up to " + str(upto) + "\n--> ") + c = getcard(gain,supply,upto=upto) + if c: + self.discard.append(c) + supply[gain].remove(c) + break + def yesnoinput(self,prompt,yesstring='',nostring=''): + print(prompt + "\n1 - Yes" + yesstring + "\n0 - No" + nostring) + while True: + r = input("--> ") + if r == "0": + return False + if r == "1": + return True + + def choose_discard(self,prompt): + return input(prompt) + + def show(self): + print (self.name) + print ("hand:", ", ".join(sorted(namesinlist(self.hand)))) + if len(self.deck)>0: + print ("deck (alphabetical order):", ", ".join(sorted(namesinlist(self.deck)))) + if len(self.discard)>0: + print ("discard:", ", ".join(sorted(namesinlist(self.discard)))) + if len(self.played)>0: + print ("played:", ", ".join(sorted(namesinlist(self.played)))) + if len(self.aside)>0: + print ("aside:", ", ".join(sorted(namesinlist(self.aside)))) + print ("\r") + + def action_balance(self): + balance = 0 + for c in self.stack(): + if c.category == "action": + balance = balance - 1 + c.actions + return 70*balance / len(self.stack()) + + def cardsummary(self): + summary = {} + for c in self.stack(): + if c.name in summary: + summary[c.name] += 1 + else: + summary[c.name] = 1 + summary['VICTORY POINTS']=self.calcpoints() + return summary + + def calcpoints(self): + tally = 0 + gardens = 0 + n = 0 + for c in self.stack(): + tally += c.vpoints + n += 1 + if c.name == "Gardens": + gardens+=1 + return tally + n//10 * gardens + +class ComputerPlayer(Player): + def __init__(self,name): + Player.__init__(self,name) + self.index = 0 + self.buygaintable = [] + #beginning and middle of game + self.buygaintable1 = ["Province","Gold","Laboratory","Festival","Witch", + "Council Room","Market","Militia","Adventurer","Smithy","Bureaucrat","Silver","Moat",""] + #end of game + self.buygaintable2 = ["Province","Gardens","Duchy","Estate","Gold","Silver",""] + #beginning and middle of the game, too many action cards + self.buygaintable3 = ["Province","Gold","Festival","Laboratory","Market","Village", + "Silver",""] + self.playtable1 = ["Village","Festival","Market","Laboratory","Witch", + "Council Room","Militia","Adventurer","Smithy","Bureaucrat","Moat",""] + self.discardtable1 = ["Gardens","Duchy","Province","Estate","Curse","Copper", + "Village","Bureaucrat","Silver","Militia","Smithy","Council Room","Witch", + "Festival","Market","Adventurer","Laboratory","Gold","Moat"] + + def turn(self,players,supply,trash): + self.show() + self.actions = 1 + self.buys = 1 + self.purse = 0 + #action phase + self.index = 0 + while self.actions>0 and 'action' in catinlist(self.hand): + playthis = self.playtable1[self.index] + if playthis: + c = self.getcard(playthis,supply,self.hand,"your hand",['action']) + if c: + print (self.name + " plays " + c.name) + self.actions = self.actions - 1 + c.use(self,trash) + self.index=0 + c.augment(self) + c.play(self,players,supply,trash) + self.show() + else: + self.index += 1 + else: + self.actions=0 + #buy phase + if len(supply["Province"])>len(players)+totalbuypower(self.deck)/8: + if self.action_balance()<-10: + self.buygaintable = self.buygaintable3 + else: + self.buygaintable = self.buygaintable1 + else: + self.buygaintable = self.buygaintable2 + self.index = 0 + for c in self.hand: + self.purse += c.buypower + while self.buys>0: + purchase = self.buygaintable[self.index] + if not purchase: + break + else: + c = self.getcard(purchase,supply,upto=self.purse) + if c: + self.discard.append(supply[purchase].pop()) + self.index = 0 + self.buys = self.buys -1 + self.purse = self.purse - c.cost + print (self.name + " bought " + c.name) + else: + self.index += 1 + + #cleanup phase + self.discard = self.discard + self.played + self.hand + self.aside + self.played = [] + self.hand = [] + self.aside = [] + for i in range(5): + self.draw() + + def getcard(self,name,supply,target_list=None,target_name= "the supply anymore",categories=['action','coin','curse','victory'],upto=100): + if not name in supply: + #print name + " is not in this game." + return None + if not target_list: + target_list = supply[name] + nameslist = namesinlist(target_list) + if not name in nameslist: + #print "There is no " + name + " in " + target_name + return None + i = nameslist.index(name) + c = target_list[i] + if c.category not in categories: + catstring = categories[0] + for i in categories[1:]: + catstring = catstring + " or " + categories[i] + #print name + " is not a " + catstring + " card." + return None + if c.cost > upto: + #print name + " costs " + str(c.cost) + return None + return c + + def choose_discard(self,prompt): + index = 0 + while True: + dis_card = self.discardtable1[index] + c = self.getcard(dis_card,[dis_card],self.hand) + if c: + return c.name + else: + index+=1 + + def yesnoinput(self,prompt,yesstring='',nostring=''): + return True + + def show(self): + pass + +class TablePlayer(ComputerPlayer): + def __init__(self,name): + ComputerPlayer.__init__(self,name) + self.index=0 + self.buygaintable=[] + q=re.match(r'([a-zA-Z]+)(\d+)',name) + if q: + self.number = q.group(2) + else: + print(name) + self.playtable1 = ["Village","Festival","Market","Laboratory","Witch", + "Council Room","Militia","Adventurer","Smithy","Bureaucrat","Moat",""] + self.discardtable1 = ["Gardens","Duchy","Province","Estate","Curse","Copper", + "Village","Bureaucrat","Silver","Militia","Smithy","Council Room","Witch", + "Festival","Market","Adventurer","Laboratory","Gold","Moat"] + + def turn(self,players,supply,trash): + self.show() + self.actions = 1 + self.buys = 1 + self.purse = 0 + #action phase + self.index = 0 + while self.actions>0 and 'action' in catinlist(self.hand): + playthis = self.playtable1[self.index] + if playthis: + c = self.getcard(playthis,supply,self.hand,"your hand",['action']) + if c: + #print (self.name + " plays " + c.name) + self.actions = self.actions - 1 + c.use(self,trash) + self.index=0 + c.augment(self) + c.play(self,players,supply,trash) + self.show() + else: + self.index += 1 + else: + self.actions=0 + #buy phase + v= self.number + csvstring = r"Dominionbuy" + str(v) + ".csv" + buydf=pandas.read_csv(csvstring,na_filter=False) + sortedbuydf=buydf.sort_values("Buyvalues",ascending=False) + ranktable = sortedbuydf.Cardname + self.index = 0 + for c in self.hand: + self.purse += c.buypower + while self.buys>0: + purchase = ranktable.iloc[self.index] + if not purchase: + break + else: + c = self.getcard(purchase,supply,upto=self.purse) + if c: + self.discard.append(supply[purchase].pop()) + self.index = 0 + self.buys = self.buys -1 + self.purse = self.purse - c.cost + else: + self.index += 1 + + #cleanup phase + self.discard = self.discard + self.played + self.hand + self.aside + self.played = [] + self.hand = [] + self.aside = [] + for i in range(5): + self.draw() + + +def gameover(supply): + if len(supply["Province"])==0: + return True + out = 0 + for stack in supply: + if len(supply[stack])==0: + out+=1 + if out>=3: + return True + return False + +def namesinlist(cardlist): + namelist = [] + for c in cardlist: + namelist.append(c.name) + return namelist + +def catinlist(cardlist): + catlist = [] + for c in cardlist: + catlist.append(c.category) + return catlist + +def getcard(name,supply,target_list=None,target_name= "the supply anymore",categories=['action','coin','curse','victory'],upto=100): + if not name in supply: + print( name + " is not in this game.") + return None + if not target_list: + target_list = supply[name] + nameslist = namesinlist(target_list) + if not name in nameslist: + print ("There is no " + name + " in " + target_name) + return None + i = nameslist.index(name) + c = target_list[i] + if c.category not in categories: + catstring = categories[0] + for i in categories[1:]: + catstring = catstring + " or " + categories[i] + print (name + " is not a " + catstring + " card.") + return None + if c.cost > upto: + print (name + " costs " + str(c.cost)) + return None + return c + +def totalbuypower(cardlist): + TBP = 0 + for c in cardlist: + TBP += c.buypower + if c.category == "action": + TBP += c.coins + return TBP + +def cardsummaries(players): + cardsums={} + for player in players: + cardsums[player.name]=player.cardsummary() + cardsdf = pandas.DataFrame(cardsums).fillna(0).sort_index() + vp=cardsdf.loc[['VICTORY POINTS']] + cardsdf.drop(['VICTORY POINTS'],inplace=True) + return pandas.concat([cardsdf,vp],axis=0).fillna(0).astype(int) + +def countsupply(supply,form): + return [len(supply[a]) for a in form] + +def countcards(cards,form): + dcount = Counter(namesinlist(cards)) + return [dcount[a] for a in form] + +def rankcards(form,vector): + od = OrderedDict(zip(form,vector)) + sd = OrderedDict(sorted(od.items(),key = itemgetter(1),reverse=True)) + return list(sd.keys()) diff --git a/projects/lagmayj/README.md b/projects/lagmayj/README.md new file mode 100644 index 0000000..9e00834 --- /dev/null +++ b/projects/lagmayj/README.md @@ -0,0 +1,10 @@ +# Dominion +complete Dominion game in python, text-only + +Dominion is a popular deck-building game, for 2-4 players. Each card in the deck gives a player points or money, or lets a player perform a certain action when played. Each player tries to get the most points before the end of the game. As of July 2016, I have only programmed the base version. + +There are two versions of the game--one that uses a module and a main page, and one that includes all the code in one main page, useful for sharing with a friend who doesn't have python. (It can be executed in an online REPL, hence the name.) + +The list of players is hard-coded, so edit to play. Add a * at the beginning of the name for a computer player. + +The computer (currently) plays a very simple direct strategy. Its main advantage is that is has a perfect memory of every transaction, and an accurate, dispassionate calculation of when the game is likely to end. diff --git a/projects/lagmayj/REPLdominion.py b/projects/lagmayj/REPLdominion.py new file mode 100644 index 0000000..5499972 --- /dev/null +++ b/projects/lagmayj/REPLdominion.py @@ -0,0 +1,853 @@ +import random +from collections import Counter, OrderedDict +from operator import itemgetter +import re +import pandas + +class Card(): + def __init__(self,name,category,cost,buypower,vpoints): + self.name = name + self.category = category + self.cost = cost + self.buypower = buypower + self.vpoints = vpoints + def react(self,player): + return False + +class Coin_card(Card): + def __init__(self,name,cost,buypower): + Card.__init__(self,name,"coin",cost,buypower,0) + +class Copper(Coin_card): + def __init__(self): + Coin_card.__init__(self,"Copper",0,1) + +class Silver(Coin_card): + def __init__(self): + Coin_card.__init__(self,"Silver",3,2) + +class Gold(Coin_card): + def __init__(self): + Coin_card.__init__(self,"Gold",6,3) + +class Victory_card(Card): + def __init__(self,name,cost,vpoints): + Card.__init__(self,name,"victory",cost,0,vpoints) + +class Estate(Victory_card): + def __init__(self): + Victory_card.__init__(self,"Estate",2,1) + +class Duchy(Victory_card): + def __init__(self): + Victory_card.__init__(self,"Duchy",5,3) + +class Province(Victory_card): + def __init__(self): + Victory_card.__init__(self,"Province",8,6) + +class Garden(Victory_card): + def __init__(self): + Victory_card.__init__(self,"Garden",4,0) + +class Curse(Card): + def __init__(self): + Card.__init__(self,"Curse","curse",0,0,-1) + +class Action_card(Card): + def __init__(self,name,cost,actions,cards,buys,coins): + Card.__init__(self,name,"action",cost,0,0) + self.actions = actions + self.cards = cards + self.buys = buys + self.coins = coins + def use(self,player,trash): + player.played.append(self) + player.hand.remove(self) + def augment(self,player): + player.actions+=self.actions + player.buys+=self.buys + player.purse+=self.coins + for i in range(self.cards): + player.draw() + def play(self,player,players,supply,trash): + pass + +class Woodcutter(Action_card): + def __init__(self): + Action_card.__init__(self,"Woodcutter",3,0,0,1,2) + +class Smithy(Action_card): + def __init__(self): + Action_card.__init__(self,"Smithy",4,0,3,0,0) + +class Laboratory(Action_card): + def __init__(self): + Action_card.__init__(self,"Laboratory",5,1,2,0,0) + +class Village(Action_card): + def __init__(self): + Action_card.__init__(self,"Village",3,2,1,0,0) + +class Festival(Action_card): + def __init__(self): + Action_card.__init__(self,"Festival",5,2,0,1,2) + +class Market(Action_card): + def __init__(self): + Action_card.__init__(self,"Market",5,1,1,1,1) + +class Chancellor(Action_card): + def __init__(self): + Action_card.__init__(self,"Chancellor",3,0,0,0,2) + def play(self,player,players,supply,trash): + if player.yesnoinput('Would you like to discard your entire deck?'): + player.discard = player.discard + player.deck + player.deck = [] + +class Workshop(Action_card): + def __init__(self): + Action_card.__init__(self,"Workshop",3,0,0,0,0) + def play(self,player,players,supply,trash): + player.gaincard(supply,4) + +class Moneylender(Action_card): + def __init__(self): + Action_card.__init__(self,"Moneylender",4,0,0,0,0) + def play(self,player,players,supply,trash): + c = getcard("Copper",supply,player.hand,"your hand") + if c: + trash.append(c) + player.hand.remove(c) + player.purse += 3 + +class Chapel(Action_card): + def __init__(self): + Action_card.__init__(self,"Chapel",2,0,0,0,0) + def play(self,player,players,supply,trash): + trashed=0 + while trashed<4 and len(player.hand)>0: + trashcard = input("Choose a card from your hand to trash: ") + if not trashcard: + break + c = getcard(trashcard,supply,player.hand,"your hand") + if c: + trash.append(c) + player.hand.remove(c) + trashed+=1 + +class Cellar(Action_card): + def __init__(self): + Action_card.__init__(self,"Cellar",2,1,0,0,0) + def play(self,player,players,supply,trash): + discarded=0 + while len(player.hand)>0: + dis_card=input("Choose a card from your hand to discard: ") + if not dis_card: + break + c = getcard(dis_card,supply,player.hand,"your hand") + if c: + player.discard.append(c) + player.hand.remove(c) + discarded+=1 + for j in range(discarded): + player.draw() + +class Remodel(Action_card): + def __init__(self): + Action_card.__init__(self,"Remodel",4,0,0,0,0) + def play(self,player,players,supply,trash): + while len(player.hand)>0: + this_card = input("Choose a card from your hand to remodel: ") + c = getcard(this_card,supply,player.hand,"your hand") + if c: + trash.append(c) + player.hand.remove(c) + player.gaincard(supply,c.cost+2) + break + +class Adventurer(Action_card): + def __init__(self): + Action_card.__init__(self,"Adventurer",6,0,0,0,0) + def play(self,player,players,supply,trash): + coins_added = 0 + while (player.deck or player.discard) and coins_added <2: + player.draw() + if player.hand[-1].category == "coin": + coins_added +=1 + else: + player.aside.append(player.hand.pop()) + +class Feast(Action_card): + def __init__(self): + Action_card.__init__(self,"Feast",4,0,0,0,0) + def use(self,player,trash): + trash.append(self) + player.hand.remove(self) + def play(self,player,players,supply,trash): + player.gaincard(supply,5) + +class Mine(Action_card): + def __init__(self): + Action_card.__init__(self,"Mine",5,0,0,0,0) + def play(self,player,players,supply,trash): + while "coin" in catinlist(player.hand): + this_card = input("Choose a coin from your hand to upgrade: ") + c = getcard(this_card,supply,player.hand,"your hand",["coin"]) + if c: + trash.append(c) + player.hand.remove(c) + while c.cost>2 or len(supply["Copper"])>0 or len(supply["Silver"])>0: + pick = input("Choose a coin to gain. Any coin up to " + str(c.cost+3)+": ") + g = getcard(pick,supply,categories=["coin"],upto=c.cost+3) + if g: + player.hand.append(g) + supply[pick].remove(g) + break + break + +class Library(Action_card): + def __init__(self): + Action_card.__init__(self,"Library",5,0,0,0,0) + def play(self,player,players,supply,trash): + while (player.deck or player.discard) and len(player.hand) <7: + player.draw(player.hold) + if (player.hold[-1].category == "action" and not player.yesnoinput("You drew " + player.hold[-1].name + ". Would you like to keep it?",", add to my hand", ", set it aside")): + player.aside.append(player.hold.pop()) + else: + player.hand.append(player.hold.pop()) + +class Moat(Action_card): + def __init__(self): + Action_card.__init__(self,"Moat",2,0,2,0,0) + def react(self,player): + player.show() + return player.yesnoinput(player.name + ", you have a " + self.name + + " in your hand. Do you want to block the attack?") + +class Council_Room(Action_card): + def __init__(self): + Action_card.__init__(self,"Council Room",5,0,3,1,0) + def play(self,this_player,players,supply,trash): + for player in players: + player.draw() + +class Witch(Action_card): + def __init__(self): + Action_card.__init__(self,"Witch",5,0,2,0,0) + def play(self,this_player,players,supply,trash): + if len(supply["Curse"])>0: + for player in players: + if player==this_player: + pass + else: + for c in player.hand: + if c.react(player): + break + else: + if len(supply["Curse"])>0: + player.discard.append(supply["Curse"].pop()) + +class Bureaucrat(Action_card): + def __init__(self): + Action_card.__init__(self,"Bureaucrat",4,0,0,0,0) + def play(self,this_player,players,supply,trash): + if len(supply["Silver"])>0: + this_player.deck.insert(0,supply["Silver"].pop()) + for player in players: + if (not player==this_player) and ("victory" in catinlist(player.hand)): + for c in player.hand: + if c.react(player): + break + else: + player.show() + while True: + putback = player.choose_discard(player.name + ", which victory card" + + " do you want to put on top of your deck?\n--> ") + c = getcard(putback,supply,player.hand,"your hand",["victory"]) + if c: + player.hand.remove(c) + player.deck.insert(0,c) + break + +class Militia(Action_card): + def __init__(self): + Action_card.__init__(self,"Militia",4,0,0,0,2) + def play(self,this_player,players,supply,trash): + for player in players: + if (not player==this_player) and len(player.hand)>3: + for c in player.hand: + if c.react(player): + break + else: + player.show() + while len(player.hand)>3: + dis_card=player.choose_discard(player.name + ", choose a card from your hand to discard: ") + if dis_card: + c = getcard(dis_card,supply,player.hand,"your hand") + if c: + player.hand.remove(c) + player.discard.append(c) + +class Spy(Action_card): + def __init__(self): + Action_card.__init__(self,"Spy",4,1,1,0,0) + def play(self,this_player,players,supply,trash): + for player in players: + for c in player.hand: + if c.react(player): + break + else: + b = player.draw([]) + if not b: + continue + else: + print("The first card in the deck of " + player.name + " is " + b.name) + if this_player.yesnoinput(this_player.name + ", do you want " + player.name + " to discard this?", + ", discard",", keep"): + player.discard.append(b) + else: + player.deck.insert(0,b) + #check logic of this structure + +class Thief(Action_card): + def __init__(self): + Action_card.__init__(self,"Thief",4,0,0,0,0) + def play(self,this_player,players,supply,trash): + for player in players: + if player == this_player: + continue + for c in player.hand: + if c.react(player): + break + else: + for i in range(2): + player.draw(player.hold) + print( player.name, namesinlist(player.hold)) + if "coin" in catinlist(player.hold): + while True: + burn = input("Which card would you like " + player.name + " to trash?\n-->") + c = getcard(burn,supply,player.hold," the top 2 cards",["coin"]) + if c: + player.hold.remove(c) + break + if this_player.yesnoinput("Do you want to steal it?",", it's mine!",", leave it in the trash."): + this_player.discard.append(c) + else: + trash.append(c) + player.discard=player.discard+player.hold + player.hold = [] + +class Throne_Room(Action_card): + def __init__(self): + Action_card.__init__(self,"Throne Room",4,0,0,0,0) + def play(self,player,players,supply,trash): + if "action" in catinlist(player.hand): + while True: + double = input("What card would you like to double?\n--> ") + c = getcard(double,supply,player.hand," your hand",["action"]) + if c: + c.use(player,trash) + c.augment(player) + c.play(player,players,supply,trash) + player.show() + c.augment(player) + c.play(player,players,supply,trash) + break + + +class Player(): + def __init__(self,name): + self.name = name + deal = [Copper()]*7 + [Estate()]*3 + random.shuffle(deal) + self.hand = deal[:5] + self.deck = deal[5:] + self.played = [] + self.discard = [] + self.aside = [] + self.hold = [] + random.shuffle(self.deck) + for i in range(5): + self.draw + def other(self): + return self.played+self.discard+self.hold+self.aside + def stack(self): + return (self.deck + self.hand + self.played + self.discard + self.aside + self.hold) + def draw(self,dest=None): + #defualt destination is player's hand + if dest==None: + dest = self.hand + #Replenish deck if necessary. + if len(self.deck)==0: + self.deck = self.discard + self.discard = [] + random.shuffle(self.deck) + #If deck has cards, add card to destination list + if len(self.deck)>0: + c = self.deck.pop(0) + dest.append(c) + return c + def turn(self,players,supply,trash): + self.show() + self.actions = 1 + self.buys = 1 + self.purse = 0 + #action phase + while self.actions>0 and 'action' in catinlist(self.hand): + playthis = input("Which card would you like to play? You have " + + str(self.actions) + " action(s). \n-Hit enter for no play. --> ") + if playthis: + c = getcard(playthis,supply,self.hand,"your hand",['action']) + if c: + self.actions = self.actions - 1 + c.use(self,trash) + c.augment(self) + c.play(self,players,supply,trash) + self.show() + else: + self.actions=0 + #buy phase + for c in self.hand: + self.purse += c.buypower + while self.buys>0: + buy_string = "Buying power is " + str(self.purse) + ". You have " + str(self.buys) + " buy" + if self.buys>1: + buy_string += "s" + buy_string += "." + print( buy_string) + purchase = input("What would you like to purchase? -Hit enter for no purchase.-\n--> ") + if not purchase: + break + else: + c = getcard(purchase,supply,upto=self.purse) + if c: + self.discard.append(supply[purchase].pop()) + self.buys = self.buys -1 + self.purse = self.purse - c.cost + + #cleanup phase + self.discard = self.discard + self.played + self.hand + self.aside + self.played = [] + self.hand = [] + self.aside = [] + for i in range(5): + self.draw() + + def gaincard(self,supply,upto): + while True: + gain = input("What would you like to get? Any card up to " + str(upto) + "\n--> ") + c = getcard(gain,supply,upto=upto) + if c: + self.discard.append(c) + supply[gain].remove(c) + break + def yesnoinput(self,prompt,yesstring='',nostring=''): + print(prompt + "\n1 - Yes" + yesstring + "\n0 - No" + nostring) + while True: + r = input("--> ") + if r == "0": + return False + if r == "1": + return True + + def choose_discard(self,prompt): + return input(prompt) + + def show(self): + print (self.name) + print ("hand: ", namesinlist(self.hand)) + shuffled_deck = namesinlist(self.deck) + random.shuffle(shuffled_deck) + print ("deck (not in order): ", shuffled_deck) + print ("discard: ", namesinlist(self.discard)) + if len(self.played)>0: + print ("played: ", namesinlist(self.played)) + if len(self.aside)>0: + print ("aside: ",namesinlist(self.aside)) + print ("\r") + + def action_balance(self): + balance = 0 + for c in self.stack(): + if c.category == "action": + balance = balance - 1 + c.actions + return 70*balance / len(self.stack()) + + def cardsummary(self): + summary = {} + for c in self.stack(): + if c.name in summary: + summary[c.name] += 1 + else: + summary[c.name] = 1 + return summary + + def calcpoints(self): + tally = 0 + gardens = 0 + n = 0 + for c in self.stack(): + tally += c.vpoints + n += 1 + if c.name == "Garden": + gardens+=1 + return tally + n//10 * gardens + +class ComputerPlayer(Player): + def __init__(self,name): + Player.__init__(self,name) + self.index = 0 + self.buygaintable = [] + #beginning and middle of game + self.buygaintable1 = ["Province","Gold","Laboratory","Festival","Witch", + "Council Room","Market","Militia","Adventurer","Smithy","Bureaucrat","Silver","Moat",""] + #end of game + self.buygaintable2 = ["Province","Garden","Duchy","Estate","Gold","Silver",""] + #beginning and middle of the game, too many action cards + self.buygaintable3 = ["Province","Gold","Festival","Laboratory","Market","Village", + "Silver",""] + self.playtable1 = ["Village","Festival","Market","Laboratory","Witch", + "Council Room","Militia","Adventurer","Smithy","Bureaucrat","Moat",""] + self.discardtable1 = ["Garden","Duchy","Province","Estate","Curse","Copper", + "Village","Bureaucrat","Silver","Militia","Smithy","Council Room","Witch", + "Festival","Market","Adventurer","Laboratory","Gold","Moat"] + + def turn(self,players,supply,trash): + self.show() + self.actions = 1 + self.buys = 1 + self.purse = 0 + #action phase + self.index = 0 + while self.actions>0 and 'action' in catinlist(self.hand): + playthis = self.playtable1[self.index] + if playthis: + c = self.getcard(playthis,supply,self.hand,"your hand",['action']) + if c: + #print (self.name + " plays " + c.name) + self.actions = self.actions - 1 + c.use(self,trash) + self.index=0 + c.augment(self) + c.play(self,players,supply,trash) + self.show() + else: + self.index += 1 + else: + self.actions=0 + #buy phase + if len(supply["Province"])>len(players)+totalbuypower(self.deck)/8: + if self.action_balance()<-10: + self.buygaintable = self.buygaintable3 + else: + self.buygaintable = self.buygaintable1 + else: + self.buygaintable = self.buygaintable2 + self.index = 0 + for c in self.hand: + self.purse += c.buypower + while self.buys>0: + purchase = self.buygaintable[self.index] + if not purchase: + break + else: + c = self.getcard(purchase,supply,upto=self.purse) + if c: + self.discard.append(supply[purchase].pop()) + self.index = 0 + self.buys = self.buys -1 + self.purse = self.purse - c.cost + #print (self.name + " bought " + c.name) + else: + self.index += 1 + + #cleanup phase + self.discard = self.discard + self.played + self.hand + self.aside + self.played = [] + self.hand = [] + self.aside = [] + for i in range(5): + self.draw() + + def getcard(self,name,supply,target_list=None,target_name= "the supply anymore",categories=['action','coin','curse','victory'],upto=100): + if not name in supply: + #print name + " is not in this game." + return None + if not target_list: + target_list = supply[name] + nameslist = namesinlist(target_list) + if not name in nameslist: + #print "There is no " + name + " in " + target_name + return None + i = nameslist.index(name) + c = target_list[i] + if c.category not in categories: + catstring = categories[0] + for i in categories[1:]: + catstring = catstring + " or " + categories[i] + #print name + " is not a " + catstring + " card." + return None + if c.cost > upto: + #print name + " costs " + str(c.cost) + return None + return c + + def choose_discard(self,prompt): + index = 0 + while True: + dis_card = self.discardtable1[index] + c = self.getcard(dis_card,[dis_card],self.hand) + if c: + return c.name + else: + index+=1 + + def yesnoinput(self,prompt,yesstring='',nostring=''): + return True + + def show(self): + pass + +class TablePlayer(ComputerPlayer): + def __init__(self,name): + ComputerPlayer.__init__(self,name) + self.index=0 + self.buygaintable=[] + q=re.match(r'([a-zA-Z]+)(\d+)',name) + if q: + self.number = q.group(2) + else: + print(name) + self.playtable1 = ["Village","Festival","Market","Laboratory","Witch", + "Council Room","Militia","Adventurer","Smithy","Bureaucrat","Moat",""] + self.discardtable1 = ["Garden","Duchy","Province","Estate","Curse","Copper", + "Village","Bureaucrat","Silver","Militia","Smithy","Council Room","Witch", + "Festival","Market","Adventurer","Laboratory","Gold","Moat"] + + def turn(self,players,supply,trash): + self.show() + self.actions = 1 + self.buys = 1 + self.purse = 0 + #action phase + self.index = 0 + while self.actions>0 and 'action' in catinlist(self.hand): + playthis = self.playtable1[self.index] + if playthis: + c = self.getcard(playthis,supply,self.hand,"your hand",['action']) + if c: + #print (self.name + " plays " + c.name) + self.actions = self.actions - 1 + c.use(self,trash) + self.index=0 + c.augment(self) + c.play(self,players,supply,trash) + self.show() + else: + self.index += 1 + else: + self.actions=0 + #buy phase + v= self.number + csvstring = r"Dominionbuy" + str(v) + ".csv" + buydf=pandas.read_csv(csvstring,na_filter=False) + sortedbuydf=buydf.sort_values("Buyvalues",ascending=False) + ranktable = sortedbuydf.Cardname + self.index = 0 + for c in self.hand: + self.purse += c.buypower + while self.buys>0: + purchase = ranktable.iloc[self.index] + if not purchase: + break + else: + c = self.getcard(purchase,supply,upto=self.purse) + if c: + self.discard.append(supply[purchase].pop()) + self.index = 0 + self.buys = self.buys -1 + self.purse = self.purse - c.cost + else: + self.index += 1 + + #cleanup phase + self.discard = self.discard + self.played + self.hand + self.aside + self.played = [] + self.hand = [] + self.aside = [] + for i in range(5): + self.draw() + + +def gameover(supply): + if len(supply["Province"])==0: + return True + out = 0 + for stack in supply: + if len(supply[stack])==0: + out+=1 + if out>=3: + return True + return False + +def namesinlist(cardlist): + namelist = [] + for c in cardlist: + namelist.append(c.name) + return namelist + +def catinlist(cardlist): + catlist = [] + for c in cardlist: + catlist.append(c.category) + return catlist + +def getcard(name,supply,target_list=None,target_name= "the supply anymore",categories=['action','coin','curse','victory'],upto=100): + if not name in supply: + print( name + " is not in this game.") + return None + if not target_list: + target_list = supply[name] + nameslist = namesinlist(target_list) + if not name in nameslist: + print ("There is no " + name + " in " + target_name) + return None + i = nameslist.index(name) + c = target_list[i] + if c.category not in categories: + catstring = categories[0] + for i in categories[1:]: + catstring = catstring + " or " + categories[i] + print (name + " is not a " + catstring + " card.") + return None + if c.cost > upto: + print (name + " costs " + str(c.cost)) + return None + return c + +def totalbuypower(cardlist): + TBP = 0 + for c in cardlist: + TBP += c.buypower + if c.category == "action": + TBP += c.coins + return TBP + +def countsupply(supply,form): + return [len(supply[a]) for a in form] + +def countcards(cards,form): + dcount = Counter(namesinlist(cards)) + return [dcount[a] for a in form] + +def rankcards(form,vector): + od = OrderedDict(zip(form,vector)) + sd = OrderedDict(sorted(od.items(),key = itemgetter(1),reverse=True)) + return list(sd.keys()) + +#import Dominion +#import random +from collections import defaultdict + +#Get player names +player_names = ["Timothy","*Grover","*Elmo"] + +#number of curses and victory cards +if len(player_names)>2: + nV=12 +else: + nV=8 +nC = -10 + 10 * len(player_names) + +#Define box +box = {} +box["Woodcutter"]=[Woodcutter()]*10 +box["Smithy"]=[Smithy()]*10 +box["Laboratory"]=[Laboratory()]*10 +box["Village"]=[Village()]*10 +box["Festival"]=[Festival()]*10 +box["Market"]=[Market()]*10 +box["Chancellor"]=[Chancellor()]*10 +box["Workshop"]=[Workshop()]*10 +box["Moneylender"]=[Moneylender()]*10 +box["Chapel"]=[Chapel()]*10 +box["Cellar"]=[Cellar()]*10 +box["Remodel"]=[Remodel()]*10 +box["Adventurer"]=[Adventurer()]*10 +box["Feast"]=[Feast()]*10 +box["Mine"]=[Mine()]*10 +box["Library"]=[Library()]*10 +box["Garden"]=[Garden()]*nV +box["Moat"]=[Moat()]*10 +box["Council Room"]=[Council_Room()]*10 +box["Witch"]=[Witch()]*10 +box["Bureaucrat"]=[Bureaucrat()]*10 +box["Militia"]=[Militia()]*10 +box["Spy"]=[Spy()]*10 +box["Thief"]=[Thief()]*10 +box["Throne Room"]=[Throne_Room()]*10 + +supply_order = {0:['Curse','Copper'],2:['Estate','Cellar','Chapel','Moat'], + 3:['Silver','Chancellor','Village','Woodcutter','Workshop'], + 4:['Garden','Bureaucrat','Feast','Militia','Moneylender','Remodel','Smithy','Spy','Thief','Throne Room'], + 5:['Duchy','Market','Council Room','Festival','Laboratory','Library','Mine','Witch'], + 6:['Gold','Adventurer'],8:['Province']} + +#Pick 10 cards from box to be in the supply. +boxlist = [k for k in box] +random.shuffle(boxlist) +random10 = boxlist[:10] +supply = defaultdict(list,[(k,box[k]) for k in random10]) + + +#The supply always has these cards +supply["Copper"]=[Copper()]*(60-len(player_names)*7) +supply["Silver"]=[Silver()]*40 +supply["Gold"]=[Gold()]*30 +supply["Estate"]=[Estate()]*nV +supply["Duchy"]=[Duchy()]*nV +supply["Province"]=[Province()]*nV +supply["Curse"]=[Curse()]*nC + +#initialize the trash +trash = [] + +#Costruct the Player objects +players = [] +for name in player_names: + if name[0]=="*": + players.append(ComputerPlayer(name[1:])) + elif name[0]=="^": + players.append(TablePlayer(name[1:])) + else: + players.append(Player(name)) + +#Play the game +turn = 0 +while not gameover(supply): + turn += 1 + print("\r") + for value in supply_order: + print (value) + for stack in supply_order[value]: + if stack in supply: + print (stack, len(supply[stack])) + print("\r") + for player in players: + print (player.name,player.calcpoints()) + print ("\rStart of turn " + str(turn)) + for player in players: + if not gameover(supply): + print("\r") + player.turn(players,supply,trash) + + +#Final score +print ("\r") +for player in players: + print (player.name,player.calcpoints()) + +print ("\n") +for player in players: + print (player.name,player.cardsummary()) \ No newline at end of file diff --git a/projects/lagmayj/playDominion.py b/projects/lagmayj/playDominion.py new file mode 100644 index 0000000..b4d6021 --- /dev/null +++ b/projects/lagmayj/playDominion.py @@ -0,0 +1,119 @@ +# -*- coding: utf-8 -*- +""" +Created on Tue Oct 13 15:42:42 2015 + +@author: tfleck +""" + +import Dominion +import random +from collections import defaultdict + +#Get player names +player_names = ["Annie","*Ben","*Carla"] + +#number of curses and victory cards +if len(player_names)>2: + nV=12 +else: + nV=8 +nC = -10 + 10 * len(player_names) + +#Define box +box = {} +box["Woodcutter"]=[Dominion.Woodcutter()]*10 +box["Smithy"]=[Dominion.Smithy()]*10 +box["Laboratory"]=[Dominion.Laboratory()]*10 +box["Village"]=[Dominion.Village()]*10 +box["Festival"]=[Dominion.Festival()]*10 +box["Market"]=[Dominion.Market()]*10 +box["Chancellor"]=[Dominion.Chancellor()]*10 +box["Workshop"]=[Dominion.Workshop()]*10 +box["Moneylender"]=[Dominion.Moneylender()]*10 +box["Chapel"]=[Dominion.Chapel()]*10 +box["Cellar"]=[Dominion.Cellar()]*10 +box["Remodel"]=[Dominion.Remodel()]*10 +box["Adventurer"]=[Dominion.Adventurer()]*10 +box["Feast"]=[Dominion.Feast()]*10 +box["Mine"]=[Dominion.Mine()]*10 +box["Library"]=[Dominion.Library()]*10 +box["Gardens"]=[Dominion.Gardens()]*nV +box["Moat"]=[Dominion.Moat()]*10 +box["Council Room"]=[Dominion.Council_Room()]*10 +box["Witch"]=[Dominion.Witch()]*10 +box["Bureaucrat"]=[Dominion.Bureaucrat()]*10 +box["Militia"]=[Dominion.Militia()]*10 +box["Spy"]=[Dominion.Spy()]*10 +box["Thief"]=[Dominion.Thief()]*10 +box["Throne Room"]=[Dominion.Throne_Room()]*10 + +supply_order = {0:['Curse','Copper'],2:['Estate','Cellar','Chapel','Moat'], + 3:['Silver','Chancellor','Village','Woodcutter','Workshop'], + 4:['Gardens','Bureaucrat','Feast','Militia','Moneylender','Remodel','Smithy','Spy','Thief','Throne Room'], + 5:['Duchy','Market','Council Room','Festival','Laboratory','Library','Mine','Witch'], + 6:['Gold','Adventurer'],8:['Province']} + +#Pick 10 cards from box to be in the supply. +boxlist = [k for k in box] +random.shuffle(boxlist) +random10 = boxlist[:10] +supply = defaultdict(list,[(k,box[k]) for k in random10]) + + +#The supply always has these cards +supply["Copper"]=[Dominion.Copper()]*(60-len(player_names)*7) +supply["Silver"]=[Dominion.Silver()]*40 +supply["Gold"]=[Dominion.Gold()]*30 +supply["Estate"]=[Dominion.Estate()]*nV +supply["Duchy"]=[Dominion.Duchy()]*nV +supply["Province"]=[Dominion.Province()]*nV +supply["Curse"]=[Dominion.Curse()]*nC + +#initialize the trash +trash = [] + +#Costruct the Player objects +players = [] +for name in player_names: + if name[0]=="*": + players.append(Dominion.ComputerPlayer(name[1:])) + elif name[0]=="^": + players.append(Dominion.TablePlayer(name[1:])) + else: + players.append(Dominion.Player(name)) + +#Play the game +turn = 0 +while not Dominion.gameover(supply): + turn += 1 + print("\r") + for value in supply_order: + print (value) + for stack in supply_order[value]: + if stack in supply: + print (stack, len(supply[stack])) + print("\r") + for player in players: + print (player.name,player.calcpoints()) + print ("\rStart of turn " + str(turn)) + for player in players: + if not Dominion.gameover(supply): + print("\r") + player.turn(players,supply,trash) + + +#Final score +dcs=Dominion.cardsummaries(players) +vp=dcs.loc['VICTORY POINTS'] +vpmax=vp.max() +winners=[] +for i in vp.index: + if vp.loc[i]==vpmax: + winners.append(i) +if len(winners)>1: + winstring= ' and '.join(winners) + ' win!' +else: + winstring = ' '.join([winners[0],'wins!']) + +print("\nGAME OVER!!!\n"+winstring+"\n") +print(dcs) \ No newline at end of file From 6e39df7812d409688b1cc866646b657190fab2d7 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:48:10 -0800 Subject: [PATCH 08/27] Create file --- projects/lagmayj/dominion/file | 1 + 1 file changed, 1 insertion(+) create mode 100644 projects/lagmayj/dominion/file diff --git a/projects/lagmayj/dominion/file b/projects/lagmayj/dominion/file new file mode 100644 index 0000000..781e137 --- /dev/null +++ b/projects/lagmayj/dominion/file @@ -0,0 +1 @@ +df From ae8913862c1bfe498366545f546c96fe1dbda940 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:48:45 -0800 Subject: [PATCH 09/27] Delete James --- projects/lagmayj/James | 1 - 1 file changed, 1 deletion(-) delete mode 100644 projects/lagmayj/James diff --git a/projects/lagmayj/James b/projects/lagmayj/James deleted file mode 100644 index dc065e0..0000000 --- a/projects/lagmayj/James +++ /dev/null @@ -1 +0,0 @@ -james From 99b3944bfdc5581be49e85dd02916cb3ecfd27ed Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:49:21 -0800 Subject: [PATCH 10/27] Rename projects/lagmayj/Dominion.py to projects/lagmayj/dominion/Dominion.py --- projects/lagmayj/{ => dominion}/Dominion.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename projects/lagmayj/{ => dominion}/Dominion.py (100%) diff --git a/projects/lagmayj/Dominion.py b/projects/lagmayj/dominion/Dominion.py similarity index 100% rename from projects/lagmayj/Dominion.py rename to projects/lagmayj/dominion/Dominion.py From 0f84c18e833432bd06a059f488878a1dd9df6a3a Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:49:37 -0800 Subject: [PATCH 11/27] Rename projects/lagmayj/README.md to projects/lagmayj/dominion/README.md --- projects/lagmayj/{ => dominion}/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename projects/lagmayj/{ => dominion}/README.md (100%) diff --git a/projects/lagmayj/README.md b/projects/lagmayj/dominion/README.md similarity index 100% rename from projects/lagmayj/README.md rename to projects/lagmayj/dominion/README.md From 2ac3804be3983bf6d9b2789f8c7fe1ae00b544a6 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:51:11 -0800 Subject: [PATCH 12/27] Rename projects/lagmayj/REPLdominion.py to projects/lagmayj/dominion/REPLdominion.py --- projects/lagmayj/{ => dominion}/REPLdominion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename projects/lagmayj/{ => dominion}/REPLdominion.py (99%) diff --git a/projects/lagmayj/REPLdominion.py b/projects/lagmayj/dominion/REPLdominion.py similarity index 99% rename from projects/lagmayj/REPLdominion.py rename to projects/lagmayj/dominion/REPLdominion.py index 5499972..17b4071 100644 --- a/projects/lagmayj/REPLdominion.py +++ b/projects/lagmayj/dominion/REPLdominion.py @@ -850,4 +850,4 @@ def rankcards(form,vector): print ("\n") for player in players: - print (player.name,player.cardsummary()) \ No newline at end of file + print (player.name,player.cardsummary()) From a9c1a2ea0a52e7dddd6e6b847e55adda20a467fe Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:51:29 -0800 Subject: [PATCH 13/27] Rename projects/lagmayj/playDominion.py to projects/lagmayj/dominion/playDominion.py --- projects/lagmayj/{ => dominion}/playDominion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename projects/lagmayj/{ => dominion}/playDominion.py (99%) diff --git a/projects/lagmayj/playDominion.py b/projects/lagmayj/dominion/playDominion.py similarity index 99% rename from projects/lagmayj/playDominion.py rename to projects/lagmayj/dominion/playDominion.py index b4d6021..68b34a2 100644 --- a/projects/lagmayj/playDominion.py +++ b/projects/lagmayj/dominion/playDominion.py @@ -116,4 +116,4 @@ winstring = ' '.join([winners[0],'wins!']) print("\nGAME OVER!!!\n"+winstring+"\n") -print(dcs) \ No newline at end of file +print(dcs) From b8f266c0a7f154b3675ba535ddf543a8abfbf083 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:52:30 -0800 Subject: [PATCH 14/27] Create README.md --- projects/lagmayj/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 projects/lagmayj/README.md diff --git a/projects/lagmayj/README.md b/projects/lagmayj/README.md new file mode 100644 index 0000000..264b047 --- /dev/null +++ b/projects/lagmayj/README.md @@ -0,0 +1 @@ +#Your onid folder should contain a readme file that contains your name and your ONID. From e50393ab55cdd81ff0d8c07f6b2916b6dea97d3e Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:54:52 -0800 Subject: [PATCH 15/27] Delete file --- projects/lagmayj/dominion/file | 1 - 1 file changed, 1 deletion(-) delete mode 100644 projects/lagmayj/dominion/file diff --git a/projects/lagmayj/dominion/file b/projects/lagmayj/dominion/file deleted file mode 100644 index 781e137..0000000 --- a/projects/lagmayj/dominion/file +++ /dev/null @@ -1 +0,0 @@ -df From 639931876fabbe5d313790939f386e5d89a2dd97 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 13:55:25 -0800 Subject: [PATCH 16/27] Update README.md --- projects/lagmayj/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/lagmayj/README.md b/projects/lagmayj/README.md index 264b047..ae8ad86 100644 --- a/projects/lagmayj/README.md +++ b/projects/lagmayj/README.md @@ -1 +1 @@ -#Your onid folder should contain a readme file that contains your name and your ONID. +#Jacob Lagmay, lagmayj From 4076cbf4e382d396a16a3b205974dfad9fab4d39 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 14:15:58 -0800 Subject: [PATCH 17/27] Create readfile --- projects/readfile | 1 + 1 file changed, 1 insertion(+) create mode 100644 projects/readfile diff --git a/projects/readfile b/projects/readfile new file mode 100644 index 0000000..f73f309 --- /dev/null +++ b/projects/readfile @@ -0,0 +1 @@ +file From 13dfe1032b9e037cb528413dcc06477733d5bc7c Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Mon, 6 Jan 2020 14:17:04 -0800 Subject: [PATCH 18/27] Delete readfile --- projects/readfile | 1 - 1 file changed, 1 deletion(-) delete mode 100644 projects/readfile diff --git a/projects/readfile b/projects/readfile deleted file mode 100644 index f73f309..0000000 --- a/projects/readfile +++ /dev/null @@ -1 +0,0 @@ -file From fc57defd28b70f72efe23e35b305f55e59c070b0 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Sun, 19 Jan 2020 15:58:52 -0800 Subject: [PATCH 19/27] Create testDominion1.py --- dominion/testDominion1.py | 76 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 dominion/testDominion1.py diff --git a/dominion/testDominion1.py b/dominion/testDominion1.py new file mode 100644 index 0000000..c30fc02 --- /dev/null +++ b/dominion/testDominion1.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +""" +Created on Sun Jan 19 2020 + +@author: lagmayj +""" + +import Dominion +import random +import testUtility +from collections import defaultdict + +#Get player names +player_names = testUtility.playerNames() + +#number of curses and victory cards +nV = testUtility.get_nV() +nC = -10 + 10 * len(player_names) + +#Define box +box = testUtility.getBoxes(nV) +supply_order = testUtility.getSupplyOrd() + + +#Pick 10 cards from box to be in the supply. +boxlist = [k for k in box] +random.shuffle(boxlist) +random10 = boxlist[:10] +supply = defaultdict(list,[(k,box[k]) for k in random10]) + + +#The supply always has these cards +supply = testUtility.getSupply(nV, nC) + + +#initialize the trash +trash = [] + +#Costruct the Player objects +players = testUtility.getPlayers() + +#Play the game +turn = 0 +while not Dominion.gameover(supply): +# turn += 1 #Error stops the game from incrementing turns, confusing the player + print("\r") + for value in supply_order: + print (value) + for stack in supply_order[value]: + if stack in supply: + print (stack, len(supply[stack])) + print("\r") + for player in players: + print (player.name,player.calcpoints()) + print ("\rStart of turn " + str(turn)) + for player in players: + if not Dominion.gameover(supply): + print("\r") + player.turn(players,supply,trash) + + +#Final score +dcs=Dominion.cardsummaries(players) +vp=dcs.loc['VICTORY POINTS'] +vpmax=vp.max() +winners=[] +for i in vp.index: + if vp.loc[i]==vpmax: + winners.append(i) +if len(winners)>1: + winstring= ' and '.join(winners) + ' win!' +else: + winstring = ' '.join([winners[0],'wins!']) + +print("\nGAME OVER!!!\n"+winstring+"\n") +print(dcs) From c808085af1c0fcdbf13efe1f2e1eb3746038e923 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Sun, 19 Jan 2020 15:59:46 -0800 Subject: [PATCH 20/27] Create testDominion2.py --- dominion/testDominion2.py | 76 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 dominion/testDominion2.py diff --git a/dominion/testDominion2.py b/dominion/testDominion2.py new file mode 100644 index 0000000..193fed1 --- /dev/null +++ b/dominion/testDominion2.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +""" +Created on Sun Jan 19 2020 + +@author: lagmayj +""" + +import Dominion +import random +import testUtility +from collections import defaultdict + +#Get player names +player_names = testUtility.playerNames() + +#number of curses and victory cards +nV = testUtility.get_nV() +nC = -10 + 10 * len(player_names) + +#Define box +box = testUtility.getBoxes(nV) +supply_order = testUtility.getSupplyOrd() + + +#Pick 10 cards from box to be in the supply. +boxlist = [k for k in box] +random.shuffle(boxlist) +random10 = boxlist[:10] +supply = defaultdict(list,[(k,box[k]) for k in random10]) + + +#The supply always has these cards +supply = testUtility.getSupply(nV, nC) + + +#initialize the trash +trash = [] + +#Costruct the Player objects +players = testUtility.getPlayers() + +#Play the game +turn = 0 +while not Dominion.gameover(supply): + turn += 1 + print("\r") + for value in supply_order: + print (value) + for stack in supply_order[value]: + if stack in supply: + print (stack, len(supply[stack])) + print("\r") + for player in players: + print (player.name,player.calcpoints()) + print ("\rStart of turn " + str(turn)) + for player in players: + if not Dominion.gameover(supply): + print("\r") + player.turn(players,supply,trash) + + +#Final score +dcs=Dominion.cardsummaries(players) +vp=dcs.loc['VICTORY POINTS'] +vpmax=vp.max() +winners=[] +for i in vp.index: + if vp.loc[i]==vpmax: + winners.append(i) +if len(winners)>1: + winstring= ' and '.join(winners) + ' win!' +else: + winstring = ' '.join([winners[0],'wins!']) + +#print("\nGAME OVER!!!\n"+winstring+"\n") #Does not display the winner or the scoreboard +#print(dcs) From aadeb2b07ffac0f01343ccb995f49f825fcd95f4 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Sun, 19 Jan 2020 16:00:49 -0800 Subject: [PATCH 21/27] Create testUtility.py --- dominion/testUtility.py | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 dominion/testUtility.py diff --git a/dominion/testUtility.py b/dominion/testUtility.py new file mode 100644 index 0000000..bc377f8 --- /dev/null +++ b/dominion/testUtility.py @@ -0,0 +1,79 @@ +""" +Created on Sun Jan 19 2020 + +@author: lagmayj +""" + +import Dominion + +def playerNames(): + player_names = ["Annie", "*Ben", "*Carla"] + return player_names + +def get_nV(): + if len(playerNames()) > 2: + nV = 12 + else: + nV = 8 + + return nV + +def getBoxes(nV): + box = {} + box["Woodcutter"] = [Dominion.Woodcutter()] * 10 + box["Smithy"] = [Dominion.Smithy()] * 10 + box["Laboratory"] = [Dominion.Laboratory()] * 10 + box["Village"] = [Dominion.Village()] * 10 + box["Festival"] = [Dominion.Festival()] * 10 + box["Market"] = [Dominion.Market()] * 10 + box["Chancellor"] = [Dominion.Chancellor()] * 10 + box["Workshop"] = [Dominion.Workshop()] * 10 + box["Moneylender"] = [Dominion.Moneylender()] * 10 + box["Chapel"] = [Dominion.Chapel()] * 10 + box["Cellar"] = [Dominion.Cellar()] * 10 + box["Remodel"] = [Dominion.Remodel()] * 10 + box["Adventurer"] = [Dominion.Adventurer()] * 10 + box["Feast"] = [Dominion.Feast()] * 10 + box["Mine"] = [Dominion.Mine()] * 10 + box["Library"] = [Dominion.Library()] * 10 + box["Gardens"] = [Dominion.Gardens()] * nV + box["Moat"] = [Dominion.Moat()] * 10 + box["Council Room"] = [Dominion.Council_Room()] * 10 + box["Witch"] = [Dominion.Witch()] * 10 + box["Bureaucrat"] = [Dominion.Bureaucrat()] * 10 + box["Militia"] = [Dominion.Militia()] * 10 + box["Spy"] = [Dominion.Spy()] * 10 + box["Thief"] = [Dominion.Thief()] * 10 + box["Throne Room"] = [Dominion.Throne_Room()] * 10 + return box + +def getSupply(nV, nC): + supply = {} + supply["Copper"] = [Dominion.Copper()] * (60 - len(playerNames()) * 7) + supply["Silver"] = [Dominion.Silver()] * 40 + supply["Gold"] = [Dominion.Gold()] * 30 + supply["Estate"] = [Dominion.Estate()] * nV + supply["Duchy"] = [Dominion.Duchy()] * nV + supply["Province"] = [Dominion.Province()] * nV + supply["Curse"] = [Dominion.Curse()] * nC + return supply + +def getSupplyOrd(): + supplyOrder = {0: ['Curse', 'Copper'], 2: ['Estate', 'Cellar', 'Chapel', 'Moat'], + 3: ['Silver', 'Chancellor', 'Village', 'Woodcutter', 'Workshop'], + 4: ['Gardens', 'Bureaucrat', 'Feast', 'Militia', 'Moneylender', 'Remodel', 'Smithy', 'Spy', 'Thief', + 'Throne Room'], + 5: ['Duchy', 'Market', 'Council Room', 'Festival', 'Laboratory', 'Library', 'Mine', 'Witch'], + 6: ['Gold', 'Adventurer'], 8: ['Province']} + return supplyOrder + +def getPlayers(): + players = [] + for name in playerNames(): + if name[0] == "*": + players.append(Dominion.ComputerPlayer(name[1:])) + elif name[0] == "^": + players.append(Dominion.TablePlayer(name[1:])) + else: + players.append(Dominion.Player(name)) + return players From 3772481fab738108ed4b8983fa226c56f7da96c7 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Sun, 19 Jan 2020 16:01:53 -0800 Subject: [PATCH 22/27] Delete testDominion1.py --- dominion/testDominion1.py | 76 --------------------------------------- 1 file changed, 76 deletions(-) delete mode 100644 dominion/testDominion1.py diff --git a/dominion/testDominion1.py b/dominion/testDominion1.py deleted file mode 100644 index c30fc02..0000000 --- a/dominion/testDominion1.py +++ /dev/null @@ -1,76 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Sun Jan 19 2020 - -@author: lagmayj -""" - -import Dominion -import random -import testUtility -from collections import defaultdict - -#Get player names -player_names = testUtility.playerNames() - -#number of curses and victory cards -nV = testUtility.get_nV() -nC = -10 + 10 * len(player_names) - -#Define box -box = testUtility.getBoxes(nV) -supply_order = testUtility.getSupplyOrd() - - -#Pick 10 cards from box to be in the supply. -boxlist = [k for k in box] -random.shuffle(boxlist) -random10 = boxlist[:10] -supply = defaultdict(list,[(k,box[k]) for k in random10]) - - -#The supply always has these cards -supply = testUtility.getSupply(nV, nC) - - -#initialize the trash -trash = [] - -#Costruct the Player objects -players = testUtility.getPlayers() - -#Play the game -turn = 0 -while not Dominion.gameover(supply): -# turn += 1 #Error stops the game from incrementing turns, confusing the player - print("\r") - for value in supply_order: - print (value) - for stack in supply_order[value]: - if stack in supply: - print (stack, len(supply[stack])) - print("\r") - for player in players: - print (player.name,player.calcpoints()) - print ("\rStart of turn " + str(turn)) - for player in players: - if not Dominion.gameover(supply): - print("\r") - player.turn(players,supply,trash) - - -#Final score -dcs=Dominion.cardsummaries(players) -vp=dcs.loc['VICTORY POINTS'] -vpmax=vp.max() -winners=[] -for i in vp.index: - if vp.loc[i]==vpmax: - winners.append(i) -if len(winners)>1: - winstring= ' and '.join(winners) + ' win!' -else: - winstring = ' '.join([winners[0],'wins!']) - -print("\nGAME OVER!!!\n"+winstring+"\n") -print(dcs) From b5dac2cb2c0dc97f648b2b322ddc95ba3b51381e Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Sun, 19 Jan 2020 16:02:30 -0800 Subject: [PATCH 23/27] Delete testDominion2.py --- dominion/testDominion2.py | 76 --------------------------------------- 1 file changed, 76 deletions(-) delete mode 100644 dominion/testDominion2.py diff --git a/dominion/testDominion2.py b/dominion/testDominion2.py deleted file mode 100644 index 193fed1..0000000 --- a/dominion/testDominion2.py +++ /dev/null @@ -1,76 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Sun Jan 19 2020 - -@author: lagmayj -""" - -import Dominion -import random -import testUtility -from collections import defaultdict - -#Get player names -player_names = testUtility.playerNames() - -#number of curses and victory cards -nV = testUtility.get_nV() -nC = -10 + 10 * len(player_names) - -#Define box -box = testUtility.getBoxes(nV) -supply_order = testUtility.getSupplyOrd() - - -#Pick 10 cards from box to be in the supply. -boxlist = [k for k in box] -random.shuffle(boxlist) -random10 = boxlist[:10] -supply = defaultdict(list,[(k,box[k]) for k in random10]) - - -#The supply always has these cards -supply = testUtility.getSupply(nV, nC) - - -#initialize the trash -trash = [] - -#Costruct the Player objects -players = testUtility.getPlayers() - -#Play the game -turn = 0 -while not Dominion.gameover(supply): - turn += 1 - print("\r") - for value in supply_order: - print (value) - for stack in supply_order[value]: - if stack in supply: - print (stack, len(supply[stack])) - print("\r") - for player in players: - print (player.name,player.calcpoints()) - print ("\rStart of turn " + str(turn)) - for player in players: - if not Dominion.gameover(supply): - print("\r") - player.turn(players,supply,trash) - - -#Final score -dcs=Dominion.cardsummaries(players) -vp=dcs.loc['VICTORY POINTS'] -vpmax=vp.max() -winners=[] -for i in vp.index: - if vp.loc[i]==vpmax: - winners.append(i) -if len(winners)>1: - winstring= ' and '.join(winners) + ' win!' -else: - winstring = ' '.join([winners[0],'wins!']) - -#print("\nGAME OVER!!!\n"+winstring+"\n") #Does not display the winner or the scoreboard -#print(dcs) From b237e496ee52a7d359825b4c5ef410317738d39c Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Sun, 19 Jan 2020 16:03:15 -0800 Subject: [PATCH 24/27] Delete testUtility.py --- dominion/testUtility.py | 79 ----------------------------------------- 1 file changed, 79 deletions(-) delete mode 100644 dominion/testUtility.py diff --git a/dominion/testUtility.py b/dominion/testUtility.py deleted file mode 100644 index bc377f8..0000000 --- a/dominion/testUtility.py +++ /dev/null @@ -1,79 +0,0 @@ -""" -Created on Sun Jan 19 2020 - -@author: lagmayj -""" - -import Dominion - -def playerNames(): - player_names = ["Annie", "*Ben", "*Carla"] - return player_names - -def get_nV(): - if len(playerNames()) > 2: - nV = 12 - else: - nV = 8 - - return nV - -def getBoxes(nV): - box = {} - box["Woodcutter"] = [Dominion.Woodcutter()] * 10 - box["Smithy"] = [Dominion.Smithy()] * 10 - box["Laboratory"] = [Dominion.Laboratory()] * 10 - box["Village"] = [Dominion.Village()] * 10 - box["Festival"] = [Dominion.Festival()] * 10 - box["Market"] = [Dominion.Market()] * 10 - box["Chancellor"] = [Dominion.Chancellor()] * 10 - box["Workshop"] = [Dominion.Workshop()] * 10 - box["Moneylender"] = [Dominion.Moneylender()] * 10 - box["Chapel"] = [Dominion.Chapel()] * 10 - box["Cellar"] = [Dominion.Cellar()] * 10 - box["Remodel"] = [Dominion.Remodel()] * 10 - box["Adventurer"] = [Dominion.Adventurer()] * 10 - box["Feast"] = [Dominion.Feast()] * 10 - box["Mine"] = [Dominion.Mine()] * 10 - box["Library"] = [Dominion.Library()] * 10 - box["Gardens"] = [Dominion.Gardens()] * nV - box["Moat"] = [Dominion.Moat()] * 10 - box["Council Room"] = [Dominion.Council_Room()] * 10 - box["Witch"] = [Dominion.Witch()] * 10 - box["Bureaucrat"] = [Dominion.Bureaucrat()] * 10 - box["Militia"] = [Dominion.Militia()] * 10 - box["Spy"] = [Dominion.Spy()] * 10 - box["Thief"] = [Dominion.Thief()] * 10 - box["Throne Room"] = [Dominion.Throne_Room()] * 10 - return box - -def getSupply(nV, nC): - supply = {} - supply["Copper"] = [Dominion.Copper()] * (60 - len(playerNames()) * 7) - supply["Silver"] = [Dominion.Silver()] * 40 - supply["Gold"] = [Dominion.Gold()] * 30 - supply["Estate"] = [Dominion.Estate()] * nV - supply["Duchy"] = [Dominion.Duchy()] * nV - supply["Province"] = [Dominion.Province()] * nV - supply["Curse"] = [Dominion.Curse()] * nC - return supply - -def getSupplyOrd(): - supplyOrder = {0: ['Curse', 'Copper'], 2: ['Estate', 'Cellar', 'Chapel', 'Moat'], - 3: ['Silver', 'Chancellor', 'Village', 'Woodcutter', 'Workshop'], - 4: ['Gardens', 'Bureaucrat', 'Feast', 'Militia', 'Moneylender', 'Remodel', 'Smithy', 'Spy', 'Thief', - 'Throne Room'], - 5: ['Duchy', 'Market', 'Council Room', 'Festival', 'Laboratory', 'Library', 'Mine', 'Witch'], - 6: ['Gold', 'Adventurer'], 8: ['Province']} - return supplyOrder - -def getPlayers(): - players = [] - for name in playerNames(): - if name[0] == "*": - players.append(Dominion.ComputerPlayer(name[1:])) - elif name[0] == "^": - players.append(Dominion.TablePlayer(name[1:])) - else: - players.append(Dominion.Player(name)) - return players From be4a40c8065aafce1fada8e9a91fe33841827b99 Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Sun, 19 Jan 2020 16:04:59 -0800 Subject: [PATCH 25/27] Create testUtility.py --- projects/lagmayj/dominion/testUtility.py | 79 ++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 projects/lagmayj/dominion/testUtility.py diff --git a/projects/lagmayj/dominion/testUtility.py b/projects/lagmayj/dominion/testUtility.py new file mode 100644 index 0000000..bc377f8 --- /dev/null +++ b/projects/lagmayj/dominion/testUtility.py @@ -0,0 +1,79 @@ +""" +Created on Sun Jan 19 2020 + +@author: lagmayj +""" + +import Dominion + +def playerNames(): + player_names = ["Annie", "*Ben", "*Carla"] + return player_names + +def get_nV(): + if len(playerNames()) > 2: + nV = 12 + else: + nV = 8 + + return nV + +def getBoxes(nV): + box = {} + box["Woodcutter"] = [Dominion.Woodcutter()] * 10 + box["Smithy"] = [Dominion.Smithy()] * 10 + box["Laboratory"] = [Dominion.Laboratory()] * 10 + box["Village"] = [Dominion.Village()] * 10 + box["Festival"] = [Dominion.Festival()] * 10 + box["Market"] = [Dominion.Market()] * 10 + box["Chancellor"] = [Dominion.Chancellor()] * 10 + box["Workshop"] = [Dominion.Workshop()] * 10 + box["Moneylender"] = [Dominion.Moneylender()] * 10 + box["Chapel"] = [Dominion.Chapel()] * 10 + box["Cellar"] = [Dominion.Cellar()] * 10 + box["Remodel"] = [Dominion.Remodel()] * 10 + box["Adventurer"] = [Dominion.Adventurer()] * 10 + box["Feast"] = [Dominion.Feast()] * 10 + box["Mine"] = [Dominion.Mine()] * 10 + box["Library"] = [Dominion.Library()] * 10 + box["Gardens"] = [Dominion.Gardens()] * nV + box["Moat"] = [Dominion.Moat()] * 10 + box["Council Room"] = [Dominion.Council_Room()] * 10 + box["Witch"] = [Dominion.Witch()] * 10 + box["Bureaucrat"] = [Dominion.Bureaucrat()] * 10 + box["Militia"] = [Dominion.Militia()] * 10 + box["Spy"] = [Dominion.Spy()] * 10 + box["Thief"] = [Dominion.Thief()] * 10 + box["Throne Room"] = [Dominion.Throne_Room()] * 10 + return box + +def getSupply(nV, nC): + supply = {} + supply["Copper"] = [Dominion.Copper()] * (60 - len(playerNames()) * 7) + supply["Silver"] = [Dominion.Silver()] * 40 + supply["Gold"] = [Dominion.Gold()] * 30 + supply["Estate"] = [Dominion.Estate()] * nV + supply["Duchy"] = [Dominion.Duchy()] * nV + supply["Province"] = [Dominion.Province()] * nV + supply["Curse"] = [Dominion.Curse()] * nC + return supply + +def getSupplyOrd(): + supplyOrder = {0: ['Curse', 'Copper'], 2: ['Estate', 'Cellar', 'Chapel', 'Moat'], + 3: ['Silver', 'Chancellor', 'Village', 'Woodcutter', 'Workshop'], + 4: ['Gardens', 'Bureaucrat', 'Feast', 'Militia', 'Moneylender', 'Remodel', 'Smithy', 'Spy', 'Thief', + 'Throne Room'], + 5: ['Duchy', 'Market', 'Council Room', 'Festival', 'Laboratory', 'Library', 'Mine', 'Witch'], + 6: ['Gold', 'Adventurer'], 8: ['Province']} + return supplyOrder + +def getPlayers(): + players = [] + for name in playerNames(): + if name[0] == "*": + players.append(Dominion.ComputerPlayer(name[1:])) + elif name[0] == "^": + players.append(Dominion.TablePlayer(name[1:])) + else: + players.append(Dominion.Player(name)) + return players From 71d627a0f3750d2786fe9e5512a4efcc569d131a Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Sun, 19 Jan 2020 16:05:46 -0800 Subject: [PATCH 26/27] Create testDominion1.py --- projects/lagmayj/dominion/testDominion1.py | 76 ++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 projects/lagmayj/dominion/testDominion1.py diff --git a/projects/lagmayj/dominion/testDominion1.py b/projects/lagmayj/dominion/testDominion1.py new file mode 100644 index 0000000..c30fc02 --- /dev/null +++ b/projects/lagmayj/dominion/testDominion1.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +""" +Created on Sun Jan 19 2020 + +@author: lagmayj +""" + +import Dominion +import random +import testUtility +from collections import defaultdict + +#Get player names +player_names = testUtility.playerNames() + +#number of curses and victory cards +nV = testUtility.get_nV() +nC = -10 + 10 * len(player_names) + +#Define box +box = testUtility.getBoxes(nV) +supply_order = testUtility.getSupplyOrd() + + +#Pick 10 cards from box to be in the supply. +boxlist = [k for k in box] +random.shuffle(boxlist) +random10 = boxlist[:10] +supply = defaultdict(list,[(k,box[k]) for k in random10]) + + +#The supply always has these cards +supply = testUtility.getSupply(nV, nC) + + +#initialize the trash +trash = [] + +#Costruct the Player objects +players = testUtility.getPlayers() + +#Play the game +turn = 0 +while not Dominion.gameover(supply): +# turn += 1 #Error stops the game from incrementing turns, confusing the player + print("\r") + for value in supply_order: + print (value) + for stack in supply_order[value]: + if stack in supply: + print (stack, len(supply[stack])) + print("\r") + for player in players: + print (player.name,player.calcpoints()) + print ("\rStart of turn " + str(turn)) + for player in players: + if not Dominion.gameover(supply): + print("\r") + player.turn(players,supply,trash) + + +#Final score +dcs=Dominion.cardsummaries(players) +vp=dcs.loc['VICTORY POINTS'] +vpmax=vp.max() +winners=[] +for i in vp.index: + if vp.loc[i]==vpmax: + winners.append(i) +if len(winners)>1: + winstring= ' and '.join(winners) + ' win!' +else: + winstring = ' '.join([winners[0],'wins!']) + +print("\nGAME OVER!!!\n"+winstring+"\n") +print(dcs) From 94396f30a7d456e39429ec625cc2131f81115c5b Mon Sep 17 00:00:00 2001 From: jslgmy <49324049+jslgmy@users.noreply.github.com> Date: Sun, 19 Jan 2020 16:06:09 -0800 Subject: [PATCH 27/27] Create testDominion2.py --- projects/lagmayj/dominion/testDominion2.py | 76 ++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 projects/lagmayj/dominion/testDominion2.py diff --git a/projects/lagmayj/dominion/testDominion2.py b/projects/lagmayj/dominion/testDominion2.py new file mode 100644 index 0000000..193fed1 --- /dev/null +++ b/projects/lagmayj/dominion/testDominion2.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +""" +Created on Sun Jan 19 2020 + +@author: lagmayj +""" + +import Dominion +import random +import testUtility +from collections import defaultdict + +#Get player names +player_names = testUtility.playerNames() + +#number of curses and victory cards +nV = testUtility.get_nV() +nC = -10 + 10 * len(player_names) + +#Define box +box = testUtility.getBoxes(nV) +supply_order = testUtility.getSupplyOrd() + + +#Pick 10 cards from box to be in the supply. +boxlist = [k for k in box] +random.shuffle(boxlist) +random10 = boxlist[:10] +supply = defaultdict(list,[(k,box[k]) for k in random10]) + + +#The supply always has these cards +supply = testUtility.getSupply(nV, nC) + + +#initialize the trash +trash = [] + +#Costruct the Player objects +players = testUtility.getPlayers() + +#Play the game +turn = 0 +while not Dominion.gameover(supply): + turn += 1 + print("\r") + for value in supply_order: + print (value) + for stack in supply_order[value]: + if stack in supply: + print (stack, len(supply[stack])) + print("\r") + for player in players: + print (player.name,player.calcpoints()) + print ("\rStart of turn " + str(turn)) + for player in players: + if not Dominion.gameover(supply): + print("\r") + player.turn(players,supply,trash) + + +#Final score +dcs=Dominion.cardsummaries(players) +vp=dcs.loc['VICTORY POINTS'] +vpmax=vp.max() +winners=[] +for i in vp.index: + if vp.loc[i]==vpmax: + winners.append(i) +if len(winners)>1: + winstring= ' and '.join(winners) + ' win!' +else: + winstring = ' '.join([winners[0],'wins!']) + +#print("\nGAME OVER!!!\n"+winstring+"\n") #Does not display the winner or the scoreboard +#print(dcs)