简体   繁体   English

在Java中创建未定义的方法行为?

[英]Creating undefined method behavior in Java?

I have got a task about a board game to suggest the best move via a method when we get an in-progress game state. 我有一个关于棋盘游戏的任务,当我们获得进行中的游戏状态时,通过一种方法建议最佳动作。 I have to do it in Java. 我必须用Java来做。

There is a strange instruction in the task: if we get a state which can't occur in normal play then "the behavior of this method is undefined". 任务中有一条奇怪的指令:如果我们得到的状态无法正常播放,则“此方法的行为未定义”。 (You can imagine this state like in chess you get a board with two kings on adjacent fields, which can't be happened in a real chess game.) (您可以想象这种状态,就像在国际象棋中您在相邻的字段上有两个国王的棋盘一样,这在真正的国际象棋游戏中是不可能发生的。)

The question is: what can this instruction mean? 问题是:该指令意味着什么? Is there anybody who has already faced with a same problem? 是否有人已经遇到过相同的问题?

First, I thought that in such a state I should throw, for example, an IllegalStateException . 首先,我认为在这种状态下,我应该抛出例如IllegalStateException But according to another instruction line I have to throw IllegalStateException when I get a board with a state representing that one of the players has already won the game, or it is draw. 但是根据另一条指令,当我得到一个棋盘的状态表示某个球员已经赢得比赛,或者是平局时,我必须抛出IllegalStateException

I have already searched for the definition of undefined behavior, but I don't know how can I use it in this case. 我已经搜索了未定义行为的定义,但是在这种情况下我不知道如何使用它。 At all, can I create this kind of behavior in Java which can be connected to this kind of case? 完全可以在Java中创建可与这种情况关联的这种行为吗?

I think when the problem states "this behavior is undefined" it's just saying that they aren't going to test that condition because you can't reasonably be expected to do anything. 我认为,当问题表明“此行为是未定义的”时,它只是在说他们不会测试该条件,因为无法合理地期望您做任何事情。 It's not actually a task for you to complete - it's just telling you not to worry about those scenarios. 实际上,这不是您要完成的任务-只是告诉您不要担心这些情况。

Why not write an "ImpossibleStateException" and throw this if the said case is true? 如果上述情况为真,为什么不编写一个“ ImpossibleStateException”并抛出该异常? It's part of Java's design that you can write own exception classes, so why not write one and throw it when an undefined state is happening. 您可以编写自己的异常类,这是Java设计的一部分,所以为什么不编写一个异常类并在发生未定义状态时将其抛出。

I'm not sure how the said state is ever produced, I could imagine if you talk about a kind of board game, maybe somebody fiddles around with the ram or the vm and tries to cheat / hack the game. 我不确定上述状态是如何产生的,我可以想象,如果您谈论一种棋盘游戏,也许有人摆弄ram或vm并试图作弊/破解游戏。

Also, if somebody would make a deep test of EVERY possible state of the program, like every variable with every possible value the game allows, there would be around 98% cases that made no sense at all. 同样,如果有人会对程序的每个可能状态进行深入测试,就像游戏允许的每个变量和每个可能的值一样,那么大约98%的情况根本没有意义。

From Oracle doc IllegalStateException: 从Oracle文档IllegalStateException:

Signals that a method has been invoked at an illegal or inappropriate time. 表示已在非法或不适当的时间调用了方法。 In other words, the Java environment or Java application is not in an appropriate state for the requested operation. 换句话说,对于所请求的操作,Java环境或Java应用程序没有处于适当的状态。

So I think in your sitiation it is not good solution. 因此,我认为在您的情况下这不是一个好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM