<commit_before>package es.ucm.fdi.tp.views.swing.boardpanel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.List;
import java.util.Map;

import es.ucm.fdi.tp.basecode.bgame.control.Controller;
import es.ucm.fdi.tp.basecode.bgame.model.Board;
import es.ucm.fdi.tp.basecode.bgame.model.GameObserver;
import es.ucm.fdi.tp.basecode.bgame.model.Piece;


public abstract class FiniteRectBoardComponent extends BoardComponent implements GameObserver {

	private static final long serialVersionUID = 6531989076185688155L;
	
	private int rows;
	private int cols;
	
	private int vMargin = 0;
	private int hMargin = 0;

	private int pieceSize;
	
	private final int MARGIN_GAP;
	

	protected FiniteRectBoardComponent(Controller c, Map<Piece,Color> colors) {
		super(c, colors);
		this.MARGIN_GAP = getMargin();
	}
	
	@Override
	public void redraw() {
		this.repaint();
	}
	
	@Override
	final public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		calcMetrics();
		
		for(int i=0, y=vMargin; i<rows; i++, y+=pieceSize + MARGIN_GAP) {
			for(int j=0,x=hMargin; j<cols; j++, x+=pieceSize + MARGIN_GAP) {
				paintPiece(g, i, j, x, y, pieceSize);
			}
		}
		
	}
	
	private void calcMetrics() {
		calcPieceSize();
		centerBoard();
	}
	
	private void centerBoard() {
		this.vMargin = (getHeight() - (pieceSize+MARGIN_GAP)*rows -MARGIN_GAP )/2 + MARGIN_GAP;
		this.hMargin = (getWidth() - (pieceSize+MARGIN_GAP)*cols -MARGIN_GAP )/2 + MARGIN_GAP;
	}
	
	private void calcPieceSize() {
		this.pieceSize = Math.min((getWidth()-MARGIN_GAP)/cols, (getHeight()-MARGIN_GAP)/rows) - MARGIN_GAP;
	}
	
	@Override
	public void onGameStart(Board board, String gameDesc, List<Piece> pieces, Piece turn) {
		super.onGameStart(board, gameDesc, pieces, turn);
		this.rows = board.getRows();
		this.cols = board.getCols();
	}
<commit_msg>Add finite rect board methods useful for children<commit_after>package es.ucm.fdi.tp.views.swing.boardpanel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.List;
import java.util.Map;

import es.ucm.fdi.tp.basecode.bgame.control.Controller;
import es.ucm.fdi.tp.basecode.bgame.model.Board;
import es.ucm.fdi.tp.basecode.bgame.model.GameObserver;
import es.ucm.fdi.tp.basecode.bgame.model.Piece;


public abstract class FiniteRectBoardComponent extends BoardComponent implements GameObserver {

	private static final long serialVersionUID = 6531989076185688155L;
	
	private int rows;
	private int cols;
	
	private int vMargin = 0;
	private int hMargin = 0;

	private int pieceSize;
	
	private final int MARGIN_GAP;
	

	protected FiniteRectBoardComponent(Controller c, Map<Piece,Color> colors) {
		super(c, colors);
		this.MARGIN_GAP = getMargin();
	}
	
	@Override
	public void redraw() {
		this.repaint();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		calcMetrics();
		
		for(int i=0, y=vMargin; i<rows; i++, y+=pieceSize + MARGIN_GAP) {
			for(int j=0,x=hMargin; j<cols; j++, x+=pieceSize + MARGIN_GAP) {
				paintPiece(g, i, j, x, y, pieceSize);
			}
		}
		
	}
	
	protected final int getMarginTop() {
		return this.vMargin;
	}
	
	protected final int getMarginLeft() {
		return this.hMargin;
	}
	
	private void calcMetrics() {
		calcPieceSize();
		centerBoard();
	}
	
	private void centerBoard() {
		this.vMargin = (getHeight() - (pieceSize+MARGIN_GAP)*rows -MARGIN_GAP )/2 + MARGIN_GAP;
		this.hMargin = (getWidth() - (pieceSize+MARGIN_GAP)*cols -MARGIN_GAP )/2 + MARGIN_GAP;
	}
	
	private void calcPieceSize() {
		this.pieceSize = Math.min((getWidth()-MARGIN_GAP)/cols, (getHeight()-MARGIN_GAP)/rows) - MARGIN_GAP;
	}
	
	@Override
	public void onGameStart(Board board, String gameDesc, List<Piece> pieces, Piece turn) {
		super.onGameStart(board, gameDesc, pieces, turn);
		this.rows = board.getRows();
		this.cols = board.getCols();
	}

{'fraction_non_alphanumeric': 0.12795477903391572, 'fraction_numerical': 0.012846865364850977, 'mean_word_length': 4.369655172413793, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4045907', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1430, 'n_words': 302}