猜拳游戏
需求分析:
* 使用面向对象和python的基础语法,运用简单的逻辑处理实现猜拳游戏
* 要求游戏可以多次玩耍
* 要求统计分数
* 要求可以选择角色
# 玩家自己的类,
class Owns():
chose = {1: "石头", 2: "剪刀", 3: "布"}
def __init__(self, name, race):
self.name = name
self.race = race
self.score = 0
def playgame(self):
while True:
print("游戏规则:")
for index,i in self.__class__.chose.items():
print(f"{index}:{i}",end="\t")
print()
chose = input("请输入您的选择>>>")
if chose.isdigit() and 1 <= int(chose) <= 3:
chose = int(chose)
print(f"您选择了{self.__class__.chose[chose]}")
return chose
else:
print("输入有误")
def __str__(self):
return f"我方>>>>>>姓名:{self.name}\t种族:{self.race}"
# 定义电脑玩家的类
class Computer(Owns):
super(Owns)
def __str__(self):
return f"敌方>>>>>>姓名:{self.name}\t种族:{self.race}"
# 游戏逻辑处理
class Game():
race = {1: ("人族", "仁爱而睿智"), 2: ("羊族", "温顺而纯洁"), 3: ("天族", "高贵而强悍")}
def myrole_generator(self, name, race):
return Owns(name, race)
def computer_generator(self, name, race):
return Computer(name, race)
def chose_role(self):
while True:
for index, i in self.__class__.race.items():
print(f"{index}:{i[0]}~~~{i[1]}")
chose = input("请选择您的种族>>>")
if chose.isdigit() and 1 <= int(chose) <= 3:
name = input("请设计您的名字>>>")
my1 = self.myrole_generator(name, self.__class__.race[int(chose)][0])
chose = input("请选择对战电脑的种族>>>")
if chose.isdigit() and 1 <= int(chose) <= 3:
name = input("请设计电脑的名字>>>")
com1 = self.myrole_generator(name, self.__class__.race[int(chose)][0])
else:
print("输入错误,请重新输入")
continue
else:
print("输入错误,请重新输入")
continue
return my1, com1
def play(self):
while True:
print("欢迎来到猜拳游戏,祝您玩的开心(*^▽^*)")
lisrole = self.chose_role()
my1 = lisrole[0]
com1 = lisrole[1]
print("对战信息>>")
print(my1, com1, sep="\n")
print("<<<<<<<游戏开始>>>>>>>")
import random
flag = True
while flag:
mychose = my1.playgame()
comchose = random.randint(1, 3)
print(f"电脑选择了{com1.__class__.chose[comchose]}")
if mychose == comchose:
print("平局")
elif mychose == 1 and comchose == 2 or mychose == 2 and comchose == 3 or mychose == 3 and comchose == 1:
my1.score += 1
print("您赢了")
else:
com1.score+=1
print("您输了,2233333333333")
while True:
chose = input("是否继续愉快的玩耍Y(继续)/N(退出)/S(重新选择角色),????")
if chose.isalpha() and chose.upper() == "N":
print(f"对战信息:\n{my1.name}:{my1.score}\n{com1.name}:{com1.score}")
print("猜拳游戏结束,欢迎下次来happy~~~~~")
exit()
elif chose.isalpha() and chose.upper() == "Y":
break
elif chose.isalpha() and chose.upper() == "S":
print(f"当前对战信息:\n{my1.name}:{my1.score}\n{com1.name}:{com1.score}")
flag = False
else:
continue
if flag == False:
break
# 主函数,调用端口
a1 = Game()
a1.play()
扎金花游戏
需求:
编写程序,设计单张扑克牌类Card,具有花色,牌面与具体值。
同时设计整副扑克牌类Cards,具有52张牌。li=[1,2,3] li=[card1,card2,card3…]
红桃、黑桃、方片、草花 2345678910JQKA
♥♠♦♣
设计一个发牌的函数,可以任意发出三张牌。
对任意三张牌断定牌的类型。
类型包括:
三条:三张牌value一样
一对:两张value一样
顺子:三张牌挨着
同花:三张牌type一样
同花顺:挨着,类型一样
其余都是散牌
# 单张扑克类
class card():
def __init__(self,type,value,tvalue):
self.type = type
self.value = value
self.tvalue = tvalue
def __str__(self):
return f"[{self.type}{self.value}]"
# 扑克牌的类
class cards():
def __init__(self):
types = "♥♠♦♣"
values = [i for i in range(2,11)] +list("JQKA")
self.lis = []
for i in types:
for index,j in enumerate(values):
self.lis.append(card(i,j,index+2))
self.li = []
def deal(self):
self.li = []
import random
for i in range(3):
index = random.randint(0, len(self.lis) - 1)
self.li.append(self.lis.pop(index))
return self.li
# 玩家类 和 电脑玩家类
class Owns():
def __init__(self, name, race):
self.name = name
self.race = race
def __str__(self):
return f"我方>>>>>>姓名:{self.name}\t种族:{self.race}"
def playgame(self,li):
print("您的牌如下:")
for index,i in enumerate(li):
print(f"{index+1}:{i}")
chose = input("您可以选择Y(比牌)/N(放弃)>>>")
if chose.isalpha() and chose.upper() == "Y":
return True
else:
return False
class Computer(Owns):
super(Owns)
# 逻辑处理类,游戏规则制定
class Game():
race = {1: ("人族", "仁爱而睿智"), 2: ("羊族", "温顺而纯洁"), 3: ("天族", "高贵而强悍")}
def myrole_generator(self, name, race):
return Owns(name, race)
def computer_generator(self, name, race):
return Computer(name, race)
def chose_role(self):
while True:
for index, i in self.__class__.race.items():
print(f"{index}:{i[0]}~~~{i[1]}")
chose = input("请选择您的种族>>>")
if chose.isdigit() and 1 <= int(chose) <= 3:
name = input("请设计您的名字>>>")
my1 = self.myrole_generator(name, self.__class__.race[int(chose)][0])
chose = input("请选择对战电脑的种族>>>")
if chose.isdigit() and 1 <= int(chose) <= 3:
name = input("请设计电脑的名字>>>")
com1 = self.myrole_generator(name, self.__class__.race[int(chose)][0])
else:
print("输入错误,请重新输入")
continue
else:
print("输入错误,请重新输入")
continue
return my1, com1
def getResult(self,li):
li.sort(key=lambda k: k.tvalue)
one = li[0] # type,value,real_value
second = li[1]
third = li[2]
if one.value == second.value == third.value:
return "三条"
elif one.value == second.value or second.value == third.value:
return "一对"
elif (one.type == second.type == third.type) and (
one.tvalue + 1 == second.tvalue and second.tvalue + 1 == third.tvalue):
return "同花顺"
elif one.tvalue + 1 == second.tvalue and second.tvalue + 1 == third.tvalue:
return "顺子"
elif one.type == second.type == third.type:
return "同花"
else:
return "散牌"
def play(self):
while True:
print("欢迎来到炸金花炸死你游戏,祝您玩的开心(*^▽^*)")
lisrole = self.chose_role()
my1 = lisrole[0]
com1 = lisrole[1]
print("对战信息>>")
print(my1, com1, sep="\n")
print("<<<<<<<游戏开始>>>>>>>")
while True:
cards1 = cards()
li = cards1.deal()
flag = my1.playgame(li)
if flag == True:
li1 = cards1.deal()
dresult = self.getResult(li1)
myresult = self.getResult(li)
if dresult == myresult:
if li > li1:
print(f"your is {myresult},he/she is {dresult}")
print("对方的牌:")
for index, i in enumerate(li1):
print(f"{index+1}:{i}")
print("您的牌:")
for index, i in enumerate(li):
print(f"{index+1}:{i}")
print("恭喜你,你赢了,666")
else:
print(f"your is {myresult},he/she is {dresult}")
print("对方的牌:")
for index, i in enumerate(li1):
print(f"{index+1}:{i}")
print("您的牌:")
for index, i in enumerate(li):
print(f"{index+1}:{i}")
print("哈哈哈哈,你输了,223333333")
else:
if myresult == "三条":
print(f"your is {myresult},he/she is {dresult}")
print("恭喜你,你赢了,666")
elif myresult == "同花顺" and dresult != "三条":
print(f"your is {myresult},he/she is {dresult}")
print("恭喜你,你赢了,666")
elif myresult == "同花" and dresult not in ["三条", "同花顺"]:
print(f"your is {myresult},he/she is {dresult}")
print("恭喜你,你赢了,666")
elif myresult == "顺子" and dresult not in ["三条", "同花顺", "同花"]:
print(f"your is {myresult},he/she is {dresult}")
print("恭喜你,你赢了,666")
elif myresult == "一对" and dresult not in ["三条", "同花顺", "同花", "顺子"]:
print(f"your is {myresult},he/she is {dresult}")
print("恭喜你,你赢了,666")
elif myresult == "散牌":
print(f"your is {myresult},he/she is {dresult}")
print("哈哈哈哈,你输了,223333333")
chose = input("您要继续愉快的玩耍么?>>>Y/N/exit")
if chose.upper() == "Y":
continue
elif chose.upper() == "N":
print("重新开局中~~~~~")
break
elif chose.upper() == "EXIT":
print("游戏结束,欢迎下次来受虐")
exit()
else:
print("重新开局中~~~~~")
break
# 游戏入口
g1 = Game()
g1.play()
小型购物系统
需求:
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车
shopping_list = [
("苹果手机", 5000),
("破手机壳", 48),
("电纸书facebook", 9800),
("华为手机", 4800),
("python书籍", 32),
("垃圾一堆", 24)
]
#购物车
shopping_cart = []
salary = input('请输入您的总资产: ')
#如果不是数字
if not salary.isdigit():
print("总资产必须是数字,请重新输入")
#中断程序
exit()
else:
#将资产转化为十进制
salary = int(salary)
while True:
print("-----------产品列表-----------")
for index, item in enumerate(shopping_list):
#print("\033[32m%s, %s\033[0m" %(index, item))
print(f"\033[32m{index},\033[10m{item}")
choice = input('请选择您要购买的东西>>>')
if choice.isdigit():
choice = int(choice)
if choice < len(shopping_list) and choice >= 0:
product = shopping_list[choice]
if salary > product[1]:
confirm = input('您想现在购买这个商品么?[y/n]: ')
if confirm == 'y':
shopping_cart.append(product)
salary -= product[1]
print(f"您买了 {product[0]},价格是 {product[1]}, 您剩余 {salary}")
else:
print('请再次选择')
else:
add_confirm = input(f"您的余额为: {salary}, 不够, 您想充钱么?[y/n]")
if add_confirm == 'y':
add_salary = input('请输入您要充的钱: ')
if add_salary.isdigit():
add_salary = int(add_salary)
salary += add_salary
print(f"现在您的余额是{salary}: ")
else:
print("钱数必须是数字")
else:
print("------您的购物车---------")
for index, item in enumerate(shopping_cart):
print(index, item)
else:
print("您必须选择0~5以内的商品")
elif choice == 'q':
remove_product = input("您现在要删除产品还是退出 [y/n] ")
if remove_product == "y":
print("-----------购物车列表-------------: ")
for index, item in enumerate(shopping_cart):
print(index, item)
remove_choice = input('请选择您要删除的商品>>> ')
if remove_choice.isdigit() and int(remove_choice) < len(shopping_cart) and int(remove_choice) >= 0:
salary += shopping_cart[int(remove_choice)][1]
del shopping_cart[int(remove_choice)]
print("-----------新的购物车列表-------------: ")
for index, item in enumerate(shopping_cart):
print(index, item)
print(f"your balance is {salary}")
else:
print("input error, again")
else:
print("exit now")
exit()
else:
print("-----------购物车列表-------------: ")
for index, item in enumerate(shopping_cart):
print(index, item)
print("\033[31m选择必须是数字,退出\033[0m")