android - Detect winning in TicTacToe game.... in 'for' loop -


i new in android programming, want write tictactoe game more learning, followed

this article (enter link description here) , changed part of article better understanding ;)

my problem in validategame() method, when write cross in 1 horizontal line (i mean

exapmle : coordinate[0][0]=x, coordinate0=x, coordinate[0][2]=x), method should run first

'for' loop , write in log , toast "horizontal x", method run second 'for' loop ,

write in log "vertical x".

anyway, method can detect x or o can not detect horizontal line or vertical line. problem? how can solved this? can not understand validate_game() method in

article , want write method myself ;) please me.

cheers.

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; } 

sorry on poor english :p

update: want answer question , changed question site not allowed :( anyway, update first answer.

i write proposed method program not run right again :( think problem of other thing. write cell , empty , osymbol , xsymbol class, please read , me, problem??

your method, when touch screen first click, toast shows 'horizontal 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;     }         .         .         .      } 

sorry long question :( thanks. cheers

your check variable not initialized. should break when test difference:

//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;          }     } } 

in english, reads as:

for each line, cell different previous cell, not winning line.

if winning line, winner player put symbol on first cell of line.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -