码迷,mamicode.com
首页 > 其他好文 > 详细

测开之路四十九:用Django实现扑克牌游戏

时间:2019-05-30 22:12:23      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:root   sel   cube   正则   相同   else   col   static   game   

 

用Django实现和之前flask一样的扑克牌游戏

 

项目结构

技术图片

 

html

技术图片

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>扎金花</title>
</head>
<body>
{% for poker in player1 %}
<img src="/static/poker/{{ poker }}"/>
{% endfor %}
<br>
{% for poker in player2 %}
<img src="/static/poker/{{ poker }}"/>
{% endfor %}
<br>
<h1>{{ status }}</h1>
</body>
</html>

rule规则

技术图片

import random

class Rule(object):

def __init__(self):
self.data = list(range(0, 52))
self.player1 = []
for i in range(0, 3):
poker = self.deal()
self.player1.append({
‘color‘: poker[0],
‘value‘: poker[1],
})
self.player2 = []
for i in range(0, 3):
poker = self.deal()
self.player2.append({
‘color‘: poker[0],
‘value‘: poker[1],
})

def deal(self):
data = random.choice(self.data)
index = self.data.index(data)
self.data.pop(index)
if data < 13:
color = ‘black‘
value = data % 13 + 1
if value == 1:
value == 14
elif data < 26:
color = ‘red‘
value = data % 13 + 1
if value == 1:
value == 14
elif data < 39:
color = ‘cube‘
value = data % 13 + 1
if value == 1:
value == 14
elif data < 52:
color = ‘plum‘
value = data % 13 + 1
if value == 1:
value == 14
return color, value

def compare_value(self, data1, data2):
"""
先对其进行排序,排序后便于比较。
:param data1: 这里是pai1的牌面值
:param data2: 这里是pai2的牌面值
:return:
"""
data1.sort(reverse=True)
data2.sort(reverse=True)
for i in range(0, 3):
if data1[i] == data2[i]:
continue
elif data1[i] > data2[i]:
return 1
else:
return 2

def get_color(self, player):
color = []
for poker in player:
color.append(poker[‘color‘])
return color

def get_value(self, player):
value = []
for poker in player:
value.append(poker[‘value‘])
return value

def is_jinhua(self, player):
"""
花色相同,非顺子。例:黑桃368,方块145。
:param pai:
:return:
"""
data = self.get_color(player)
if data[0] != data[1] or \
data[0] != data[2] or \
data[1] != data[2]:
return False
else:
return True

def is_triple(self, player):
"""
# 用于判断是不是豹子,豹子是三个数相同。
:param pai:
:return:
"""
data = self.get_value(player)
if data[0] == data[1] and data[1] == data[2] and data[0] == data[2]:
return True
else:
return False

def is_shunzi(self, player):
"""
花色不同的顺子。例:黑桃5红桃6方块7。
:param pai:
:return:
"""
data = self.get_value(player)
data.sort(reverse=True)
if data[0] - data[1] == 1 and data[1] - data[2] == 1 and data[0] - data[2] == 2:
return True
else:
return False

def is_shunjin(self, player):
"""
:param pai:
:return:
"""
if self.is_shunzi(player) and self.is_jinhua(player):
return True
else:
return False

def is_pair(self, player):
"""
:param pai:
:return:
"""
data = self.get_value(player)
if data[0] == data[1] or data[1] == data[2] or data[0] == data[2]:
return True
else:
return False

def find_pair(self, player):
"""
返回相等数值的索引
:param pai:
:return:
"""
data = self.get_value(player)
if data[0] == data[1]:
return 0, 1
if data[1] == data[2]:
return 1, 2
if data[0] == data[2]:
return 0, 2

def compare_baozi(self):
"""
:return:
"""
if self.is_triple(self.player1) and not self.is_triple(self.player2):
return 1
if not self.is_triple(self.player1) and self.is_triple(self.player2):
return 2
if not self.is_triple(self.player1) and not self.is_triple(self.player2):
return None
if self.is_triple(self.player1) and self.is_triple(self.player2):
data1 = self.get_value(self.player1)
data2 = self.get_value(self.player2)
return self.compare_value(data1, data2)

def compare_shunjin(self):
"""
:return:
"""
if self.is_shunjin(self.player1) and not self.is_shunjin(self.player2):
return 1
if not self.is_shunjin(self.player1) and self.is_shunjin(self.player2):
return 2
if not self.is_shunjin(self.player1) and not self.is_shunjin(self.player2):
return None
if self.is_shunjin(self.player1) and self.is_shunjin(self.player2):
data1 = self.get_value(self.player1)
data2 = self.get_value(self.player2)
return self.compare_value(data1, data2)

def compare_jinhua(self):
"""
:return:
"""
if self.is_jinhua(self.player1) and not self.is_jinhua(self.player2):
return 1
if not self.is_jinhua(self.player1) and self.is_jinhua(self.player2):
return 2
if not self.is_jinhua(self.player1) and not self.is_jinhua(self.player2):
return None
if self.is_jinhua(self.player1) and self.is_jinhua(self.player2):
data1 = self.get_value(self.player1)
data2 = self.get_value(self.player2)
return self.compare_value(data1, data2)

