Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Intranet two palyer board game  (Read 4192 times)
0 Members and 1 Guest are viewing this topic.
Offline macx

Junior Newbie





« Posted 2013-09-19 08:02:32 »

Hi to all Java developers!!

I'm planning to develop a board game in an intranet environment for my Thesis, this game is somewhat similar to Checkers.
The game is called "Bataan: Battle of the Philippines". I need some help on where to start or what to implement first. Any help or advice from you guys is a big help for me.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-09-19 10:00:27 »

What have you done so far. Also, *internet. And we aren't going to spoon feed you code if thats what you want.

Online Mike

JGO Wizard


Medals: 74
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #2 - Posted 2013-09-19 10:02:42 »

Why can't it be an intranet? Maybe they have several computers at home?

I'd suggest the Resource page to start with as it has a ton of information! http://www.java-gaming.org/index.php?action=resources Smiley

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline macx

Junior Newbie





« Reply #3 - Posted 2013-09-23 02:03:10 »

I've done the board and put up the pieces on to it. I'm having a problem on making them move according to the mouseclicked by the user. These is my code so far:


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Compendio
 */
public class Bataan extends JPanel implements MouseListener, ActionListener {

    private BufferedImage img;      // image for the background of the game interface.
    boolean inGameState;            // gives true or false if a game is in progress.
    int currentPlayer;              // the current player to move.
    int rowSelected, colSelected;   // the row and column of the selected piece of the current player.
    private JButton newGameButton;  // Button for starting a new game.
    private JButton quitButton;     // Button to quit a game.
    private JLabel message;         // Label for displaying messages to the user.
    int numberofJapanese;           // the number of Japanese pieces.
   
    /* instance of the class Piece, the pieces and the moves
     *  of the game are saved at set here.
     */
    Piece boardPiece;
   
   
    BataanMoves[] validMoves;       //array containing all the valid moves for the current player.

    /* The main method creates a new JFrame as the window
     * and sets the calss Bataan as it's content.
    */
    public static void main(String args[]) {
        JFrame window = new JFrame("Bataan: Battle of the Philippines");
        Bataan game = new Bataan();
        window.setSize(700, 700);
        window.add(game);
        window.setLocation(400, 20);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.setVisible(true);
    }

