首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让用户选择使用哪一种模式?

如何让用户选择使用哪一种模式?
EN

Stack Overflow用户
提问于 2016-04-10 19:52:49
回答 1查看 52关注 0票数 1

我想让用户选择他们是想让坐标随机化,还是他们想要为Python上的战舰游戏输入坐标。应该注意的是,我正在使用一个可移植的PyScripter应用程序来构建这个程序。

随机版本

代码语言:javascript
复制
from random import randint

Battleship_Board =[]


for x in range (0,5):
Battleship_Board.append(["O"] * 5)

def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
    print (" ".join(row))

print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)

def random_row(Battleship_Board):
return randint(0, len(Battleship_Board) - 1)

def random_col(Battleship_Board):
return randint(0, len(Battleship_Board[0]) - 1)

Battleship_Row = random_row(Battleship_Board)
Battleship_Column = random_col(Battleship_Board)

for turn in range(5):
    Guess_Board_Row = int(input("Guesss the X value"))
    Guess_Board_Column = int(input("Guess the Y value"))

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
    print("You sunk my battleship!")
    break
else:

    if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
            print("Apologies, that's not on the grid")

    elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
            print("You already guessed that value")

    else:
        print("You missed my battleship")
        Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"

        print("Turn" + str(turn+1) + " out of 4.")
        print_Battleship_Board(Battleship_Board)

    if turn >=4:
        print("Game Over")

如您所见,我让用户根据列和行的随机值来玩游戏。

输入版本

代码语言:javascript
复制
Battleship_Board =[]


for x in range (0,5):
Battleship_Board.append(["O"] * 5)



def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
    print (" ".join(row))

print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)

Battleship_Row =  int(input("Please enter a X value"))
Battleship_Column = int(input("Please enter a Y value"))

if (Battleship_Row < 1 or Battleship_Row > 5) or (Battleship_Column < 1 or Battleship_Row > 5):
print("Apologies, that's not on the grid")

for turn in range(5):
    Guess_Board_Row = int(input("Guess the X value"))
    Guess_Board_Column = int(input("Guess the Y value"))

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
    print("You sunk my battleship!")
    print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")
    break

else:
    if turn == 5:
        Battleship_Board[Battleship_Row][Battleship_Column] = "X"
        print_Battleship_Board(Battleship_Board)
        print("Game Over")
        print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")

    else:
        if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
            print("Apologies, that's not on the grid")

        elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
            print("You already guessed that value")

        else:
            print("You missed my battleship!")
            Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"

        print("Turns taken out of 5:", turn + 1)
        print_Battleship_Board(Battleship_Board)

这里是用户输入值的地方。

我希望在这两个版本之间建立选项,供用户在启动应用程序时选择。我怎么能这么做?

EN

回答 1

Stack Overflow用户

发布于 2016-04-10 20:11:54

您可以使用命令行参数,允许用户在启动游戏时选择该模式:

代码语言:javascript
复制
import sys

if len(sys.argv) > 1 and sys.argv[1] == '<some-value>':
    # Randomize
else:
    # Prompt user for input

更多关于命令行参数这里的信息。对于更方便用户的命令行选项,请查看argparse解析

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36534795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档