匹配的游戏程序没有控制台输出?
Matching game program provides no output from console?
基本上,项目描述要求用户创建一个配对游戏,包括:
二维打法table(4x4),16对匹配牌,运行宁数面朝下的牌数。包含一种方法,用于从 table 的输入 x,y 位置检索特定卡片。
A gameBoard
,并循环播放直到所有牌都面朝上。
该循环包括一个界面,用户可以选择两张牌(输入 x,y table 位置),检查两张牌是否相等,
减少 faceDown
张牌的数量,并设置 faceUp boolean
张牌。
本质上,程序应该 运行 直到所有的牌都面朝上,并且游戏获胜。我将我的程序分成下面四个单独的 classes。
1.
public class Card {
private final int cardValue;
private boolean faceUp;
public Card(int value) {
cardValue = value;
faceUp = false;
}
public int getValue() {
return cardValue;
}
public boolean isFaceUp() {
return faceUp;
}
public void setFaceUp(boolean input) {
faceUp = input;
}
public static void printBoard(Card[][] cards) {
System.out.println("\t\t1\t2\t3\t4");
System.out.println("\t____________");
for(int i = 0; i < cards.length; i++) {
System.out.println((i + 1) + "\t|\t");
for(int j = 0; j < cards[0].length; j++)
if(cards[i][j].isFaceUp()) {
System.out.print(cards[i][j].getValue() + "\t"); }
else
System.out.println("*\t");
}
System.out.println();
}
}
2.
public class CreateBoard {
public static Card[][] createBoard() {
Card[][] board = new Card[4][4];
for(int i = 1; i <= 8; i++) {
for(int j = 1; j <= 2; j++) {
boolean boardLocation = false;
while(!boardLocation) {
int row = (int)(Math.random() * 4);
int column = (int)(Math.random() * 4);
if(board[row] == null && board[column] == null) {
boardLocation = true;
board[row][column] = new Card(i);
}
}
}
}
return board;
}
}
3.
public class Game {
public static boolean wonGame(Card[][] board) {
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(!board[i][j].isFaceUp())
return false;
}
}
return true;
}
}
最后,主要 class:
public class GameDriver {
public static void main(String[] args) {
Card[][] board = CreateBoard.createBoard();
Scanner keyboard = new Scanner(System.in);
System.out.println("Starting Game...");
while(!Game.wonGame(board)) {
Card.printBoard(board);
System.out.println("Enter X-Coordinate #1 (1-4): ");
int column1 = keyboard.nextInt();
System.out.println("Enter Y-Coordinate #1 (1-4): ");
int row1 = keyboard.nextInt();
System.out.println("Enter X-Coordinate #2 (1-4): ");
int column2 = keyboard.nextInt();
System.out.println("Enter Y-Coordinate #2 (1-4): ");
int row2 = keyboard.nextInt();
Card card1 = board[row1][column1];
Card card2 = board[row2][column2];
if(card1.getValue() == card2.getValue() && !(row1 == row2 && column1 == column2))
{
card1.setFaceUp(true);
card2.setFaceUp(true);
}
else if(row1 == row2 && column1 == column2)
{
System.out.println("Points selected are the same, try again");
}
else
{
System.out.println(card1.getValue() + " and " + card2.getValue() + " do not match");
}
}
Card.printBoard(board);
}
}
代码似乎 运行 没问题,没有任何错误。然而,经过多次测试,最明显的问题是它没有向控制台输出任何东西……我是不是漏掉了什么?帮助将不胜感激!
创建板应该是
public class CreateBoard {
public static Card[][] createBoard() {
Card[][] board = new Card[4][4];
for(int i = 1; i <= 8; i++) {
for(int j = 1; j <= 2; j++) {
boolean boardLocation = false;
while(!boardLocation) {
int row = (int)(Math.random() * 4);
int column = (int)(Math.random() * 4);
if(board[row][column] == null) {
boardLocation = true;
board[row][column] = new Card(i);
}
}
}
}
return board;
}
}
而对于游戏 class,由于 java 数组索引从 0 开始,因此
这些行应该是
Card card1 = board[row1-1][column1-1];
Card card2 = board[row2-1][column2-1];
基本上,项目描述要求用户创建一个配对游戏,包括:
二维打法table(4x4),16对匹配牌,运行宁数面朝下的牌数。包含一种方法,用于从 table 的输入 x,y 位置检索特定卡片。
A
gameBoard
,并循环播放直到所有牌都面朝上。
该循环包括一个界面,用户可以选择两张牌(输入 x,y table 位置),检查两张牌是否相等, 减少faceDown
张牌的数量,并设置faceUp boolean
张牌。
本质上,程序应该 运行 直到所有的牌都面朝上,并且游戏获胜。我将我的程序分成下面四个单独的 classes。
1.
public class Card {
private final int cardValue;
private boolean faceUp;
public Card(int value) {
cardValue = value;
faceUp = false;
}
public int getValue() {
return cardValue;
}
public boolean isFaceUp() {
return faceUp;
}
public void setFaceUp(boolean input) {
faceUp = input;
}
public static void printBoard(Card[][] cards) {
System.out.println("\t\t1\t2\t3\t4");
System.out.println("\t____________");
for(int i = 0; i < cards.length; i++) {
System.out.println((i + 1) + "\t|\t");
for(int j = 0; j < cards[0].length; j++)
if(cards[i][j].isFaceUp()) {
System.out.print(cards[i][j].getValue() + "\t"); }
else
System.out.println("*\t");
}
System.out.println();
}
}
2.
public class CreateBoard {
public static Card[][] createBoard() {
Card[][] board = new Card[4][4];
for(int i = 1; i <= 8; i++) {
for(int j = 1; j <= 2; j++) {
boolean boardLocation = false;
while(!boardLocation) {
int row = (int)(Math.random() * 4);
int column = (int)(Math.random() * 4);
if(board[row] == null && board[column] == null) {
boardLocation = true;
board[row][column] = new Card(i);
}
}
}
}
return board;
}
}
3.
public class Game {
public static boolean wonGame(Card[][] board) {
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(!board[i][j].isFaceUp())
return false;
}
}
return true;
}
}
最后,主要 class:
public class GameDriver {
public static void main(String[] args) {
Card[][] board = CreateBoard.createBoard();
Scanner keyboard = new Scanner(System.in);
System.out.println("Starting Game...");
while(!Game.wonGame(board)) {
Card.printBoard(board);
System.out.println("Enter X-Coordinate #1 (1-4): ");
int column1 = keyboard.nextInt();
System.out.println("Enter Y-Coordinate #1 (1-4): ");
int row1 = keyboard.nextInt();
System.out.println("Enter X-Coordinate #2 (1-4): ");
int column2 = keyboard.nextInt();
System.out.println("Enter Y-Coordinate #2 (1-4): ");
int row2 = keyboard.nextInt();
Card card1 = board[row1][column1];
Card card2 = board[row2][column2];
if(card1.getValue() == card2.getValue() && !(row1 == row2 && column1 == column2))
{
card1.setFaceUp(true);
card2.setFaceUp(true);
}
else if(row1 == row2 && column1 == column2)
{
System.out.println("Points selected are the same, try again");
}
else
{
System.out.println(card1.getValue() + " and " + card2.getValue() + " do not match");
}
}
Card.printBoard(board);
}
}
代码似乎 运行 没问题,没有任何错误。然而,经过多次测试,最明显的问题是它没有向控制台输出任何东西……我是不是漏掉了什么?帮助将不胜感激!
创建板应该是
public class CreateBoard {
public static Card[][] createBoard() {
Card[][] board = new Card[4][4];
for(int i = 1; i <= 8; i++) {
for(int j = 1; j <= 2; j++) {
boolean boardLocation = false;
while(!boardLocation) {
int row = (int)(Math.random() * 4);
int column = (int)(Math.random() * 4);
if(board[row][column] == null) {
boardLocation = true;
board[row][column] = new Card(i);
}
}
}
}
return board;
}
}
而对于游戏 class,由于 java 数组索引从 0 开始,因此 这些行应该是
Card card1 = board[row1-1][column1-1];
Card card2 = board[row2-1][column2-1];