Advertisement
Guest User

valid reversi player

a guest
Nov 28th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. package cz.cvut.agents.rph.reversi.players.nguyet31;
  2.  
  3. import cz.cvut.agents.rph.reversi.ReversiMove;
  4. import cz.cvut.agents.rph.reversi.ReversiPlayer;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7.  
  8.  
  9. public class MyPlayer extends ReversiPlayer {
  10.  
  11. @Override
  12. public String getName() {
  13. return "nguyet31";
  14. }
  15.  
  16. @Override
  17. public ReversiMove makeNextMove(int[][] board) {
  18.  
  19. for (int i = 0; i < this.height; i++) {
  20. for (int j = 0; j < this.width; j++) {
  21. if (board[i][j] == getEmptySquareColor()) {
  22. if (isValidMove(board, i, j)) {
  23. return new ReversiMove(i, j);
  24. }
  25. }
  26. }
  27. }
  28. return new ReversiMove(0, 0);
  29. }
  30. public int biggerNumber() {
  31. if (this.height > this.width) return this.height;
  32. else return this.width;
  33. }
  34.  
  35. public int getColor(int[][] board, int x, int y) {
  36. if ((x < 0) || (y < 0) || (x > board.length-1) || (y > board.length-1)) {
  37. return getEmptySquareColor();
  38. } else {
  39. return board[x][y];
  40. }
  41.  
  42. }
  43.  
  44.  
  45. public boolean isValidMove(int[][] board, int x, int y) {
  46. int[][] directions = new int[8][biggerNumber()]; //8 směrů, počet polí
  47. for (int i = 0; i < 8; i++) {
  48. Arrays.fill(directions[i], getEmptySquareColor()); // naplnim to prázdnotou
  49. }
  50.  
  51. for (int i = 0; i < biggerNumber(); i++) {
  52. directions[0][i] = getColor(board, x + i, y);
  53. directions[1][i] = getColor(board, x + i, y + i);
  54. directions[2][i] = getColor(board, x + i, y - i);
  55. directions[3][i] = getColor(board, x - i, y + i);
  56. directions[4][i] = getColor(board, x - i, y - i);
  57. directions[5][i] = getColor(board, x - i, y);
  58. directions[6][i] = getColor(board, x, y - i);
  59. directions[7][i] = getColor(board, x, y + i);
  60. }
  61. for (int i = 0; i < 8; i++) {
  62. if (directions[i][1] == getOpponentColor()) {
  63. for (int j = 1; j < biggerNumber(); j++) {
  64. if (directions[i][j] != getOpponentColor()) {
  65. if (directions[i][j] == this.getMyColor()) {
  66. return true;
  67. } else {
  68. break;
  69. }
  70. }
  71. }
  72.  
  73. }
  74.  
  75. }
  76. return false;
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement