public Board(int width, int height, Player player1, Player player2) { //check minimal board size if (width < 3) width = 3; if (height < 3) height = 3; //initialize properties this.width = width; this.height = height; fields = new Field[width, height]; this.player1 = player1; this.player2 = player2; curPlayer = player1; //player1 begins //initialize empty fields for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) fields[x, y] = new Field(); } //initialize starting position int midX = width / 2; int midY = height / 2; fields[midX, midY] = new Field(player1); fields[midX - 1, midY - 1] = new Field(player1); fields[midX - 1, midY] = new Field(player2); fields[midX, midY - 1] = new Field(player2); }
//player clicked on a field public ClickStatus FieldClicked(int x, int y) { if (!IsInBounds(x, y)) return ClickStatus.InvalidMove; if (!IsValidMove(curPlayer, x, y)) return ClickStatus.InvalidMove; //handle the valid move fields[x, y] = new Field(curPlayer); fieldChanged(curPlayer, x, y); return switchPlayer(); }
//a tile 'moved' to (x,y) from (?,?) => recolor fields private void fieldChanged(Player player, int x, int y) { for (int dx = -1; dx < 2; dx++) //go over all directions { for (int dy = -1; dy < 2; dy++) { //own fields while there is a field of ours in the current direction for (int xi = x, yi = y; isFieldAt(player, xi, yi, dx, dy); xi += dx, yi += dy) fields[xi, yi] = new Field(player); } } }