<commit_before>package com.jensen.game.othello.model;

import com.jensen.game.inteface.Game;
import com.jensen.game.model.*;

import java.util.Random;

public class OthelloModel implements Game {

    private Board board;
    private OthelloPlayer[] players;
    private int currentPlayerIndex;
    private String message = "";

    public OthelloModel(String[] playerNames, int width, int height) {
        initPlayers(playerNames);
        initBoard(width, height);
        players[1].setComputerControlled(Difficulty.HARD);
        setMessage(getCurrentPlayer().getName() + "s' turn!");
    }

    /**
     * Initiates the board.
     *
     * @param width  The amount of columns of the board.
     * @param height The amount of rows of the board.
     */
    private void initBoard(int width, int height) {
        board = new OthelloBoardFactory().createBoard(width, height);
    }

    /**
     * Initiates players.
     *
     * @param playerNames The names of the players.
     */
    private void initPlayers(String[] playerNames) {
        players = new OthelloPlayer[playerNames.length];

        PieceColor[] colors = PieceColor.values();

        for (int i = 0; i < playerNames.length; i++) {
            String name = playerNames[i];
            PieceColor color = colors[i];

            players[i] = new OthelloPlayer(name, color);
        }
    }

    @Override
    public boolean move(int x, int y) {
        OthelloPlayer player = getCurrentPlayer();

        if (isGameOver()) {
            // TODO Check who won and add to or replace message
            setMessage("Game Over!" + " (" + getScore() + ")");
            return false;
        }

        if (getValidMoves(player).length == 0) {
            setMessage(player.getName() + " can't make a move!");
            nextPlayer();
            move(x, y);
            return false;
        }

        if (!isValidMove(player, x, y)) {
            setMessage("Invalid Move!");
            return false;
        }

        // flip other player disks
        int flips = 0;

        for (Direction direction : Direction.values()) {
            if (!isValidMove(player, x, y, direction)) {
                continue;
            }

            Cell cell = board.getCell(x, y);

            while ((cell = cell.getAdjacentCell(direction)) != null) {
                if (cell.isEmpty()) {
                    break;
                }

                Piece piece = cell.getPiece();

                if (!(piece instanceof Disk)) {
                    break;
                }

                Disk disk = (Disk) piece;

                if (disk.getColor() == player.getColor()) {
                    break;
                } else {
                    disk.flip();
                    flips++;
                }
            }
        }

        // place disk
        Disk newDisk = new Disk(player.getColor());
        board.getCell(x, y).place(newDisk);

        setMessage(player.getName() + " flipped " + flips + " disks.");

        // TODO what if player can't move?
        player = nextPlayer();
        setMessage(player.getName() + "s' turn!");

        // what if player can't move tryout // TODO scrap
        if (getValidMoves(player).length == 0) {
            player = nextPlayer();
            setMessage(player.getName() + "s' turn!");
        }

        // TODO what if only computer controlled players?
        if (player.isComputerControlled()) {
            Cell chosenMove = player.getAI().getMove(getValidMoves(player));
            if (chosenMove != null) {
                GridPosition movePosition = chosenMove.getPosition();
                move(movePosition.getX(), movePosition.getY());
            } else {
                System.out.println("fail");
                player = nextPlayer();
                setMessage(player.getName() + "s' turn!");
            }
        }

        return true;
    }

