gpt4 book ai didi

java - 用java编写宾果游戏

转载 作者:行者123 更新时间:2023-12-01 10:28:33 29 4
gpt4 key购买 nike

我对 Java 和我的第二个 self 项目完全陌生。我正在尝试编写宾果游戏。我知道有一些关于此的帖子,但我认为我的方法与其他人的方法有点不同,如果它看起来多余,很抱歉。我的宾果游戏是 5x5 矩阵,有 25 个按钮,为了方便起见,我将它们标记为 1~25。我还制作了一个名为 grid 变量的伪游戏板,它是一个 5x5 2d 字符数组。它最初是用“O”填充的,但是当我按下其中一个按钮时,相应的索引就会变成“X”。我使用这个网格来检查宾果游戏,因为我认为这比检查实际按钮容易得多。下面是我的代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class exex extends JFrame{
final int size =5; //5x5 bingo board
int bingos =0; // how many bingos?
int count=0; // int value for each btn
JButton[][] btn = new JButton[size][size]; //actual display of btns (bingo board)
char[][] grid = new char[size][size]; // grid to check whether it's bingo or not
int presscount = 1; // how many times ive pressed
int row; // row for action event
int column; // column for action event
boolean bingo; //check if bingo
exex(){
setTitle("Bingo for everyone");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(new GridLayout(size,size));//basic board setup
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
btn[i][j] = new JButton(Integer.toString(count)); //setting buttons 1~25;
grid[i][j] ='O'; // make a grid board to check bingos
btn[i][j].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
isBingo(grid,'X'); // check if grid has any bingos, if it does, then set bingo to true.
System.out.print(bingo);
if(bingo){
bingos++;
}
System.out.print(bingos);
JButton btnR = (JButton)e.getSource();
System.out.println(presscount);
presscount++;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(btn[i][j] == btnR){
row = i;
column =j;
grid[row][column]='X'; //set pressed button to 'X' char on my grid(2D char array)
}
System.out.print(grid[i][j]);
}
System.out.println("");
}
System.out.println("-----------");


//Color change when pressed,
btnR.setBackground(Color.YELLOW);
btnR.setOpaque(true);


}
});
add(btn[i][j]);
count++;
}
System.out.println();
}
setSize(500,500);
setVisible(true);

}
public void isBingo(char[][] array,char g){
for(int i=0;i<array.length;i++){
for(int j=0;j<=i;j++){
if(array[0][j]==g&&array[1][j]==g&&array[2][j]==g&&array[3][j]==g&&array[4][j]==g){ //check for vertical bingos
bingo = true;
}
else if(array[i][0]==g&&array[i][1]==g&&array[i][2]==g&&array[i][3]==g&&array[i][4]==g){//check for horizontal bingos
bingo = true;
}
else if(array[0][0]==g&&array[1][1]==g&&array[2][2]==g&&array[3][3]==g&&array[4][4]==g){ //check for S.E diagonal bingos
bingo =true;
}
else if(array[0][4]==g&&array[1][3]==g&&array[2][2]==g&&array[3][1]==g&&array[4][0]==g){ //check for S.W diagonal bingos
bingo= true;
}
else // if no bingos, remains false
bingo = false;
}
}
}

public static void main(String[] args) {
new exex();


}

}

所以问题是,如果我在任何方向上进行一次宾果游戏,那么我不知道如何计算我进行的下一次宾果游戏。由于任何方向的宾果游戏都会将我的 boolean 宾果游戏设置为真,并且只要有宾果游戏它就会保持为真...任何额外的宾果游戏都不会被计算在内。 .我该如何解决这个问题?我知道代码确实很乱而且写得不好。任何帮助表示赞赏。

最佳答案

有几个问题,您正在 isBingo 的循环中覆盖 boolean bingo 变量。所以大多数宾果游戏都不会真正返回 true。我删除了一些类属性并将它们放入上下文中,需要它们来计算唯一的结果。我还设置了存储已经发生的宾果游戏的可能性......

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class exex extends JFrame {
private static final long serialVersionUID = 1172300329115978744L;

final int size = 5; // 5x5 bingo board
int bingos = 0; // how many bingos?
JButton[][] btn = new JButton[size][size]; // actual display of btns (bingo
// board)
char[][] grid = new char[size][size]; // grid to check whether it's bingo or
// not
int presscount = 1; // how many times ive pressed

boolean[] rowBingos = new boolean[size];
boolean[] columnBingos = new boolean[size];
boolean se, sw;

exex() {
setTitle("Bingo for everyone");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(new GridLayout(size, size));// basic board setup
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
btn[i][j] = new JButton(Integer.toString(i * 5 + j + 1)); // setting
// buttons
// 1~25;
grid[i][j] = 'O'; // make a grid board to check bingos
btn[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton btnR = (JButton) e.getSource();
System.out.println(presscount);
presscount++;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (btn[i][j] == btnR) {
int row = i;
int column = j;
grid[row][column] = 'X'; // set pressed
// button to 'X'
// char on my
// grid(2D char
// array)
}
System.out.print(grid[i][j]);
}
System.out.println("");
}
boolean bingo = isBingo('X'); // check if grid has
// any bingos, if it
// does, then set
// bingo to true.
System.out.print(bingo);
if (bingo) {
bingos++;
}
System.out.print(bingos);
System.out.println("-----------");

// Color change when pressed,
btnR.setBackground(Color.YELLOW);
btnR.setOpaque(true);

}
});
add(btn[i][j]);
}
System.out.println();
}
setSize(500, 500);
setVisible(true);

}

public boolean isBingo(char g) {
if (grid[0][0] == g && grid[1][1] == g
&& grid[2][2] == g && grid[3][3] == g
&& grid[4][4] == g && se != true) { // check for S.E
// diagonal bingos
se = true;
return true;
} else if (grid[0][4] == g && grid[1][3] == g
&& grid[2][2] == g && grid[3][1] == g
&& grid[4][0] == g && sw != true) { // check for S.W
// diagonal bingos
sw = true;
return true;
}

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j <= i; j++) {
if (grid[0][j] == g && grid[1][j] == g && grid[2][j] == g
&& grid[3][j] == g && grid[4][j] == g
&& columnBingos[j] != true) { // check for
// vertical
// bingos
columnBingos[j] = true;
return true;
} else if (grid[i][0] == g && grid[i][1] == g
&& grid[i][2] == g && grid[i][3] == g
&& grid[i][4] == g && rowBingos[i] != true) {// check
// for
// horizontal
// bingos
rowBingos[i] = true;
return true;
}
}
}
// if no bingos, remains false
return false;
}

public static void main(String[] args) {
new exex();

}
}

关于java - 用java编写宾果游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35220053/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com