我有这个数组,使用随机字符串比较一个7x7矩阵,当我尝试获取数组的值,当我使用pritnln时,值以行格式打印矩阵是创建我需要存储println的值变量或重新排列数组,以便值以矩阵格式。
Random rand = new Random();
String[][] debug_board_state = new String[7][7];
setCurrentState(new State(WIDTH_EASY));
for (int row = 0; row < debug_board_state.length; row++) {
for (int column = 0; column < debug_board_state[row].length; column++) {
debug_board_state[row][column] = String.valueOf(rand.nextInt(5));
}
}
for (int row = 0; row < debug_board_state.length; row++) {
for (int column = 0; column < debug_board_state[row].length; column++) {
System.out.print(debug_board_state[row][column] + " ");
}
System.out.println();
}
for (int row = 0; row < WIDTH_EASY; ++row) {
for (int column = 0; column < WIDTH_EASY; ++column) {
getCurrentState().board_elements[row][column] = new BoardElement();
getCurrentState().board_elements[row][column].max_connecting_bridges = Integer.parseInt(debug_board_state[row][column]);
getCurrentState().board_elements[row][column].row = row;
getCurrentState().board_elements[row][column].col = column;
if (getCurrentState().board_elements[row][column].max_connecting_bridges > 0) {
getCurrentState().board_elements[row][column].is_island = true;
}
++column;
}
}
}
** 编辑我添加了pritnln,它打印出矩阵但仍然得到相同的错误 **
日志:
04-11 16:26:03.836 26217-26217/Island_and_Bridges.Hashi I/System.out: 0 3 3 1 2 1 1
04-11 16:26:03.836 26217-26217/Island_and_Bridges.Hashi I/System.out: 3 3 2 4 2 3 3
04-11 16:26:03.836 26217-26217/Island_and_Bridges.Hashi I/System.out: 0 2 1 3 4 1 2
04-11 16:26:03.836 26217-26217/Island_and_Bridges.Hashi I/System.out: 0 1 0 0 4 1 3
04-11 16:26:03.836 26217-26217/Island_and_Bridges.Hashi I/System.out: 1 0 2 4 3 3 2
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/System.out: 3 0 4 3 0 2 3
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/System.out: 0 2 1 4 2 1 0
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 0 0
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 0 2
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 0 4
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 0 6
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 1 0
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 1 2
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 1 4
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 1 6
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 2 0
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 2 2
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 2 4
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 2 6
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 3 0
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 3 2
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 3 4
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 3 6
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 4 0
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 4 2
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 4 4
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 4 6
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 5 0
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 5 2
04-11 16:26:03.837 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 5 4
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 5 6
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 6 0
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 6 2
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 6 4
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 6 6
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi D/Island_and_Bridges.Hashi.BoardState: Setting board_elements to old_elements
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 0 0
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 0 2
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 0 4
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 0 6
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 1 0
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 1 2
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 1 4
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 1 6
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 2 0
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 2 2
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 2 4
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 2 6
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 3 0
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 3 2
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 3 4
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 3 6
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 4 0
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 4 2
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 4 4
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 4 6
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 5 0
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 5 2
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 5 4
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 5 6
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 6 0
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 6 2
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 6 4
04-11 16:26:03.838 26217-26217/Island_and_Bridges.Hashi I/BoardElemnet: Cloning 6 6
Board Element Class:
import android.util.Log;
// This class holds information about a grid point.
// Every grid point does have
// -- a coordinate = (row , col).
// -- a state "is_island" whether it is an island or empty.
// If the grid point is an island then it furthermore contains
// -- number of possible other islands it connects to.
// -- possible connections to its neighbours N,S,E and W.
public class BoardElement {
// *********************************************************
// *********************************************************
// General members
// *********************************************************
// *********************************************************
public int row = 0;
public int col = 0;
// Is this an island?
public boolean is_island = false;
// *********************************************************
// *********************************************************
// Island specific members.
// *********************************************************
// *********************************************************
public int max_connecting_bridges = 0;
public int column = 0;
// It is easier to refer to neighbours via directions.
public enum Direction {
EAST, SOUTH, WEST, NORTH;
}
// Pairs of a BoardElement and the number of connecting bridges
public Connection connecting_north = null;
public Connection connecting_south = null;
public Connection connecting_east = null;
public Connection connecting_west = null;
public BoardElement clone() {
BoardElement elt = new BoardElement();
elt.row = row;
elt.col = col;
Log.i("BoardElemnet", "Cloning" + elt.row + " " + elt.col);
elt.max_connecting_bridges = max_connecting_bridges;
elt.is_island = is_island;
if (connecting_east != null)
elt.connecting_east = new Connection(1,1);
else
elt.connecting_east = null;
if (connecting_north!= null)
elt.connecting_north = new Connection(2,2);
else
elt.connecting_north = null;
if (connecting_south!= null)
elt.connecting_south = new Connection(3,3);
else
elt.connecting_south = null;
if (connecting_west != null)
elt.connecting_west = new Connection(3,3);
else
elt.connecting_west = null;
return elt;
}
private int GetConnectionCount(Connection connection){
if (connection == null) {
return 0;
} else {
return 1;
}
}
// Return the current count of connections connected
// to this island.
public int GetCurrentCount() {
if (!is_island) {
return 0;
}
int s = GetConnectionCount(connecting_east);
s += GetConnectionCount(connecting_south);
s += GetConnectionCount(connecting_north);
s += GetConnectionCount(connecting_west);
return s;
}
void AddConnection(Direction dir, BoardElement dest, int value) {
Connection connection = null;
switch (dir) {
case EAST:
connecting_east = new Connection(1, 1);
connection = connecting_east;
break;
case WEST:
connecting_west = new Connection(2, 2);
connection = connecting_west;
break;
case SOUTH:
connecting_south = new Connection(3, value);
connection = connecting_south;
break;
case NORTH:
connecting_north = new Connection(4, 4);
connection = connecting_north;
break;
}
}
}; // BoardElement
我的应用程序崩溃,我收到此错误
junit.framework.AssertionFailedError: Island_and_Bridges.Hashi.BoardView 0 1: null element
at junit.framework.Assert.fail(Assert.java:50)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertNotNull(Assert.java:218)
at Island_and_Bridges.Hashi.BoardView.onDraw(BoardView.java:109)
BoardView类(错误类)
package Island_and_Bridges.Hashi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.os.Vibrator;
import static junit.framework.Assert.*;
public class BoardView extends View {
private BoardState board_state;
final private Paint background = new Paint();
final private Paint cell_lines = new Paint();
final private Paint text_paint = new Paint();
final private Paint bridge_paint = new Paint();
private PixelPosition[][] positions;
private boolean drawing_bridge = false;
private boolean live_bridge_legal = false;
private PixelPosition start_candidate = null;
private PixelPosition end_candidate = null;
private float end_point_x = 0;
private float end_point_y = 0;
private final Bitmap default_icon = BitmapFactory.decodeResource(this.getResources(),R.drawable.yellowicon);
private final Bitmap selected_icon = BitmapFactory.decodeResource(this.getResources(),R.drawable.greenicon);
private final Bitmap done_icon = BitmapFactory.decodeResource(this.getResources(),R.drawable.greenicon);
class PixelPosition {
public float x;
public float y;
public PixelPosition(float x, float y) {
this.x = x;
this.y = y;
}
public BoardElement element_reference = null;
public boolean is_selected = false;
}
public BoardView(Context context, BoardState state) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
board_state = state;
cell_lines.setColor(0xFFc0c0c0);
text_paint.setColor(0xff00003f);
text_paint.setTextSize(20);
Paint text_paint_done = new Paint();
text_paint_done.setColor(0xffff003f);
text_paint_done.setTextSize(20);
bridge_paint.setStrokeWidth(5);
bridge_paint.setColor(0xff000000);
Reset();
}
public void Reset() {
int board_width = board_state.getCurrentState().board_width;
positions = new PixelPosition[board_width][board_width];
for (int row = 0; row < board_width; ++row) {
for (int col= 0; col < board_width; ++col) {
positions[row][col] = new PixelPosition(0, 0);
GetCellCenter(row, col,positions[row][col]);
positions[row][col].element_reference =
board_state.getCurrentState().board_elements[row][col];
}
}
}
@Override
protected void onSizeChanged(int newx, int newy, int oldx, int oldy) {
Reset();
}
@Override
protected void onDraw(Canvas canvas) {
final int board_width = board_state.getCurrentState().board_width;
// Draw the background...
background.setColor(getResources().getColor(
R.color.background));
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
// We draw a grid of the size of the board.
for (int i = 0; i < board_width; ++i) {
PixelPosition p0 = positions[i][0];
PixelPosition p1 = positions[i][board_width - 1];
canvas.drawLine(p0.x, p0.y, p1.x,p1.y, cell_lines);
p0 = positions[0][i];
p1 = positions[board_width - 1][i];
canvas.drawLine(p0.x, p0.y, p1.x,p1.y, cell_lines);
}
// Draw accepted bridges.
// From top to bottom and left to right.
// We exploit symmetry.
for (int row = 0; row < board_width; ++row) {
for (int col= 0; col < board_width; ++col) {
final String logmarker = getClass().getName() + " " + row + " " + col;
BoardElement elt = null;
elt = board_state.getCurrentState().board_elements[row][col];
assertNotNull(logmarker + ": null element", elt);
if (elt.is_island) {
if (elt.connecting_east != null) {
assertNotNull(logmarker + ": connecting east destination is null", elt.connecting_east.destination);
PaintBridge(canvas, elt, elt.connecting_east.destination,elt.connecting_east.second);
}
if (elt.connecting_south!= null) {
assertNotNull(logmarker + ": connecting south destination is null", elt.connecting_south.destination);
PaintBridge(canvas, elt, elt.connecting_south.destination,elt.connecting_south.second);
}
}
}
}
// We draw live bridge lines if needed.
if (drawing_bridge) {
canvas.drawLine(start_candidate.x, start_candidate.y,
end_point_x, end_point_y, bridge_paint);
}
// On top of this we draw possibly two sets of icons.
// The not selected numbers and the selected ones.
for (int row = 0; row < board_width; ++row) {
for (int col= 0; col < board_width; ++col) {
if (board_state.getCurrentState().board_elements[row][col].is_island) {
PixelPosition p = positions[row][col];
Bitmap icon = null;
if (p.is_selected) {
icon = selected_icon;
} else {
icon = default_icon;
}
int max_connecting_bridges =
board_state.getCurrentState().board_elements[row][col].max_connecting_bridges;
int count =
board_state.getCurrentState().board_elements[row][col].GetCurrentCount();
PaintNumber(canvas, p, max_connecting_bridges, count == max_connecting_bridges, icon);
}
}
}
}
private Rect paint_source = new Rect();
private Rect paint_destination = new Rect();
private void PaintNumber(Canvas canvas, PixelPosition p, int number, boolean done, Bitmap icon) {
float x0 = p.x;
float y0 = p.y;
float x = p.x - text_paint.getTextSize() / 2 + 4;
float y = p.y + text_paint.getTextSize() / 2 - 2;
paint_source.left = 0;
paint_source.right = icon.getWidth();
paint_source.top = 0;
paint_source.bottom = icon.getHeight();
paint_destination.left = (int) (x0 - 12);
paint_destination.top = (int) (y0 - 12);
paint_destination.right= (int) (x0 + 12);
paint_destination.bottom= (int) (y0 + 12);
if (!done)
canvas.drawBitmap(icon, paint_source, paint_destination, text_paint);
else
canvas.drawBitmap(done_icon, paint_source, paint_destination, text_paint);
canvas.drawText(String.format("%d", number), x, y, text_paint);
}
private void PaintBridge(Canvas canvas, BoardElement start, BoardElement end, int bridges) {
float startX = positions[start.row][start.col].x;
float startY = positions[start.row][start.col].y;
float endX = positions[end.row][end.col].x;
float endY = positions[end.row][end.col].y;
float linewidth = bridge_paint.getStrokeWidth();
if (bridges == 1) {
canvas.drawLine(startX, startY, endX, endY, bridge_paint);
} else if (bridges == 2) {
if (startY == endY) {
canvas.drawLine(startX, startY - linewidth - 2, endX, endY - linewidth -2, bridge_paint);
canvas.drawLine(startX, startY + linewidth + 2, endX, endY + linewidth + 2, bridge_paint);
} else {
canvas.drawLine(startX - linewidth - 2, startY, endX - linewidth - 2, endY, bridge_paint);
canvas.drawLine(startX + linewidth + 2, startY, endX + linewidth +2, endY, bridge_paint);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ||
event.getAction() == MotionEvent.ACTION_MOVE ||
event.getAction() == MotionEvent.ACTION_UP) {
float x = event.getX();
float y = event.getY();
end_point_x = x;
end_point_y = y;
PixelPosition candidate = null;
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(getClass().getName(), "ACTION DOWN");
candidate = TrySelect(x, y);
if (candidate != null) {
drawing_bridge = true;
candidate.is_selected = true;
start_candidate = candidate;
Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50L);
return true;
}
start_candidate = null;
break;
case MotionEvent.ACTION_MOVE:
if (drawing_bridge) {
float dx = start_candidate.x - x;
float dy = start_candidate.y - y;
candidate = TrySelect(x,y);
if (candidate != null && candidate != start_candidate) {
// Allow only non-diagonal neighbors.
if (candidate.x == start_candidate.x ||
candidate.y == start_candidate.y) {
candidate.is_selected = true;
}
if (candidate != end_candidate && end_candidate != null) {
end_candidate.is_selected = false;
}
if (end_candidate != candidate) {
Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50L);
}
end_candidate = candidate;
} else {
if (end_candidate != null) {
end_candidate.is_selected = false;
}
end_candidate = null;
}
}
break;
case MotionEvent.ACTION_UP:
Log.d(getClass().getName(), "ACTION UP");
if (start_candidate == null)
return false;
drawing_bridge = false;
candidate = TrySelect(x, y);
boolean result = false;
if (candidate != null) {
BoardElement start_elt = start_candidate.element_reference;
BoardElement end_elt = candidate.element_reference;
result = board_state.TryAddNewBridge(start_elt, end_elt, 1);
}
// Cleanup
start_candidate.is_selected = false;
start_candidate = null;
if (end_candidate != null) {
end_candidate.is_selected = false;
end_candidate = null;
}
if (result) {
CheckEndCondition();
}
}
invalidate();
return true;
}
return super.onTouchEvent(event);
}
void CheckEndCondition() {
}
private PixelPosition TrySelect(float x, float y) {
for (PixelPosition[] position : positions) {
for (int j = 0; j < position.length; ++j) {
double sq_distance = Math.sqrt((position[j].x - x) * (position[j].x - x) +
(position[j].y - y) * (position[j].y - y));
float MAX_DISTANCE_FOR_SELECTION = 20;
if (sq_distance < MAX_DISTANCE_FOR_SELECTION &&
position[j].element_reference != null &&
position[j].element_reference.is_island) {
Log.d(getClass().getName(),
String.format("Choosing %d %d",
position[j].element_reference.row,
position[j].element_reference.col));
return position[j];
}
}
}
return null;
}
private void GetCellCenter(int board_row, int board_column, PixelPosition result) {
final int board_width = board_state.getCurrentState().board_width;
final int grid_start = 5;
final int cell_width = (getWidth() - 2 * grid_start) / board_width;
final int cell_height = (getHeight() - 2 * grid_start) / board_width;
float x = grid_start + board_column * cell_width + cell_width / 2;
float y = grid_start + board_row * cell_height + cell_height / 2;
result.x = x;
result.y = y;
}
}
将rror poitns编码为
for (int row = 0; row < board_width; ++row) {
for (int col= 0; col < board_width; ++col) {
final String logmarker = getClass().getName() + " " + row + " " + col;
BoardElement elt = null;
elt = board_state.getCurrentState().board_elements[row][col];
assertNotNull(logmarker + ": null element", elt);
if (elt.is_island) {
if (elt.connecting_east != null) {
assertNotNull(logmarker + ": connecting east destination is null", elt.connecting_east.destination);
PaintBridge(canvas, elt, elt.connecting_east.destination,elt.connecting_east.second);
}
if (elt.connecting_south!= null) {
assertNotNull(logmarker + ": connecting south destination is null", elt.connecting_south.destination);
PaintBridge(canvas, elt, elt.connecting_south.destination,elt.connecting_south.second);
}
}
}
}
答案 0 :(得分:0)
删除以下代码中的++column;
,因为您已在column
声明中增加了for
:
for (int row = 0; row < WIDTH_EASY; ++row) {
for (int column = 0; column < WIDTH_EASY; ++column) {
getCurrentState().board_elements[row][column] = new BoardElement();
getCurrentState().board_elements[row][column].max_connecting_bridges = Integer.parseInt(debug_board_state[row][column]);
getCurrentState().board_elements[row][column].row = row;
getCurrentState().board_elements[row][column].col = column;
if (getCurrentState().board_elements[row][column].max_connecting_bridges > 0) {
getCurrentState().board_elements[row][column].is_island = true;
}
++column; // remove this line
}
}