首页 文章

A和B和国际象棋

提问于
浏览
0

我正在处理Codeforces问题“A和B和国际象棋”,如下:

A. A和B以及每次测试的国际象棋时限每次测试1秒内存限制256兆字节输入标准输入输出标准输出A和B正在为编程竞赛做好准备 . 为了训练他们的逻辑思维并更好地解决问题,A和B决定下棋 . 在比赛期间,A想知道谁的位置现在更强 . 对于每个棋子,我们知道它的重量:女王的体重是9,
车的重量是5,
主教的体重是3,
骑士的体重是3,
典当的重量是1,
在评估位置时不考虑国王的体重 .
玩家的体重等于棋盘上所有棋子的重量总和 . 由于A不喜欢数数,他请你帮助他确定哪个球员的位置重量更大 . 输入输入包含八行,每行八个字符 - 电路板的描述 . 主板上的白色部分用大写字母标记,黑色部分用小写字母标记 . 白色部分表示如下:女王代表'Q',车 - 代表'R',主教 - 代表'B',骑士 - 代表'N',典当代表'P',国王 - 作为'K' . 黑色部分分别表示为“q”,“r”,“b”,“n”,“p”,“k” . 电路板的空方块标记为“ . ” (一个点) . 不能保证在真实游戏中可以实现给定的国际象棋位置 . 具体来说,每种类型都可以有任意(可能是零)个数字,国王可能受到攻击等等 . 如果白色部件的位置重量大于黑色部件的重量,则输出打印“白色”(不带引号),如果黑色部件的重量大于重量,则打印“黑色”如果白色和黑色碎片的重量相等,则打印白色碎片并打印“绘制” . 样品测试案例1

输入

... ... QK
........
........
........
........
........
........
... ... RK

产量

白色

案例2

输入

rnbqkbnr
pppppppp
........
........
........
........
PPPPPPPP
RNBQKBNR

产量

案例3:

输入

rppppppr
...,K ....
........
........
........
........
ķ . 问...
........

产量

黑色
注意在第一个测试样品中,白色碎片的位置重量等于9,黑色碎片的位置重量等于5.在第二个测试样品中,黑色和白色碎片的位置重量相等在第三个测试样品中,白色碎片的位置重量等于9,黑色碎片的位置重量等于16 .

我的代码是:

import java.util.Scanner;

public class chess {


    private final static int QUEEN = 9;
    private final static int ROOK = 5;
    private final static int BISHOP = 3;
    private final static int KNIGHT = 3;
    private final static int PAWN = 1;
    private final static int KING = 0;


    public static void main(String[] args) {

        int blackScore = 0;
        int whiteScore = 0;

        Scanner scan = new Scanner(System.in);

        String[] input = new String[8];
        input[0] = scan.nextLine();
        input[1] = scan.nextLine();
        input[2] = scan.nextLine();
        input[3] = scan.nextLine();
        input[4] = scan.nextLine();
        input[5] = scan.nextLine();
        input[6] = scan.nextLine();
        input[7] = scan.nextLine();

        for(int i = 0; i < input.length; i++) {
            if(input[i].contains("Q")) {
                whiteScore += QUEEN;
            }

            if(input[i].contains("R")) {
                whiteScore += ROOK;
            }

            if(input[i].contains("N")) {
                whiteScore += KNIGHT;
            }

            if(input[i].contains("B")) {
                whiteScore += BISHOP;
            }

            if(input[i].contains("P")) {
                whiteScore += PAWN;
            }

            if(input[i].contains("K")) {
                whiteScore += KING;
            }


            if(input[i].contains("q")) {
                blackScore += QUEEN;
            }

            if(input[i].contains("n")) {
                blackScore += KNIGHT;
            }

            if(input[i].contains("b")) {
                blackScore += BISHOP;
            }

            if(input[i].contains("p")) {
                blackScore += PAWN;
            }

            if(input[i].contains("k")) {
                blackScore += KING;
            }

            if(input[i].contains("r")) {
                blackScore += ROOK;
            }    


        }

        if(whiteScore != blackScore) {

            if(blackScore > whiteScore) {
                System.out.println("Black");
            }

            if (whiteScore > blackScore) {
                System.out.println("White");

            } 
        }else {
            System.out.println("Draw");
        }    


        System.out.println(whiteScore);
        System.out.println(blackScore);
    }

}

谁能解释我做错了什么?谢谢!!

2 回答

  • 0

    我认为你必须使用“else if”语句而不是“if”,试试这个,我希望这有帮助 .

    import java.util.Scanner;
    
    public class chess {
    
    private final static int QUEEN = 9;
    private final static int ROOK = 5;
    private final static int BISHOP = 3;
    private final static int KNIGHT = 3;
    private final static int PAWN = 1;
    private final static int KING = 0;
    
    
    public static void main(String[] args) {
    
        int blackScore = 0;
        int whiteScore = 0;
    
        Scanner scan = new Scanner(System.in);
    
        String[] input = new String[8];
        input[0] = scan.nextLine();
        input[1] = scan.nextLine();
        input[2] = scan.nextLine();
        input[3] = scan.nextLine();
        input[4] = scan.nextLine();
        input[5] = scan.nextLine();
        input[6] = scan.nextLine();
        input[7] = scan.nextLine();
    
        for(int i = 0; i < input.length; i++) {
            if(input[i].contains("Q")) {
                whiteScore += QUEEN;
            }
    
            else if(input[i].contains("R")) {
                whiteScore += ROOK;
            }
    
            else if(input[i].contains("N")) {
                whiteScore += KNIGHT;
            }
    
            else if(input[i].contains("B")) {
                whiteScore += BISHOP;
            }
    
            else if(input[i].contains("P")) {
                whiteScore += PAWN;
            }
    
            else if(input[i].contains("K")) {
                whiteScore += KING;
            }
    
    
            else if(input[i].contains("q")) {
                blackScore += QUEEN;
            }
    
            else if(input[i].contains("n")) {
                blackScore += KNIGHT;
            }
    
            else if(input[i].contains("b")) {
                blackScore += BISHOP;
            }
    
            else if(input[i].contains("p")) {
                blackScore += PAWN;
            }
    
            else if(input[i].contains("k")) {
                blackScore += KING;
            }
    
            else if(input[i].contains("r")) {
                blackScore += ROOK;
            }
    
    
        }
    
        if(whiteScore != blackScore) {
    
        if(blackScore > whiteScore) {
            System.out.println("Black");
        }
    
        if (whiteScore > blackScore) {
            System.out.println("White");
    
        } 
        }else {
            System.out.println("Draw");
        }
    
    
        System.out.println(whiteScore);
        System.out.println(blackScore);
    }
    }
    
  • 0

    问题是,只要在一条线上发现任何数量的棋子,你就只包括其中一个棋子的分数 . 看到你与解决方案有多接近,我认为仅仅提示就足够了!

相关问题