<commit_before>import java.util.Random;

public class Opponent {

	Random rand = new Random();
	String colour;

	public Opponent(String colour) {
		this.colour = colour;
	}

	public Position[] getRandomMove(Board board) {

		Position[] fromTo = new Position[2];

		Position[][] allMoves = board.getAllLegalMoves(this.colour);

		int choice = rand.nextInt(allMoves[0].length);

		fromTo[0] = allMoves[0][choice];
		fromTo[1] = allMoves[1][choice];

		return fromTo;
	}

	public int countPieces(Board board) {
		int pieceCount = 0;

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				Object pieceAt = board.getPieceAt(i, j);
				if (pieceAt instanceof Piece) {
					if (((Piece) pieceAt).getColour() == this.colour
							&& ((Piece) pieceAt).canMove(board)) {
						pieceCount++;
					}
				}
			}
		}
		return pieceCount;
	}

	public Piece[] getPieces(Board board) {
		Piece[] pieces = new Piece[countPieces(board)];
		int index = 0;

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				Object pieceAt = board.getPieceAt(i, j);
				if (pieceAt instanceof Piece) {
					if (((Piece) pieceAt).getColour() == this.colour
							&& ((Piece) pieceAt).canMove(board)) {
						pieces[index] = (Piece) pieceAt;
						index++;
					}
				}
			}
		}
		return pieces;
	}
}
<commit_msg>Added some basic checks in the computer move before choosing a random one<commit_after>import java.util.Random;

public class Opponent {

	Random rand = new Random();
	String colour;

	public Opponent(String colour) {
		this.colour = colour;
	}

	public Position[] getRandomMove(Board board) {

		Position[] bestMove = new Position[2];
		Position[][] allMoves = board.getAllLegalMoves(this.colour);

		for (int i = 0; i < allMoves[0].length; i++) {
			board.move(allMoves[0][i], allMoves[1][i]);
			// If a move will checkmate the opponent, take it
			if (board.isCheckmate(getOppositeColour())) {
				board.undo();
				bestMove[0] = allMoves[0][i];
				bestMove[1] = allMoves[1][i];
//				System.out.println("Checkmate");
				return bestMove;
			}
			board.undo();
		}

		for (int i = 0; i < allMoves[0].length; i++) {
			board.move(allMoves[0][i], allMoves[1][i]);
			// If a move will check the opponent, take it
			if (board.isCheck(getOppositeColour())) {
				board.undo();
				bestMove[0] = allMoves[0][i];
				bestMove[1] = allMoves[1][i];
//				System.out.println("Check");
				return bestMove;
			}
			board.undo();
		}

		int highestValue = 0;

		// Take the piece of highest value
		for (int i = 0; i < allMoves[0].length; i++) {
			if (board.getColourAt(allMoves[1][i]).equals(getOppositeColour())) {
				Piece piece = ((Piece)board.getPieceAt(allMoves[1][i]));
				if (piece.getValue() > highestValue) {
					highestValue = piece.getValue();
					bestMove[0] = allMoves[0][i];
					bestMove[1] = allMoves[1][i];
//					System.out.println("Take piece");
				}
			}
		}

		if (highestValue > 0) {
			return bestMove;
		}

		// Make a random move
		int choice = rand.nextInt(allMoves[0].length);

		bestMove[0] = allMoves[0][choice];
		bestMove[1] = allMoves[1][choice];

		return bestMove;
	}

	public int countPieces(Board board) {
		int pieceCount = 0;

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				Object pieceAt = board.getPieceAt(i, j);
				if (pieceAt instanceof Piece) {
					if (((Piece) pieceAt).getColour() == this.colour
							&& ((Piece) pieceAt).canMove(board)) {
						pieceCount++;
					}
				}
			}
		}
		return pieceCount;
	}

	public Piece[] getPieces(Board board) {
		Piece[] pieces = new Piece[countPieces(board)];
		int index = 0;

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				Object pieceAt = board.getPieceAt(i, j);
				if (pieceAt instanceof Piece) {
					if (((Piece) pieceAt).getColour() == this.colour
							&& ((Piece) pieceAt).canMove(board)) {
						pieces[index] = (Piece) pieceAt;
						index++;
					}
				}
			}
		}
		return pieces;
	}

	public String getOppositeColour() {
		if (this.colour == "Black") {
			return "White";
		} else {
			return "Black";
		}
	}
}

{'fraction_non_alphanumeric': 0.16592920353982302, 'fraction_numerical': 0.014257620452310717, 'mean_word_length': 3.0207509881422925, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 5, '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': '6220989', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1491, 'n_words': 360}