在'for'循环中检测TicTacToe游戏中的胜利....

时间:2013-07-24 08:27:47

标签: android arrays

我是Android编程的新手,我想写一个TicTacToe游戏以获得更多学习,我跟着

这篇文章(enter link description here)并更改了本文的一部分以便更好地理解;)

我的问题在于validateGame()方法,当我在一条水平线上写交叉时(我的意思是

exapmle:coordinate [0] [0] = X,坐标0 = X,坐标[0] [2] = X),此方法应首先运行

'for'循环并写入日志和吐司“水平x”,但该方法第二次'for'循环和

在日志“vertical X”中写入。

无论如何,我的方法可以检测X或O但无法检测水平线或垂直线。 问题是什么?怎么解决这个问题?我无法理解本

中的validate_game()方法

文章,我想自己写一个方法;)请帮助我。

干杯。

public boolean validateGame(){
    Cell check=null;
    int counter=0;
    XSymbol xsym=new XSymbol();
    OSymbol osym=new OSymbol();

    //horizontal
    for(int i=0;i<coordinate.length;i++){
        check=null;
        for(int j=0;j<coordinate.length;j++){
            if(!coordinate[i][j].equals(check)||coordinate[i][j] instanceof Empty){
                check=coordinate[i][j];
                counter=0;
            }
            else
                counter++;
            if(counter==playerWin-1){
                if(coordinate[i][j].equals(xsym)){
                    winX=true;
                    Log.e("horizontal", "x");
                    Toast.makeText(getContext(), "HORIZONTAL X", Toast.LENGTH_LONG).show();
                }
                else{
                    winO=true;
                    Log.e("horizontal", "o");
                    Toast.makeText(getContext(), "HORIZONTAL O", Toast.LENGTH_LONG).show();
                }
                return true;
            }
        }
        counter=0;
    }

    //vertical
    for(int i=0;i<coordinate.length;i++){
        check=null;
        counter=0;
        for(int j=0;j<coordinate.length;j++){
            if(!coordinate[j][i].equals(check)||coordinate[j][i] instanceof Empty){
                check=coordinate[j][i];
                counter=0;
            }
            else
                counter++;
            if(counter==playerWin-1){
                if(coordinate[j][i].equals(osym)){
                    winO=true;
                    Log.e("vertical", "o");
                    Toast.makeText(getContext(), "VERTICAL O", Toast.LENGTH_LONG).show();
                }
                else{
                    winX=true;
                    Log.e("vertical", "x");
                    Toast.makeText(getContext(), "VERTIC" +"AL X", Toast.LENGTH_LONG).show();
                }
                return true;
            }
        }
        counter=0;
    }

    return false;
}
抱歉我的英语不好:P

更新:我想回答我的问题并改变了我的问题,但网站不允许:(无论如何,我更新了我的第一个答案。

我写了你提出的方法,但我的程序没有再次运行:(我认为我的另一个问题 事情。我写了Cell and Empty和OSymbol和XSymbol课程,请阅读并告诉我,这是什么 问题??

你的方法,当我触摸我的屏幕第一次点击,吐司显示'水平O'!

Cell.java:

public abstract class Cell extends Point {
    public Cell(int x, int y) {
       super(x, y);
    }
    public Cell(){
       super();
    }
    abstract public void draw(Canvas g,Resources res, int x, int y, int w, int h);
 }

Empty.java:

public class Empty extends Cell {
    public Empty(int x, int y) {
        super(x, y);
    }
    public Empty(){
        super();
    }    
    public void draw(Canvas g, Resources res, int x, int y, int w, int h) {
        Bitmap im = BitmapFactory.decodeResource(res, R.drawable.blank);
        g.drawBitmap(im, null, new Rect(x*w, y*h, (x*w)+w, (y*h)+h), new Paint());
    }
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Empty) {
            return true;
        } else {
            return false;
        }
    }
 }

XSymbol.java:

public class XSymbol extends Cell {
    public XSymbol(int x, int y) {
        super(x, y);
    }
    public XSymbol(){
        super();
    }
    public void draw(Canvas g, Resources res, int x, int y, int w, int h) {
        Bitmap im = BitmapFactory.decodeResource(res, R.drawable.x);
        g.drawBitmap(im, null, new Rect(x*w, y*h, (x*w)+w, (y*h)+h), new Paint());
    }
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof XSymbol) {
            return true;
        } else {
            return false;
        }
    }
 }

OSymbol.java:

public class OSymbol extends Cell { 
   public OSymbol(int x, int y) {
      super(x, y);
   }
   public OSymbol(){
      super();
   }
   public void draw(Canvas g, Resources res, int x, int y, int w, int h) {
      Bitmap im = BitmapFactory.decodeResource(res, R.drawable.o);
      g.drawBitmap(im, null, new Rect(x*w, y*h, (x*w)+w, (y*h)+h), new Paint());
   }
   @Override
   public boolean equals(Object obj) {
      if (obj instanceof OSymbol) {
         return true;
       } else {
          return false;
       }
    }
 }

Game.java:

 public class Game extends View{
    .
    .
    . 
    @Override
public boolean onTouchEvent(MotionEvent event){
     int x_touch=(int)(event.getX()/(this.getWidth()/x));
     int y_touch=(int)(event.getY()/(this.getHeight()/y));
     drawImage(x_touch,y_touch);
     return super.onTouchEvent(event);
} 
    public void drawImage(int x_touch,int y_touch){
    Cell cell=null;
    if(whatDrawn){
    cell=new XSymbol(x_touch,y_touch);
    whatDrawn=false;
    }else{
    cell=new OSymbol(x_touch,y_touch);
    whatDrawn=true;
    }
        coordinate[x_touch][y_touch]=cell;
    validate();
     }
     public boolean validate(){
    XSymbol xsym=new XSymbol();
    OSymbol osym=new OSymbol();

    boolean xWin=false;
    boolean oWin=false;

    for(int i=0;i<coordinate.length;i++){
        boolean won=true;
        for(int j=1;j<coordinate.length;j++){
            if(!coordinate[i][j-1].equals(coordinate[i][j])){
                won=false;
                break;
            }
        }
        if(won){
            if(coordinate[i][0].equals(xsym)){
                xWin=true;
                Toast.makeText(getContext(), "horizontal X", Toast.LENGTH_LONG).show();
            }
            else{
                oWin=true;
                Toast.makeText(getContext(), "horizontal O", Toast.LENGTH_LONG).show();
            }
        }
    }


    //vertical
    for(int i=0;i<coordinate.length;i++){
        boolean won=true;
        for(int j=1;j<coordinate.length;j++){
            if(!coordinate[j-1][i].equals(coordinate[j][i])){
                won=false;
                break;
            }
        }
        if(won){
            if(coordinate[0][i].equals(xsym)){
                xWin=true;
                Toast.makeText(getContext(), "vertical X", Toast.LENGTH_LONG).show();
            }
            else{
                oWin=true;
                Toast.makeText(getContext(), "vertical O", Toast.LENGTH_LONG).show();
            }
        }
    }


    return false;
    }
        .
        .
        .
     }

抱歉很长的问题:( 谢谢。 干杯

1 个答案:

答案 0 :(得分:-1)

您的检查变量未正确初始化。当你测试差异时你应该打破:

//horizontal
for(int i=0;i<coordinate.length;i++){
    boolean won = true;
    for(int j=1;j<coordinate.length;j++){
        if (!coordinates[i][j].equals(coordinates[i][j-1]) {
            won = false;
            break;
        }
    }
    if (won) {
         if(coordinate[i][0].equals(xsym)){
             xWin = true;
         } else {
             oWin = true;
         }
    }
}

在英语中,其内容如下:

对于每一行,只要一个单元格与前一单元格不同,它就不是获胜线。

如果是获胜线,则获胜者是在该线的第一个单元格上放置符号的玩家。

相关问题