def compare_shunzi(self):
"""
:param pai1:
:param pai2:
:return:
"""
# 顺子大于非顺子
if self.is_shunzi(self.player1) and not self.is_shunzi(self.player2):
return 1
if not self.is_shunzi(self.player1) and self.is_shunzi(self.player2):
return 2
if not self.is_shunzi(self.player1) and not self.is_shunzi(self.player2):
return None
# 都是顺子的话比较大小
if self.is_shunzi(self.player1) and self.is_shunzi(self.player2):
data1 = self.get_value(self.player1)
data2 = self.get_value(self.player2)
return self.compare_value(data1, data2)

def compare_pair(self):
"""
两张点数相同的牌。例:223,334。
:param pai1:
:param pai2:
:return:
"""
if self.is_pair(self.player1) and not self.is_pair(self.player2):
return 1
if not self.is_pair(self.player1) and self.is_pair(self.player2):
return 2
if not self.is_pair(self.player1) and not self.is_pair(self.player2):
return None
if self.is_pair(self.player1) and self.is_pair(self.player2):
data1 = self.get_value(self.player1)
data2 = self.get_value(self.player2)
f1, s1 = self.find_pair(self.player1)
f2, s2 = self.find_pair(self.player2)
if data1[f1] > data2[f2]:
return 1
elif data1[f1] < data2[f2]:
return 2
else:
data1.pop(f1)
data1.pop(s1)
data2.pop(f2)
data2.pop(s2)
if data1[0] > data2[0]:
return 1
elif data1[0] < data2[0]:
return 2
else:
return 3

def comapre_common(self):
"""
# 这里是一般性的比较,不是豹子,顺子,金花等。
:return:
"""
data1 = self.get_value(self.player1)
data2 = self.get_value(self.player2)
return self.compare_value(data1, data2)

def compare(self):
player1, player2 = self.wrapper()
flag = self.compare_baozi()
if flag is not None:
return flag, player1, player2
flag = self.compare_shunjin()
if flag is not None:
return flag, player1, player2
flag = self.compare_jinhua()
if flag is not None:
return flag, player1, player2
flag = self.compare_shunzi()
if flag is not None:
return flag, player1, player2
flag = self.compare_pair()
if flag is not None:
return flag, player1, player2
flag = self.comapre_common()
if flag is not None:
return flag, player1, player2

def wrapper(self):
player1 = []
for poker in self.player1:
player1.append(poker[‘color‘] + ‘_‘ + str(‘{0:02}‘.format(poker[‘value‘]))+‘.jpg‘)
player2 = []
for poker in self.player2:
player2.append(poker[‘color‘] + ‘_‘ + str(‘{0:02}‘.format(poker[‘value‘])) + ‘.jpg‘)
return player1, player2


if __name__ == ‘__main__‘:
rule = Rule()
print(rule.compare())

视图与配置

技术图片

import os
import sys
from django.shortcuts import render
from django.conf.urls import url
from django.conf import settings
from django.core.management import execute_from_command_line
from rule import Rule


BASE_DIR = os.path.dirname(__file__) # 定义当前工程目录为basedir

# 设置框架配置
settings.configure(
DEBUG=True,
ALLOWED_HOSTS=[‘*‘],
SECRET_KEY=‘aaa‘, # 用于加密的字符串
ROOT_URLCONF=__name__, # 此配置为在当前文件里面找url映射的配置
MIDDLEWARE_CLASSES=(
‘django.middleware.commom.CommonMiddleware‘,
‘django.middleware.csrf.CsrfViewMiddleware‘,
‘django.middleware.clickjacking.XFrameOptionsMiddleware‘,
),
STATIC_URL=‘/static/‘, # 定义静态文件的存放目录,和flask一致
STATICFILES_DIRS=(os.path.join(BASE_DIR, ‘static‘),), # 静态文件目录,指向BASE_DIR/static(括号里的逗号不能省)
INSTALLED_APPS=(‘django.contrib.staticfiles‘,), # Django使用静态文件需要安装即在配置中录入此信息(括号里的逗号不能省)
# 模板的配置
TEMPLATES=[{
# ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘, # Django自带的模板引擎
‘BACKEND‘: ‘django.template.backends.jinja2.Jinja2‘, # Jinja2的模板引擎
‘APP_DIRS‘: True, # APP_DIRS为True则默认app下的templates目录,否则使用下一行DIRS声明的目录
‘DIRS‘: [os.path.join(BASE_DIR, ‘templates‘)],
‘OPTIONS‘: {}}]
)

def game(request):
rule = Rule()
status, player1, player2 = rule.compare()
context = {
‘status‘: status,
‘player1‘: player1,
‘player2‘: player2,
}
return render(request, ‘game.html‘, context)

# 通过正则匹配url,做视图映射
urlpatterns = (
url(r‘^game$‘, game),
)
if __name__ == ‘__main__‘:
execute_from_command_line(sys.argv)

 

访问

技术图片

技术图片

 

测开之路四十九:用Django实现扑克牌游戏

标签:root   sel   cube   正则   相同   else   col   static   game   

原文地址:https://www.cnblogs.com/zhongyehai/p/10952221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!