Java 中的宾果纸牌游戏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18387760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bingo Card Game in Java
提问by theGreenCabbage
I created two methods for my Bingo Game in Java. One method creates a new board which populates the Bingo Board with integers according to the Bingo rule (1-75). My second method generates random numbers with a range of 1 - 75.
我用 Java 为我的宾果游戏创建了两种方法。一种方法创建一个新的棋盘,它根据宾果规则 (1-75) 用整数填充宾果棋盘。我的第二种方法生成范围为 1 - 75 的随机数。
public static int drawNum(){
Random rand = new Random();
int num = rand.nextInt(75)+1;
return num;
}
public static void bingoCard(){
int [][]card=new int [5][5];
ArrayList<Integer> alreadyUsed = new ArrayList<Integer>();
boolean valid = false;
int tmp = 0;
for(int i = 0; i <= 4; i++){
for(int row = 0; row < card.length; row++){
while(!valid){
tmp = (int)(Math.random() * 15) + 1 + 15 * i;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][i] = tmp;
valid = false;
}
}
card[2][2] = 0;
//create array to make title.
String title []={"B","I","N","G","O"};
for(int i=0;i<title.length;i++){
System.out.print(title[i]+ "\t");
}
System.out.println();
for(int row=0;row<card.length;row++){
for(int col=0;col<card[row].length;col++){
System.out.print(card[row][col]+ "\t");
}
System.out.println();
}
}
What I need help with is, how do I check whether or not the drawNum() method corresponds to any values stored inside my bingoCard() array? If so, print out a new array with the integers filled in. If the condition is met for a bingo, then you win.
我需要帮助的是,如何检查 drawNum() 方法是否对应于存储在我的 bingoCard() 数组中的任何值?如果是这样,打印出一个填充了整数的新数组。如果满足宾果游戏的条件,那么你赢了。
I hope I don't make it sound like I want you to do it for me, but I am confused as to how to start coding that part. Thank you.
我希望我不要让它听起来像是我希望你为我做这件事,但我对如何开始编码那部分感到困惑。谢谢你。
采纳答案by Logan Murphy
This my recommendation - Learn Object Oriented Programming immediately
这是我的建议 - 立即学习面向对象编程
I see you are using objects provided in the JDK, so why not learn to make your own?
我看到您正在使用 JDK 中提供的对象,那么为什么不学习创建自己的对象呢?
Make two classes with the following methods (-) and members (+) (PS. This is not a formal way to document code)
使用以下方法 (-) 和成员 (+) 创建两个类(PS。这不是记录代码的正式方式)
BingoCard
+list of numbers on card
-reset() : gets new numbers for this card
-test(BingoDrawer) : Tests to see if this card won on this drawing
-toString() : returns a String representation of this card
BingoDrawer
+list of numbers drawn
-reset() : draws new numbers
-hasNumber(int number) : tests if this number was drawn
-toString() : returns a String representation of this drawing
One more suggestions
还有一个建议
- Instead of keeping track of what you used, keep track of what you have not used, it will make things much easier because you can just choose stuff from that list randomly. Unlike your current action which is choosing (a logical number) from thin air and hoping (which causes issues) it is not a collision
- 与其跟踪你用过的东西,不如跟踪你没有用过的东西,它会让事情变得更容易,因为你可以从列表中随机选择东西。与您当前的行动不同,它是从稀薄的空气中选择(一个逻辑数字)并希望(这会导致问题)它不是碰撞
If you follow my recommendation you can write code like this
如果你按照我的建议你可以写这样的代码
public static void main(String[] args) {
BingoCard bc = new BingoCard();
BingoDrawer bd = new BingoDrawer();
while(thePlayerWantsToPlay()) { //function to be defined by you
bc.reset();
bd.reset();
System.out.println(bc);
System.out.println(bd);
System.out.println(bc.test(bd));
}
}
You can take it a step further and make a BingoGame
class and do what I did in main
there and just create an instance of BingoGame
and call some start
method on the object.
您可以更进一步,创建一个BingoGame
类并执行我在main
那里所做的操作,然后创建一个实例BingoGame
并start
在该对象上调用某些方法。
回答by Shade
For checking if you have the number in your board, read through the board in a similar manner as you do for the already_used numbers, except with the number the user just entered.
要检查您的看板中是否有号码,请以与处理已经使用过的号码类似的方式通读看板,除了用户刚刚输入的号码。
The conditions for the user to win should be checked after the board has another number guessed.
There are a few ways to do this, a simple one would be to iterate over every possible pattern that could win, checking to see if there are tokens there.
在棋盘猜出另一个数字后,应检查用户获胜的条件。
有几种方法可以做到这一点,一个简单的方法是迭代每个可能获胜的模式,检查那里是否有令牌。
All of this would be in a loop, that goes a little like this:
所有这些都将处于循环中,有点像这样:
Set up board via user entering numbers.
Start loop
set either a timer to wait for, or wait for a keypress (so the game doesn't just play really fast)
Get random number
Possibly add to board
Check if winner
if winner, break the loop and do something else.
Print the new board out.
(end of loop)
If they got here, that could mean they won!
Wait to exit
回答by sdasdadas
You can just write it out as pseudo-code and fill in the methods after that. It usually helps to work on these things in a top-down fashion. So, for bingo you might have:
你可以把它写成伪代码,然后填写方法。以自上而下的方式处理这些事情通常会有所帮助。因此,对于宾果游戏,您可能有:
board = generateBoard();
while (!bingoFound(board)) {
number = drawNumber();
board = stampNumbers(board, number);
}
If that makes sense, you can go a step deeper and define each method. For example, bingoFound
might look like:
如果这是有道理的,您可以更深入地定义每种方法。例如,bingoFound
可能看起来像:
public boolean bingoFound(int[][] board) {
boolean wasFound = bingoRowFound(board)
|| bingoColFound(board)
|| bingoDiagonalFound(board);
return wasFound;
}
Again, I've defined everything in (mostly) pseudo-code. If this looks ok, you can move a step deeper. Let's define the bingoRowFound
method.
同样,我已经用(大部分)伪代码定义了所有内容。如果这看起来没问题,您可以更深入一步。让我们定义bingoRowFound
方法。
public boolean bingoRowFound(int[][] board) {
for (int row = 0; row < NUM_ROWS; row++) {
boolean rowIsABingo = true;
for (int col = 0; col < NUM_COLS; col++) {
// We have to check that everything up until this point has
// been marked off. I am using -1 to indicate that a spot has
// been marked.
rowIsABingo = rowIsABingo && board[row][col] == -1;
}
if (rowIsABingo) { return rowIsABingo; }
}
return false; // If we didn't find a bingo, return false.
}
Some of the methods (like drawNumber
) will be reallyeasy to implement. Others, like looking for a diagonal bingo might be a bit more difficult.
某些方法(如drawNumber
)将非常容易实现。其他人,例如寻找对角宾果游戏可能会更困难一些。
回答by theGreenCabbage
Feb 12 2014 Update:
2014 年 2 月 12 日更新:
Retracted code, since this was a college course assignment, and I want to prevent people just copying the code. I almost got in trouble for being accused of sharing code (which is a nono in assignments) when another student lifted my code from my Github repo and sent it in as their own.
撤回代码,因为这是大学课程作业,我想防止人们只是复制代码。当另一个学生从我的 Github 存储库中提取我的代码并将其作为他们自己的代码发送时,我几乎因为被指控共享代码(这是作业中的 nono)而惹上麻烦。
There were two classes, one main class and a class to hold my methods and constructors.
有两个类,一个主类和一个用于保存我的方法和构造函数的类。
BINGOFINAL.java
was my main
class.
BINGOFINAL.java
是我的main
班级。
Bingo_Card.java
held my constructor
and methods
.
Bingo_Card.java
举行我constructor
和methods
。
If you want to run this, make sure you create a new project called BINGOFINAL, and put Bingo_Card.java into that same */src/ extension.
如果要运行它,请确保创建一个名为 BINGOFINAL 的新项目,并将 Bingo_Card.java 放入相同的 */src/ 扩展名中。