我是Java的初学者,我们有一个关于Bingo的程序。你必须让用户输入玩家数量,每个玩家都会收到一个棋盘。然后从另一个类调用随机令牌,您必须使用该令牌在板上标记它。我不知道如何弄清楚谁赢了......而Mark()上有错误。有人可以帮忙吗?感谢。
import java.util.Scanner;
import java.util.ArrayList;
public class Bingo
{
public static void main(String[] args)
{
Scanner a = new Scanner(System.in);
System.out.println("Enter number of players: ");
int num = a.nextInt();
ArrayList<Board> player = new ArrayList<Board>();
for(int i = 0; i < num; i++)
player.add(new Board());
Token one = new Token();
int tempNumber = 0, tempLetter = 0;
boolean win = false;
while(win = false)
{
tempLetter = one.getLetter();
tempNumber = one.getNumber();
char letter;
if(tempLetter == 0)
letter = 'B';
else if(tempLetter == 1)
letter = 'I';
else if(tempLetter == 2)
letter = 'N';
else if(tempLetter == 3)
letter = 'G';
else
letter = 'O';
System.out.println("Token: " + tempNumber + letter);
System.out.println();
win = player.Mark(tempNumber,tempLetter);
}
}
}
以下是Board类:
public class Board
{
private int[][] card;
private int row, column;
boolean bingo;
public Board()
{
card = new int[5][5];
for(int i = 0; i < card.length; i++)
for(int j = 0; j < card[i].length; j++)
card[i][j] = (int) (Math.random() * 75 + 1);
card[2][2] = 0;
for(int i = 0; i < card.length; i++)
for(int j = 0; j < card[i].length; j++)
{
System.out.print(card[i][j] + " ");
if(j == 4)
System.out.println();
}
bingo = false; //False
row = 0;
column = 0;
}
public boolean Mark(int x, int y)
{
for(int i = 0; i < card.length; i++)
for(int j = 0; j < card[i].length; j++)
{
if(card[i][j] == x)
if(j == y)
{
card[i][j] = 0;
row = i;
column = j;
}
}
System.out.println("Mark: ");
for(int i = 0; i < card.length; i++)
for(int j = 0; j < card[i].length; j++)
{
System.out.print(card[i][j] + " ");
if(j == 4)
System.out.println();
}
Check();
return bingo;
}
private void Check()
{
int flagRow = 0, flagColumn = 0, flagDiagonal = 0;
int tempRow = 0, tempColumn = 0, tempRow1 = 4, tempColumn1 = 4;
//Diagonal check
if(row == column)
{
while(bingo == false)
{
if(card[tempRow][tempColumn] == 0)
{
tempRow++;
tempColumn++;
flagDiagonal++;
}
else if(card[tempRow1][tempColumn1] == 0)
{
tempRow1--;
tempColumn1--;
flagDiagonal++;
}
if(flagDiagonal == 5)
bingo = true;
else
{
tempRow = 0;
tempColumn = 0;
tempRow1 = 4;
tempColumn1 = 4;
flagDiagonal = 0;
}
}
}
//Row and column check
for(int i = 0; i < card.length; i++)
for(int j = 0; j < card[i].length; j++)
{
if(card[i][column] == 0) //Column check
flagColumn++;
if(card[row][j] == 0) //Row check
flagRow++;
}
if(flagRow == 5)
bingo = true;
else if(flagColumn == 5)
bingo = true;
else
{
flagRow = 0;
flagColumn = 0;
}
System.out.println("\nCheck: ");
for(int i = 0; i < card.length; i++)
for(int j = 0; j < card[i].length; j++)
{
System.out.print(card[i][j] + " ");
if(j == 4)
System.out.println();
}
System.out.println("Bingo status: " + bingo);
}
}
这是Token类:
import java.util.ArrayList;
import java.util.Random;
public class Token
{
ArrayList<Integer> number;
public Token()
{
number = new ArrayList<Integer>();
for(int i = 0; i < 75; i++)
number.add((int)(Math.random() * 75 + 1));
}
public int getNumber()
{
Random rand = new Random();
int temp = number.get(rand.nextInt(number.size()));
number.remove(temp);
return temp;
}
public int getLetter()
{
Random rand = new Random();
int temp = number.get(rand.nextInt(number.size()));
if(temp >= 1 && temp <= 15)
return 0; //B
else if(temp >= 16 && temp <= 30)
return 1; //I
else if(temp >= 31 && temp <= 45)
return 2; //N
else if(temp >=46 && temp <= 60)
return 3; //G
return 4; //O
}
}
谢谢大家!我知道......这是一个很长的计划。
答案 0 :(得分:1)
您正在调用player.Mark(tempNumber,tempLetter);
,其中player
是ArrayList<Board>
,但没有该方法。它在类Board
本身中定义。
因此,您希望get
此列表中的当前Board
个实例,并在 上致电Mark
。
即。 win = player.get(currentPlayerIndex).Mark(tempNumber,tempLetter);
根据您的逻辑,您需要一些获得当前播放器/电路板的currentPlayerIndex
。
注意:如@opensam所述,您需要将while(win = false)
替换为while(win == false)
,否则您将false
分配给win
并且在你可以进行一次迭代之前,循环就会中断。
答案 1 :(得分:0)