Advertisement
Guest User

OthelloField.java

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