Java - HELP

 
Vista:
sin imagen de perfil

HELP

Publicado por Jorgi (1 intervención) el 29/01/2023 01:36:26
Make a JAVA progam that requests the name of parts and color of a chessboard and count how many are and say when the board is full. Can not be more pieces than established (32 pieces)
A chess board has:
8 white pawns
8 black pawns
2 black rooks, bishop and knight
2 white rooks, bishop and knight
1 black and white queen
1 black and white king
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

HELP

Publicado por Kabuto (1383 intervenciones) el 30/01/2023 02:00:24
We can use two arrays of 6 elements as a counters for each color.

User inputs a string with color and name of piece. We separate this input using the split() method of String class so we can evaluate the name of the piece and the color.

To evaluate the name we can use a switch. In each case, we have to evaluate the color and increment the proper counter.
To avoid repeating the same code many times, we can use a method to which we indicate the color, the position of the counter that has to increase according to the name of the piece and the limit for that piece.

Another method will calculate the sum of all pieces to know when finishes the main loop.

I hope you can understand this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.Scanner;
 
public class FillBoard {
 
	//Pieces counters, there are six types of pieces for each color
	private static int[] whitePieces = new int[6];
	private static int[] blackPieces = new int[6];
	//We also could use a matrix of 2x6
 
	public static void main(String[] args) {
 
		Scanner keyboard = new Scanner(System.in);
 
		while (sumOfPieces() < 32) {
			System.out.println("Pieces remaining: "  + (32 - sumOfPieces()));
			System.out.println("Insert color and name of piece (e.g. 'white pawn'):");
			String input = keyboard.nextLine().toLowerCase();
			//We use "split" to separate name from color in an array
			String[] piece = input.split(" ");
			//Evaluating name of piece using "switch"
			switch(piece[1]) {
			//for each piece, we also evaluate the color with "if else"
			case "pawn":
				evaluateColorForCounters(piece[0], 0, 8);
				break;
			case "rook":
				evaluateColorForCounters(piece[0], 1, 2);
				break;
			case "bishop":
				evaluateColorForCounters(piece[0], 2, 2);
				break;
			case "knight":
				evaluateColorForCounters(piece[0], 3, 2);
				break;
			case "queen":
				evaluateColorForCounters(piece[0], 4, 1);
				break;
			case "king":
				evaluateColorForCounters(piece[0], 5, 1);
				break;
			default:
				System.out.println("Unknow name of piece");
			}
 
			System.out.println("\n");
		}
 
		System.out.println("The chessboard is full");
 
		System.out.println("\n\t\t PROGRAM EXIT");
		keyboard.close();
 
	}
 
	private static int sumOfPieces() {
		int sumWhite = 0, sumBlack = 0;
		for (int i = 0; i < 6; i++) {
			sumWhite += whitePieces[i];
			sumBlack += blackPieces[i];
		}
		return sumWhite + sumBlack;
	}
 
	private static void evaluateColorForCounters(String color, int position, int limit) {
		if (color.equals("white")) {
			if (whitePieces[position] < limit) {
				whitePieces[position]++;
				System.out.println("Piece added to the board.");
			}
			else
				System.out.println("Piece not added. Limit reached.");
		}
		else if (color.equals("black")) {
			if (blackPieces[position] < limit) {
				blackPieces[position]++;
				System.out.println("Piece added to the board.");
			}
			else
				System.out.println("Piece not added. Limit reached.");
		}
		else
			System.out.println("Unknow color.");
	}
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar