如果您的第一轮掷骰数是7或11,那么您将获胜。如果是2或3或12,那么您输了。如果是其他任何数字,则必须再次滚动,直到击中该数字或按7或11退出。
我遇到的问题基本上是打了一个数字,但我需要程序来保存随机选择的数字并重新开始。并且如果它重新开始并命中了另一个数字,而没有跳开,则会再次开始直到该数字被击中或突然消失为止。
对理论和基本的基础编程的一些解释确实可以帮助我。我不确定从已经开始的地方去哪里。
Python:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_screen_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Top Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary">
<android.support.v7.widget.Toolbar
android:id="@+id/drawer_menu_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<FrameLayout
android:id="@+id/drawer_menu_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="@+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="10dp"
android:background="@android:"
<EditText
android:id="@+id/searchHere"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/seachbar_homescreen"
android:hint="Search Here"
android:textSize="15sp" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</RelativeLayout>
<!--<android.support.design.widget.NavigationView
android:id="@+id/home_screen_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
android:visibility="gone"
app:headerLayout="@layout/home_screen_menu_drawer_header"
app:menu="@menu/home_screen_menu_drawer" />
-->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/home_screen_menu_drawer_header"
app:menu="@menu/home_screen_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
我只希望程序像“好,这是滚动的数字。我要记住这一点,然后再次开始查看它是否可以击中相同的数字。”
答案 0 :(得分:1)
您似乎丢失的实际上是在循环中将骰子滚动。
通常的想法是掷骰子一次,并检查立即赢或输。
如果不是瞬间赢/输,然后进入循环,连续掷骰子直到赢或输。< / p>
以下伪代码(a)(带有注释)显示了如何执行此操作:
# Store first throw.
firstThrow = rnd(1..6) + rnd(1..6)
print "First throw " firstThrow
# Set winner if house or player wins on first throw.
winner = ''
if firstThrow is one of (7, 11): winner = 'player'
else if firstThrow is one of (2, 3, 12): winner = 'house'
# Loop until a winner found (may have already happened above).
while winner == '':
# Get subsequent throw.
throw = rnd(1..6) + rnd(1..6)
print "Next throw " throw
# Set winner (exits loop) if win or lose.
if throw is one of (7, 11): winner = 'house'
else if throw is same as firstThrow: winner = 'player'
# Have a winner, output it.
print "Winner was " winner
(a)对于几乎可以肯定是课堂作业的问题,我发现最好提供伪代码,并让提问者将其转换为他们选择的语言,因为这样做往往会使开发人员变得更好。
在这种情况下,转换为Python相对简单,但我敦促您自己尝试一下。如果您为此而苦苦挣扎,我在下面提供了一些Python代码(希望您不会一字不漏地使用它,因为您(或其他人)可能拥有的任何教育者,反正还是个坏主意,能够看到此答案并相应地评分):
import random
# Store first throw.
firstThrow = random.randint(1, 6) + random.randint(1, 6)
print("First throw", firstThrow)
# Set winner if house or player wins on first throw.
if firstThrow in (7, 11): winner = 'player'
elif firstThrow in (2, 3, 12): winner = 'house'
else: winner = ''
# Loop until a winner found (may have already happened above).
while winner == '':
# Get subsequent throw.
throw = random.randint(1, 6) + random.randint(1, 6)
print("Next throw", throw)
# Set winner (exits loop) if win or lose condition found.
if throw in (7, 11): winner = 'house'
elif throw == firstThrow: winner = 'player'
# Have a winner, output it.
print("Winner was", winner)
这是一些示例运行供确认:
First throw 11
Winner was player
First throw 7
Winner was player
First throw 4
Next throw 7
Winner was house
First throw 9
Next throw 10
Next throw 7
Winner was house
First throw 11
Winner was player
First throw 10
Next throw 5
Next throw 11
Winner was house
First throw 8
Next throw 5
Next throw 11
Winner was house
First throw 8
Next throw 7
Winner was house
答案 1 :(得分:-1)
import random
numbers_occurred_till_now =[] # here we are going to store all the dice value that will occur.
while True:
roll = random.randint(1, 6) + random.randint(1, 6)
numbers_occurred_till_now.append(roll) # when the number is generated, we are appending it in the list.
if roll == 7 or roll == 11:
print('nice')
print(numbers_occurred_till_now)
break
elif roll == 3 or roll == 2 or roll == 12:
print('crapped out')
print(numbers_occurred_till_now)
break
elif numbers:
print('your number is:', roll, "roll again...")
print(numbers_occurred_till_now)