From 19cd2472c862d60403aa950af9eefb122b0cc25b Mon Sep 17 00:00:00 2001 From: Pat Wick Date: Sat, 3 Feb 2024 22:31:40 -0500 Subject: [PATCH] Finished yahtzee_goals.py with all but the bonuses Python OOP is just different enough from Java's syntax that it's driving me crazy, but I'm still having fun. Honestly I always looked at OOP as having a different set of "scopes" as functional Programming but the more I've been using OOP the more I see it's just the same scope and access paradigm. You need to either receive or create data, do something with it, and possibly pass data back to something else. This unit is going to be an exercise in patience for me because I see a lot of framework building before actually getting to the result that I want. --- __pycache__/yahtzee.cpython-310.pyc | Bin 4468 -> 4516 bytes __pycache__/yahtzee_goals.cpython-310.pyc | Bin 1397 -> 8408 bytes play.py | 20 ++ yahtzee_goals.py | 235 ++++++++++++++++++++++ 4 files changed, 255 insertions(+) diff --git a/__pycache__/yahtzee.cpython-310.pyc b/__pycache__/yahtzee.cpython-310.pyc index bb12e89bb44a297aac6b68944a27d0d409786ce1..3aed1ee9245880a36b06337bdd7dad667d9e37a9 100644 GIT binary patch delta 335 zcmXZXt4l;d6bA4)b6+#}F?)B_U2Q&-P0(Q2Ah<;ki-xfn&aO!WlNdC(2nInpU^Xmv zxmaz2m<0a-1r1^rG5Uoa_~!i1*Ur!MV8-niMXK;_KOVZiapig^m)N?>6DzNoe88H6 zzVe|}9C8FjP|OJwLxwGsKq+UCfpRV&3puVqDO7SD%Af|1KshwZV^9G#xdl1Mj`IXq zv+C~iMU$)UgH58AWYIUx$!~N`v(-iHiRP=XWS6Kd8}^abq?w-6mfWNR+LqPqk67CI z6YW(=(IGmPb=Rjud3EpE8SXon#CNnPJLQzT)p~R)o$`5k38hgXnLtNI>liadLZlqp Qq7RT{7=zyCS{CXn|AW&-+5i9m delta 287 zcmWO1D^CMK6b0b9`<$KKncbE*v_M(~f}oL*)S-BY2_Tx&Qiny5U}`FxoA3+DA7Bj@ zK|%=p0WfH=2qgEOeW&gm(j zqJ9rgh??Auzp2eGxuaHnpLn9p2YpGj%cJa>_ISrk=zt&1jCy>Y|Is0z7B6&MZ`dAD upNDQtXZ+*7XvA;jlm>iJy>`~+L#-mxp^kN;jgE9Boykrmy2!ZS*#8IM_d{v` diff --git a/__pycache__/yahtzee_goals.cpython-310.pyc b/__pycache__/yahtzee_goals.cpython-310.pyc index c8b0b1fd3e7c621544de6a4d6bd79dd78fea56cf..e281c45b328b5668959b71b000bbab383a4a47bf 100644 GIT binary patch literal 8408 zcmcgxO>A6O6@Gv7V|zUQOX4_bo0o>RaYJplNhm*QaGT1lXybrvkQ#`F>D*^~n(;j6 zzH!rz90@5E3l^YC>?%-FvA`0DEn)!@3lM8ojKqSUO*XJF3lxFi`_7%2H_!HX##v;n zd!EnzIp^MU&v(yx_l3j5gBE^2bbk~4^9jrP8=20Z3uYcA`W46%n5z1ZXIypzV z;9PJvI8UX(rNBAh(kcTk1I`7PRRiD#z<*HbE`(s*JmkdbX<-w2D9Yt)!R*HEa_Q#mdhuqtx>~qh zuB{bHp$cZ#Z^t=VvR$cMFs>`(bgdRu6*0Di#yXY<$>AZ|HD+qn740J&-{= zXV*tsYn?HziIuTedu24B!kV`1*R2~vcib)aJ*@1eqer$J<-Ba&ac+5Aj;!+mx>^rx z5G>PMDVBn5PiMjE0gy(j7S@WDhO5efEY6S|+c`P9>JB;2&f0@0&UZY!o^MZ7?6SC> zX=Dq9YH>X%6dHqt!g{DSEBrrKDBRjCR+=rDLP3QkRC}9Ip!9G{Nrz(ESBrM;D$5S1 zkcEECz{z=-NHwCGVhA8`J9oa6|}E*0E}Db`Pd7~_iyV|;x)IV7Ii zDKw0+@hK8=Jq^3&yiw8ay^YWK=BLVrjF`UeC#pd3M%`sU`Ay0Wx(^Cp?ILIO$Hv$e&Xc^ zy5P&yA@-Al%IN52zB3p!%YkA(a($Yw{61W8Y5|YE=0jy)=A(?h%=a2+*kL{nJe>LD zd^pg23)InJKGD(3e9vIeWeybck?YTVOXUw>zDYdxST8Dn(1-C-9NN#>_chKgHk~Lw zoauPH9gN(svqx+?qobGUp2MK494MwE*VS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Q+CL#WLf2%btBA7?>wTw)_S&jbA? zC+RLekT=sN`j=7H75#r?<5xiHyLS9A*cT05vHupN|C(yPOhT?V+TSkf)u4sN`U?i_ z7dp`%zXd-E*hT)~!2V~>-he$ZyT=Chzj6Ww?8yl{BG~^zc?RtJ$V>Pb!S136*qK`; z!0z-3?I#sJkSGsAt0*i-=+^i$4zQg~!h1Gws(Eux{^f15@4PAlt4XF4N)d;Z2 z^#oSt9rL}YK4&0xu?wV%2(Cy}{Rm%P{P&BM&3^8h2AabAik<)F#05_I=CH>Gr2kU2 z0O@o~^&>*)Ka?a88cs+`_z{7_w;=%!V^odrk+|`?&-mvc5;}KnW%C;wlQ)87+B2Ex z^1`&M8C*>?aw|a<61Sc9GrH+rJ6T|Kft^G>MnCjAmPR-{%D+KHa6StQC<9)=!A*N4|WY zl$Wx@T&QM^l)ij?l=cuGA2y@*b8)%`T}0gD3H-C?M$3vnyUI)S!B^Txpj0-!b%RyU zHIAo&0OdJOS+WOmi_^pMSyVpp>C>puXGr+DKtD&qdn$Qn*7N+2(OvT-k}c7c n(Q#VGM|Sp-`I37X>a2*DXn8w7(((6v{;B+l{K@>u@#FsoRD;)u delta 78 zcmccN_?3$W@g6yEc||&%tcH< cWkvEJLUD2+r}*U63jADbKrSC6AHNVk0LtADWB>pF diff --git a/play.py b/play.py index a8e683c..9e47524 100644 --- a/play.py +++ b/play.py @@ -3,12 +3,32 @@ from yahtzee_goals import ( GoalOnes, GoalTwos, GoalThrees, + GoalFours, + GoalFives, + GoalSixes, + ThreeOfAKind, + FourOfAKind, + FullHouse, + SmallStraight, + LargeStraight, + Yahtzee, + Chance ) goals = [ GoalOnes(), GoalTwos(), GoalThrees(), + GoalFours(), + GoalFives(), + GoalSixes(), + ThreeOfAKind(), + FourOfAKind(), + FullHouse(), + SmallStraight(), + LargeStraight(), + Yahtzee(), + Chance() ] game = Yachtzee(goals) diff --git a/yahtzee_goals.py b/yahtzee_goals.py index fce4e5a..6838fc1 100644 --- a/yahtzee_goals.py +++ b/yahtzee_goals.py @@ -43,3 +43,238 @@ class GoalThrees: if die.face == 3: total += 3 return total + +class GoalFours: + "Four points for each four" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Fours ({potential_score})" + + def score(self, dice): + total = 0 + for die in dice: + if die.face == 4: + total += 4 + return total + +class GoalFives: + "Five points for each five" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Fives ({potential_score})" + + def score(self, dice): + total = 0 + for die in dice: + if die.face == 5: + total += 5 + return total + +class GoalSixes: + "Six points for each six" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Six ({potential_score})" + + def score(self, dice): + total = 0 + for die in dice: + if die.face == 6: + total += 6 + return total + +class ThreeOfAKind: + "Total of all 5 dice" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Three of a kind ({potential_score})" + + def faces(self, dice): + #print([die.face for die in dice]) + return [die.face for die in dice] + + def is_three_of_a_kind(self,dice): + sorted = self.faces(dice) + sorted.sort() + #print(sorted) + if sorted[0] == sorted[1] and sorted[0] == sorted[2] and sorted[0] != sorted[3]: + return True + elif sorted[1] == sorted[2] and sorted[1] == sorted[3] and sorted[1] != sorted[4]: + return True + elif sorted[2] == sorted[3] and sorted[2] == sorted[4]: + return True + else: + return False + # if self.faces(dice)[0] == self.faces(dice)[1] and self.faces(dice)[0] == self.faces(dice)[2] and self.faces(dice)[0] != self.faces(dice)[3]: + # return True + # elif self.faces(dice)[1] == self.faces(dice)[2] and self.faces(dice)[1] == self.faces(dice)[3] and self.faces(dice)[1] != self.faces(dice)[4]: + # return True + # elif self.faces(dice)[2] == self.faces(dice)[3] and self.faces(dice)[2] == self.faces(dice)[4]: + # return True + # else: + # return False + + def score(self, dice): + if self.is_three_of_a_kind(dice): + total = 0 + for die in dice: + total += die.face + return total + else: + return 0 + +class FourOfAKind: + "Total of all 5 dice" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Four of a kind ({potential_score})" + + def faces(self,dice): + return [die.face for die in dice] + + def is_four_of_a_kind(self,dice): + sorted = self.faces(dice) + sorted.sort() + if sorted[0] != sorted[4] and sorted[0] == sorted[1] and sorted[0] == sorted[2] and sorted[0] == sorted[3]: + return True + elif sorted[0] != sorted[1] and sorted[1] == sorted[2] and sorted[1] == sorted[3] and sorted[1] == sorted[4]: + return True + else: + return False + + def score(self, dice): + if self.is_four_of_a_kind(dice): + total = 0 + for die in dice: + total += die.face + return total + else: + return 0 + +class FullHouse: + "25 points for three of one number and two of another" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Full House ({potential_score})" + + def faces(self,dice): + return [die.face for die in dice] + + def is_full_house(self,dice): + counts = [] + for i in range(1,7): + counts.append(self.faces(dice).count(i)) + if 2 in counts and 3 in counts: + return True + else: + return False + + def score(self,dice): + if self.is_full_house(dice): + return 25 + else: + return 0 + +class SmallStraight: + "Score 30 points if 4 of 5 dice show a sequence" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Small Straight ({potential_score})" + + def faces(self,dice): + return [die.face for die in dice] + + def is_small_straight(self,dice): + small_straights = [[1,2,3,4], [2,3,4,5], [3,4,5,6], [1,2,3,4,5], [2,3,4,5,6]] + ss_list = self.faces(dice) + check = list(set(ss_list)) + check.sort() + if check in small_straights: + return True + else: + return False + + def score(self,dice): + if self.is_small_straight(dice): + return 30 + else: + return 0 + +class LargeStraight: + "Score 40 points if all 5 dice show a sequence" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Large Straight ({potential_score})" + + def faces(self,dice): + return [die.face for die in dice] + + def is_large_straight(self,dice): + lorge_straights = [[1,2,3,4,5], [2,3,4,5,6]] + ls_list = self.faces(dice) + check = list(set(ls_list)) + check.sort() + if check in lorge_straights: + return True + else: + return False + + def score(self,dice): + if self.is_large_straight(dice): + return 40 + else: + return 0 + +class Yahtzee: + "Score 50 points if all 5 dice are the same value" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Yahtzee ({potential_score})" + + def faces(self,dice): + return [die.face for die in dice] + + def is_yahtzee(self,dice): + check = set(self.faces(dice)) + if len(check) == 1: + return True + else: + return False + + def score(self,dice): + if self.is_yahtzee(dice): + return 50 + else: + return 0 + +class Chance: + "Scores total of all 5 dice" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Chance ({potential_score})" + + def score(self, dice): + total = 0 + for die in dice: + total += die.face + return total \ No newline at end of file