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: Player play incorrect move in Reversi

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Player play incorrect move in Reversi

    I'm trying to create a player for Reversi game, but I have a little problem, after playing x moves the player (which until that played correctly) sends incorrect move at x = 0, y = 1, which is at that moment already filled, it sends this move (after different numbers of moves) in every game, for help in finding error that causes it I will be grateful

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    import cz.cvut.agents.rph.reversi.ReversiMove;
    import cz.cvut.agents.rph.reversi.ReversiPlayer;
     
    /**
     *
     * @author Martin Vajdl
     */
    public class PlayerVajdlmic extends ReversiPlayer {
     
     
      public int coordinateX;
      public int coordinateY;
      public int[][] board;
      public int line = 0;
      public int neighborX;
      public int neighborY;
     
        @Override
      public String getName()
      {
        return "vajdlmic";
      }
     
      protected void startGame(int myColor, int opponentColor, int[][] playground)
      {
        this.width = board.length;
        this.height = board[0].length;
      }
     
        @Override
      public ReversiMove makeNextMove(int[][] board) {
        int[][] CORNER = { { 0, 0 }, { 0, this.height - 1 }, { this.width - 1, 0 }, { this.width - 1, this.height - 1 } };
     
        for (int i = 0; i < CORNER.length; i++)
        {
          this.coordinateX = CORNER[i][0];
          this.coordinateY = CORNER[i][1];
     
          if (correctMove(opponentColor, board) == true)
          {
     
              return new ReversiMove(this.coordinateX, this.coordinateY);
     
     
          }
     
        }
     
        int[][] PROFITABLE = { { 2, 0 }, { this.width - 3, 0 }, { 0, 2 }, { this.width - 1, 2 }, { 0, this.height - 3 }, { this.width - 1, this.height - 3 }, { 2, this.height - 1 }, { this.width - 3, this.height - 1 } };
     
        for (int i = 0; i < PROFITABLE.length; i++)
        {
          this.coordinateX = PROFITABLE[i][0];
          this.coordinateY = PROFITABLE[i][1];
     
          if (correctMove(opponentColor, board) == true)
          {
            return new ReversiMove(this.coordinateX, this.coordinateY);
          }
     
        }
     
        if ((this.width > 4) && (this.height > 4)) {
          int[][] MIDDLE = new int[(this.width - 4) * (this.width - 4)][(this.width - 4) * (this.width - 4)];
     
          MIDDLE[0][0] = 2;
          MIDDLE[0][1] = 2;
          MIDDLE[((this.width - 4) * (this.width - 4) - 1)][0] = (this.width - 3);
          MIDDLE[((this.width - 4) * (this.width - 4) - 1)][1] = (this.width - 3);
     
          this.line = 0;
          for (int i = 0; i < (this.width - 4) * (this.width - 4) - 1; i++) {
            this.line += 1;
     
            if (this.line == this.width - 3) {
              MIDDLE[i][0] += 1;
              MIDDLE[i][1] = 2;
              this.line = 1;
            }
     
            MIDDLE[(i + 1)][0] = MIDDLE[i][0];
            MIDDLE[(i + 1)][1] = (MIDDLE[i][1] + 1);
          }
     
          for (int i = 0; i < MIDDLE.length; i++)
          {
            this.coordinateX = MIDDLE[i][0];
            this.coordinateY = MIDDLE[i][1];
     
            if (correctMove(opponentColor, board) == true)
            {
     
                return new ReversiMove(this.coordinateX, this.coordinateY);
     
            }
     
          }
     
        }
     
        if (this.width > 6) {
          int[][] BORDER = new int[(this.width - 6) * 4][(this.width - 6) * 4];
     
          BORDER[0][0] = 3;
          BORDER[0][1] = 0;
          BORDER[((this.width - 6) * 2 - 1)][0] = (this.width - 4);
          BORDER[((this.width - 6) * 2 - 1)][1] = (this.width - 1);
          BORDER[((this.width - 6) * 2)][0] = 0;
          BORDER[((this.width - 6) * 2)][1] = 3;
          BORDER[((this.width - 6) * 4 - 1)][0] = (this.width - 1);
          BORDER[((this.width - 6) * 4 - 1)][1] = (this.width - 4);
          this.line = 0;
     
          for (int i = 0; i < (this.width - 6) * 2 - 1; i++)
          {
            this.line += 1;
     
            if (this.line == this.width - 5) {
              BORDER[i][0] = 3;
              BORDER[i][1] = (this.width - 1);
              this.line = 1;
            }
            BORDER[(i + 1)][0] = (BORDER[i][0] + 1);
            BORDER[(i + 1)][1] = BORDER[i][1];
          }
     
          this.line = 0;
          for (int i = (this.width - 6) * 2; i < (this.width - 6) * 4 - 1; i++)
          {
            this.line += 1;
     
            if (this.line == this.width - 5) {
              BORDER[i][1] = 3;
              BORDER[i][0] = (this.width - 1);
              this.line = 1;
            }
            BORDER[(i + 1)][1] = (BORDER[i][1] + 1);
            BORDER[(i + 1)][0] = BORDER[i][0];
          }
     
          for (int i = 0; i < BORDER.length; i++)
          {
            this.coordinateX = BORDER[i][0];
            this.coordinateY = BORDER[i][1];
     
            if (correctMove(opponentColor, board) == true)
            {
              return new ReversiMove(this.coordinateX, this.coordinateY);
            }
          }
     
        }
     
        if ((this.width > 4) && (this.height > 4))
        {
          int[][] NEXTBORDER = new int[(this.width - 4) * 4][(this.width - 4) * 4];
     
          NEXTBORDER[0][0] = 2;
          NEXTBORDER[0][1] = 1;
          NEXTBORDER[((this.width - 4) * 2 - 1)][0] = (this.width - 3);
          NEXTBORDER[((this.width - 4) * 2 - 1)][1] = (this.width - 2);
          NEXTBORDER[((this.width - 4) * 2)][0] = 1;
          NEXTBORDER[((this.width - 4) * 2)][1] = 2;
          NEXTBORDER[((this.width - 4) * 4 - 1)][0] = (this.width - 2);
          NEXTBORDER[((this.width - 4) * 4 - 1)][1] = (this.width - 3);
          this.line = 0;
     
          for (int i = 0; i < (this.width - 4) * 2 - 1; i++)
          {
            this.line += 1;
     
            if (this.line == this.width - 3) {
              NEXTBORDER[i][0] = 2;
              NEXTBORDER[i][1] = (this.width - 2);
              this.line = 1;
            }
            NEXTBORDER[(i + 1)][0] = (NEXTBORDER[i][0] + 1);
            NEXTBORDER[(i + 1)][1] = NEXTBORDER[i][1];
          }
     
          this.line = 0;
          for (int i = (this.width - 4) * 2; i < (this.width - 4) * 4 - 1; i++)
          {
            this.line += 1;
     
            if (this.line == this.width - 3) {
              NEXTBORDER[i][1] = 2;
              NEXTBORDER[i][0] = (this.width - 2);
              this.line = 1;
            }
            NEXTBORDER[(i + 1)][1] = (NEXTBORDER[i][1] + 1);
            NEXTBORDER[(i + 1)][0] = NEXTBORDER[i][0];
          }
     
          for (int i = 0; i < NEXTBORDER.length; i++)
          {
            this.coordinateX = NEXTBORDER[i][0];
            this.coordinateY = NEXTBORDER[i][1];
     
            if (correctMove(opponentColor, board) == true)
            {
              return new ReversiMove(this.coordinateX, this.coordinateY);
            }
     
          }
     
        }
     
        int[][] NEXTCORNER = { { 0, 1 }, { 1, 0 }, { 0, this.height - 2 }, { this.width - 2, 0 }, { 1, this.height - 1 }, { this.width - 1, 1 }, { this.width - 2, this.height - 1 }, { this.width - 1, this.height - 2 } };
     
        for (int i = 0; i < NEXTCORNER.length; i++)
        {
          this.coordinateX = NEXTCORNER[i][0];
          this.coordinateY = NEXTCORNER[i][1];
     
     
          {
            return new ReversiMove(this.coordinateX, this.coordinateY);
          }
     
        }
     
        int[][] BAD = { { 1, 1 }, { 1, this.height - 2 }, { this.width - 2, 1 }, { this.width - 2, this.height - 2 } };
     
        for (int i = 0; i < BAD.length; i++)
        {
          this.coordinateX = BAD[i][0];
          this.coordinateY = BAD[i][1];
     
          if (correctMove(opponentColor, board) == true)
          {
            return new ReversiMove(this.coordinateX, this.coordinateY);
          }
        }
        return new ReversiMove(this.coordinateX, this.coordinateY);
      }
     
      private boolean correctMove(int opponentColor, int[][] board)
      {
        if (board[this.coordinateX][this.coordinateY] != -1) {
          return false;
        }
     
        int[][] neighbori = { { -1, -1 }, { 0, -1 }, { 1, -1 }, { -1, 0 }, { 1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 } };
     
        for (int[] neighbor : neighbori)
        {
          this.neighborX = (this.coordinateX + neighbor[0]);
          this.neighborY = (this.coordinateY + neighbor[1]);
     
          if (onBoard(this.neighborX, this.neighborY, board))
          {
            if (board[this.neighborX][this.neighborY] == opponentColor)
            {
              if (tryCorrect(neighbor, board))
              {
              return true;
              }
            }
          }
     
        }
     
        return false;
      }
     
      private boolean tryCorrect(int[] neighbor, int[][] board) {
        int newX = this.coordinateX + neighbor[0];
        int newY = this.coordinateY + neighbor[1];
        do
        {
          newX += neighbor[0];
          newY += neighbor[1];
          if (!onBoard(newX, newY, board)) {
            return false;
          }
     
          if (board[newX][newY] == -1) {
                return false;
            }
        }
        while (board[newX][newY] != this.myColor);
        return true;
      }
     
      private boolean onBoard(int x, int y, int[][] board)
      {
        if ((x < 0) || (x >= board.length)) {
          return false;
        }
        if ((y < 0) || (y >= board[0].length)) {
          return false;
        }
        return true;
      }
    }

    .jar file with game and Dummyplayer opponent - http://cw.felk.cvut.cz/lib/exe/fetch...eversi.jar.zip


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Player play incorrect move in Reversi

    Try debugging the code by adding calls to the println method that prints out the values of the variables that control what the program does. The print out will show you what the computer sees and help you find where the program doesn't do what you want it to do.

    If you need further help, please post the code for a small complete program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] is there any good formula's for 2d java game - to make ur player move right or left?
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 23rd, 2012, 01:42 AM
  2. Player wont move
    By jokr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 25th, 2012, 09:46 AM
  3. Replies: 0
    Last Post: May 22nd, 2012, 09:08 AM
  4. java game, both players move for player 2 but not player 1
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 21st, 2011, 12:52 PM