Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jp.starfree.cpsv;
- /*
- * OthelloFieldClass
- * @author CreeperSaviour
- * @version 0.0.1
- */
- public class OthelloField extends Field {
- private int width, height;
- private final char[][] field;
- private final char black = '●', white = '○', space = ' ', wall ='■';
- private int blacks = 0, whites = 0;
- private boolean move = true;
- /*
- * 10 * 10 のFieldをインスタンス化する
- * @since 0.0.1
- */
- OthelloField() {
- this.width = 10;
- this.height = 10;
- this.field = new char[this.width][this.height];
- for(var i = 0; i < this.width; i += 1) {
- for(var j = 0; j < this.height; j += 1) {
- if(i == 0 || i == this.width - 1 || j == 0 || j == this.height - 1) this.field[i][j] = wall;
- else if((i == (this.width - 1) / 2 && j == (this.height - 1) / 2) || (i == (this.width + 1) / 2 && j == (this.height + 1) / 2) ) this.field[i][j] = white;
- else if((i == (this.width + 1) / 2 && j == (this.height - 1) / 2) || (i == (this.width - 1) / 2 && j == (this.height + 1) / 2) ) this.field[i][j] = black;
- else this.field[i][j] = space;
- }
- }
- }
- /*
- * width * height のFieldをインスタンス化する
- * @since 0.0.1
- * @param width 幅
- * 壁も含める
- * @param height 高さ
- * 壁も含める
- */
- OthelloField(int width, int height) {
- if(width != height) {
- try { throw new Exception("Failed Instantiating.");
- } catch (Exception e) { }
- System.exit(-1);
- }
- this.width = width;
- this.height = height;
- this.field = new char[this.width][this.height];
- for(var i = 0; i < this.width; i += 1) {
- for(var j = 0; j < this.height; j += 1) {
- if(i == 0 || i == this.width - 1 || j == 0 || j == this.height - 1) this.field[i][j] = wall;
- else if((i == (this.width - 1) / 2 && j == (this.height - 1) / 2) || (i == (this.width + 1) / 2 && j == (this.height + 1) / 2) ) this.field[i][j] = white;
- else if((i == (this.width + 1) / 2 && j == (this.height - 1) / 2) || (i == (this.width - 1) / 2 && j == (this.height + 1) / 2) ) this.field[i][j] = black;
- else this.field[i][j] = space;
- }
- }
- }
- /*
- * 引数のfieldをもとにFieldをインスタンス化する
- * @since 0.0.1
- * @param field フィールド
- */
- OthelloField(char[][] field) {
- this.field = field;
- }
- /*
- * 左からwidth番目、上からheight番目においての文字を返す
- * @since 0.0.1
- * @param width 幅
- * 壁も含める
- * @param height 高さ
- * 壁も含める
- * @return 失敗したら◇
- */
- @Override
- public char getSquare(int width, int height) {
- switch(this.field[width][height]) {
- case wall: return wall;
- case black: return black;
- case white: return white;
- case space: return space;
- default: return '◇';
- }
- }
- /*
- * フィールドに左からwidth番目、上からheight番目においての文字を入れる
- * @since 0.0.1
- * @param width 幅
- * 壁も含める
- * @param height 高さ
- * 壁も含める
- * @param type 種類
- * @return 成功したらtrue, 失敗したらfalse
- */
- @Override
- public boolean setSquare(int width, int height, String type) {
- if(!(this.field[width][height] == space)) return false;
- switch(type) {
- case "black": this.field[width][height] = black; return true;
- case "white": this.field[width][height] = white; return true;
- case "wall": case "space":
- }
- return false;
- }
- /*
- * 黒の数の取得
- * @since 0.0.1
- * @return 黒の数
- */
- public int getValueOfBlack() {
- this.blacks = 0;
- for(var i = 0; i < this.width; i += 1) {
- for(var j = 0; j < this.height; j += 1) {
- if(this.field[i][j] == this.black) this.blacks += 1;
- }
- }
- return blacks;
- }
- /*
- * 白の数の取得
- * @since 0.0.1
- * @return 白の数
- */
- public int getValueOfWhite() {
- this.whites = 0;
- for(var i = 0; i < this.width; i += 1) {
- for(var j = 0; j < this.height; j += 1) {
- if(this.field[i][j] == this.white) this.whites += 1;
- }
- }
- return this.whites;
- }
- /*
- * 手番の取得
- * @since 0.0.1
- * @return 先手ならtrue,後手ならfalse
- */
- public boolean getIsFirstMove() {
- return this.move;
- }
- /*
- * パス
- * @since 0.0.1
- */
- public void changeMove() {
- this.move = ! this.move;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement