我是Java的新手,也是我的第二个自我项目。
我正在尝试写一个宾果游戏。我知道有一些关于此的文章,但是我认为我的方法与其他方法有些不同,如果它看起来多余,请对不起。
我的宾果游戏是5x5矩阵,带有25个按钮,为了方便起见,我从1〜25标记了它们。我还制作了一个名为grid variable的伪游戏板,它是一个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();
}
}
所以问题是,如果我在任何方向进行一个宾果游戏,那么我不确定如何计算下一个宾果游戏。由于在任何方向上的宾果游戏都会将我的布尔宾果游戏设置为true,并且只要有宾果游戏,它将保持为true ...任何额外的宾果都不会被计算在内。我该如何解决呢?
我知道代码真的很乱而且编写得不好。任何帮助表示赞赏。
最佳答案
有几个问题,您在isBingo的循环中覆盖了布尔宾果变量。因此,几乎没有宾果游戏真正返回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();
}
}