    /**
     * Gets the status (content) of a board cell.
     *
     * @param x The column of the cell
     * @param y The row of the cell
     * @return One of the following strings: "WHITE", "BLACK", "OBSTRUCTION", "" (empty string)
     */
    @Override
    public String getStatus(int x, int y) {
        Cell cell = board.getCell(x, y);

        if (cell.isEmpty()) {
            return "";
        }

        Piece piece = cell.getPiece();

        if (piece instanceof Disk) {
            return ((Disk) piece).getColor().toString();
        }

        if (piece instanceof Obstruction) {
            return "OBSTRUCTION";
        }<commit_msg>added valid status<commit_after>package com.jensen.game.othello.model;

import com.jensen.game.inteface.Game;
import com.jensen.game.model.*;

import java.util.Random;

public class OthelloModel implements Game {

    private Board board;
    private OthelloPlayer[] players;
    private int currentPlayerIndex;
    private String message = "";

    public OthelloModel(String[] playerNames, int width, int height) {
        initPlayers(playerNames);
        initBoard(width, height);
        players[1].setComputerControlled(Difficulty.HARD);
        setMessage(getCurrentPlayer().getName() + "s' turn!");
    }

    /**
     * Initiates the board.
     *
     * @param width  The amount of columns of the board.
     * @param height The amount of rows of the board.
     */
    private void initBoard(int width, int height) {
        board = new OthelloBoardFactory().createBoard(width, height);
    }

    /**
     * Initiates players.
     *
     * @param playerNames The names of the players.
     */
    private void initPlayers(String[] playerNames) {
        players = new OthelloPlayer[playerNames.length];

        PieceColor[] colors = PieceColor.values();

        for (int i = 0; i < playerNames.length; i++) {
            String name = playerNames[i];
            PieceColor color = colors[i];

            players[i] = new OthelloPlayer(name, color);
        }
    }

    @Override
    public boolean move(int x, int y) {
        OthelloPlayer player = getCurrentPlayer();

        if (isGameOver()) {
            // TODO Check who won and add to or replace message
            setMessage("Game Over!" + " (" + getScore() + ")");
            return false;
        }

        if (getValidMoves(player).length == 0) {
            setMessage(player.getName() + " can't make a move!");
            nextPlayer();
            move(x, y);
            return false;
        }

        if (!isValidMove(player, x, y)) {
            setMessage("Invalid Move!");
            return false;
        }

        // flip other player disks
        int flips = 0;

        for (Direction direction : Direction.values()) {
            if (!isValidMove(player, x, y, direction)) {
                continue;
            }

            Cell cell = board.getCell(x, y);

            while ((cell = cell.getAdjacentCell(direction)) != null) {
                if (cell.isEmpty()) {
                    break;
                }

                Piece piece = cell.getPiece();

                if (!(piece instanceof Disk)) {
                    break;
                }

                Disk disk = (Disk) piece;

                if (disk.getColor() == player.getColor()) {
                    break;
                } else {
                    disk.flip();
                    flips++;
                }
            }
        }

        // place disk
        Disk newDisk = new Disk(player.getColor());
        board.getCell(x, y).place(newDisk);

        setMessage(player.getName() + " flipped " + flips + " disks.");

        // TODO what if player can't move?
        player = nextPlayer();
        setMessage(player.getName() + "s' turn!");

        // what if player can't move tryout // TODO scrap
        if (getValidMoves(player).length == 0) {
            player = nextPlayer();
            setMessage(player.getName() + "s' turn!");
        }

        // TODO what if only computer controlled players?
        if (player.isComputerControlled()) {
            Cell chosenMove = player.getAI().getMove(getValidMoves(player));
            if (chosenMove != null) {
                GridPosition movePosition = chosenMove.getPosition();
                move(movePosition.getX(), movePosition.getY());
            } else {
                System.out.println("fail");
                player = nextPlayer();
                setMessage(player.getName() + "s' turn!");
            }
        }

        return true;
    }

    /**
     * Gets the status (content) of a board cell.
     *
     * @param x The column of the cell
     * @param y The row of the cell
     * @return One of the following strings: "WHITE", "BLACK", "OBSTRUCTION", "" (empty string)
     */
    @Override
    public String getStatus(int x, int y) {
        Cell cell = board.getCell(x, y);
        
        if (isValidMove(getCurrentPlayer(), x, y)) {
            return "VALID-" + getCurrentPlayer().getColor();
        }


        if (cell.isEmpty()) {
            return "";
        }

        Piece piece = cell.getPiece();

        if (piece instanceof Disk) {
            return ((Disk) piece).getColor().toString();
        }

        if (piece instanceof Obstruction) {
            return "OBSTRUCTION";
        }
{'fraction_non_alphanumeric': 0.1190605043835913, 'fraction_numerical': 0.0010823682216690117, 'mean_word_length': 1.6759339704604692, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5390370', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2431, 'n_words': 673}