Strings
Drop the b: & p: prefixes as the length of the string distinguishes the base from the piece. Three characters means a base, more than 3 means a piece.
boolean isBase = ( string.length() == 3 ) ;
boolean isPiece = ( string.length() > 3 ) ;
To get the team number, you know the digits are always in the first three characters.
int teamId = Integer.parseInt( string.substring( 0 , 2 ) ) ;
Objects
But as others suggested, you should be using smart objects rather than dumb strings.
Something like this quick-and-dirty demo.
Make an interface to cover both base and piece.
package work.basil.example.game;
public interface GamePart
{
int teamId ( );
}
Implement both base and piece. I would define them as a record if their main purpose is to transparently communicate shallowly-immutable data.
package work.basil.example.game;
public record Base ( int teamId ) implements GamePart
{
}
package work.basil.example.game;
public record Piece ( int teamId , int id ) implements GamePart
{
}
Represent the board. The board holds elements of the type of the interface GamePart. The actual objects can be an instance of either of our two record classes, Base & Piece.
package work.basil.example.game;
public class Board
{
final GamePart[][] grid = new GamePart[ 8 ][ 8 ];
GamePart gamePartAtIndices ( final int xIndex , final int yIndex )
{
return grid[ xIndex ][ yIndex ];
}
void placeGamePartAtIndices ( final int xIndex , final int yIndex , final GamePart gamePart )
{
this.grid[ xIndex ][ yIndex ] = gamePart;
}
String report ( )
{
StringBuilder stringBuilder = new StringBuilder( );
for ( int row = 0 ; row < grid.length ; row++ )
{
for ( int column = 0 ; column < grid[ row ].length ; column++ )
{
stringBuilder.append( "Row: " ).append( row ).append( " | Column: " ).append( column ).append( " = " ).append( grid[ row ][ column ] ).append( System.lineSeparator( ) );
}
}
return stringBuilder.toString( );
}
}
Drive the whole thing through a Game class.
package work.basil.example.game;
public class Game
{
final Board board = new Board( );
public static void main ( String[] args )
{
Game game = new Game( );
game.board.placeGamePartAtIndices( 1 , 0 , new Piece( 7 , 42 ) );
game.demo( );
}
private void demo ( )
{
System.out.println( this.board.report( ) );
}
}
When run:
Row: 0 | Column: 0 = null
Row: 0 | Column: 1 = null
Row: 0 | Column: 2 = null
Row: 0 | Column: 3 = null
Row: 0 | Column: 4 = null
Row: 0 | Column: 5 = null
Row: 0 | Column: 6 = null
Row: 0 | Column: 7 = null
Row: 1 | Column: 0 = Piece[teamId=7, id=42]
Row: 1 | Column: 1 = null
Row: 1 | Column: 2 = null
Row: 1 | Column: 3 = null
…
grid, can you maybe work directly with that instead of concatenation? Maybe we could help you better if you provide some examples of the input data and the expected output.