    /* The class contructor sets all the properties of the class
     * and initializes the background image.
    */
    public Bataan() {

        Font F = new Font("kristen ITC", Font.BOLD, 15);
        setLayout(null);
        addMouseListener(this);
        setVisible(true);
        setSize(700, 700);
        quitButton = new JButton("Quit");
        quitButton.addActionListener(this);
        newGameButton = new JButton("New Game");
        newGameButton.addActionListener(this);
        message = new JLabel("", JLabel.CENTER);
        message.setFont(F);
        message.setForeground(Color.white);
        boardPiece = new Piece();
        add(newGameButton);
        add(quitButton);
        add(message);
        newGameButton.setFont(F);
        quitButton.setFont(F);
        newGameButton.setBounds(100, 60, 120, 30);
        quitButton.setBounds(300, 60, 100, 30);
        message.setBounds(80, 600, 400, 30);
        try {
            img = ImageIO.read(new File(
                    "D:/Documents/NetBeansProjects/MyProjects/build/classes/Images/war2.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        quitButton.setEnabled(false);

    }
   
    //Method called when the user clicks one of the buttons.
    public void actionPerformed(ActionEvent evt) {
        Object src = evt.getSource();
        if (src == newGameButton) {
            NewGame();
        } else if (src == quitButton) {
            QuitGame();
        }
    }
   
    /*Sets a new game, the starting properties and paints the
     * board for a new game. Called when the user clicks the New Game button.
     */
    void NewGame() {
        numberofJapanese = 50;
        boardPiece.setGame();
        currentPlayer = Piece.Japanese;
        validMoves = boardPiece.getValidMoves(Piece.Japanese);
        rowSelected = -1;
        message.setText("Japanese: Make your move.");
        inGameState = true;
        newGameButton.setEnabled(false);
        quitButton.setEnabled(true);
        repaint();
    }

    // Called when the user clicks the Quit button.
    void QuitGame() {
        if (currentPlayer == Piece.Japanese) {
            GameisOver("Japanese quits, American wins!");
        } else {
            GameisOver("American quits, Japanese wins!");
        }
    }

    // Sets the properties when the game is over.
    void GameisOver(String str) {
        message.setText(str);
        newGameButton.setEnabled(true);
        quitButton.setEnabled(false);
        inGameState = false;
    }

    /**
     * Respond to a user click on the board.  If no game is in progress, show
     * an error message.  Otherwise, find the row and column that the user
     * clicked and call squareClicked() method to handle it.
     */
    public void mousePressed(MouseEvent e) {
        if (inGameState == false) {
            message.setText("Click the New Game button to start a game.");
        } else {
            int col = (e.getY() - 150) / 50;
            int row = (e.getX() - 50) / 50;
            if (col >= 0 && col <= 8 && row >= 0 && row <= Cool {
                squareClicked(row, col);
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }
   
    /*When a user clicks on the board, checks if it is a piece or
     * a square on the board then, it makes the move.
     */
    void squareClicked(int r, int c) {
        for (int i = 0; i < validMoves.length; i++)
            if (validMoves.currentRow == r && validMoves.currentCol == c) {
                rowSelected = r;
                colSelected = c;

                if (currentPlayer == Piece.American) {
                    message.setText("American: Make your move.");
                } else {
                    message.setText("Japanese: Make your move.");
                }
                repaint();
                return;
            }
       
        if (rowSelected < 0) {
            message.setText("Click the piece you want to move.");
            return;
        }

        for (int i = 0; i < validMoves.length; i++)
            if (validMoves.currentRow == rowSelected && validMoves.currentCol == colSelected
                    && validMoves.nxtRow == r && validMoves.nxtCol == c) {
                MakeMove(validMoves);
                return;
            }
       


        message.setText("Click the square you want to move to.");
    }

    void MakeMove(BataanMoves move) {
        boardPiece.piecedoMove(move);

        if (move.aJump()) {
            validMoves = boardPiece.getValidJumps(currentPlayer, move.nxtRow, move.nxtCol);
            if (validMoves != null) {
                message.setText("American: You must continue jumping.");
            }
            rowSelected = move.nxtRow;
            colSelected = move.nxtCol;
            repaint();
            return;
        }
        if (currentPlayer == Piece.Japanese) {
            currentPlayer = Piece.American;
            validMoves = boardPiece.getValidMoves(currentPlayer);
            if (validMoves[0].aJump()) {
                message.setText("American: Make your move, you must jump.");
            } else {
                message.setText("American: Make your move.");
            }
        } else {
            currentPlayer = Piece.Japanese;
            validMoves = boardPiece.getValidMoves(currentPlayer);
            message.setText("Japanese: Make your move.");
        }

        rowSelected = -1;
       
        if(validMoves != null){
            boolean sameSquare = true;
           
            for(int i=1; i<validMoves.length; i++){
                if(validMoves.currentRow != validMoves[0].currentRow || validMoves.currentCol != validMoves[0].currentCol){
                    sameSquare = false;
                    break;
                }
            }
           
            if(sameSquare){
                rowSelected = validMoves[0].currentRow;
                colSelected = validMoves[0].currentCol;
            }
           
        }
        repaint();
    }

    /* Draws the playing board for the game
     * ands add the right pieces to it.
     */
    public void paintComponent(Graphics g) {
        int[] xPoints = {100, 150, 150};
        int[] yPoints = {150, 200, 150};
        int[] xPoints2 = {350, 400, 350};
        int[] yPoints2 = {200, 150, 150};
        int nPoints = 3;
        Graphics2D g2 = (Graphics2D) g;

        g2.setStroke(new BasicStroke(5));
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawImage(img, 0, 0, getWidth(), getHeight(), this);


        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                if ((row == 0 && (col == 6 || col == 7))
                        || (row == 1 && (col == 6 || col == 7))
                        || (row == 6 && (col == 7 || col == 6))
                        || (row == 7 && (col == 6 || col == 7))
                        || (row == 0 && (col == 1 || col == 0))
                        || (row == 1 && (col == 1 || col == 0))
                        || (row == 6 && (col == 0 || col == 1))
                        || (row == 7 && (col == 1 || col == 0)))  {
                } else {

                    if (col == 0 || col == 1) {
                        g2.setColor(Color.blue);
                        g2.fillRect(50 + row * 50, 150 + col * 50, 50, 50);

                    } else {
                        g2.setColor(Color.gray);
                        g2.fillRect(50 + row * 50, 150 + col * 50, 50, 50);
                    }
                    g2.setColor(Color.red);
                    g2.drawRect(50 + row * 50, 150 + col * 50, 50, 50);

                    if (row % 2 == col % 2) {
                        g2.setColor(Color.red);
                        g2.drawLine(50 + row * 50, 150 + col * 50, 100 + row * 50, 200 + col * 50);

                    } else {
                        g2.setColor(Color.red);
                        g2.drawLine(100 + row * 50, 150 + col * 50, 50 + row * 50, 200 + col * 50);
                    }

                    g2.setColor(Color.blue);
                    g2.fillPolygon(xPoints, yPoints, nPoints);
                    g2.fillPolygon(xPoints2, yPoints2, nPoints);
                    g2.setColor(Color.red);
                    g2.drawPolygon(xPoints, yPoints, nPoints);
                    g2.drawPolygon(xPoints2, yPoints2, nPoints);
                }
            }
        }

        for (int row = 0; row <= 8; row++) {
            for (int col = 0; col <= 8; col++) {
                if (((row == 0 || row == Cool && col == 0) || ((row == 0 | row == 1) && col == 1) || ((row == 7 || row == Cool && col == 1) || ((row == 0 || row == 1 || row == 7 || row == Cool && col == 7) || ((row == 0 || row == 1 || row == 8 || row == 7) && col == Cool) {
                } else if (col == 0 || col == 1
                        || (col == 2 && row == 2)
                        || (col == 2 && row == 3)
                        || (col == 2 && row == 4)
                        || (col == 2 && row == 5)
                        || (col == 2 && row == 6)) {

                    g2.setColor(Color.green);
                    g2.drawRect(40 + row * 50, 140 + col * 50, 20, 20);
                    g2.fillRect(40 + row * 50, 140 + col * 50, 20, 20);

                } else if (col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7 || col == Cool {
                    g2.setColor(Color.white);
                    g2.drawRect(40 + row * 50, 140 + col * 50, 20, 20);
                    g2.fillRect(40 + row * 50, 140 + col * 50, 20, 20);
                }

                if (boardPiece.pieceAt(row, col) == Piece.American) {
                    g.setColor(Color.black);
                    g.drawOval(42 + row * 50, 142 + col * 50, 15, 15);
                    g.setColor(Color.blue);
                    g.fillOval(42 + row * 50, 142 + col * 50, 15, 15);
                } else if (boardPiece.pieceAt(row, col) == Piece.Japanese) {
                    g.setColor(Color.black);
                    g.drawOval(42 + row * 50, 142 + col * 50, 15, 15);
                    g.setColor(Color.yellow);
                    g.fillOval(42 + row * 50, 142 + col * 50, 15, 15);
                }
            }
        }

        //g2.setStroke(new BasicStroke(3));
        if (inGameState){
            g.setColor(Color.blue);
            for (int i = 0; i < validMoves.length; i++) {
                g2.drawRect(38 + validMoves.currentRow * 50, 138 + validMoves.currentCol * 50, 24, 24);
            }

            if (rowSelected >= 0) {
                g.setColor(Color.yellow);
                g2.drawRect(38 + rowSelected * 50, 138 + colSelected * 50, 24, 24);
                g.setColor(Color.black);
                for (int i = 0; i < validMoves.length; i++) {
                    if (validMoves.currentCol == colSelected && validMoves.currentRow == rowSelected) {
                        g2.drawRect(38 + validMoves.nxtRow * 50, 138 + validMoves.nxtCol * 50, 24, 24);
                    }
                }
            }
        }
    }

    private static class BataanMoves {

        int currentRow, currentCol, nxtRow, nxtCol;

        public BataanMoves(int row1, int col1, int row2, int col2) {
            currentRow = row1;
            currentCol = col1;
            nxtRow = row2;
            nxtCol = col2;
        }

        boolean aJump() {
            return (currentRow - nxtRow == 2 || currentRow - nxtRow == -2 || currentCol - nxtCol == 2 || currentCol - nxtCol == -2);
        }
    }

    /**
     * An object of this class holds data about the game of Bataan: Battle of the Philippines.
     * It knows what kind of piece is on each square of the board.
     * Methods are provided to return lists of available legal moves.
     */
    private static class Piece {

        static final int American = 1, Japanese = 2, Empty = 0, NotPartofBoard = -1; //constants that hold the players and the empty slot in the board.
        int[][] myBoard; //array to store the contents of the board in specifis row and column.

        public Piece() { //contructor to save the pieces in the board and set it up for a new game.
            myBoard = new int[9][9];
            setGame();
        }

        /* Set the contents of the board with American and Japanese pieces in position
         * for the beggining of the game. Note that there are only 3 American pieces(blue)
         * and 50 Japanese pieces(yellow).
         */
        public void setGame() {
            for (int r = 0; r <= 8; r++) {
                for (int c = 0; c <= 8; c++) {
                    if(((r == 0 || r == 1 || r == 7 || r == Cool && c == 7) || ((r == 0 || r == 1 || r == 8 || r == 7) && c == Cool||((r == 0 || r == Cool && c == 0) || ((r == 0 || r == 1 || r == 8 || r == 7) && c == 1)){
                        myBoard[r][c] = NotPartofBoard;
                    }else if (c == 0 || c == 1 || (c == 2 && (r == 3 || r == 5))) {
                        myBoard[r][c] = Empty;
                    } else if (c == 2 && r == 2 || c == 2 && r == 4 || c == 2 && r == 6) {
                        myBoard[r][c] = American;
                    } else {
                        myBoard[r][c] = Japanese;
                    }
                }
            }

        }

        //returns the content of the specific pair of row and column in the board.
        int pieceAt(int Row, int Col) {
            return myBoard[Row][Col];
        }

        /**
         * Make the specified move.  It is assumed that move
         * is non-null and that the move it represents is legal.
         */
        void piecedoMove(BataanMoves move) {
            piecedoMove(move.currentRow, move.currentCol, move.nxtRow, move.nxtCol, pieceAt(move.currentRow, move.currentCol));
        }

        /**
         * Make the move from (currentRow,currentCol) to (nxtRow,nxtCol).  It is
         * assumed that this move is legal.  If the move is a jump, the
         * jumped piece is removed from the board. 
         */
        void piecedoMove(int currentRow, int currentCol, int nxtRow, int nxtCol, int player) {
           
            if (player == American) {     //check if the current player is the American, it can jump.
                int jumpedRow, jumpedCol;
                if (((nxtRow - currentRow == 2) && (currentCol == nxtCol))) {
                    jumpedCol = currentCol;
                    jumpedRow = nxtRow - 1;
                } else if (((nxtRow - currentRow == -2) && (currentCol == nxtCol))) {
                    jumpedCol = currentCol;
                    jumpedRow = nxtRow + 1;
                } else if ((nxtCol - currentCol == 2) && (currentRow == nxtRow)) {
                    jumpedCol = nxtCol - 1;
                    jumpedRow = currentRow;
                } else if ((nxtCol - currentCol == -2) && (currentRow == nxtRow)) {
                    jumpedCol = nxtCol + 1;
                    jumpedRow = currentRow;
                } else {
                    jumpedCol = (nxtCol + currentCol) / 2;
                    jumpedRow = (nxtRow + currentRow) / 2;
                }
                myBoard[jumpedRow][jumpedCol] = Empty;
           
            }else{
                myBoard[nxtRow][nxtCol] = myBoard[currentRow][currentCol];
                myBoard[currentRow][currentCol] = Empty;
            }
        }

        /**
         * This is called by the two getValidMoves() and getValidJumps() methods to check whether the
         * player can legally jump from (r1,c1) to (r3,c3).  It is assumed
         * that the player has a piece at (r1,c1), that (r3,c3) is a position
         * that is 2 rows and 2 columns distant from (r1,c1) and that
         * (r2,c2) is the square between (r1,c1) and (r3,c3).
         */
        private boolean pieceCanJump(int player, int r1, int c1, int r2, int c2, int r3, int c3) {
            //only the American piece can do a jump.
            if (player == American && myBoard[r1][c1] == American) {
                //(r3,c3) is out of the board.
                if (r3 < 0 || r3 >8 || c3 < 0 || c3 >8 || myBoard[r3][c3] == NotPartofBoard) {
                    return false;
                }
                //(r3,c3) contains a piece.
                if (myBoard[r3][c3] != Empty) {
                    return false;
                }
                //there is no Japanese piece to jump on.
                if (myBoard[r2][c2] != Japanese) {
                    return false;
                }

                return true; //jump is valid.

            } else {
                return false;
            }

        }

        /**
         * This is called by the getValidMoves() method to determine whether
         * the player can legally move from (r1,c1) to (r2,c2).  It is
         * assumed that (r1,r2) contains one of the player's pieces and
         * that (r2,c2) is a square beside it.
         */
        private boolean pieceCanMove(int player, int r1, int c1, int r2, int c2) {
            //(r2,c2) is out of the board.
            if (r2 < 0 || r2 > 8 || c2 < 0 || c2 > 8 || myBoard[r2][c2] == NotPartofBoard){
                return false;
            }
            // (r2,c2) already contains a piece.
            if (myBoard[r2][c2] != Empty) {
                return false;
            }
            //Japanese can only move downwards.
            if (player == Japanese) {
                if (myBoard[r1][c1] == Japanese && c2 <= c1) {
                    return true;
                } else {
                    return false;
                }
            } else {  //American can move in any available direction.
                if (myBoard[r1][c1] == American) {
                    return true;
                } else {
                    return false;
                }
            }

        }

        /**
         * Return an array containing all the legal BataanMoves
         * for the specified player on the current board.  If the player
         * has no legal moves, null is returned.  The value of player
         * should be one of the constants American and Japanese; if not, null
         * is returned.  If the returned value is non-null, it consists
         * entirely of jump moves if the player is an American or entirely of regular moves if Japanese, since
         * if the player can jump, only jumps are legal moves.
         */
        BataanMoves[] getValidMoves(int player) {
            //player is not an American or a Japanese.
            if (player != American && player != Japanese) {
                return null;
            }

            //array list to store the valid moves.
            ArrayList<BataanMoves> moves = new ArrayList<BataanMoves>();

            /* Check all squares on the board for any possible jumps for the current
             * player if the player is Japanese nothing will be stored. If the player is an American
             * it checks the eight possible directions from that square if a jump is available it is
             * stored in the array list.
             */
            for (int row = 0; row <= 8; row++) {
                for (int col = 0; col <= 8; col++) {
                    if (myBoard[row][col] == player) {
                        if(row%2 == col%2){
                           
                            if (pieceCanJump(player, row, col, row + 1, col + 1, row + 2, col + 2)) {
                                moves.add(new BataanMoves(row, col, row + 2, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col + 1, row - 2, col + 2)) {
                                moves.add(new BataanMoves(row, col, row - 2, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col - 1, row + 2, col - 2)) {
                                moves.add(new BataanMoves(row, col, row + 2, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col - 1, row - 2, col - 2)) {
                                moves.add(new BataanMoves(row, col, row - 2, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col - 1, row, col - 2)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col + 1, row, col + 2)) {
                                moves.add(new BataanMoves(row, col, row, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col, row - 2, col)) {
                                moves.add(new BataanMoves(row, col, row - 2, col));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col, row + 2, col)) {
                                moves.add(new BataanMoves(row, col, row + 2, col));
                            }
                       
                        }else{
                            if (pieceCanJump(player, row, col, row-1, col, row-2, col)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col - 1, row, col - 2)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col + 1, row, col + 2)) {
                                moves.add(new BataanMoves(row, col, row, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col, row + 2, col)) {
                                moves.add(new BataanMoves(row, col, row + 2, col));
                            }
                        }
                    }
                }
            }

            /*After checking if there are available jumps and the array list is still empty,
             * the second check is if there are valid normal move for the player,still every
             * square of the board is checked for all the valid moves in the players piece.
             */
            if (moves.isEmpty()) {
                for (int row = 0; row <= 8; row++) {
                    for (int col = 0; col <= 8; col++) {
                        if (myBoard[row][col] == player) {
                            if(row%2 == col%2){

                                if (pieceCanMove(player, row, col, row + 1, col)) {
                                    moves.add(new BataanMoves(row, col, row + 1, col));
                                }
                                if (pieceCanMove(player, row, col, row - 1, col)) {
                                    moves.add(new BataanMoves(row, col, row - 1, col));
                                }
                                if (pieceCanMove(player, row, col, row + 1, col + 1)) {
                                    moves.add(new BataanMoves(row, col, row + 1, col + 1));
                                }
                                if (pieceCanMove(player, row, col, row + 1, col - 1)) {
                                    moves.add(new BataanMoves(row, col, row + 1, col - 1));
                                }
                                if (pieceCanMove(player, row, col, row, col + 1)) {
                                    moves.add(new BataanMoves(row, col, row, col + 1));
                                }
                                if (pieceCanMove(player, row, col, row, col - 1)) {
                                    moves.add(new BataanMoves(row, col, row, col - 1));
                                }
                                if (pieceCanMove(player, row, col, row - 1, col + 1)) {
                                    moves.add(new BataanMoves(row, col, row - 1, col + 1));
                                }
                                if (pieceCanMove(player, row, col, row - 1, col - 1)) {
                                    moves.add(new BataanMoves(row, col, row - 1, col - 1));
                                }
                           
                            }else{
                                if (pieceCanMove(player, row, col, row - 1, col)) {
                                    moves.add(new BataanMoves(row, col, row, col - 2));
                                }
                                if (pieceCanMove(player, row, col, row, col - 1)) {
                                    moves.add(new BataanMoves(row, col, row, col - 2));
                                }
                                if (pieceCanMove(player, row, col, row, col + 1)) {
                                    moves.add(new BataanMoves(row, col, row, col + 2));
                                }
                                if (pieceCanMove(player, row, col, row + 1, col)) {
                                    moves.add(new BataanMoves(row, col, row + 2, col));
                                }
                           
                           }

                        }
                    }
                }
            }

            /*if still no legal moves are found return null else
             * copy the legal moves into another array list and return it.
             */
            if (moves.isEmpty()) {
                return null;
            } else {
                BataanMoves[] movesArray = new BataanMoves[moves.size()];
                for (int i = 0; i < moves.size(); i++) {
                    movesArray = moves.get(i);
                }
                return movesArray;
            }
        }

        /**
         * Return a list of the legal jumps that the specified player can
         * make starting from the specified row and column.  If no such
         * jumps are possible, null is returned.  The logic is similar
         * to the logic of the getLegalMoves() method.
         */
        BataanMoves[] getValidJumps(int player, int row, int col) {
            if (player != American) {
                return null;
            }

            ArrayList<BataanMoves> moves = new ArrayList<BataanMoves>();

            if (myBoard[row][col] == player) {
                if(row%2 == col%2){
                           
                            if (pieceCanJump(player, row, col, row + 1, col + 1, row + 2, col + 2)) {
                                moves.add(new BataanMoves(row, col, row + 2, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col + 1, row - 2, col + 2)) {
                                moves.add(new BataanMoves(row, col, row - 2, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col - 1, row + 2, col - 2)) {
                                moves.add(new BataanMoves(row, col, row + 2, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col - 1, row - 2, col - 2)) {
                                moves.add(new BataanMoves(row, col, row - 2, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col - 1, row, col - 2)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col + 1, row, col + 2)) {
                                moves.add(new BataanMoves(row, col, row, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col, row - 2, col)) {
                                moves.add(new BataanMoves(row, col, row - 2, col));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col, row + 2, col)) {
                                moves.add(new BataanMoves(row, col, row + 2, col));
                            }
                       
                        }else{
                            if (pieceCanJump(player, row, col, row-1, col, row-2, col)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col - 1, row, col - 2)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col + 1, row, col + 2)) {
                                moves.add(new BataanMoves(row, col, row, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col, row + 2, col)) {
                                moves.add(new BataanMoves(row, col, row + 2, col));
                            }
                        }
            }

            if (moves.isEmpty()) {
                return null;
            } else {
                BataanMoves[] movesArray = new BataanMoves[moves.size()];
                for (int i = 0; i < moves.size(); i++) {
                    movesArray = moves.get(i);
                }
                return movesArray;
            }
        }
    }
}
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2013-09-23 02:06:20 »

Sorry, didnt think about what intranet meant!
Please dont dump your code here without code tags. And for extra long code dumps like yours, host them on a code hosting site.
Again, we aren't going to spoon feed you code. Go look at the link and read everything you can find on google. Then come back with specific problems. People will be more willing to help!

Offline macx

Junior Newbie





« Reply #5 - Posted 2013-09-23 02:15:29 »

okay this what i'be done done so far, drawing the board and setting up the the pieces. Im having a dificulty on making a move when the user clicks a piece.


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Compendio
 */
public class Bataan extends JPanel implements MouseListener, ActionListener {

    private BufferedImage img;      // image for the background of the game interface.
    boolean inGameState;            // gives true or false if a game is in progress.
    int currentPlayer;              // the current player to move.
    int rowSelected, colSelected;   // the row and column of the selected piece of the current player.
    private JButton newGameButton;  // Button for starting a new game.
    private JButton quitButton;     // Button to quit a game.
    private JLabel message;         // Label for displaying messages to the user.
    int numberofJapanese;           // the number of Japanese pieces.
   
    /* instance of the class Piece, the pieces and the moves
     *  of the game are saved at set here.
     */
    Piece boardPiece;
   
   
    BataanMoves[] validMoves;       //array containing all the valid moves for the current player.

    /* The main method creates a new JFrame as the window
     * and sets the calss Bataan as it's content.
    */
    public static void main(String args[]) {
        JFrame window = new JFrame("Bataan: Battle of the Philippines");
        Bataan game = new Bataan();
        window.setSize(700, 700);
        window.add(game);
        window.setLocation(400, 20);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.setVisible(true);
    }

    /* The class contructor sets all the properties of the class
     * and initializes the background image.
    */
    public Bataan() {

        Font F = new Font("kristen ITC", Font.BOLD, 15);
        setLayout(null);
        addMouseListener(this);
        setVisible(true);
        setSize(700, 700);
        quitButton = new JButton("Quit");
        quitButton.addActionListener(this);
        newGameButton = new JButton("New Game");
        newGameButton.addActionListener(this);
        message = new JLabel("", JLabel.CENTER);
        message.setFont(F);
        message.setForeground(Color.white);
        boardPiece = new Piece();
        add(newGameButton);
        add(quitButton);
        add(message);
        newGameButton.setFont(F);
        quitButton.setFont(F);
        newGameButton.setBounds(100, 60, 120, 30);
        quitButton.setBounds(300, 60, 100, 30);
        message.setBounds(80, 600, 400, 30);
        try {
            img = ImageIO.read(new File(
                    "D:/Documents/NetBeansProjects/MyProjects/build/classes/Images/war2.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        quitButton.setEnabled(false);

    }
   
    //Method called when the user clicks one of the buttons.
    public void actionPerformed(ActionEvent evt) {
        Object src = evt.getSource();
        if (src == newGameButton) {
            NewGame();
        } else if (src == quitButton) {
            QuitGame();
        }
    }
   
    /*Sets a new game, the starting properties and paints the
     * board for a new game. Called when the user clicks the New Game button.
     */
    void NewGame() {
        numberofJapanese = 50;
        boardPiece.setGame();
        currentPlayer = Piece.Japanese;
        validMoves = boardPiece.getValidMoves(Piece.Japanese);
        rowSelected = -1;
        message.setText("Japanese: Make your move.");
        inGameState = true;
        newGameButton.setEnabled(false);
        quitButton.setEnabled(true);
        repaint();
    }

    // Called when the user clicks the Quit button.
    void QuitGame() {
        if (currentPlayer == Piece.Japanese) {
            GameisOver("Japanese quits, American wins!");
        } else {
            GameisOver("American quits, Japanese wins!");
        }
    }

    // Sets the properties when the game is over.
    void GameisOver(String str) {
        message.setText(str);
        newGameButton.setEnabled(true);
        quitButton.setEnabled(false);
        inGameState = false;
    }

    /**
     * Respond to a user click on the board.  If no game is in progress, show
     * an error message.  Otherwise, find the row and column that the user
     * clicked and call squareClicked() method to handle it.
     */
    public void mousePressed(MouseEvent e) {
        if (inGameState == false) {
            message.setText("Click the New Game button to start a game.");
        } else {
            int col = (e.getY() - 150) / 50;
            int row = (e.getX() - 50) / 50;
            if (col >= 0 && col <= 8 && row >= 0 && row <= Cool {
                squareClicked(row, col);
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }
   
    /*When a user clicks on the board, checks if it is a piece or
     * a square on the board then, it makes the move.
     */
    void squareClicked(int r, int c) {
        for (int i = 0; i < validMoves.length; i++)
            if (validMoves.currentRow == r && validMoves.currentCol == c) {
                rowSelected = r;
                colSelected = c;

                if (currentPlayer == Piece.American) {
                    message.setText("American: Make your move.");
                } else {
                    message.setText("Japanese: Make your move.");
                }
                repaint();
                return;
            }
       
        if (rowSelected < 0) {
            message.setText("Click the piece you want to move.");
            return;
        }

        for (int i = 0; i < validMoves.length; i++)
            if (validMoves.currentRow == rowSelected && validMoves.currentCol == colSelected
                    && validMoves.nxtRow == r && validMoves.nxtCol == c) {
                MakeMove(validMoves);
                return;
            }
       


        message.setText("Click the square you want to move to.");
    }

    void MakeMove(BataanMoves move) {
        boardPiece.piecedoMove(move);

        if (move.aJump()) {
            validMoves = boardPiece.getValidJumps(currentPlayer, move.nxtRow, move.nxtCol);
            if (validMoves != null) {
                message.setText("American: You must continue jumping.");
            }
            rowSelected = move.nxtRow;
            colSelected = move.nxtCol;
            repaint();
            return;
        }
        if (currentPlayer == Piece.Japanese) {
            currentPlayer = Piece.American;
            validMoves = boardPiece.getValidMoves(currentPlayer);
            if (validMoves[0].aJump()) {
                message.setText("American: Make your move, you must jump.");
            } else {
                message.setText("American: Make your move.");
            }
        } else {
            currentPlayer = Piece.Japanese;
            validMoves = boardPiece.getValidMoves(currentPlayer);
            message.setText("Japanese: Make your move.");
        }

        rowSelected = -1;
       
        if(validMoves != null){
            boolean sameSquare = true;
           
            for(int i=1; i<validMoves.length; i++){
                if(validMoves.currentRow != validMoves[0].currentRow || validMoves.currentCol != validMoves[0].currentCol){
                    sameSquare = false;
                    break;
                }
            }
           
            if(sameSquare){
                rowSelected = validMoves[0].currentRow;
                colSelected = validMoves[0].currentCol;
            }
           
        }
        repaint();
    }

    /* Draws the playing board for the game
     * ands add the right pieces to it.
     */
    public void paintComponent(Graphics g) {
        int[] xPoints = {100, 150, 150};
        int[] yPoints = {150, 200, 150};
        int[] xPoints2 = {350, 400, 350};
        int[] yPoints2 = {200, 150, 150};
        int nPoints = 3;
        Graphics2D g2 = (Graphics2D) g;

        g2.setStroke(new BasicStroke(5));
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawImage(img, 0, 0, getWidth(), getHeight(), this);


        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                if ((row == 0 && (col == 6 || col == 7))
                        || (row == 1 && (col == 6 || col == 7))
                        || (row == 6 && (col == 7 || col == 6))
                        || (row == 7 && (col == 6 || col == 7))
                        || (row == 0 && (col == 1 || col == 0))
                        || (row == 1 && (col == 1 || col == 0))
                        || (row == 6 && (col == 0 || col == 1))
                        || (row == 7 && (col == 1 || col == 0)))  {
                } else {

                    if (col == 0 || col == 1) {
                        g2.setColor(Color.blue);
                        g2.fillRect(50 + row * 50, 150 + col * 50, 50, 50);

                    } else {
                        g2.setColor(Color.gray);
                        g2.fillRect(50 + row * 50, 150 + col * 50, 50, 50);
                    }
                    g2.setColor(Color.red);
                    g2.drawRect(50 + row * 50, 150 + col * 50, 50, 50);

                    if (row % 2 == col % 2) {
                        g2.setColor(Color.red);
                        g2.drawLine(50 + row * 50, 150 + col * 50, 100 + row * 50, 200 + col * 50);

                    } else {
                        g2.setColor(Color.red);
                        g2.drawLine(100 + row * 50, 150 + col * 50, 50 + row * 50, 200 + col * 50);
                    }

                    g2.setColor(Color.blue);
                    g2.fillPolygon(xPoints, yPoints, nPoints);
                    g2.fillPolygon(xPoints2, yPoints2, nPoints);
                    g2.setColor(Color.red);
                    g2.drawPolygon(xPoints, yPoints, nPoints);
                    g2.drawPolygon(xPoints2, yPoints2, nPoints);
                }
            }
        }

        for (int row = 0; row <= 8; row++) {
            for (int col = 0; col <= 8; col++) {
                if (((row == 0 || row == Cool && col == 0) || ((row == 0 | row == 1) && col == 1) || ((row == 7 || row == Cool && col == 1) || ((row == 0 || row == 1 || row == 7 || row == Cool && col == 7) || ((row == 0 || row == 1 || row == 8 || row == 7) && col == Cool) {
                } else if (col == 0 || col == 1
                        || (col == 2 && row == 2)
                        || (col == 2 && row == 3)
                        || (col == 2 && row == 4)
                        || (col == 2 && row == 5)
                        || (col == 2 && row == 6)) {

                    g2.setColor(Color.green);
                    g2.drawRect(40 + row * 50, 140 + col * 50, 20, 20);
                    g2.fillRect(40 + row * 50, 140 + col * 50, 20, 20);

                } else if (col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7 || col == Cool {
                    g2.setColor(Color.white);
                    g2.drawRect(40 + row * 50, 140 + col * 50, 20, 20);
                    g2.fillRect(40 + row * 50, 140 + col * 50, 20, 20);
                }

                if (boardPiece.pieceAt(row, col) == Piece.American) {
                    g.setColor(Color.black);
                    g.drawOval(42 + row * 50, 142 + col * 50, 15, 15);
                    g.setColor(Color.blue);
                    g.fillOval(42 + row * 50, 142 + col * 50, 15, 15);
                } else if (boardPiece.pieceAt(row, col) == Piece.Japanese) {
                    g.setColor(Color.black);
                    g.drawOval(42 + row * 50, 142 + col * 50, 15, 15);
                    g.setColor(Color.yellow);
                    g.fillOval(42 + row * 50, 142 + col * 50, 15, 15);
                }
            }
        }

        //g2.setStroke(new BasicStroke(3));
        if (inGameState){
            g.setColor(Color.blue);
            for (int i = 0; i < validMoves.length; i++) {
                g2.drawRect(38 + validMoves.currentRow * 50, 138 + validMoves.currentCol * 50, 24, 24);
            }

            if (rowSelected >= 0) {
                g.setColor(Color.yellow);
                g2.drawRect(38 + rowSelected * 50, 138 + colSelected * 50, 24, 24);
                g.setColor(Color.black);
                for (int i = 0; i < validMoves.length; i++) {
                    if (validMoves.currentCol == colSelected && validMoves.currentRow == rowSelected) {
                        g2.drawRect(38 + validMoves.nxtRow * 50, 138 + validMoves.nxtCol * 50, 24, 24);
                    }
                }
            }
        }
    }

    private static class BataanMoves {

        int currentRow, currentCol, nxtRow, nxtCol;

        public BataanMoves(int row1, int col1, int row2, int col2) {
            currentRow = row1;
            currentCol = col1;
            nxtRow = row2;
            nxtCol = col2;
        }

        boolean aJump() {
            return (currentRow - nxtRow == 2 || currentRow - nxtRow == -2 || currentCol - nxtCol == 2 || currentCol - nxtCol == -2);
        }
    }

    /**
     * An object of this class holds data about the game of Bataan: Battle of the Philippines.
     * It knows what kind of piece is on each square of the board.
     * Methods are provided to return lists of available legal moves.
     */
    private static class Piece {

        static final int American = 1, Japanese = 2, Empty = 0, NotPartofBoard = -1; //constants that hold the players and the empty slot in the board.
        int[][] myBoard; //array to store the contents of the board in specifis row and column.

        public Piece() { //contructor to save the pieces in the board and set it up for a new game.
            myBoard = new int[9][9];
            setGame();
        }

        /* Set the contents of the board with American and Japanese pieces in position
         * for the beggining of the game. Note that there are only 3 American pieces(blue)
         * and 50 Japanese pieces(yellow).
         */
        public void setGame() {
            for (int r = 0; r <= 8; r++) {
                for (int c = 0; c <= 8; c++) {
                    if(((r == 0 || r == 1 || r == 7 || r == Cool && c == 7) || ((r == 0 || r == 1 || r == 8 || r == 7) && c == Cool||((r == 0 || r == Cool && c == 0) || ((r == 0 || r == 1 || r == 8 || r == 7) && c == 1)){
                        myBoard[r][c] = NotPartofBoard;
                    }else if (c == 0 || c == 1 || (c == 2 && (r == 3 || r == 5))) {
                        myBoard[r][c] = Empty;
                    } else if (c == 2 && r == 2 || c == 2 && r == 4 || c == 2 && r == 6) {
                        myBoard[r][c] = American;
                    } else {
                        myBoard[r][c] = Japanese;
                    }
                }
            }

        }

        //returns the content of the specific pair of row and column in the board.
        int pieceAt(int Row, int Col) {
            return myBoard[Row][Col];
        }

        /**
         * Make the specified move.  It is assumed that move
         * is non-null and that the move it represents is legal.
         */
        void piecedoMove(BataanMoves move) {
            piecedoMove(move.currentRow, move.currentCol, move.nxtRow, move.nxtCol, pieceAt(move.currentRow, move.currentCol));
        }

        /**
         * Make the move from (currentRow,currentCol) to (nxtRow,nxtCol).  It is
         * assumed that this move is legal.  If the move is a jump, the
         * jumped piece is removed from the board. 
         */
        void piecedoMove(int currentRow, int currentCol, int nxtRow, int nxtCol, int player) {
           
            if (player == American) {     //check if the current player is the American, it can jump.
                int jumpedRow, jumpedCol;
                if (((nxtRow - currentRow == 2) && (currentCol == nxtCol))) {
                    jumpedCol = currentCol;
                    jumpedRow = nxtRow - 1;
                } else if (((nxtRow - currentRow == -2) && (currentCol == nxtCol))) {
                    jumpedCol = currentCol;
                    jumpedRow = nxtRow + 1;
                } else if ((nxtCol - currentCol == 2) && (currentRow == nxtRow)) {
                    jumpedCol = nxtCol - 1;
                    jumpedRow = currentRow;
                } else if ((nxtCol - currentCol == -2) && (currentRow == nxtRow)) {
                    jumpedCol = nxtCol + 1;
                    jumpedRow = currentRow;
                } else {
                    jumpedCol = (nxtCol + currentCol) / 2;
                    jumpedRow = (nxtRow + currentRow) / 2;
                }
                myBoard[jumpedRow][jumpedCol] = Empty;
           
            }else{
                myBoard[nxtRow][nxtCol] = myBoard[currentRow][currentCol];
                myBoard[currentRow][currentCol] = Empty;
            }
        }

        /**
         * This is called by the two getValidMoves() and getValidJumps() methods to check whether the
         * player can legally jump from (r1,c1) to (r3,c3).  It is assumed
         * that the player has a piece at (r1,c1), that (r3,c3) is a position
         * that is 2 rows and 2 columns distant from (r1,c1) and that
         * (r2,c2) is the square between (r1,c1) and (r3,c3).
         */
        private boolean pieceCanJump(int player, int r1, int c1, int r2, int c2, int r3, int c3) {
            //only the American piece can do a jump.
            if (player == American && myBoard[r1][c1] == American) {
                //(r3,c3) is out of the board.
                if (r3 < 0 || r3 >8 || c3 < 0 || c3 >8 || myBoard[r3][c3] == NotPartofBoard) {
                    return false;
                }
                //(r3,c3) contains a piece.
                if (myBoard[r3][c3] != Empty) {
                    return false;
                }
                //there is no Japanese piece to jump on.
                if (myBoard[r2][c2] != Japanese) {
                    return false;
                }

                return true; //jump is valid.

            } else {
                return false;
            }

        }

        /**
         * This is called by the getValidMoves() method to determine whether
         * the player can legally move from (r1,c1) to (r2,c2).  It is
         * assumed that (r1,r2) contains one of the player's pieces and
         * that (r2,c2) is a square beside it.
         */
        private boolean pieceCanMove(int player, int r1, int c1, int r2, int c2) {
            //(r2,c2) is out of the board.
            if (r2 < 0 || r2 > 8 || c2 < 0 || c2 > 8 || myBoard[r2][c2] == NotPartofBoard){
                return false;
            }
            // (r2,c2) already contains a piece.
            if (myBoard[r2][c2] != Empty) {
                return false;
            }
            //Japanese can only move downwards.
            if (player == Japanese) {
                if (myBoard[r1][c1] == Japanese && c2 <= c1) {
                    return true;
                } else {
                    return false;
                }
            } else {  //American can move in any available direction.
                if (myBoard[r1][c1] == American) {
                    return true;
                } else {
                    return false;
                }
            }

        }

        /**
         * Return an array containing all the legal BataanMoves
         * for the specified player on the current board.  If the player
         * has no legal moves, null is returned.  The value of player
         * should be one of the constants American and Japanese; if not, null
         * is returned.  If the returned value is non-null, it consists
         * entirely of jump moves if the player is an American or entirely of regular moves if Japanese, since
         * if the player can jump, only jumps are legal moves.
         */
        BataanMoves[] getValidMoves(int player) {
            //player is not an American or a Japanese.
            if (player != American && player != Japanese) {
                return null;
            }

            //array list to store the valid moves.
            ArrayList<BataanMoves> moves = new ArrayList<BataanMoves>();

            /* Check all squares on the board for any possible jumps for the current
             * player if the player is Japanese nothing will be stored. If the player is an American
             * it checks the eight possible directions from that square if a jump is available it is
             * stored in the array list.
             */
            for (int row = 0; row <= 8; row++) {
                for (int col = 0; col <= 8; col++) {
                    if (myBoard[row][col] == player) {
                        if(row%2 == col%2){
                           
                            if (pieceCanJump(player, row, col, row + 1, col + 1, row + 2, col + 2)) {
                                moves.add(new BataanMoves(row, col, row + 2, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col + 1, row - 2, col + 2)) {
                                moves.add(new BataanMoves(row, col, row - 2, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col - 1, row + 2, col - 2)) {
                                moves.add(new BataanMoves(row, col, row + 2, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col - 1, row - 2, col - 2)) {
                                moves.add(new BataanMoves(row, col, row - 2, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col - 1, row, col - 2)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col + 1, row, col + 2)) {
                                moves.add(new BataanMoves(row, col, row, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col, row - 2, col)) {
                                moves.add(new BataanMoves(row, col, row - 2, col));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col, row + 2, col)) {
                                moves.add(new BataanMoves(row, col, row + 2, col));
                            }
                       
                        }else{
                            if (pieceCanJump(player, row, col, row-1, col, row-2, col)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col - 1, row, col - 2)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col + 1, row, col + 2)) {
                                moves.add(new BataanMoves(row, col, row, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col, row + 2, col)) {
                                moves.add(new BataanMoves(row, col, row + 2, col));
                            }
                        }
                    }
                }
            }

            /*After checking if there are available jumps and the array list is still empty,
             * the second check is if there are valid normal move for the player,still every
             * square of the board is checked for all the valid moves in the players piece.
             */
            if (moves.isEmpty()) {
                for (int row = 0; row <= 8; row++) {
                    for (int col = 0; col <= 8; col++) {
                        if (myBoard[row][col] == player) {
                            if(row%2 == col%2){

                                if (pieceCanMove(player, row, col, row + 1, col)) {
                                    moves.add(new BataanMoves(row, col, row + 1, col));
                                }
                                if (pieceCanMove(player, row, col, row - 1, col)) {
                                    moves.add(new BataanMoves(row, col, row - 1, col));
                                }
                                if (pieceCanMove(player, row, col, row + 1, col + 1)) {
                                    moves.add(new BataanMoves(row, col, row + 1, col + 1));
                                }
                                if (pieceCanMove(player, row, col, row + 1, col - 1)) {
                                    moves.add(new BataanMoves(row, col, row + 1, col - 1));
                                }
                                if (pieceCanMove(player, row, col, row, col + 1)) {
                                    moves.add(new BataanMoves(row, col, row, col + 1));
                                }
                                if (pieceCanMove(player, row, col, row, col - 1)) {
                                    moves.add(new BataanMoves(row, col, row, col - 1));
                                }
                                if (pieceCanMove(player, row, col, row - 1, col + 1)) {
                                    moves.add(new BataanMoves(row, col, row - 1, col + 1));
                                }
                                if (pieceCanMove(player, row, col, row - 1, col - 1)) {
                                    moves.add(new BataanMoves(row, col, row - 1, col - 1));
                                }
                           
                            }else{
                                if (pieceCanMove(player, row, col, row - 1, col)) {
                                    moves.add(new BataanMoves(row, col, row, col - 2));
                                }
                                if (pieceCanMove(player, row, col, row, col - 1)) {
                                    moves.add(new BataanMoves(row, col, row, col - 2));
                                }
                                if (pieceCanMove(player, row, col, row, col + 1)) {
                                    moves.add(new BataanMoves(row, col, row, col + 2));
                                }
                                if (pieceCanMove(player, row, col, row + 1, col)) {
                                    moves.add(new BataanMoves(row, col, row + 2, col));
                                }
                           
                           }

                        }
                    }
                }
            }

            /*if still no legal moves are found return null else
             * copy the legal moves into another array list and return it.
             */
            if (moves.isEmpty()) {
                return null;
            } else {
                BataanMoves[] movesArray = new BataanMoves[moves.size()];
                for (int i = 0; i < moves.size(); i++) {
                    movesArray = moves.get(i);
                }
                return movesArray;
            }
        }

        /**
         * Return a list of the legal jumps that the specified player can
         * make starting from the specified row and column.  If no such
         * jumps are possible, null is returned.  The logic is similar
         * to the logic of the getLegalMoves() method.
         */
        BataanMoves[] getValidJumps(int player, int row, int col) {
            if (player != American) {
                return null;
            }

            ArrayList<BataanMoves> moves = new ArrayList<BataanMoves>();

            if (myBoard[row][col] == player) {
                if(row%2 == col%2){
                           
                            if (pieceCanJump(player, row, col, row + 1, col + 1, row + 2, col + 2)) {
                                moves.add(new BataanMoves(row, col, row + 2, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col + 1, row - 2, col + 2)) {
                                moves.add(new BataanMoves(row, col, row - 2, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col - 1, row + 2, col - 2)) {
                                moves.add(new BataanMoves(row, col, row + 2, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col - 1, row - 2, col - 2)) {
                                moves.add(new BataanMoves(row, col, row - 2, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col - 1, row, col - 2)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col + 1, row, col + 2)) {
                                moves.add(new BataanMoves(row, col, row, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row - 1, col, row - 2, col)) {
                                moves.add(new BataanMoves(row, col, row - 2, col));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col, row + 2, col)) {
                                moves.add(new BataanMoves(row, col, row + 2, col));
                            }
                       
                        }else{
                            if (pieceCanJump(player, row, col, row-1, col, row-2, col)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col - 1, row, col - 2)) {
                                moves.add(new BataanMoves(row, col, row, col - 2));
                            }
                            if (pieceCanJump(player, row, col, row, col + 1, row, col + 2)) {
                                moves.add(new BataanMoves(row, col, row, col + 2));
                            }
                            if (pieceCanJump(player, row, col, row + 1, col, row + 2, col)) {
                                moves.add(new BataanMoves(row, col, row + 2, col));
                            }
                        }
            }

            if (moves.isEmpty()) {
                return null;
            } else {
                BataanMoves[] movesArray = new BataanMoves[moves.size()];
                for (int i = 0; i < moves.size(); i++) {
                    movesArray = moves.get(i);
                }
                return movesArray;
            }
        }
    }
}

Offline SHC
« Reply #6 - Posted 2013-09-23 02:28:26 »

Please format before you post. If you had code, use the [code] tags. For example

[code]
public void bar()
{
    System.out.println("bar");
}
[/code]

Renders as

1  
2  
3  
4  
public void bar()
{
    System.out.println("bar");
}

If you are having large code dumps, use the pastebin. Also try to post an SSCCE (Short, Self Contained, Correct (Compilable), Example)

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (9 views)
2014-08-31 22:59:12

TehJavaDev (22 views)
2014-08-28 18:26:30

CopyableCougar4 (27 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (34 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (71 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!