用户:.................20查看:3 回复:11 评论:3 创建时间:2021-07-29T15:26:32
【作品展示】

【作品介绍】
因为玩24点游戏的时候想不到答案,所以就做了一个这样的查询器。。。
【作品源代码】
pa = ['+', '-', '*', '/']
pa_s = []
for i in range(len(pa)):
a = pa[i]
for j in range(len(pa)):
b = pa[j]
for k in range(len(pa)):
c = pa[k]
pa_s.append([a, b, c])
def plus(na, p, nb):
if p=='+':
return na+nb
if p=='-':
return na-nb
if p=='*':
return na*nb
if p=='/':
if nb == 0:
return 0
else:
return na/nb
def cr(plus_l, num_l, ty):
t = ty.split("x")
an = t[0]
for i in range(1, 4):
an += str(num_l[i-1])
an += t[i*2-1]
an += plus_l[i-1]
an += t[i*2]
an += str(num_l[-1])
an += t[-1]
return an
def check(plus_l, num_l):
A = plus(num_l[0], plus_l[0], num_l[1])
A = plus(A, plus_l[1], num_l[2])
A = plus(A, plus_l[2], num_l[3])
if A == 24:
if plus_l[2] == '*' or plus_l[2] == '/' and plus_l[1] == '+' or plus_l[1] == '-':
return cr(plus_l, num_l, '(x x x x x)x x ')
elif plus_l[1] == '*' or plus_l[1] == '/' and plus_l[0] == '+' or plus_l[0] == '-':
return cr(plus_l, num_l, '(x x x)x x x x ')
else:
return cr(plus_l, num_l, ' x x x x x x x ')
A = plus(num_l[1], plus_l[1], num_l[2])
A = plus(num_l[0], plus_l[0], A)
A = plus(A, plus_l[2], num_l[3])
if A == 24:
if plus_l[2] == '*' or plus_l[2] == '/' and plus_l[0] == '+' or plus_l[0] == '-':
return cr(plus_l, num_l, '[x x(x x x)]x x ')
else:
return cr(plus_l, num_l, ' x x(x x x)x x ')
# A = plus(num_l[2], plus_l[2], num_l[3])
# B = plus(num_l[0], plus_l[0], num_l[1])
# A = plus(B, plus_l[1], A)
# if A == 24:
# if plus_l[2] == '*' or plus_l[2] == '/' and plus_l[1] == '+' or plus_l[1] == '-':
# return cr(plus_l, num_l, '(x x x x x)x x ')
# return cr(plus_l, num_l, 'x x x x(x x x)')
A = plus(num_l[1], plus_l[1], num_l[2])
A = plus(A, plus_l[2], num_l[3])
A = plus(num_l[0], plus_l[0], A)
if A == 24:
if plus_l[2] == '*' or plus_l[2] == '/' and plus_l[1] == '+' or plus_l[1] == '-':
return cr(plus_l, num_l, ' x x[(x x x)x x]')
return cr(plus_l, num_l, 'x x(x x x x x)')
A = plus(num_l[2], plus_l[2], num_l[3])
A = plus(num_l[1], plus_l[1], A)
A = plus(num_l[0], plus_l[0], A)
if A == 24:
return cr(plus_l, num_l, ' x x[x x(x x x)]')
A = plus(num_l[0], plus_l[0], num_l[1])
B = plus(num_l[2], plus_l[2], num_l[3])
A = plus(A, plus_l[1], B)
if A == 24:
return cr(plus_l, num_l, '(x x x)x(x x x)')
return None
def twenty_four(putin):
global pa_s
putin = int(putin)
w = putin // 1000
x = putin // 100 % 10
y = putin // 10 % 10
z = putin % 10
if w == 0:
w = 10
if x == 0:
x = 10
if y == 0:
y = 10
if z == 0:
z = 10
nums=[]
num=[w, x, y, z]
for i in range(len(num)):
a = num.pop(i)
for j in range(len(num)):
b = num.pop(j)
for k in range(len(num)):
c = num.pop(k)
for l in range(len(num)):
d = num.pop(l)
nums.append([a, b, c, d])
num = [w, x, y, z]
num.pop(i)
num.pop(j)
num = [w, x, y, z]
num.pop(i)
num = [w, x, y, z]
for k in pa_s:
for i in nums:
A = check(k, i)
if A:
return A
return False
print("输入四个1-10的数字,如:1234(10写做0)")
for i in range(10):
x = input("")
print(twenty_four(x))
# nums = []
# num = [喵]
# for i in range(len(num)):
# num = [喵]
# a = num.pop(i)
# num = [喵]
# for j in range(len(num)):
# num = [喵]
# b = num.pop(j)
# num = [喵]
# for k in range(len(num)):
# num = [喵]
# c = num.pop(k)
# num = [喵]
# for l in range(len(num)):
# num = [喵]
# d = num.pop(l)
# z = twenty_four(a, b, c, d)
# if z == False:
# pass
# else:
# nums.append(z)
# num = [喵]
# num = [喵]
# num = [喵]
# num = [喵]
# print(len(nums))
# print(nums)
# with open("24点.txt", 'w')as f:
# f.write(str(nums))
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn
KIHIO五子棋程序:
def jd(x):
try:
x = float(x)
except ValueError:
return 0
if x-(float(int(x))) > 0:
return 0
x = int(x)
if x < 1 or x > 10:
return 0
return x
def sy(a, b, c, a2, b2, c2, s):
x = 0
while x <= a2:
y = 0
while y <= b2:
n = 0
lxh = 0
lxb = 0
while n <= c2:
if s[x+y+n] == '黑':
lxh += 1
else:
lxh = 0
if s[x+y+n] == '白':
lxb += 1
else:
lxb = 0
n += c
if lxb == 5:
return 0
if lxh == 5:
return 2
y += b
x += a
return 1
def xsy(xx, xs, yx, ys, js, js2, s):
x = xx
while x <= xs:
y = yx
while y <= yx:
lxb = 0
lxh = 0
n = 0
while n <= 4:
if s[(y+n*js2)*10+x+n*js] == '黑':
lxh += 1
else:
lxh = 0
if s[(y+n*js2)*10+x+n*js] == '白':
lxb += 1
else:
lxb = 0
n += 1
if lxb == 5:
return 0
if lxh == 5:
return 2
y += 1
x += 1
return 1
print("棋盘为10*10。")
s = []
a = 0
while a < 100:
s.append('无')
a += 1
print("你告诉我黑棋还是白棋先下?(输入A代表黑,输入B代表白):")
while True:
hb = input()
if hb != 'A' and hb != 'B':
print("输入错误,请重试:")
else:
break
b = 0
while True:
f = 0
fh = 0
hqs = 0
bqs = 0
if sy(10, 1, 1, 90, 5, 4, s) == 2:
hqs = 1
if sy(1, 10, 10, 9, 50, 40, s) == 2:
hqs = 1
if sy(10, 1, 1, 90, 5, 4, s) == 0:
bqs = 1
if sy(1, 10, 10, 9, 50, 40, s) == 0:
bqs = 1
if xsy(0, 5, 0, 5, 1, 1, s) == 2:
hqs = 1
if xsy(4, 9, 4, 9, -1, -1, s) == 2:
hqs = 1
if xsy(0, 5, 0, 5, 1, 1, s) == 0:
bqs = 1
if xsy(4, 9, 4, 9, -1, -1, s) == 0:
bqs = 1
if xsy(0, 5, 4, 9, 1, -1, s) == 2:
hqs = 1
if xsy(0, 5, 4, 9, 1, -1, s) == 0:
bqs = 1
if xsy(4, 9, 0, 5, -1, 1, s) == 2:
hqs = 1
if xsy(4, 9, 0, 5, -1, 1, s) == 0:
bqs = 1
while f < 100:
k = 0
bs = ' '
while k < 10:
if k == 0:
bs = s[f+k]
else:
bs = bs+' '+s[f+k]
k += 1
print(bs)
f += 10
if hqs == 1:
print("黑棋赢!")
break
if bqs == 1:
print("白棋赢!")
break
if hb == 'A':
if b % 2 == 0:
jhb = 'A'
else:
jhb = 'B'
else:
if b % 2 == 0:
jhb = 'B'
else:
jhb = 'A'
if jhb == 'A':
print("黑棋的x坐标(从左到右,1~10):")
while True:
x = input()
if jd(x) == 0:
print("输入错误,请重试:")
else:
x = jd(x)
break
print("黑棋的y坐标(从上到下,1~10):")
while True:
y = input()
if jd(y) == 0:
print("输入错误,请重试:")
else:
y = jd(y)
break
if (s[(y-1)*10+x-1] == '黑') or (s[(y-1)*10+x-1] == '白'):
print("禁止重叠,下次做决策看棋盘现状。")
fh = 1
else:
s[(y-1)*10+x-1] = '黑'
else:
print("白棋的x坐标(从左到右,1~10):")
while True:
x = input()
if jd(x) == 0:
print("输入错误,请重试:")
else:
x = jd(x)
break
print("白棋的y坐标(从上到下,1~10):")
while True:
y = input()
if jd(y) == 0:
print("输入错误,请重试:")
else:
y = jd(y)
break
if (s[(y-1)*10+x-1] == '黑') or (s[(y-1)*10+x-1] == '白'):
print("禁止重叠,下次做决策看棋盘现状。")
fh = 1
else:
s[(y-1)*10+x-1] = '白'
if fh == 0:
b += 1点赞0
评论