java - Tic Tac Toe 'X' is not appearing until after 2nd 'O' has been clicked -


hello have been working on tic tac toe application in java , have working, issue when play against x x not visible after click spot place first o. x there not visible until after click 2nd spot second o. can not figure out why. provide code below.

public class tictactoeapp{ public static void main(string[] args){     tictactoeview view = new tictactoeview();     tictactoemodel model = new tictactoemodel();     tictactoeviewcontroller controller = new tictactoeviewcontroller(view,model);     view.setvisible(true); } } 

public class tictactoemodel {  double xpos,ypos,xr,yr; char[][] position = {{' ',' ',' '},                      {' ',' ',' '},                      {' ',' ',' '}};  /**  * turns row, col center of cells of screen of resolution h       w.  * ideal view.  */ public void computepos(int row, int col, int h, int w){     xpos=(col+0.5)*w/3.0;     ypos=(row+0.5)*h/3.0;     xr=w/8.0;     yr=h/8.0; }  /**  * returns true if cell @ xpos ypos blank * validates xpos , ypos within range.  */ public boolean isempty(int xpos, int ypos){     if(position[xpos][ypos]==' ')   {   return true;   }   return false; }  /*  * places o @ position xpos , ypos. no additional validation.  */ public void placeo(int xpos, int ypos) {     position[xpos][ypos]='o'; }  /**  * dumb strategy computer play tic-tac-toe. * places x in next available space left right , * top bottom.  */ public int putx(){     for(int i=0; i<3;i++)         for(int j = 0;j<3;j++) {             if(position[i][j]==' ') {                 position[i][j]='x';                 return 0;             }         }     return -1; //some error occurred. odd. no cells free. }       public void printboard(){     for(int i=0;i<3;i++)         system.out.println(position[i][0]+"|"+position[i][1]+"|"+position[i]   [2]);     }   } 

