黑白棋,又叫翻转棋(Reversi)、苹果棋或奥赛罗棋(Othello)。棋盘共有8行8列共64格。开局时,棋盘正中央的4格先置放黑白相隔的4枚棋子。双方轮流落子,只要落子和棋盘上任一枚己方的棋子

黑白棋,又叫翻转棋(Reversi)、苹果棋或奥赛罗棋(Othello)。棋盘共有8行8列共64格。

开局时,棋盘正中央的4格先置放黑白相隔的4枚棋子。双方轮流落子,只要落子和棋盘上任一枚己方的棋子在一条线上(横、直、斜线皆可)夹着对方棋子,就能将对方的这些棋子转变为我己方(翻面即可)。如果在任一位置落子都不能夹住对手的任一颗棋子,就要让对手下子。当双方皆不能下子时,游戏就结束,子多的一方胜。

现在给你一个8x8的棋局,以及下一步玩家的落子位置。请输出翻转好的新棋局。

解析:

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;
			}
		}
	}
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_72431373

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值