黑白棋,又叫翻转棋(Reversi)、苹果棋或奥赛罗棋(Othello)。棋盘共有8行8列共64格。
开局时,棋盘正中央的4格先置放黑白相隔的4枚棋子。双方轮流落子,只要落子和棋盘上任一枚己方的棋子在一条线上(横、直、斜线皆可)夹着对方棋子,就能将对方的这些棋子转变为我己方(翻面即可)。如果在任一位置落子都不能夹住对手的任一颗棋子,就要让对手下子。当双方皆不能下子时,游戏就结束,子多的一方胜。
现在给你一个8×8的棋局,以及下一步玩家的落子位置。请输出翻转好的新棋局。
解析:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
Character[][] map = new Character[8][8];
for (int i = 0; i < 8; i ++ ) {
String s = sc.next();
for (int j = 0; j < 8; j ++ )
map[i][j] = s.charAt(j);
}
int row = sc.nextInt() - 1;
int col = sc.nextInt() - 1;
Character c = sc.next().charAt(0);
map[row][col] = c;
fun(map, row, col, c);
for (int i = 0; i < map.length; i ++ ) {
for (int j = 0; j < map[0].length; j ++ ) {
if(j == map[0].length - 1) System.out.println(map[i][j]);
else System.out.print(map[i][j]);
}
}
System.out.println();
}
}
public static void fun(Character[][] map, int row, int col, Character c) {
int[][] direction = {
{0, 1}, {0, - 1}, {1, 0}, { - 1, 0}, {1, 1}, {1, - 1}, { - 1, 1}, { - 1, - 1}};
for (int i = 0; i < direction.length; i ++ ) {
int x = row + direction[i][0];
int y = col + direction[i][1];
while (x >= 0 && x < map.length && y >= 0 && y < map[0].length && map[x][y] != c && map[x][y] != '.') {
x += direction[i][0];
y += direction[i][1];
}
if(x >= 0 && x < map.length && y >= 0 && y < map[0].length && map[x][y] == c) {
while (map[x -= direction[i][0]][y -= direction[i][1]] != c)
map[x][y] = c;
}
}
}
}
今天的文章翻转棋_古代的黑白棋是什么棋分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/85972.html
