尝试使用Python中的列表进行策划游戏 [英] Trying to make a mastermind game using lists in Python

查看:157
本文介绍了尝试使用Python中的列表进行策划游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个策划游戏(你必须猜测密码,它会告诉你在尝试时你得到了多少代码)。 BUUUUUT,我用一个单词代词,而不是多个单词或颜色



例如:

I'm making a mastermind game (the one where you gotta guess the password and it tells you how many of the codes you got right when you tried). BUUUUUT, I'm doing mine with a single word, and not multiple words or colors

example:

answer = "this"
given = "that"

print(str(answer - given) + "/4 correct")
>>> "2/4 correct"



I需要找到一种方法来根据它们的顺序找到变量具有这种方式的字符数。



这是代码尝试的原始程序如果需要,请运行:

https://repl.it/@BeardedPC/recreation



我尝试过:



我尝试过使用:




I need to find a way to find the number of characters the variables have this way based on the order they're in.

Here's the original program the code is trying to run in, in case it is needed:
https://repl.it/@BeardedPC/recreation

What I have tried:

I've tried using:

crct = len([i for i, j in zip(a,b) if i == j])
      gmtxt("> Entry denied \n> "+str(crct)+"/4 correct.")



但是它会显示为0或错误。



我也试过把变量变成li sts,这似乎让我最接近,但它仍然没有像我希望的那样工作。


but it either comes up as "0" or error.

I've also tried turning the variables into lists, and that seems to get me the closest, but it still doesn't work as I had hoped.

推荐答案

首先,mastermind有两个不同的分数:黑色白色。

黑色是一个字母正确,位置正确的地方。

白色字母是正确的,但在错误的地方。
所以this和that将是2 Black,0 White。

而且this和here将是0 Black,1 White。

这样做分为两个阶段:

1)按字符匹配每个输入字符。

1.1)如果它们相同,请在Blacks计数中加一个

1.2)否则,将字母复制到输出数组。

1.3)循环后,检查黑方计数。如果它匹配长度,他就赢了。

2)对两个输出数组进行排序。 (这会让生活变得更轻松)

2.1)逐个字符地比较两个输出数组,用两个索引。

2.2)如果它们是相同的,添加一个你的白人数,并增加两个指数。

2.3)否则,忽略最低,并增加该指数。

2.4)循环后,打印黑白计数。



试一试 - 它应该很简单!
First off, mastermind has two different scores: Black and White.
Black is where a letter is correct, and in the right place.
White is where a letter is correct, but in the wrong place.
So "this" and "that" would be 2 Black, 0 White.
And "this" and "here" would be 0 Black, 1 White.
So do this in two stages:
1) Match each input character by character.
1.1) If they are the same, add one to your Blacks count
1.2) Otherwise, copy the letters to output arrays.
1.3) After the loop, check the Blacks count. If it matches the length, he's won.
2) Sort both output arrays. (This makes life a lot easier)
2.1) Compare the two output arrays character by character, usine two indexes.
2.2) If they are the same, add one to your Whites count, and increment both indexes.
2.3) Otherwise, ignore the lowest, and increment that index.
2.4) After the loop, print the Black and White counts.

Try it - it should be pretty simple!


这篇关于尝试使用Python中的列表进行策划游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