終了の判断を作りたい。今はプレイヤー任せ。
終了処理
終了処理の中に、パスの処理も含まれるみたいなので、どっちも作っていきたい。
終了条件は
- マスが全部埋まる
- 石数の多いほうが勝ち
- 同数の場合は引き分け
- それ以外
- どちらか一色になる
- どちらもパスする
これでいいみたい。
checkFinish()
void checkFinish (){
int AllStone = BlackStoneCount + WhiteStoneCount;
TextView ttv = parentView.findViewWithTag("turn");
switch(AllStone){
case(64):
if(BlackStoneCount > WhiteStoneCount) {
ttv.setText("黒の勝ちです");
}else if(BlackStoneCount < WhiteStoneCount) {
ttv.setText("白の勝ちです");
}else{
ttv.setText("引き分けです");
}
return;
default:
if(BlackStoneCount == 0){
ttv.setText("白の勝ちです");
return;
}else if(WhiteStoneCount == 0){
ttv.setText("黒の勝ちです");
return;
}
if(!canPutStone()){
PlayerTurn = PlayerTurn * (-1);
setTurn();
scanBoard();
}
if(!canPutStone()){
if(BlackStoneCount > WhiteStoneCount) {
ttv.setText("黒の勝ちです");
}else if(BlackStoneCount < WhiteStoneCount) {
ttv.setText("白の勝ちです");
}else{
ttv.setText("引き分けです");
}
}
}
}
private boolean canPutStone(){
String s;
for(TextView[] cc : CellArray) {
for (TextView c : cc) {
s = c.getText().toString();
if(s.equals("*")){ return true; }
}
}
return false;
}
ちょっと冗長な気もする。上の終了条件を順にコード化しただけ。
最後2ブロックがパスの処理で、canPutStone()で置ける場所があるかの判定を行い、手番を入れ替え、置き石表示を更新し、もう一度置ける場所があるか判断する。これでずっと俺のターン時にも対応できてるはず。
なかなかサクサク行った。ただなんかリバーシ作るの飽きてきたな。
追記
思ったんだけど、マスが全部埋まる場合も、オール一色になる場合も、盤面に「*」は表示されていないわけで。ていうことは、パスと、終了だけでいいのでは…?
つまり
void checkFinish() {
TextView ttv = parentView.findViewWithTag("turn");
if (!canPutStone()) {
PlayerTurn = PlayerTurn * (-1);
setTurn();
scanBoard();
}
if (!canPutStone()) {
if (BlackStoneCount > WhiteStoneCount) {
ttv.setText("黒の勝ちです");
} else if (BlackStoneCount < WhiteStoneCount) {
ttv.setText("白の勝ちです");
} else {
ttv.setText("引き分けです");
}
}
}
こうね。動いたよおい。ちくしょうめ。