Advertisement
Guest User

Field.java

a guest
Sep 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. package jp.starfree.cpsv;
  2.  
  3. public class Field {
  4.  
  5. private int width, height;
  6.  
  7. private final char[][] field;
  8. private final char black = '●', white = '○', space = ' ', wall ='■';
  9.  
  10. private int blacks = 0, whites = 0;
  11.  
  12. /*
  13. * 8 * 8 のFieldをインスタンス化する
  14. * @since 0.0.1
  15. */
  16. Field() {
  17. this.width = 10;
  18. this.height = 10;
  19. this.field = new char[this.width][this.height];
  20. for(var i = 0; i < this.width; i += 1) {
  21. for(var j = 0; j < this.height; j += 1) {
  22. if(i == 0 || i == this.width - 1 || j == 0 || j == this.height - 1) this.field[i][j] = wall;
  23. 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;
  24. 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;
  25. else this.field[i][j] = space;
  26. }
  27. }
  28. }
  29.  
  30. /*
  31. * width * height のFieldをインスタンス化する
  32. * @since 0.0.1
  33. * @param width 幅
  34. * 壁も含める
  35. * @param height 高さ
  36. * 壁も含める
  37. */
  38. Field(int width, int height) {
  39. if(width != height) {
  40. try { throw new Exception("Failed Instantiating.");
  41. } catch (Exception e) { }
  42. System.exit(-1);
  43. }
  44. this.width = width;
  45. this.height = height;
  46. this.field = new char[this.width][this.height];
  47. for(var i = 0; i < this.width; i += 1) {
  48. for(var j = 0; j < this.height; j += 1) {
  49. if(i == 0 || i == this.width - 1 || j == 0 || j == this.height - 1) this.field[i][j] = wall;
  50. 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;
  51. 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;
  52. else this.field[i][j] = space;
  53. }
  54. }
  55. }
  56.  
  57. /*
  58. * 引数のfieldをもとにFieldをインスタンス化する
  59. * @since 0.0.1
  60. * @param field フィールド
  61. */
  62. Field(char[][] field) {
  63. this.field = field;
  64. }
  65.  
  66. /*
  67. * widthを返す
  68. * @since 0.0.1
  69. * @return width
  70. */
  71. public int getWidth() {
  72. return this.width;
  73. }
  74.  
  75. /*
  76. * heightを返す
  77. * @since 0.0.1
  78. * @return height
  79. */
  80. public int getHeight() {
  81. return this.height;
  82. }
  83.  
  84. /*
  85. * 左からwidth番目、上からheight番目においての文字を返す
  86. * @since 0.0.1
  87. * @param width 幅
  88. * 壁も含める
  89. * @param height 高さ
  90. * 壁も含める
  91. * @return 失敗したら◇
  92. */
  93. public char getSquare(int width, int height) {
  94. switch(this.field[width][height]) {
  95. case wall: return wall;
  96. case black: return black;
  97. case white: return white;
  98. case space: return space;
  99. default: return '◇';
  100. }
  101. }
  102.  
  103. /*
  104. * フィールドに左からwidth番目、上からheight番目においての文字を入れる
  105. * @since 0.0.1
  106. * @param width 幅
  107. * 壁も含める
  108. * @param height 高さ
  109. * 壁も含める
  110. * @param type 種類
  111. * @return 成功したらtrue, 失敗したらfalse
  112. */
  113. public boolean setSquare(int width, int height, String type) {
  114. if(!(this.field[width][height] == space)) return false;
  115. switch(type) {
  116. case "black": this.field[width][height] = black; return true;
  117. case "white": this.field[width][height] = white; return true;
  118. case "wall": case "space":
  119. }
  120. return false;
  121. }
  122.  
  123. /*
  124. * フィールドの取得
  125. * @since 0.0.1
  126. * @return Field型のフィールド
  127. */
  128. public Field getField() {
  129. return new Field(this.field);
  130. }
  131.  
  132. /*
  133. * フィールドの取得
  134. * @since 0.0.1
  135. * @return char[][]型のフィールド
  136. */
  137. public char[][] toCharArray() {
  138. return this.field;
  139. }
  140.  
  141. /*
  142. * 黒の数の取得
  143. * @since 0.0.1
  144. * @return 黒の数
  145. */
  146. public int getValueOfBlack() {
  147. for(var i = 0; i < this.width; i += 1) {
  148. for(var j = 0; j < this.height; j += 1) {
  149. if(this.field[i][j] == this.black) this.blacks += 1;
  150. }
  151. }
  152. return blacks;
  153. }
  154.  
  155. /*
  156. * 白の数の取得
  157. * @since 0.0.1
  158. * @return 白の数
  159. */
  160. public int getValueOfWhite() {
  161. for(var i = 0; i < this.width; i += 1) {
  162. for(var j = 0; j < this.height; j += 1) {
  163. if(this.field[i][j] == this.white) this.whites += 1;
  164. }
  165. }
  166. return this.whites;
  167. }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement