Question

Must be done in Java.

20 points! Complete this and the game is won PROBLEM 3: CHECK AND MATE! In checkers (in the first part of the game) all valid

PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL.

30 points! Now you are well PROBLEM 2: CHECK IT OUT! on your way! In checkers, players take turns moving their pieces. Starti

PROBLEM 1 INFORMATION IF YOU NEED IT AS WELL:

50 points! Complete this and youll have passed! PROBLEM 1: A CHECKERED PAST Checkers is played on an N XN checker board, wit

Provide the rest of the code with full comments and explanation and with proper indentation.

Use simple methods for better understanding.

Must compile.

At the end, show the exact Output that's shown in Problem3.

CODE PROVIDED FOR PROBLEM 2:

import java.util.Scanner;

public class Problem2

{

public static void main( String [] args )

{

//N denoting the size of the board

int n;

//B denoting the number of black pieces to be place on the board

int b;

//row and column position of a piece

int r,c;

//W denoting the number of white pieces to be place on the board

int w;

int i,j;

Scanner in = new Scanner(System.in);

n = in.nextInt();

String[][] board = new String[n][n];

//initialize the board with periods

for(i = 0;i

{

for(j = 0;j

{

board[i][j] = ".";

}

}

//take b and its r and c positions in one line of input

//and remove any whitespaces at end and start, split at space to separate values

in.nextLine();

String[] inp = in.nextLine().trim().split(" ");

//B will the first value in the splitted input stored in the string array

b = Integer.parseInt(inp[0]);

for(i = 1;i<2*b;i+=2)

{

r = Integer.parseInt(inp[i]);

c = Integer.parseInt(inp[i+1]);

//check for row and column exceed conditions .

//positions must be in range of n

if(r >=0 && r

{

if(c >=0 && c< n)

{

//set black posiiton in board

board[n - r - 1][c] = "*";

}

}

}

//take W and its r and c positions in one line of input

//and remove any whitespaces at end and start, split at space to separate values

inp = in.nextLine().trim().split(" ");

//W will the first value in the splitted input stored in the string array

w = Integer.parseInt(inp[0]);

for(i = 1; i<2*w ; i+=2)

{

r = Integer.parseInt(inp[i]);

c = Integer.parseInt(inp[i+1]);

//check for row and column exceed conditions .

//positions must be in range of n

if(r >=0 && r

{

if(c >=0 && c< n)

{

//set white piece position in board

board[n - r - 1][c] = "0";

}

}

}

// Get valid moves and modify the board accoridng to them

inp = in.nextLine().trim().split(" ");

int moveNum = Integer.parseInt(inp[0]);

for(i = 1; i <= 4*moveNum; i+=4) {

int r1, c1, r2, c2;

// read moves

r1 = Integer.parseInt(inp[i]);

c1 = Integer.parseInt(inp[i+1]);

r2 = Integer.parseInt(inp[i+2]);

c2 = Integer.parseInt(inp[i+3]);

//Move (r1,c1) to (r2, c2)

board[n-r2-1][c2] = board[n-r1-1][c1];

//Clear (r1,c1)

board[n-r1-1][c1] = ".";

}

//finally print out the board

for(i = 0;i

{

for(j = 0;j

{

System.out.print(board[i][j]+" ");

}

System.out.println();

}

}

}

________________________________________________________________

Show the exact Output that's shown in Problem 3. Or else it will be flagged.

20 points! Complete this and the game is won PROBLEM 3: CHECK AND MATE! In checkers (in the first part of the game) all valid move a piece forward on a diagonal, to an empty space. Starting with your solution to Problem 2, extend it so that it takes the same input as Problem 2, but checks each move for validity is read. If an invalid move is encounte red, a message is printed out followed by the checker board, which shows all the preceding valid moves. If all the moves are valid, then the resulting checker board is displayed. C as it INPUT Sample Input Invalid move The same input as in Problem 2 4. 30 2 1 1 1 3 PROCESSING: 2 2 3 3 3 1 1 2 2 20 1 1 2 2 3 2 A move is valid if there is a checker at position R, C1, an empty space on the board at R2 C2. Furthermore, C2 C1t1, for black checkers R2 R1+1 and for white checkers R2 Rj-1 Sample Output: Invalid Move ! ...0 .OUTPUT If an invalid move is encountered print "Invalid Move!" Lastly, print the resulting board (as in Problem 2), reflecting all valid moves prior to the first invalid move (or all moves if none are invalid).
30 points! Now you are well PROBLEM 2: CHECK IT OUT! on your way! In checkers, players take turns moving their pieces. Starting with your solution to Problem 1, extend it so that given a board size N, the positions of the black and white checkers, a sequence of valid moves, it prints out the resulting checker board. INPUT Sample Input One move The same input as in Problem 1, followed by Integer M denoting the number of moves, followed by 4M integers R, C, R2 C2 denoting the row and column of the start and end position 30 2 1 1 1 3 2 2 0 3 3 3 1 1 2 2 2 1 1 2 2 3 1 of a move l.e., the piece at R, c, is moved to R2 C2 Sample Output PROCESSING: *.0 You may assume that every move is valid The final board configuration is what is to be printed .0. OUTPUT Output is the same format as Problem 1
50 points! Complete this and you'll have passed! PROBLEM 1: A CHECKERED PAST Checkers is played on an N XN checker board, with one side playing the black pieces and the other side playing the white pieces. Given a board size N and the positions of the black and white checkers, create a program to print out the resulting checker board. INPUT Row, Column Integer N denoting the size of the board Integer B denoting the number of black pieces to be placed on the board, followed by B pairs of integers R C denoting the row and column of B black pieces Sample Input N Integer W denoting the number of white pieces to be placed on the board, followed by W pairs of integers R C denoting the row and column of W white pieces 4 3 2 1 1 1 3 2 2 3 3 W PROCESSING: The bottom left corner of the board is (0, o) and the top right corner is (N-1,N-1) (4,0) Sample Output 2 0 |(4,4) ...0 OUTPUT 0. Output an Nx N board, where an empty square is denoted by a period ., a black piece is denoted by an asterisk *', and a white piece is denoted by the letter o' 0 2 (0,0 |(0,4)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.Scanner;

public class Problem1

{

public static void main( String [] args )

{

//N denoting the size of the board

int n;

//B denoting the number of black pieces to be place on the board

int b;

//row and column position of a piece

int r,c;

//W denoting the number of white pieces to be place on the board

int w;

int i,j;

Scanner in = new Scanner(System.in);

n = in.nextInt();

String[][] board = new String[n][n];

//initialize the board with periods

for(i = 0;i<n;i++)

{

for(j = 0;j<n;j++)

{

board[i][j] = ".";

}

}

//take b and its r and c positions in one line of input

//and remove any whitespaces at end and start, split at space to separate values

in.nextLine();

String[] inp = in.nextLine().trim().split(" ");

//B will the first value in the splitted input stored in the string array

b = Integer.parseInt(inp[0]);

for(i = 1;i<2*b;i+=2)

{

r = Integer.parseInt(inp[i]);

c = Integer.parseInt(inp[i+1]);

//check for row and column exceed conditions .

//positions must be in range of n

if(r >=0 && r<n)

{

if(c >=0 && c< n)

{

//set black posiiton in board

board[n - r - 1][c] = "*";

}

}

}

//take W and its r and c positions in one line of input

//and remove any whitespaces at end and start, split at space to separate values

inp = in.nextLine().trim().split(" ");

//W will the first value in the splitted input stored in the string array

w = Integer.parseInt(inp[0]);

for(i = 1; i<2*w ; i+=2)

{

r = Integer.parseInt(inp[i]);

c = Integer.parseInt(inp[i+1]);

//check for row and column exceed conditions .

//positions must be in range of n

if(r >=0 && r<n)

{

if(c >=0 && c< n)

{

//set white piece position in board

board[n - r - 1][c] = "0";

}

}

}

// Get valid moves and modify the board accoridng to them

inp = in.nextLine().trim().split(" ");

int moveNum = Integer.parseInt(inp[0]);

for(i = 1; i <= 4*moveNum; i+=4) {

int r1, c1, r2, c2;

// read moves

r1 = Integer.parseInt(inp[i]);

c1 = Integer.parseInt(inp[i+1]);

r2 = Integer.parseInt(inp[i+2]);

c2 = Integer.parseInt(inp[i+3]);

// Check if destination is empty

if(board[n-r2-1][c2] != ".") {

System.out.println("Invalid Move!");

break;

}

// Check if the move is deiagonal

if(!(r1 == r2-1 && c1 == c2-1) &&

!(r1 == r2+1 && c1 == c2-1) &&

!(r1 == r2-1 && c1 == c2+1) &&

!(r1 == r2+1 && c1 == c2+1)) {

System.out.println("Invalid Move!");

break;

}

//Move (r1,c1) to (r2, c2)

board[n-r2-1][c2] = board[n-r1-1][c1];

//Clear (r1,c1)

board[n-r1-1][c1] = ".";

}

//finally print out the board

for(i = 0;i<n;i++)

{

for(j = 0;j<n;j++)

{

System.out.print(board[i][j]+" ");

}

System.out.println();

}

}

}

Output:
Input (stdin) 3 0 2 1 1 1 3 2 20 3 3 3 1 1 2 2 2 0 1 1 2 2 3 2 Your Output (stdout) Invalid Move!comment down for any queries
​​​​​​​ please give a thumbs up

Add a comment
Know the answer?
Add Answer to:
Must be done in Java. PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int n;       ...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end show the exact Output that's shown in the Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int...

  • please use JAVA to solve it. Checkers is played on an N XN checker board, with...

    please use JAVA to solve it. Checkers is played on an N XN checker board, with one side playing the black pieces and the ofther side playing the white pieces. Glven a board size N and the positions of the black and white checkers, create a program to print out the resulting checker board. INPUT Row, Column Integer N denoting the size of the board Integer B denoting the number of black pieces to be placed on the board, followed...

  • In Python, starting with the 8x8 board solution that will be appended here add the following...

    In Python, starting with the 8x8 board solution that will be appended here add the following functionality: 1) Add code to create a list, containing 8 lists, with each of the 8 lists containing 8 null strings as values. Call the list of lists board. code provided by prof: import turtle validMovesList=['A0','A2','A4','A6','B1','B3','B5','B7', 'C0','C2','C4','C6','D1','D3','D5','D7', 'E0','E2','E4','E6','F1','F3','F5','F7', 'G0','G2','G4','G6','H1','H3','H5','H7','quit'] def drawSquare(t,length,color): t.fillcolor(color) t.begin_fill() for num in range(4): t.forward(length) t.left(90) t.end_fill() def drawRow(t,length,color1,color2): for i in range(4): drawSquare(t,length,color1) t.forward(length) drawSquare(t,length,color2) t.forward(length) def drawCircleFilled(t,size,color): t.fillcolor(color) t.begin_fill()...

  • How to make a reversi/othello game in JAVA? Ideally with multiple classes and without GUI You...

    How to make a reversi/othello game in JAVA? Ideally with multiple classes and without GUI You are implementing a strategy board game played by two players, Black and White. It is played on an N by N board. The winner is the player who has more discs of his color than his opponent at the end of the game. This will happen when neither of the two players has a legal move or there are no spaces left on the...

  • Please develop the following code using C programming and using the specific functions, instructi...

    Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...

  • Lab 6.3.1 Polymorphism: part 2 Objectives Familiarie the student with: . polymorphism, or using o...

    solve in c++ Lab 6.3.1 Polymorphism: part 2 Objectives Familiarie the student with: . polymorphism, or using objocts of different types through a common interface using polymorphism in real programs Scenario Let's assume you write a code for the game of checkers (aka draughts). There are two types of pieces in this game: men and kings. You need to implement classes (one abstract for the pieces and two separate classes for the men and the kings) to chock if the...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • You are going to write a method (to be called validateBoard) that is going to validate...

    You are going to write a method (to be called validateBoard) that is going to validate whether or not a Tic-Tac-Toe board is possible. Tic- Tac-Toe is played on a 3 x 3 board and players take turns placing either an x or an o on the board. We will assume that in Tic-Tac-Toe the player placing x will go first arld that o will go second. (Learn more about the game here) As the player placing x pieces goes...

  • # In this file, fill in the ... parts with lines of code. Do not # create new functions. from ran...

    # In this file, fill in the ... parts with lines of code. Do not # create new functions. from random import seed, randrange P=[" ♟♜♝♞♛♚"]; L,R,BL,TL=["▌▐▄▀"] BonR=WonR=WonB=DonR=DonB=RonB=GonR=GonB=RonG='\033[1;m\033[' WonR+='7;31;47m' # For drawing a white piece on a red background WonB+='7;30;47m' # For drawing a white piece on a black background DonR+='2;37;41m' # For drawing a dark piece on a red background DonB+='2;37;40m' # For drawing a dark piece on a black background GonR+='2;33;41m' # For drawing gold on a red...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT