使用面向对象的编程来创建纸牌游戏

问题描述

我刚刚开始用python编写有关“ OOP”的讲座,我的老师为我设置了使用它创建纸牌游戏的任务。我已经开始“做某事”,但是我想联系更好的和知识渊博的人,以指导我朝正确的方向前进,以防万一我做错了什么,如果是,请解释一下我应该遵循/调整的思维方式也可以将其应用于其他所有内容

这是我关于堆栈溢出的第一个问题,因此我不确定格式是否正确,但是请确保尽可能的严格!学习艰辛的方法对我来说是最好的方法

我要重新创建的游戏是“战争”


###THE PROJECT IS NOT FINISHED,I STOPPED 1/4 OF THE WAY THROUGH JUST TO MAKE SURE IM DOING IT CORRECTLY :)

import random
import itertools


class person():
  def __init__(self,name,hand,score):
    self.name = name
    self.hand = hand
    self.score = score
  

class Deck():
  name_input1 = None
  name_input2 = None
  suit = ('hearts','spades','diamond','clubs')
  ranks = ('2','3','4','5','6','7','8','9','10','Ace','Jack','Queen','King')
  values = {'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'Ace':1,'Jack':11,'Queen':12,'King':13}
  
  def shuffle_and_split(self):
    self.deck1 = list(itertools.product(Deck.ranks,Deck.suit))
    random.shuffle(self.deck1) 
    hand1 = self.deck1[0:int(len(self.deck1)/2)]
    hand2 = self.deck1[int(len(self.deck1)/2):len(self.deck1)]
    person1 = person(name_input1,hand1,0)
    person2 = person(name_input2,hand2,0)

class Game():
  def main_part_game():
    while person1.hand1 and person2.hand2:
      handOne = person1.hand1.pop()
      handTwo = person2.hand2.pop()
      if handOne > handTwo :
        print ("Player 1 put down:")
        #finish this part off 

  def rules():
    player_input_rules=input ("""
                       The rules are simple:
    1)Both players will put a card down 
    2)The person with the bigger card wins the round
    3)After all cards are exhausted the person with the highest score wins
    -Ties are settled by 0 points being awarded to both sides
    -To save time the game will not add cards won to your deck
    -This is a knock-off version of 'War'

                     Would you like to proceed?:
                          Enter Yes or No:
    """)
    if player_input_rules == 'Yes' or 'yes':
      main_part_game()
    elif player_input_rules == 'No' or 'no':
      quit()
    else: 
      print ("Next time input a valid answer!")
      quit()

  def initialise_name(self):
    name_input1 = input ("Player 1 Name:")
    name_input2 = input ("Player 2 Name:")
    rules()

  def menu (self):
    print("\nHello and welcome to \"War\":")
    menu_option = input("""
       -------------------
      [A)   Start Game   ]
      [B)      Exit      ] 
       ___________________
      """)
    if menu_option == 'A' or menu_option == 'a':
        initialise_name()
    elif menu_option == 'B' or menu_option == 'b':
        quit()    
    else:
      print ("That wasn't a valid answer,Try Again!")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)