Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Othello pieces that can be flipped

    Hello,

    I'm trying to complete this programming challenge which is completing a game which is based on othello (little less advanced). I'm stuck on this question which is really getting stressful now...

    Here is an example:

    We have a 6 by 6 board.

    Let's say that we have these pieces on the board (where H represents a humans move, and C represents a computers move)

    -63cf173b56324c5f8f9ee13c2765544f-png

    My task is have a message printed to the screen with the best move possible for the human player. The best move should be a move that the human can make which will result in the opponents pieces being flipped. E.g. me placing my H piece at (5,3) or (1,4) would be the best moves as it will result in the opponents C pieces being flipped. All I need to do is calculate the best moves throughout the game before the human enters their move. I haven't made too much progress yet... All I've done so far is calculate the positions of each piece on the board, but now I'm mind boggled.

    The completed code is here:


    Code:
    package aqareverse;
    
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.regex.Pattern;
    
    public class AQAReverse {
    
        AQAConsole2016 console = new AQAConsole2016();
        Random random = new Random();
    
        public AQAReverse() {
            char choice;
            String playerName;
            int boardSize;
            boardSize = 6;
            playerName = "";
            do {
                displayMenu();
                choice = getMenuChoice(playerName);
                switch (choice) {
                    case 'p':
                        playGame(playerName, boardSize);
                        break;
                    case 'e':
                        playerName = getPlayersName();
                        break;
                    case 'c':
                        boardSize = changeBoardSize();
                        break;
                }
            } while (choice != 'q');
        }
    
        void GetFlipSquares(char[][] board, int boardSize) {
            boolean canFlipSquare = false;
    
            ArrayList<Integer> squaresThatCanFlip = new ArrayList<Integer>();
            /*console.println("Choosing one of the following squares will\n" +
             "mean you flip some pieces:");
    
             int flipSquaresChoice = console.readInteger("");*/
            for (int row = 1; row <= boardSize; row++) {
                for (int column = 1; column <= boardSize; column++) {
                    if (board[row][column] == 'C') {
                        System.out.println("C pieces " + column + "" + row);
                    } else if (board[row][column] == 'H') {
                        System.out.println("H pieces " + row + "" + column);
                    }
                }
    
            }
    
        }
    
        void setUpGameBoard(char[][] board, int boardSize) {
            String setUpPosition = console.readLine("Select if you want to use (m)iddle or (c)orner configuration ");
    
            for (int row = 1; row <= boardSize; row++) {
                for (int column = 1; column <= boardSize; column++) {
                    if (setUpPosition.equalsIgnoreCase("c")) {
                        if (row == (boardSize + 1) - boardSize && column == (boardSize + 1) - boardSize + (boardSize - 1) || column == (boardSize - boardSize + 1) && row == (boardSize) - (boardSize - boardSize)) {
                            board[row][column] = 'C';
                        } else if (row == (boardSize) / 2 + (boardSize / 2) && column == (boardSize + 1) / 2 + (boardSize / 2) || column == (boardSize - boardSize + 1) && row == (boardSize - boardSize + 1)) {
                            board[row][column] = 'H';
                        } else {
                            board[row][column] = ' ';
                        }
                    } else if (row == (boardSize + 1) / 2 && column == (boardSize + 1) / 2 + 1 || column == (boardSize + 1) / 2 && row == (boardSize + 1) / 2 + 1) {
                        board[row][column] = 'C';
                    } else if (row == (boardSize + 1) / 2 + 1 && column == (boardSize + 1) / 2 + 1 || column == (boardSize + 1) / 2 && row == (boardSize + 1) / 2) {
                        board[row][column] = 'H';
                    } else {
                        board[row][column] = ' ';
                    }
    
                }
            }
        }
    
        int changeBoardSize() {
            int boardSize;
            do {
                console.print("Enter a board size (between 4 and 9): ");
                boardSize = console.readInteger("");
            } while (!(boardSize >= 4 && boardSize <= 9));
            return boardSize;
        }
    
        int getHumanPlayerMove(String playerName) {
            int coordinates;
            console.print(playerName + " enter the coordinates of the square where you want to place your piece: ");
            coordinates = console.readInteger("");
            return coordinates;
        }
    
        int getComputerPlayerMove(int boardSize) {
            return ((random.nextInt(boardSize) + 1) * 10 + (random.nextInt(boardSize) + 1));
        }
    
        boolean gameOver(char[][] board, int boardSize) {
            for (int row = 1; row <= boardSize; row++) {
                for (int column = 1; column <= boardSize; column++) {
                    if (board[row][column] == ' ') {
                        return false;
                    }
                }
            }
            return true;
        }
    
        String getPlayersName() {
            String playerName;
            console.print("What is your name? ");
            playerName = console.readLine();
            if (playerName.length() < 1) {
                console.print("That is not a valid name, using default name");
                playerName = "Human player.";
            }
            return playerName;
        }
    
        boolean checkIfMoveIsValid(char[][] board, int move, int boardSize) {
            int row;
            int column;
            boolean moveIsValid;
            row = move % 10;
            column = move / 10;
            moveIsValid = false;
    
            if (row <= boardSize && column <= boardSize && row > 0 && column > 0) {
                if (board[row][column] == ' ') {
                    moveIsValid = true;
    
                }
            } else {
                moveIsValid = false;
            }
            return moveIsValid;
    
        }
    
        int getPlayerScore(char[][] board, int boardSize, char piece) {
            int score;
            score = 0;
            for (int row = 1; row <= boardSize; row++) {
                for (int column = 1; column <= boardSize; column++) {
                    if (board[row][column] == piece) {
                        score = score + 1;
                    }
                }
            }
            return score;
        }
    
        boolean checkIfThereArePiecesToFlip(char[][] board, int boardSize, int startRow, int startColumn, int rowDirection, int columnDirection) {
            int rowCount;
            int columnCount;
            boolean flipStillPossible;
            boolean flipFound;
            boolean opponentPieceFound;
            rowCount = startRow + rowDirection;
            columnCount = startColumn + columnDirection;
            flipStillPossible = true;
            flipFound = false;
            opponentPieceFound = false;
            while (rowCount <= boardSize && rowCount >= 1 && columnCount >= 1 && columnCount <= boardSize && flipStillPossible && !flipFound) {
                if (board[rowCount][columnCount] == ' ') {
                    flipStillPossible = false;
                } else if (board[rowCount][columnCount] != board[startRow][startColumn]) {
                    opponentPieceFound = true;
                } else if (board[rowCount][columnCount] == board[startRow][startColumn] && !opponentPieceFound) {
                    flipStillPossible = false;
                } else {
                    flipFound = true;
                }
                rowCount = rowCount + rowDirection;
                columnCount = columnCount + columnDirection;
            }
            return flipFound;
        }
    
        void flipOpponentPiecesInOneDirection(char[][] board, int boardSize, int startRow, int startColumn, int rowDirection, int columnDirection) {
            int rowCount;
            int columnCount;
            boolean flipFound;
            flipFound = checkIfThereArePiecesToFlip(board, boardSize, startRow, startColumn, rowDirection, columnDirection);
            if (flipFound) {
                rowCount = startRow + rowDirection;
                columnCount = startColumn + columnDirection;
                while (board[rowCount][columnCount] != ' ' && board[rowCount][columnCount] != board[startRow][startColumn]) {
                    if (board[rowCount][columnCount] == 'H') {
                        board[rowCount][columnCount] = 'C';
                    } else {
                        board[rowCount][columnCount] = 'H';
                    }
                    rowCount = rowCount + rowDirection;
                    columnCount = columnCount + columnDirection;
                }
            }
        }
    
        void makeMove(char[][] board, int boardSize, int move, boolean humanPlayersTurn) {
            int row;
            int column;
            row = move % 10;
            column = move / 10;
            if (humanPlayersTurn) {
                board[row][column] = 'H';
            } else {
                board[row][column] = 'C';
            }
            flipOpponentPiecesInOneDirection(board, boardSize, row, column, 1, 0);
            flipOpponentPiecesInOneDirection(board, boardSize, row, column, -1, 0);
            flipOpponentPiecesInOneDirection(board, boardSize, row, column, 0, 1);
            flipOpponentPiecesInOneDirection(board, boardSize, row, column, 0, -1);
        }
    
        void printLine(int boardSize) {
            console.print("   ");
            for (int count = 1; count <= boardSize * 2 - 1; count++) {
                console.print("_");
            }
            console.println();
        }
    
        void displayGameBoard(char[][] board, int boardSize) {
            console.println();
            console.print("  ");
            for (int column = 1; column <= boardSize; column++) {
                console.print(" ");
                console.print(column);
            }
            console.println();
            printLine(boardSize);
            for (int row = 1; row <= boardSize; row++) {
                console.print(row);
                console.print(" ");
                for (int column = 1; column <= boardSize; column++) {
                    console.print("|");
                    console.print(board[row][column]);
                }
                console.println("|");
                printLine(boardSize);
                console.println();
            }
        }
    
        void displayMenu() {
            console.println("(p)lay game");
            console.println("(e)nter name");
            console.println("(c)hange board size");
            console.println("(q)uit");
            console.println();
        }
    
        char getMenuChoice(String playerName) {
            char choice;
            console.print(playerName + " enter the letter of your chosen option: ");
            choice = console.readChar();
            return choice;
        }
    
        void playGame(String playerName, int boardSize) {
            char[][] board = new char[boardSize + 1][boardSize + 1];
            boolean humanPlayersTurn;
            int move;
            int humanPlayerScore;
            int computerPlayerScore;
            boolean moveIsValid;
            setUpGameBoard(board, boardSize);
            humanPlayersTurn = false;
    
            do {
                humanPlayersTurn = !humanPlayersTurn;
                displayGameBoard(board, boardSize);
                moveIsValid = false;
    
                do {
    
                    if (humanPlayersTurn) {
                        move = getHumanPlayerMove(playerName);
                    } else {
                        move = getComputerPlayerMove(boardSize);
                    }
                    moveIsValid = checkIfMoveIsValid(board, move, boardSize);
    
                    if (moveIsValid) {
                        makeMove(board, boardSize, move, humanPlayersTurn);
                    }
    
                } while (!moveIsValid);
                if (!humanPlayersTurn) {
                    console.print("Press the Enter key and the computer will make its move");
                    console.readLine("");
                }
                GetFlipSquares(board, boardSize);
                //makeMove(board, boardSize, move, humanPlayersTurn);
            } while (!gameOver(board, boardSize));
            displayGameBoard(board, boardSize);
            humanPlayerScore = getPlayerScore(board, boardSize, 'H');
            computerPlayerScore = getPlayerScore(board, boardSize, 'C');
            if (humanPlayerScore > computerPlayerScore) {
                console.println("Well done, " + playerName + ", you have won the game!");
            } else {
                if (humanPlayerScore == computerPlayerScore) {
                    console.println("that was a draw!");
                } else {
                    console.println("The computer has won the game!");
                }
                console.println();
            }
        }
    
        public static void main(String[] args) {
            new AQAReverse();
        }
    }


    I don't WANT or EXPECT somebody to give me a full working solution in Java, all I want is some guidance.

    Note: If you didn't really understand my explanation of what needs to be done, then the actual question I'm being asked is as follows:

    Create a subroutine called GetFlipSquares which takes two parameters (the board
    and the board size) and displays a list of all the squares where the human player could
    place a piece that would result in some of the computerís pieces being flipped.
    You might find it easier to answer this question if you make subroutine calls to the
    CheckIfThereArePiecesToFlip subroutine from within your new subroutine
    GetFlipSquares.
    You are likely to get some marks for this task even if your subroutine is only partially
    working.

    - Thanks.

  2. #2
    Regular Coder
    Join Date
    Oct 2015
    Posts
    365
    Thanks
    1
    Thanked 45 Times in 42 Posts
    The answer is in the question: by using the routines you have already written and then looping through all your pieces to see which can move you should arrive at the answer you are looking for.

    You might want to change the return type of checkIfThereArePiecesToFlip to int and make changes to return a value which represents the number of pieces which would be flipped.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •