Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jp.starfree.cpsv;
- public class Field {
- private int width, height;
- private final char[][] field;
- private final char black = '●', white = '○', space = ' ', wall ='■';
- private int blacks = 0, whites = 0;
- /*
- * 8 * 8 のFieldをインスタンス化する
- * @since 0.0.1
- */
- Field() {
- 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 高さ
- * 壁も含める
- */
- Field(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 フィールド
- */
- Field(char[][] field) {
- this.field = field;
- }
- /*
- * widthを返す
- * @since 0.0.1
- * @return width
- */
- public int getWidth() {
- return this.width;
- }
- /*
- * heightを返す
- * @since 0.0.1
- * @return height
- */
- public int getHeight() {
- return this.height;
- }
- /*
- * 左からwidth番目、上からheight番目においての文字を返す
- * @since 0.0.1
- * @param width 幅
- * 壁も含める
- * @param height 高さ
- * 壁も含める
- * @return 失敗したら◇
- */
- 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
- */
- 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 Field型のフィールド
- */
- public Field getField() {
- return new Field(this.field);
- }
- /*
- * フィールドの取得
- * @since 0.0.1
- * @return char[][]型のフィールド
- */
- public char[][] toCharArray() {
- return this.field;
- }
- /*
- * 黒の数の取得
- * @since 0.0.1
- * @return 黒の数
- */
- public int getValueOfBlack() {
- 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() {
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement