Java棋盘边框?
我有点卡住了试图为我的棋盘做一个边框,从上到下 8-1,从左到右啊。我不太确定该怎么做。非常感谢建议和帮助!干杯:)
当前输出为:
BR BKn BB BK BQ BB BKn BR
BP BP BP BP BP BP BP BP
WP WP WP WP WP WP WP
WR WKn WB WK WQ WB WKn WR
下面是 Java 代码:
public class ex5 {
public enum Chessmen {
WHITE_KING,
WHITE_QUEEN,
WHITE_ROOK,
WHITE_BISHOP,
WHITE_KNIGHT,
WHITE_PAWN,
BLACK_KING,
BLACK_QUEEN,
BLACK_ROOK,
BLACK_BISHOP,
BLACK_KNIGHT,
BLACK_PAWN,
EMPTY
}
public static void printBoard (Chessmen [][] chessboard){
for (int i=0; i<chessboard.length;i++){
for (int j = 0; j<chessboard.length;j++){
switch (chessboard [i][j]){
case WHITE_KING:
System.out.print("WK\t");
break;
case WHITE_QUEEN:
System.out.print("WQ\t");
break;
case WHITE_ROOK:
System.out.print("WR\t");
break;
case WHITE_BISHOP:
System.out.print("WB\t");
break;
case WHITE_KNIGHT:
System.out.print("WKn\t");
break;
case WHITE_PAWN:
System.out.print("WP\t");
break;
case BLACK_KING:
System.out.print("BK\t");
break;
case BLACK_QUEEN:
System.out.print("BQ\t");
break;
case BLACK_ROOK:
System.out.print("BR\t");
break;
case BLACK_BISHOP:
System.out.print("BB\t");
break;
case BLACK_KNIGHT:
System.out.print("BKn\t");
break;
case BLACK_PAWN:
System.out.print("BP\t");
break;
default:
System.out.print(" " + "\t");
break;
}
}
System.out.println("");
}
}
public static void main(String[] args) {
int Rows = 8;
int Columns = 8;
Chessmen [][] chessboard = new Chessmen [Rows][Columns];
for (int i=0;i<chessboard.length; i++){
for (int j=0;j<chessboard[0].length;j++){
if (i==1){
chessboard [i][j]= Chessmen.BLACK_PAWN;
}else if (i==6){
chessboard [i][j]= Chessmen.WHITE_PAWN;
}else if ((i==0&&j==7)||(i==0&&j==0)){
chessboard [i][j]= Chessmen.BLACK_ROOK;
}else if ((i==0&&j==1)||(i==0&&j==6)){
chessboard [i][j] = Chessmen.BLACK_KNIGHT;
}else if ((i==0&&j==2)||(i==0&&j==5)){
chessboard [i][j] = Chessmen.BLACK_BISHOP;
}else if (i==0&&j==3){
chessboard [i][j] = Chessmen.BLACK_KING;
}else if (i==0&&j==4){
chessboard [i][j] = Chessmen.BLACK_QUEEN;
}else if ((i==7&&j==0)||(i==7&&j==7)){
chessboard [i][j]= Chessmen.WHITE_ROOK;
}else if ((i==7&&j==1)||(i==7&&j==6)){
chessboard [i][j] = Chessmen.WHITE_KNIGHT;
}else if ((i==7&&j==2)||(i==7&&j==5)){
chessboard [i][j] = Chessmen.WHITE_BISHOP;
}else if (i==7&&j==3){
chessboard [i][j] = Chessmen.WHITE_KING;
}else if (i==7&&j==4){
chessboard [i][j] = Chessmen.WHITE_QUEEN;
}else {
chessboard [i][j]= Chessmen.EMPTY;
}
}
}
printBoard (chessboard);
}
}
Im kinda stuck trying to make a border for my chessboard, running top to bottom 8-1 and left to right a-h. Im not quite sure how to do it. Advice and help is much appreciated! Cheers :)
The output is currently:
BR BKn BB BK BQ BB BKn BR
BP BP BP BP BP BP BP BP
WP WP WP WP WP WP WP WP
WR WKn WB WK WQ WB WKn WR
Below is the Java code:
public class ex5 {
public enum Chessmen {
WHITE_KING,
WHITE_QUEEN,
WHITE_ROOK,
WHITE_BISHOP,
WHITE_KNIGHT,
WHITE_PAWN,
BLACK_KING,
BLACK_QUEEN,
BLACK_ROOK,
BLACK_BISHOP,
BLACK_KNIGHT,
BLACK_PAWN,
EMPTY
}
public static void printBoard (Chessmen [][] chessboard){
for (int i=0; i<chessboard.length;i++){
for (int j = 0; j<chessboard.length;j++){
switch (chessboard [i][j]){
case WHITE_KING:
System.out.print("WK\t");
break;
case WHITE_QUEEN:
System.out.print("WQ\t");
break;
case WHITE_ROOK:
System.out.print("WR\t");
break;
case WHITE_BISHOP:
System.out.print("WB\t");
break;
case WHITE_KNIGHT:
System.out.print("WKn\t");
break;
case WHITE_PAWN:
System.out.print("WP\t");
break;
case BLACK_KING:
System.out.print("BK\t");
break;
case BLACK_QUEEN:
System.out.print("BQ\t");
break;
case BLACK_ROOK:
System.out.print("BR\t");
break;
case BLACK_BISHOP:
System.out.print("BB\t");
break;
case BLACK_KNIGHT:
System.out.print("BKn\t");
break;
case BLACK_PAWN:
System.out.print("BP\t");
break;
default:
System.out.print(" " + "\t");
break;
}
}
System.out.println("");
}
}
public static void main(String[] args) {
int Rows = 8;
int Columns = 8;
Chessmen [][] chessboard = new Chessmen [Rows][Columns];
for (int i=0;i<chessboard.length; i++){
for (int j=0;j<chessboard[0].length;j++){
if (i==1){
chessboard [i][j]= Chessmen.BLACK_PAWN;
}else if (i==6){
chessboard [i][j]= Chessmen.WHITE_PAWN;
}else if ((i==0&&j==7)||(i==0&&j==0)){
chessboard [i][j]= Chessmen.BLACK_ROOK;
}else if ((i==0&&j==1)||(i==0&&j==6)){
chessboard [i][j] = Chessmen.BLACK_KNIGHT;
}else if ((i==0&&j==2)||(i==0&&j==5)){
chessboard [i][j] = Chessmen.BLACK_BISHOP;
}else if (i==0&&j==3){
chessboard [i][j] = Chessmen.BLACK_KING;
}else if (i==0&&j==4){
chessboard [i][j] = Chessmen.BLACK_QUEEN;
}else if ((i==7&&j==0)||(i==7&&j==7)){
chessboard [i][j]= Chessmen.WHITE_ROOK;
}else if ((i==7&&j==1)||(i==7&&j==6)){
chessboard [i][j] = Chessmen.WHITE_KNIGHT;
}else if ((i==7&&j==2)||(i==7&&j==5)){
chessboard [i][j] = Chessmen.WHITE_BISHOP;
}else if (i==7&&j==3){
chessboard [i][j] = Chessmen.WHITE_KING;
}else if (i==7&&j==4){
chessboard [i][j] = Chessmen.WHITE_QUEEN;
}else {
chessboard [i][j]= Chessmen.EMPTY;
}
}
}
printBoard (chessboard);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
调用 System.out.println("+--------------------------------+") 外部渲染循环之前和之后。
在内部渲染循环之前和之后调用
System.out.print("|")
。你需要稍微调整一下。
call
System.out.println("+--------------------------------+")
before and after your outer rendering loop.call
System.out.print("|")
before and after the inner rendering loop.You'll need to tweak it a bit.
正如其他人所说,您需要在一行上显示空行和空方块。另外,您需要为每个方格打印相同数量的字符,因此我建议您使用 BN 表示黑夜,使用 BK 表示黑王。 N在代数国际象棋符号中用来表示马,以区别于国王。
由于我已经用 C++ 解决了这个问题,所以我将在下面发布我的代码。您可以使用它作为一种算法来将其翻译成 Java。它将显示这样的面板:
我认为它有一些有趣的功能。没有碎片的黑色正方形的中心有一个点。棋盘底部或顶部的箭头告诉用户下一步要移动哪一侧。一个“X”或者没有它告诉你车是否可以城堡。列下方的箭头“^”表示该列中的棋子可以被“en passant”拿走。
所以这里是代码(可能有错误,我刚刚翻译了法语中的标识符和注释,没有再次编译):
我希望这会有所帮助。
议员
As other have said, you need to display the empty lines and the empty squares on a line. Also, you need to print the same amount of characters for every squares, so I suggest you use BN for a black night and BK for a black king. N is used to represenet the knight in algebraic chess notation to differentiate it from the king.
Since I already solve this problem in C++, I'll post my code below. You could use it as an algorithm to translate it in Java. It will display a board like this :
It has, I think some interesings features. Black square without piece have a dot in their center. An arrow on the bottom on the board or on the top tells the user which side is to move next. An 'X' or the absence of it tells you if a rook can castle. An arrow '^' bellow a column indicate that the pawn in the said column can be taken "en passant".
So here is the code (There may be errors, I just translated the identifiers and comments from french whitout compiling it again) :
I hope this helps.
mp
[编辑]
为了回应你的评论......
我认为你想要的实际上是一个 10x10 网格,你需要调整你的逻辑,只将游戏区域打印为 10x10 内的 8x8 并使用外部正方形(不包括角)打印您的字母和数字。
[结束编辑]
你的默认值不是空的,它只是什么都没有。 ,您将看到所有空白
抓住开关中的 EMPTY 情况并打印选项卡,换句话说
,而不是您想要的:
[EDIT]
In response to your comment...
I think what you want is actually a 10x10 grid, and you'll need to adjust your logic to only print the game area as 8x8 inside the 10x10 and use the outer squares (not including the corners) to print your letters and numbers.
[ END EDIT]
your default isn't EMPTY it's just nothing. catch the EMPTY case in your switch and print your tab and you'll see all your empty spaces
in other words, instead of
you want to do:
我为 Java 中的 N 个皇后问题做了这个。因此,如果您尝试打印皇后之外的其他部件,请相应地定制您的代码。如果您不熟悉这个问题,那么挑战就是将 N 个皇后放在 N x N 棋盘上,使得没有两个皇后互相检查。我知道这是您发布问题多年后的事,但我想我应该将代码片段放在这里以供将来的人使用。
无论如何,第一步是创建 N x N 数组,表示棋盘上的空间以及棋盘上当前棋子的值。值得注意的是,由于这是 Java,我们的板是基于 0 的(因为每行中的空格编号为 0 到 7,而不是 1 到 8):
请参阅我们将每个点初始化为一个空格,以便我们可以迭代通过我对皇后位置的矢量,只改变需要放置皇后的位置。
以下是解决方案的示例向量:
[1 6 2 5 7 4 0 3]
。假设该向量由 int[] vector = new int[N] 表示。元素数量等于棋盘中的行数。它在向量中的索引对应于行,而值本身对应于皇后在该行中的列号。因此,我们只需循环这些并相应地更新棋盘数组:如果您的问题涉及实际的国际象棋游戏,您将需要迭代每个棋子并以类似的方式更新其棋盘位置。
在我解释如何制作这个板之前,让我向您展示与前面提到的向量相对应的板。顺便说一句,注意没有皇后检查!我用遗传算法解决了这个问题。
我们的下一步是在行之间(以及顶部和底部)创建边框。我将每一行视为格式化字符串。由于我们有空间行和边框行,因此行数组的大小为 2 * N + 1。
接下来,我们为棋盘空间本身创建字符串。由于我们的 board[][] 中已经有这些,我们只需要格式化。
剩下的就是打印该板。
希望这对某人有帮助!
I did this for an N queens problem in Java. So, if you're trying to print other pieces besides a queen, then tailor your code accordingly. If you are unfamiliar with this problem, it's the challenge of placing N queens on an N x N chessboard such that no two queens are checking each other. I know this is years after you posted the question, but I figured I'd put the code snippets on here for someone to use in the future.
Anyway, first step is to create our N x N array representing the spaces on a chessboard and the value of the pieces currently on the board. It's worth noting that since this is Java, our board is 0-based (as in the spaces in each row are numbered 0 to 7, not 1 to 8):
See we initialize every spot to a space, so that we can then iterate through the vector I have for the queens locations and only alter the spots where a Queen needs to be placed.
Here's an example vector of a solution:
[1 6 2 5 7 4 0 3]
. Let's assume this vector is represented byint[] vector = new int[N]
.The number of elements is equal to the number of rows in the board. It's index in the vector corresponds to the row, and the value itself corresponds to the column number where the queen is in that row. So we just loop through these and update the board array accordingly:If your problem involves an actual game of chess, you would need to iterate through every piece and update its board location in a similar manner.
Before I explain how to make this board, let me show you the board that corresponds to the vector mentioned previously. As an aside, notice how no queens are checking! I solved this problem using genetic algorithms.
Our next step is to create the borders in between rows (and on the top and bottom). I treat each row as a formatted string. Since we have space rows and border rows, our row array is 2 * N + 1 in size.
Next we create the strings for the board spaces themselves. Since we already have these in our board[][], we only need to format.
All that's left is to print that board.
Hope this helps somebody!