帮助需要基于WRT文本的TicTacToe游戏改编。 [英] Help needed WRT text based adaptation of the TicTacToe game.

查看:69
本文介绍了帮助需要基于WRT文本的TicTacToe游戏改编。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学校学习java,到目前为止我们已经处理过基于文本的编译器JGrasp;因此;我试图编写Tictactoe或X和O的代码。游戏是作为基于文本的程序编写的。


我使用了2维整数数组来表示3x3网格。因此每个块都有一个协调的。 grid [0] [0]。最初网格中的所有块都等于零。计算机使用(1)并且播放器使用两个(2)。提示用户输入行号和列号并输入他们的选择。


当问题出现时计算机必须通过选择一个块来响应。我认为我可以通过生成随机数来解决这个问题。故障是,当生成相同的随机坐标集时,看起来好像计算机根本没有播放。这意味着游戏仍然无法播放。

I am currently learning java at school and thus far we have oly dealt with the text-based compiler JGrasp; therefore; my attempt to code Tictactoe or "X and O''s" game is written as a text based program.

I have made use of a 2 dimensional integer array to represent the 3x3 grid. Each block therefore has a co-ordinate.eg. grid[0][0].Initially all blocks in the grid are equal to zero. The computer uses ones(1) and the player uses twos(2).The user is prompted to enter the row and column number and their choice is entered.

The problemm arises when the computer has to respond by choosig a block to play in. I thought that i could solve this by generating random numbers. The glitch is that when the same set of random co-ordinates is generated it appears as though the computer hasnt played at all. This means that as yet the game is still un-playable.

展开 | 选择 | Wrap | 行号

推荐答案

你需要提供一个种子。随机方法。 (参见 Math.Random(种子)了解更多信息)。


如果你每次都没有指定不同的种子,那么相同的随机数就是结果。


尝试使用当前日期/时间作为随机方法的种子(它总是在改变)
You need to provide a "seed" to the random method. (See Math.Random(seed) for more info).

If you don''t specify a different seed each time then the same random number will be the result.

Try using the current date/time as the seed for the random method (it''s always changing)


Frinavle-

感谢您的时间,但我认为在提出问题时我可能会留下一些不足之处。我真正想知道的是如何让程序停止覆盖已经有价值的网格上的块。

你给我的信息非常有趣,我已经阅读过了。我毫不怀疑,在稍后的某个时刻它将证明是非常宝贵的。我将非常感谢有关本回复中所述问题的任何帮助 - 即如何让程序停止覆盖数组中已有值的元素?
Frinavle-
Thank you for your time , however I think that I may have left something to be desired in the phrasing of my question. What I actually wanted to know was how to get the program to stop overwriting blocks on the grid that already have a value.
The information that you gave me was extremely interesting and I have read up on it. I have no doubt that at some later juncture it will prove invaluable. I will be extremely grateful for any help regarding the problem stated in this reply-i.e.How do I get the program to stop overwriting elements in the array that already have a value?


使用无效数字(如-1或0或其他东西)初始化数组(因为您的用户使用的是1和2)。


现在,只写1或2数组,如果该特定元素中没有1或2(只要它小于1)。如果在所选元素的数组中已经有1或2,请告诉用户他们必须选择一个不同的位置(或者如果它是计算机的轮流,让它生成一个新的随机选择,直到它成功了)


-Frinny
Initialize your array with an invalid number like -1 or 0 or something (since your users are using 1 and 2).

Now, only write a 1 or 2 in the array if that particular element doesn''t have a 1 or 2 already in it (as long as it''s less than 1). If there is a 1 or 2 already in the array at the element selected, tell the user that they have to choose a different spot (or if it''s the computer''s turn, have it generate a new random selection until it was successful)

-Frinny


这篇关于帮助需要基于WRT文本的TicTacToe游戏改编。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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