Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.cvut.agents.rph.reversi.players.nguyet31;
- import cz.cvut.agents.rph.reversi.ReversiMove;
- import cz.cvut.agents.rph.reversi.ReversiPlayer;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class MyPlayer extends ReversiPlayer {
- @Override
- public String getName() {
- return "nguyet31";
- }
- @Override
- public ReversiMove makeNextMove(int[][] board) {
- for (int i = 0; i < this.height; i++) {
- for (int j = 0; j < this.width; j++) {
- if (board[i][j] == getEmptySquareColor()) {
- if (isValidMove(board, i, j)) {
- return new ReversiMove(i, j);
- }
- }
- }
- }
- return new ReversiMove(0, 0);
- }
- public int biggerNumber() {
- if (this.height > this.width) return this.height;
- else return this.width;
- }
- public int getColor(int[][] board, int x, int y) {
- if ((x < 0) || (y < 0) || (x > board.length-1) || (y > board.length-1)) {
- return getEmptySquareColor();
- } else {
- return board[x][y];
- }
- }
- public boolean isValidMove(int[][] board, int x, int y) {
- int[][] directions = new int[8][biggerNumber()]; //8 směrů, počet polí
- for (int i = 0; i < 8; i++) {
- Arrays.fill(directions[i], getEmptySquareColor()); // naplnim to prázdnotou
- }
- for (int i = 0; i < biggerNumber(); i++) {
- directions[0][i] = getColor(board, x + i, y);
- directions[1][i] = getColor(board, x + i, y + i);
- directions[2][i] = getColor(board, x + i, y - i);
- directions[3][i] = getColor(board, x - i, y + i);
- directions[4][i] = getColor(board, x - i, y - i);
- directions[5][i] = getColor(board, x - i, y);
- directions[6][i] = getColor(board, x, y - i);
- directions[7][i] = getColor(board, x, y + i);
- }
- for (int i = 0; i < 8; i++) {
- if (directions[i][1] == getOpponentColor()) {
- for (int j = 1; j < biggerNumber(); j++) {
- if (directions[i][j] != getOpponentColor()) {
- if (directions[i][j] == this.getMyColor()) {
- return true;
- } else {
- break;
- }
- }
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement