Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: YO MY CODE NOT WORKING BROS

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default YO MY CODE NOT WORKING BROS

    SO IM LIKE MAKING AN OTHELLO GAME AND I NEED TO CHECK WHY MY DIAGONAL FLANK (TOP LEFT) IS NOT WORKING

    /**
    * Othello.java
    *
    * This class represents a Othello (TM)
    * game, which allows two players to place
    * pieces onto a board. Each move can
    * result in outflanking 0 or more opponent's
    * piece.
    */

    public class Othello
    {

    /* constants */
    final int MAXGAME; // the number of games a player needs to win to win the match
    final int NUMPLAYER; // number of players in the game
    final int NUMROW; // number of rows in the game board
    final int NUMCOL; // number of columns in the game board
    final int EMPTY = -1; // represents an empty square on the game board
    final int PLAYER1 = 0; // identification of player 1
    final int PLAYER2 = 1; // identification of player 2

    OthelloGUI gui;
    int numMove;
    int curPlayer;
    int board[][];
    int score[];

    /**
    * Constructor: Othello
    */
    public Othello(OthelloGUI gui)
    {
    this.gui = gui;
    NUMPLAYER = gui.NUMPLAYER;
    NUMROW = gui.NUMROW;
    NUMCOL = gui.NUMCOL;
    MAXGAME = gui.MAXGAME;

    board = new int [NUMROW][NUMCOL];
    curPlayer = PLAYER1;

    for (int i = 0; i < NUMROW; i++)
    {
    for (int j = 0; j < NUMCOL; j++)
    {
    board [i][j] = EMPTY;
    }
    }

    initBoard ();

    // TO DO: creation of arrays, and initialization of variables should be added here
    }

    /**
    * play
    * This method will be called when a square is clicked. Parameter "row" and "column" is
    * the location of the square that is clicked by the user
    */



    public void play (int row, int column)
    {
    if (validMove(row,column) == true)
    {
    gui.setPiece (row,column,curPlayer);
    board [row][column] = curPlayer;
    outFlankHori (row,column);
    outFlankVert (row,column);
    gui.showOutflankMessage(curPlayer,outFlankDiag (row,column));

    if (curPlayer == PLAYER1)
    {
    curPlayer = PLAYER2;
    }
    else
    {
    curPlayer = PLAYER1;
    }
    gui.setNextPlayer(curPlayer);
    }
    } // TO DO: implement the logic of the game
    private void initBoard ()
    {
    gui.setPiece (3,3,PLAYER1);
    board [3][3] = PLAYER1;
    gui.setPiece (4,4,PLAYER1);
    board [4][4] = PLAYER1;
    gui.setPiece (3,4,PLAYER2);
    board [3][4] = PLAYER2;
    gui.setPiece (4,3,PLAYER2);
    board [4][3] = PLAYER2;
    }
    public boolean validMove (int row, int column)
    {
    boolean valid = false;

    if (board [row][column] == EMPTY)
    {
    for (int i = row - 1; i <= row + 1; i++)
    {
    for (int j = column - 1; j <= column + 1; j++)
    {
    if ( i >= 0 && i < NUMROW && j >= 0 && j < NUMCOL)
    {
    if (board [i][j] != EMPTY)
    {
    valid = true;
    }
    }
    }
    }
    }
    return valid;
    }

    private int outFlankHori (int row, int column)
    {
    int flipped = 0;
    int flippedLeft = 0;
    int flippedRight = 0;
    boolean checkLeft = false;
    boolean checkRight = false;

    if (column != 0)
    {
    if (board [row][column - 1] != EMPTY && board [row][column - 1] != curPlayer)
    {
    for (int i = 1; column - i >= 0 && board [row][column - i] != EMPTY; i++)
    {
    if (board [row][column - i] == curPlayer && checkLeft == false)
    {
    checkLeft = true;
    flippedLeft = i;
    }
    }
    }
    }


    if (column != 7)
    {
    if (board [row][column + 1] != EMPTY && board [row][column + 1] != curPlayer)
    {
    for (int i = 1; column + i <= 7 && board [row][column + i] != EMPTY; i++)
    {
    if (board [row][column + i] == curPlayer && checkRight == false)
    {
    checkRight = true;
    flippedRight = i;
    }
    }
    }
    }

    if (checkLeft == true)
    {
    for (int i = 1; i <= flippedLeft; i++)
    {
    gui.setPiece (row,column - i,curPlayer);
    board [row][column - i] = curPlayer;
    }
    }

    if (checkRight == true)
    {
    for (int i = 1; i <= flippedRight; i++)
    {
    gui.setPiece (row,column + i,curPlayer);
    board [row][column + i] = curPlayer;
    }
    }
    flipped = flippedLeft + flippedRight;
    return flipped;
    }
    private int outFlankVert (int row, int column)
    {
    int flipped = 0;
    int flippedUp = 0;
    int flippedDown = 0;
    boolean checkUp = false;
    boolean checkDown = false;

    if (row != 0)
    {
    if (board [row - 1][column] != EMPTY && board [row - 1][column] != curPlayer)
    {
    for (int i = 1; row - i >= 0 && board [row - i][column] != EMPTY; i++)
    {
    if (board [row - i][column] == curPlayer && checkUp == false)
    {
    checkUp = true;
    flippedUp = i;
    }
    }
    }
    }

    if (row != 7)
    {
    if (board [row + 1][column] != EMPTY && board [row + 1][column] != curPlayer)
    {
    for (int i = 1; row + i <= 7 && board [row + i][column] != EMPTY; i++)
    {

    if (board [row + i][column] == curPlayer && checkDown == false)
    {
    checkDown = true;
    flippedDown = i;
    }
    }
    }
    }

    if (checkUp == true)
    {
    for (int i = 1; i <= flippedUp; i++)
    {
    gui.setPiece (row - i,column,curPlayer);
    board [row - i][column] = curPlayer;
    }
    }

    if (checkDown == true)
    {
    for (int i = 1; i <= flippedDown; i++)
    {
    gui.setPiece (row + i,column,curPlayer);
    board [row + i][column] = curPlayer;
    }
    }
    flipped = flippedUp + flippedDown;
    return flipped;
    }
    private int outFlankDiag (int row, int column) right here
    {
    int flipped = 0;
    int flippedUpLeft = 0;
    int flippedUpRight = 0;
    int flippedDownLeft = 0;
    int flippedDownRight = 0;
    boolean checkUpLeft = false;
    boolean checkUpRight = false;
    boolean checkDownLeft = false;
    boolean checkDownRight = false;


    if (row != 0 && column != 7)
    {
    if (board [row - 1][column + 1] != EMPTY && board [row - 1][column + 1] != curPlayer)
    {
    for (int i = 1; row - i >= 0 && column + i <= 7 && board [row - i][column + i] != EMPTY; i++)
    {
    if (board [row - i][column + i] == curPlayer && checkUpRight == false)
    {
    checkUpRight = true;
    flippedUpRight = i;
    }
    }
    }
    }

    if (row != 7 && column !=0)
    {
    if (board [row + 1][column - 1] != EMPTY && board [row + 1][column - 1] != curPlayer)
    {
    for (int i = 1; row + i <= 7 && column - i >= 0 && board [row + i][column - i] != EMPTY; i++)
    {
    if (board [row + i][column - i] == curPlayer && checkDownLeft == false)
    {
    checkDownLeft = true;
    flippedDownLeft = i;
    }
    }
    }
    }

    if (row != 0 && column != 0) this one
    {
    if (board [row - 1][column - 1] != EMPTY && board [row - 1][column - 1] != curPlayer)
    {
    for (int i = 1; row - i >= 0 && column - i >= 0 && board [row - i][column - i] != EMPTY; i++)
    {
    if (board [row - i][column - 1] == curPlayer && checkUpLeft == false)
    {
    checkUpLeft = true;
    flippedUpLeft = i;
    }
    }
    }
    }

    if (row != 7 && column != 7)
    {
    if (board [row + 1][column + 1] != EMPTY && board [row + 1][column + 1] != curPlayer)
    {
    for (int i = 1; row + i <= 7 && column + i <= 7 && board [row + i][column + i] != EMPTY; i++)
    {
    if (board [row + i][column + i] == curPlayer && checkDownRight == false)
    {
    checkDownRight = true;
    flippedDownRight = i;
    }
    }
    }
    }

    for (int i = 1; i < flippedUpLeft; i++)
    {
    gui.setPiece (row - i,column - i,curPlayer);
    board [row - i][column - i] = curPlayer;
    }

    if (checkUpRight == true)
    {
    for (int i = 1; i < flippedUpRight; i++)
    {
    gui.setPiece (row - i,column + i,curPlayer);
    board [row - i][column + i] = curPlayer;
    }
    }

    if (checkDownLeft == true)
    {
    for (int i = 1; i < flippedDownLeft; i++)
    {
    gui.setPiece (row + i,column - i,curPlayer);
    board [row + i][column - i] = curPlayer;
    }
    }

    if (checkDownRight == true)
    {
    for (int i = 1; i < flippedDownRight; i++)
    {
    gui.setPiece (row + i,column + i,curPlayer);
    board [row + i][column + i] = curPlayer;
    }
    }
    flipped = flippedUpLeft + flippedUpRight + flippedDownLeft + flippedDownRight;
    return flipped;
    }
    }


    PLEASE SEND HELP EVERY OTHER CODE FOR FLANK WORKS

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: YO MY CODE NOT WORKING BROS

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    WHY MY DIAGONAL FLANK (TOP LEFT) IS NOT WORKING
    Can you post the program's output that shows the program? Be sure to add some comments to the code saying what is wrong with it.

Similar Threads

  1. help i cant get my code working
    By alexrox27 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 23rd, 2013, 11:27 AM
  2. Code not working
    By Petrejonn in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 23rd, 2013, 02:12 PM
  3. Please why is this code not working
    By Petrejonn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2013, 10:18 AM
  4. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  5. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: March 19th, 2012, 07:00 PM