我正在用Java开发一个简单的AI游戏:Dots and Boxes。现在,它将是2 * 2。状态由两个矩阵表示:水平矩阵和垂直矩阵。例如:州
-- --
| | |
--
| | |
-- --
由水平矩阵表示:
int[][] horz = new int[][] {{1,1}, {1,0}, {1,1} };
和垂直矩阵:
int[][] ver = new int[][] { {1,1,1}, {1,1,1} };
目前,我不打算使用GUI。我有这两个矩阵。所以,给它们作为输入,我想要做的只是使用下面的代码在终端中显示状态图(如上所示),但我没有得到正确的结果。
for(int horz_row=0; horz_row<horz.length; horz_row++) //print horz matrix row
{
//after one row break, print the first row of vertical column
System.out.print(horz[horz_row][horz_col]);
for(int ver_row=0;ver_row<ver.length; ver_row++) //print vertical matrix, row
{
System.out.print(ver[horz_row][horz_col]);
}
System.out.print(" ");
}
答案 0 :(得分:0)
int[][] hor = new int[][] {{1,1}, {1,0}, {1,1} };
int[][] ver = new int[][] { {1,1,1}, {1,1,1} };
String[] vstr = { " ", "|" };
String[] hstr = { " ", "--" };
for( int iRow = 0; iRow < ver.length + 1; ++iRow ){
StringBuilder sbh = new StringBuilder( " " );
String delh = "";
for( int iCol = 0; iCol < hor[iRow].length; ++iCol ){
sbh.append(delh).append( hstr[hor[iRow][iCol]] );
delh = " ";
}
System.out.println(sbh.toString());
if( iRow < ver.length ){
StringBuilder sbv = new StringBuilder();
String delv = "";
for( int iCol = 0; iCol < ver[iRow].length; ++iCol ){
sbv.append(delv).append( vstr[ver[iRow][iCol]] );
delv = " ";
}
System.out.println(sbv.toString());
}
}
答案 1 :(得分:-1)
这应该有效:
在此处试试:http://ideone.com/iv082V
public static void main (String[] args) throws java.lang.Exception
{
int[][] horz = new int[][] {{1,1}, {1,0}, {1,1} };
int[][] ver = new int[][] { {1,1,1}, {1,1,1} };
createMatrices(horz, ver);
}
public static void createMatrices(int[][] horz, int[][] ver)
{
for (int i = 0; i < horz.length-1; i++) {
for (int j = 0; j < horz[i].length; j++) {
System.out.print(horz[i][j]==1?" -- ":" ");
}
System.out.println();
for (int k = 0; k < ver[i].length; k++) {
System.out.print(ver[i][k]==1?"| ":" ");
}
System.out.println();
}
// print last row of hyphens
int i = horz.length - 1;
for (int j = 0; j < horz[i].length; j++) {
System.out.print(horz[i][j]==1?" -- ":" ");
}
System.out.println();
}