-2

在这段代码中,我想创建一个 3x3 棋盘游戏,但屏幕上什么也没有出现..(代码编译正确但不显示输出)我认为问题出在main方法上......无法弄清楚.. 。 请帮忙!

package games;

import games.board.*;

public class BoardGameTester {

/**
 * @param args
 */
private static Board gb;
public static void main(String[] args) {
    // TODO Auto-generated method stub
    gb = new Board(3, 3);
}

 }

这是一个board.java:

package games.board;
   public class Board  {
private Cell[][] cells;
public Board(int rows, int columns) {
cells = new Cell[rows][columns];
for( int r = 0; r < cells[0].length; r++ ) {
for (int c = 0; c < cells[1].length; c++) {
cells[r][c] = new Cell(r,c);
}
}
}
public void setCell(Mark mark, int row, int column) throws
IllegalArgumentException {
if (cells[row][column].getContent() == Mark.EMPTY)
cells[row][column].setContent(mark);
else throw new IllegalArgumentException("Player already there!");
}
public Cell getCell(int row, int column) {
return cells[row][column];
}
public String toString() {
StringBuilder str = new StringBuilder();
for( int r = 0; r < cells.length; r++ ) {
str.append("|"); 


for (int c = 0; c < cells[r].length; c++) {
switch(cells[r][c].getContent()) {
case NOUGHT:
    str.append("O");
    break;
    case CROSS:
    str.append("X");
    break;
    case YELLOW:
    str.append("Y");
    break;
    case RED:
    str.append("R");
    break;
    case BLUE:
    str.append("B");
    break;
    case GREEN:
    str.append("G");
    break;
    case MAGENTA:
    str.append("M");
    break;
    case ORANGE:
    str.append("M");
    break;
    default: //Empty
    str.append("");
    }
    str.append("|");
    }
    str.append("\n");
    }
    return str.toString();

}
}

这是一个cell.java

package games.board;

public class Cell {
private Mark content;
private int row, column;
public Cell(int row, int column) {
this.row = row;
this.column = column;
content = Mark.EMPTY;
}
public Mark getContent() { return content; }
public void setContent(Mark content) { this.content = content; }
public int getRow() { return row; }
public int getColumn() { return column; }
}

这是mark.java

package games.board;

public enum Mark {

EMPTY, NOUGHT, CROSS, YELLOW, RED, BLUE, GREEN, MAGENTA, ORANGE
}

这是结果.java

package games.board;

public enum Outcome {
PLAYER1_WIN, PLAYER2_WIN, CONTINUE, TIE
}

这是 player.java

package games.board;

public enum Player {
FIRST,SECOND
}
4

2 回答 2

0

尝试这个 :

public static void main(String[] args) {
    // TODO Auto-generated method stub
    gb = new Board(3, 3);
    System.out.println(gb.toString());
}
于 2013-08-13T07:07:25.090 回答
0

您没有生成任何输出以将板打印到控制台尝试:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    gb = new Board(3, 3);
    System.out.println(gb);
}

它将调用gb.toString()方法

str.append("\n")ps:如果你使用而不是,它可能更容易阅读str.append("|")

于 2013-08-13T07:14:08.080 回答