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');
}
}
}入代码片