Androidアプリ リバーシのソース (Board.java)

スポンサーリンク
スポンサーリンク
package net.dalomo.reversi;

import android.content.Context;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Board {
    final int BLACK = 1;
    final int WHITE = -1;
    int PlayerTurn = 1;
    private Context con;
    private View parentView;

    public Board(Context context){
        this.con = context;
    }

    private TextView getTextViewFromTag (int a, int b){
        String s = String.valueOf(a) + String.valueOf(b);
        TextView res = parentView.findViewWithTag(s);
        return res;
    }

    private Integer SplitStrRCtoIntRow (String s){
        int r;
        r = Integer.parseInt(s.substring(0,1));
        return r;
    }

    private Integer SplitStrRCtoIntColumn (String s){
        int c;
        c = Integer.parseInt(s.substring(1));
        return c;
    }

    private String TagToString(TextView v){
        String s;
        s = v.getTag().toString();
        return s;
    }

    private boolean existStone(TextView v){
        return !(v.getText().toString().equals(""));
    }

    private int getStoneColor(TextView v){
        int sc = 0;

        switch (v.getText().toString()){
            case("●"):
                sc = BLACK;
                break;
            case("○"):
                sc = WHITE;
        }
        return sc;
    }

    private void setStoneToCell(TextView v){
        switch(PlayerTurn){
            case(BLACK):
                v.setText("●");
                break;
            case(WHITE):
                v.setText("○");
                break;
        }
    }

    private void CheckFlipUP(TextView v,int r,int c){
        TextView tv;
        int StoneColor;
        int StoneCount = 0;

        for(int ui = (r - 1) ; -1 < ui ; ui-- ){
            tv = getTextViewFromTag(ui,c);

            if(tv == null) return;

            if(!existStone(tv)){
                break;
            }

            StoneColor = getStoneColor(tv);

            if(PlayerTurn == StoneColor && StoneCount < 1){
                break;
            }

            if(PlayerTurn != StoneColor){
                StoneCount++;
                continue;
            }

            if(PlayerTurn == StoneColor){

                setStoneToCell(v);

                for(int j = (ui + 1) ; (ui + StoneCount + 1) > j ; j++){
                    tv = getTextViewFromTag(j,c);
                    setStoneToCell(tv);
                }

                break;
            }
         }
    }

    private void CheckFlipDOWN(TextView v,int r,int c){
        TextView tv;
        int StoneColor;
        int StoneCount = 0;

        for(int di = (r + 1) ; 8 > di ; di++ ){
            tv = getTextViewFromTag(di,c);

            if(tv == null) return;

            if(!existStone(tv)){
                break;
            }

            StoneColor = getStoneColor(tv);

            if(PlayerTurn == StoneColor && StoneCount < 1){
                break;
            }

            if(PlayerTurn != StoneColor){
                StoneCount++;
                continue;
            }

            if(PlayerTurn == StoneColor){

                setStoneToCell(v);

                for(int j = (di - 1) ; (di - StoneCount - 1) < j ; j--){
                    tv = getTextViewFromTag(j,c);
                    setStoneToCell(tv);
                }

                break;
            }
        }
    }

    private void CheckFlipRIGHT(TextView v,int r,int c){
        TextView tv;
        int StoneColor;
        int StoneCount = 0;

        for(int ri = (c + 1) ; 8 > ri ; ri++ ){
            tv = getTextViewFromTag(r,ri);

            if(tv == null) return;

            if(!existStone(tv)){
                break;
            }

            StoneColor = getStoneColor(tv);

            if(PlayerTurn == StoneColor && StoneCount < 1){
                break;
            }

            if(PlayerTurn != StoneColor){
                StoneCount++;
                continue;
            }

            if(PlayerTurn == StoneColor){

                setStoneToCell(v);

                for(int j = (ri - 1) ; (ri - StoneCount - 1) < j ; j--){
                    tv = getTextViewFromTag(r,j);
                    setStoneToCell(tv);
                }

                break;
            }
        }
    }

    private void CheckFlipLEFT(TextView v,int r,int c){
        TextView tv;
        int StoneColor;
        int StoneCount = 0;

        for(int li = (c - 1) ; -1 < li ; li-- ){
            tv = getTextViewFromTag(r,li);

            if(tv == null) return;

            if(!existStone(tv)){
                break;
            }

            StoneColor = getStoneColor(tv);

            if(PlayerTurn == StoneColor && StoneCount < 1){
                break;
            }

            if(PlayerTurn != StoneColor){
                StoneCount++;
                continue;
            }

            if(PlayerTurn == StoneColor){

                setStoneToCell(v);

                for(int j = (li + 1) ; (li + StoneCount + 1) > j ; j++){
                    tv = getTextViewFromTag(r,j);
                    setStoneToCell(tv);
                }

                break;
            }
        }
    }

    private void CheckFlipUPRIGHT(TextView v,int r,int c){
        TextView tv;
        int StoneColor;
        int StoneCount = 0;

        for(int urr = (r - 1),urc = (c + 1) ; -1 < urr || urc < 8 ; urr--, urc++){
            tv = getTextViewFromTag(urr, urc);

            if(tv == null) return;

            if (!existStone(tv)) {
                break ;
            }

            StoneColor = getStoneColor(tv);

            if (PlayerTurn == StoneColor && StoneCount < 1) {
                break ;
            }

            if (PlayerTurn != StoneColor) {
                StoneCount++;
                continue ;
            }

            if (PlayerTurn == StoneColor) {

                setStoneToCell(v);

                for (int inurr = (urr + 1),inurc = (urc - 1 ) ; (urr + StoneCount + 1) > inurr || (urc - StoneCount - 1) < inurc; inurr++, inurc--) {
                        tv = getTextViewFromTag(inurr, inurc);
                        setStoneToCell(tv);
                }

                break;
            }
        }
    }

    private void CheckFlipDOWNRIGHT(TextView v,int r,int c){
        TextView tv;
        int StoneColor;
        int StoneCount = 0;

        for(int drr = (r + 1),drc = (c + 1) ; drr < 8 || drc < 8 ; drr++ , drc++){
            tv = getTextViewFromTag(drr, drc);

            if(tv == null) return;

            if (!existStone(tv)) {
                break ;
            }

            StoneColor = getStoneColor(tv);

            if (PlayerTurn == StoneColor && StoneCount < 1) {
                break ;
            }

            if (PlayerTurn != StoneColor) {
                StoneCount++;
                continue ;
            }

            if (PlayerTurn == StoneColor) {

                setStoneToCell(v);

                for (int indrr = (drr - 1),indrc = (drc - 1 ) ; (drr - StoneCount - 1) < indrr || (drc - StoneCount - 1) < indrc; indrr--, indrc--) {
                    tv = getTextViewFromTag(indrr, indrc);
                    setStoneToCell(tv);
                }

                break;
            }
        }
    }

    private void CheckFlipDOWNLEFT(TextView v,int r,int c){
        TextView tv;
        int StoneColor;
        int StoneCount = 0;

        for(int dlr = (r + 1),dlc = (c - 1) ; dlr < 8 || -1 < dlc ; dlr++ , dlc--){
            tv = getTextViewFromTag(dlr, dlc);

            if(tv == null) return;

            if (!existStone(tv)) {
                break ;
            }

            StoneColor = getStoneColor(tv);

            if (PlayerTurn == StoneColor && StoneCount < 1) {
                break ;
            }

            if (PlayerTurn != StoneColor) {
                StoneCount++;
                continue ;
            }

            if (PlayerTurn == StoneColor) {

                setStoneToCell(v);

                for (int indlr = (dlr - 1),indlc = (dlc + 1 ) ; (dlr - StoneCount - 1) < indlr || (dlc + StoneCount + 1) > indlc; indlr--, indlc++) {
                    tv = getTextViewFromTag(indlr, indlc);
                    setStoneToCell(tv);
                }

                break;
            }
        }
    }

    private void CheckFlipUPLEFT(TextView v,int r,int c){
        TextView tv;
        int StoneColor;
        int StoneCount = 0;

        for(int ulr = (r - 1), ulc = (c - 1) ; -1 <ulr || -1 < ulc ; ulr-- , ulc--){
            tv = getTextViewFromTag(ulr, ulc);

            if(tv == null) return;

            if (!existStone(tv)) {
                break ;
            }

            StoneColor = getStoneColor(tv);

            if (PlayerTurn == StoneColor && StoneCount < 1) {
                break ;
            }

            if (PlayerTurn != StoneColor) {
                StoneCount++;
                continue ;
            }

            if (PlayerTurn == StoneColor) {

                setStoneToCell(v);

                for (int inulr = (ulr + 1),inulc = (ulc + 1 ) ; (ulr + StoneCount + 1) > inulr || (ulc + StoneCount + 1) > inulc; inulr++, inulc++) {
                    tv = getTextViewFromTag(inulr, inulc);
                    setStoneToCell(tv);
                }

                break;
            }
        }
    }

    public void putStone(TextView tv){
        String stone = tv.getText().toString();
        int Row = SplitStrRCtoIntRow(TagToString(tv));
        int Column = SplitStrRCtoIntColumn(TagToString(tv));

        parentView = (View) tv.getParent();

        if(!stone.equals("")){
            Toast.makeText(this.con,"そこに石は置けません", Toast.LENGTH_SHORT).show();
            return;
        }

        CheckFlipUP(tv,Row,Column);
        CheckFlipDOWN(tv,Row,Column);
        CheckFlipRIGHT(tv,Row,Column);
        CheckFlipLEFT(tv,Row,Column);
        CheckFlipUPRIGHT(tv,Row,Column);
        CheckFlipDOWNRIGHT(tv,Row,Column);
        CheckFlipDOWNLEFT(tv,Row,Column);
        CheckFlipUPLEFT(tv,Row,Column);

        stone = tv.getText().toString();
        if(!stone.equals("")) {
            PlayerTurn = PlayerTurn * (-1);
        }
    }
}

コメント

タイトルとURLをコピーしました