Lab: Dodge Ball

博客介绍了躲避球游戏,该游戏有两名玩家A和B,A填充n×n网格的上排,B填充下排最左方格。A先行动,双方交替进行,直至B填满下排。若A的某行与B的行匹配,A胜;否则B胜。程序需根据A完成的网格输出B的获胜策略行。

Lab: Dodge Ball
Our game of Dodge Ball is a game for two Players: A and B, where players alternate moves, with A going first. A is given an n by n grid to fill out and B is given a length n row to fill out.

A fills in one of the upper rows of a grid with X’s and O’s
B fills in the leftmost square of the lower row with an X or an O
A fills in another one of the upper rows with X’s and O’s
B fills in the leftmost blank square of the lower row with an X or an O
this continues until B fills in all n columns of their row
Player A wins if one of their n rows matches the row of B. Player B wins if their row is different from all of Player A’s rows.

In class we saw that there is a winning perfect strategy for Player B. Your program should take as input a complete grid that was made by Player A and output the row that Player B would have made using our prefect strategy from class.

在这里插import java.util.Scanner;

public class Main
{
    private static boolean[] winningRow(boolean[][] player1sGrid)
    {
        boolean[]b1 = new boolean[player1sGrid.length];
        for(int n = 0; n<player1sGrid.length;n++){
            b1[n] = (!player1sGrid[n][n]);
        }
        return b1;
    }

    // Do not edit methods below here
    public static void main(String[] args)
    {
        // Read Player 1's grid of X's and O's
        final boolean[][] grid = readGrid();

        // Print the the moves made by Player 2 to win
        printRow(winningRow(grid));
    }

    private static boolean[][] readGrid()
    {
        // Note that we are not checking for bad input
        final Scanner cin = new Scanner(System.in);
        final int n = cin.nextInt();
        final boolean[][] grid = new boolean[n][n];

        for(int rowNumber = 0; rowNumber < n; rowNumber++)
        {
            String line = cin.next();
            for(int columnNumber = 0; columnNumber < n; columnNumber++)
            {
                grid[rowNumber][columnNumber] = line.charAt(columnNumber) == 'X';
            }
        }

        return grid;
    }

    private static void printRow(boolean[] row)
    {
        for(boolean b : row)
        {
            System.out.print(b ? 'X' : 'O');
        }
    }
}入代码片
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值