为 Game of Nim 组织/创建课程时出现问题

发布于 2024-09-29 10:00:13 字数 333 浏览 6 评论 0原文

Nim 的这个特殊变体涉及: 两名玩家轮流从一堆弹珠中取出弹珠。在每次移动中,玩家选择要拿走多少弹珠。玩家必须拿走至少一个但最多一半的弹珠。然后另一位玩家轮流进行。拿走最后一颗弹珠的玩家就输了。 我需要: - 编写一个人类与计算机对战的程序 - 生成一个 10 到 100 之间的随机整数来表示堆的初始大小 - 生成一个 0 到 1 之间的随机整数来决定是计算机还是人类先进行第一回合 - 生成一个 0 到 1 之间的随机整数来决定计算机玩的是“聪明”还是“愚蠢”(计算移动或随机选择)

问题是我们必须使用三个类:Pile、Player 和 Game。我不知道如何让所有三个班级相互“交谈”以及如何将内容分开。我完全不知道如何开始这个。

This particular variant of Nim involves:
Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marbles loses.
I need to:
- Write a program in which a human plays vs a computer
- Generate a random int between 10 and 100 to denote initial size of the pile
- Generate a random int between 0 and 1 to decide whether the computer or the human takes the first turn
- Generate a random int between 0 and 1 to decide whether the computer plays "smart" or "stupid" (calculating moves or choosing randomly)

The problem is that we have to use three classes: Pile, Player, and Game. I have no clue how to get all three classes to "talk" to each other and how to split up what goes where. I am completely lost on how to start this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恬淡成诗 2024-10-06 10:00:13

首先定义您的类。

你有桩。你能对一堆做什么?你可以问它什么问题?例如:“一堆,还剩下多少弹珠?”写下这些问题和操作 - 它们将成为 Pile 中的公共方法。

你有一个玩家。玩家可以做什么?玩家可以做的一件事就是“轮流”。玩家还可以做什么?写下所有这些东西 - 它们将成为 Player 中的公共方法。

你有一个游戏。游戏有什么作用?可能是“玩”之类的东西。把这些东西写下来,它们将成为Game中的公共方法。

现在你有 3 个充满有用方法的类。把它们编码起来。一旦你做到了这一点,请回帖。

Start by defining your classes.

You have Pile. What can you do to a Pile? What questions can you ask it? ex: "Pile, how many marbles remain?" Write these questions and actions down - they will become public methods in Pile.

You have a Player. What can a Player do? One thing a player can do is "take a turn." What else might a player do? Write all these things down - they will become public methods in Player.

You have a Game. What does a game do? probably something like "play". Write these things down, they will be public methods in Game.

Now you have 3 classes full of useful methods. Code them up. Once you've gotten that far, post back.

陌上芳菲 2024-10-06 10:00:13

名义上,我希望 Game 包含一个 Pile 实例和两个 Player 实例。然后游戏将根据需要调用每个实例的方法。

Pile 至少包含一个 int 来存储堆中弹珠的数量,一种生成初始数量的方法和一种获取弹珠的方法。

Nominally, I would expect Game to contain one instance of Pile and two instances of Player. Game would then call the methods of each instance as necessary.

Pile would minimally contain an int to store the number of marbles in the pile, a method which would generate the initial number and a method for taking marbles.

病女 2024-10-06 10:00:13

如果您的意思是:“我不知道如何让一个类调用另一个类的方法”,这是您第一次开始学习编程时的常见问题。因此,我将做出一个可能没有根据的假设,即您对编程尤其是面向对象的编程非常陌生。

尝试首先编写一个名为“Pile”的类,其中包含一个 Integer 类型的私有变量。将此变量称为“marbles”并将其初始化为某个数字,例如 50。

为该类编写一个名为“how_many”的公共方法,该方法返回弹珠的数量,另一个名为“take_marbles”的公共方法,使用整数调用并减少堆的大小。

创建一个名为“游戏”的类。游戏有一个“Pile”类型的属性,在创建新游戏时创建,并且它具有允许它调用其 Pile 的“how_many”和“take_marbles”方法的方法。

然后构建一个小测试程序,创建一个新游戏,并随机减少堆中弹珠的数量,直到该数量达到零。每次移除一些弹珠时,它应该输出剩余弹珠的数量。

我想一旦你这样做了,你就会知道下一步该做什么。

If what you mean is: "I don't know how to have a class call the methods of another class" this is sort of a common problem when you first start to learn to program. So I'm going to make the possibly unwarranted assumption that you are very new to programming and OO oriented programming in particular.

Try starting by writing a class called 'Pile' with a private variable of type Integer. Call this variable "marbles" and initialize it to some number, like 50.

Write a public method for that class called 'how_many' that returns the number of marbles, and another public method called 'take_marbles' that is called with an integer and reduces the size of the pile.

Create a class called 'Game'. Game has a attribute of type 'Pile', created when a new Game is created, and it has methods that allow it to call the 'how_many' and 'take_marbles' methods of it's Pile.

Then build a little test program that creates a new Game and has it randomly reduce the number of marbles in the pile until that number reaches zero. Every time it removes some marbles, it should output the number of marbles left.

I think once you do that, you'll have an idea of what to do next.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文