  import java.awt.*;   import javax.swing.*;   import java.awt.event.*;   import java.awt.geom.*;   import javax.swing.*;   import javax.swing.event.*;   import java.util.arraylist;  public class tictactoeview extends jframe{  private jbutton obutton, xbutton; public jpanel board; public arraylist<shape> shapes;  public tictactoeview(){     shapes = new arraylist<shape>();     jpanel toppanel=new jpanel();     toppanel.setlayout(new flowlayout());     add(toppanel, borderlayout.north);     add(board=new board(), borderlayout.center);     setdefaultcloseoperation(jframe.exit_on_close);     setsize(500, 500); }   private class board extends jpanel {     public void paintcomponent(graphics g) {         super.paintcomponent(g);         int w=getwidth();         int h=getheight();         graphics2d g2d = (graphics2d) g;          // draw grid         g2d.setpaint(color.white);         g2d.fill(new rectangle2d.double(0, 0, w, h));         g2d.setpaint(color.black);         g2d.setstroke(new basicstroke(4));         g2d.draw(new line2d.double(0, h/3, w, h/3));         g2d.draw(new line2d.double(0, h*2/3, w, h*2/3));         g2d.draw(new line2d.double(w/3, 0, w/3, h));         g2d.draw(new line2d.double(w*2/3, 0, w*2/3, h));         //draw circles , xs visiting elements in array list.         for(shape shape : shapes){             g2d.setpaint(color.blue);             g2d.draw(shape);         }     }   }   /*  * adds mouse listener passed board.  */     public void addmouselistener(mouselistener ml){     board.addmouselistener(ml); }     public static void main(string[] args) {     tictactoeview ttv = new tictactoeview();     ttv.setvisible(true);     }  }    import java.awt.event.*; import java.awt.geom.*; import java.awt.graphics2d; import java.awt.color; import javax.swing.joptionpane;  /* * controller tic-tac-toe. * notice implements mouselistener  * can attached graphical component , therefore * complies interface mouselistener. */ public class tictactoeviewcontroller implements mouselistener{  tictactoeview view; tictactoemodel model; color ocolor=color.blue, xcolor=color.red; 

    public tictactoeviewcontroller(tictactoeview view, tictactoemodel model) {      this.view = view;     this.model = model;   //   view.addmouselistener(this); }  /** ask model what's next move.  */     public void play(int ypos, int xpos) {         if (model.isempty(xpos,ypos)) {// changed x , y because lands   in    wrong position.         // do: put o in xpos ypos using model.      model.placeo(xpos,ypos);      // do: put x using model.      drawboard();      view.repaint();      model.putx();       }     // do: add conditions inside () of if's determine    winner.         if( didwin('x') )          joptionpane.showmessagedialog(null,"x wins","winner",    joptionpane.information_message);     else if ( didwin('o')  )         joptionpane.showmessagedialog(null,"o    wins","winner",joptionpane.information_message); }   /** control drawing of o , x.  * looks @ model see there xs , os , draws 2     diagonal  * lines or circle (an ellipse, actually) in positions given.  */ public void drawboard() {     graphics2d g2d = (graphics2d)view.board.getgraphics();      (int i=0; i<3; i++)          for(int j=0; j<3;j++) {                model.computepos(i,j,view.board.getheight(),view.board.getwidth());             double xpos = model.xpos;             double xr = model.xr;             double ypos = model.ypos;             double yr = model.yr;             // todo: complete expressions within if statements    follows:             // if array represents board has o in position i, j... else,             // if has x...             if ( model.position[i][j]=='o' ) {                 // adds circle (which ellipse of sorts) list of elements draw            view.shapes.add(new ellipse2d.double(xpos-xr, ypos-yr, xr*2, yr*2));             }             if  (model.position[i][j]=='x' ) {           // adds 2 lines crossed (the x) list of shapes draw.               view.shapes.add(new line2d.double(xpos-xr, ypos-yr, xpos+xr,   ypos+yr));               view.shapes.add(new line2d.double(xpos-xr, ypos+yr, xpos+xr, ypos-yr));             }             system.out.println("coords: xpos:"+xpos+", ypos:"+ypos+", xr"+xr+", yr"+yr);     } }  /** mouselistener event  * converts coordinates of mouse corresponding row ,   column of cell  */   public void mouseclicked(mouseevent e) {   int xpos=e.getx()*3/view.getwidth();   int ypos=e.gety()*3/view.getheight();   //system.out.println("play "+xpos+","+ypos);   play(xpos,ypos); }  /**  * check wheather player has won. checks happen against model.  */   public boolean didwin(char player) {     //   int count = 0;   int count2 = 0;    for(int = 0; < 3; i++)   {       for(int j = 0; j<3; j++)      {          if(model.position[i][j]== player)         {         count++;         if(count == 3 )         return true;         }     }   count = 0;    }   for(int n = 0; n < 3; n++)      {         for(int n2=0; n2 <3; n2++)            {               if(model.position[n][n2]==player)                  {                  count2++;                  if(count2 == 3)// if doesnt work change 2                  return true;                  }            } count2=0;  }      if(model.position[0][0]==player && model.position[1][1]==player &&   model.position[2][2]==player)     return true;      if(model.position[0][2]==player && model.position[1][1]==player && model.position[2][0]==player)     return true;         return false;   }         /** ignore other mouse events*/     public void mousepressed(mouseevent e) {}     public void mousereleased(mouseevent e) {}     public void mouseentered(mouseevent e) {}     public void mouseexited(mouseevent e) {} } 

in tictaktoecontroller class inside play method should call drawboard() , view.repaint() after model.putx() method

model.putx(); drawboard(); view.repaint(); 

full method

 public void play(int ypos, int xpos) {         if (model.isempty(xpos,ypos)) {// changed x , y because lands   in    wrong position.         // do: put o in xpos ypos using model.      model.placeo(xpos,ypos);      // do: put x using model.      drawboard();      view.repaint();      model.putx();      drawboard();      view.repaint();        }     // do: add conditions inside () of if's determine    winner.         if( didwin('x') )          joptionpane.showmessagedialog(null,"x wins","winner",    joptionpane.information_message);     else if ( didwin('o')  )         joptionpane.showmessagedialog(null,"o    wins","winner",joptionpane.information_message); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -