Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TicTacToe

Posted on 2004-04-28
32
Medium Priority
?
448 Views
Last Modified: 2010-05-18
this is 9x9 and,win if there is 5 consecutive symbols vertically, horizontally, or diagonally.
what's wrong in my code, why the isWon dosen't work?
is there a better method to check out 5 consecutive symbols vertically, horizontally, or diagonally?

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
import java.applet.*;
import javax.swing.*;

public class TicTacToe5 extends Applet implements MouseListener {
    static int x,y;
    static String turn = "My";
   
    public static TableStatus tableStatus = new TableStatus();
    Image notImage,crossImage;
    static boolean isWon(){
        boolean win = false;
        int ver = 0, hor = 0 , dia = 0;
        for(int row1 = 0;row1<9;row1++){
            for(int col1 = 0;col1<9;col1++){
                if(ver <5){
                    if((tableStatus.slot[row1][col1] != 0) && (tableStatus.slot[row1][col1] == tableStatus.slot[row1+1][col1+1])){
                        ver += 1;
                    }else{
                        ver = 0;
                    }
                }
            }
        }
        /*for(int col2 = 0;col2<9;col2++){
            for(int row2 = 0;row2<9;row2++){
                if(hor <5){
                    if((tableStatus.slot[row2][col2] != 0) && (tableStatus.slot[row2][col2] == tableStatus.slot[row2+1][col2+1])){
                        hor += 1;
                    }else{
                        hor = 0;
                    }
                }
            }
        }*/
        if(ver ==5){
            win = true;
        }
        return win;
    }
    int status() {
      if (turn == "My") {
          return 1;
      }
      if (turn == "His") {
          return 2;
      }
      return 0;
    }
    public void init() {
      notImage = getImage(getCodeBase(), "images/not.gif");
      crossImage = getImage(getCodeBase(), "images/cross.gif");
      addMouseListener(this);
    }
    public void paint(Graphics g) {
      Dimension d = getSize();
      g.setColor(Color.black);
      int xoff = d.width / 9;
      int yoff = d.height / 9;
        for(int i =1;i<9;i++ ){
            g.drawLine(i*xoff, 0, i*xoff, d.height);
            g.drawLine(0, i*yoff, d.width, i*yoff);
        }

        for(int r = 0;r < 9;r++){
            for(int c = 0;c < 9;c++){
                if(tableStatus.slot[r][c] == 1){
                    g.drawImage(notImage, r*40, c*40, this);
                }
                if(tableStatus.slot[r][c] == 2){
                    g.drawImage(crossImage, r*40, c*40, this);
                }
            }
        }      
    }
    public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
    }
   
    public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
    }
   
    public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
    }
   
    public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
    }
   
    public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
        x = mouseEvent.getX();
        y = mouseEvent.getY();
        if(tableStatus.slot[x/40][y/40] == 0 ){
            if(turn == "My"){
                tableStatus.slot[x/40][y/40] = 1;
                turn = "His";
            }else if(turn =="His"){
                tableStatus.slot[x/40][y/40] = 2;
                turn = "My";
            }    
        }
        repaint();
        if(isWon()){
            switch (status()) {
                case 1:
                    play(getCodeBase(), "audio/yahoo1.au");
                    break;
                case 2:
                    play(getCodeBase(), "audio/yahoo2.au");
                    break;
                default:
            }
        }
    }
   
}
class TableStatus{
    final int row_Size = 9, column_Size = 9;
    public int slot[][] = new int[row_Size][column_Size];
    public TableStatus(){
        for(int r = 0;r < row_Size; r++){
            for(int c = 0;c < column_Size;c++){
                slot[r][c] = 0;
            }
        }
    }
}



0
Comment
Question by:Barca
  • 17
  • 12
  • 3
32 Comments
 
LVL 3

Author Comment

by:Barca
ID: 10945844
sorry, i solved my problem

now the question is how to check  5 consecutive symbols diagonally
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10947282
int row = 9;
int found = 0;

for (int k = 0; k < row; k++)
{
  for (int i = k; i < row; i++)
  {
     if (tableStatus.slot[r][c] == 1)
     {
       found++;
       if (found == 5)
       {
            System.out.println("found 5 consecutive tokens");

            found = 0;
       }
     }
     else
     {
        found = 0;
     }
  }
}


Hope this logic helps you...
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10947287
>> if (tableStatus.slot[r][c] == 1)

typo

should be...

if (tableStatus.slot[i][i] == 1)

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Author Comment

by:Barca
ID: 10952013
sorry,mmuruganandam,before test your code
i found out my isWon still cannot work properly
what's wrong in it....

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
import java.applet.*;
import javax.swing.*;

public class TicTacToe5 extends JApplet implements MouseListener {
    static int x,y;
    static String turn = "My";
    Image notImage,crossImage;
    static final int row_Size = 9, col_Size = 9;
    static int slot[][] = new int[row_Size][col_Size];
    static int ver = 0, hor = 0 , dia = 0;
    public static void reset(){
        for(int r = 0;r < row_Size; r++){
            for(int c = 0;c < col_Size;c++){
                slot[r][c] = 0;
            }
        }
        ver = 0;
        hor = 0;
        dia = 0;
        turn ="My";
    }
    static boolean isWon(){
        boolean win = false;
        for(int row1 = 0;row1 < row_Size;row1++){
            for(int col1 = 0;col1 < col_Size;col1++){
                if(ver <4){
                     if((slot[row1][col1+1] != 0) && (slot[row1][col1] == slot[row1][col1+1])){
                        ver++;
                    }else{ver = 0;}
                }else{break;}
            }
        }
        for(int row1 = 8 ;row1 >= 0;row1--){
            for(int col1 = 8;col1 >= 0;col1--){
                if(ver <4){
                     if((slot[row1][col1-1] != 0) && (slot[row1][col1] == slot[row1][col1-1])){
                        ver++;
                    }else{
                        ver = 0;
                    }
                }else{break;}
            }
        }
        /*for(int col2 = 0;col2 < col_Size;col2++){
            for(int row2 = 0;row2 < row_Size;row2++){
                if(hor <4){
                    if((slot[row2+1][col2] != 0) && (slot[row2][col2] == slot[row2+1][col2])){
                        hor ++;                        
                    }else{
                        hor = 0;
                    }
                }else{break;}
            }
        }
        for(int col2 = 8 ;col2 >= 0;col2--){
            for(int row2 = 8;row2 >= 0;row2--){
                if(hor <4){
                    if((slot[row2-1][col2] != 0) && (slot[row2][col2] == slot[row2-1][col2])){
                        hor ++;                        
                    }else{
                        hor = 0;
                    }
                }else{break;}
            }
        }*/
        if((ver == 4)|(hor == 4)){
            win = true;
        }
        return win;
    }
    int status() {
      if (turn == "My") {
          return 1;
      }
      if (turn == "His") {
          return 2;
      }
      return 0;
    }
    public void init() {
      notImage = getImage(getCodeBase(), "images/not.gif");
      crossImage = getImage(getCodeBase(), "images/cross.gif");
      addMouseListener(this);
    }
    public void paint(Graphics g) {
      Dimension d = getSize();
      g.setColor(Color.black);
      int xoff = d.width / 9;
      int yoff = d.height / 9;
        for(int i =1;i<9;i++ ){
            g.drawLine(i*xoff, 0, i*xoff, d.height);
            g.drawLine(0, i*yoff, d.width, i*yoff);
        }

        for(int r = 0;r < 9;r++){
            for(int c = 0;c < 9;c++){
                if(slot[r][c] == 1){
                    g.drawImage(notImage, r*40, c*40, this);
                }
                if(slot[r][c] == 2){
                    g.drawImage(crossImage, r*40, c*40, this);
                }
            }
        }      
    }
    public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
    }
   
    public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
    }
   
    public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
    }
   
    public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
    }
   
    public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
        x = mouseEvent.getX();
        y = mouseEvent.getY();
        if(slot[x/40][y/40] == 0 ){
            if(turn == "My"){
                slot[x/40][y/40] = 1;
                turn = "His";
            }else if(turn =="His"){
                slot[x/40][y/40] = 2;
                turn = "My";
            }    
        }
        repaint();
        if(isWon()){
            switch (status()) {
                case 1:
                    play(getCodeBase(), "audio/yahoo1.au");
                    JOptionPane.showMessageDialog(null,"PLAYER1 WON");
                    reset();
                    repaint();
                    break;
                case 2:
                    play(getCodeBase(), "audio/yahoo2.au");
                    JOptionPane.showMessageDialog(null,"PLAYER2 WON");
                    reset();
                    repaint();
                    break;
                default:
            }
        }
    }
   
}
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10957025
>> mmuruganandam ...

I think your code doesnt take care of the fact that there can be 4 empty squares from the nine diagonals, since the search doesnt cater for that - ?

Barca > what result do you get then? Tell us how the failure manifests itself, as this would be helpful.
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 80 total points
ID: 10957038
>> sorry,mmuruganandam,before test your code

Barca, we can give only suggestions... we can't give the full fruit on the hand.
0
 
LVL 17

Accepted Solution

by:
krakatoa earned 420 total points
ID: 10957073
>> we can't give the full fruit on the hand.   LOL! ;)

I would favour nailing down the logic flow in pseudo code or plain language first, as it's a tricky algorithm, and there are several ways of attacking it.
0
 
LVL 3

Author Comment

by:Barca
ID: 10958295
actually, the problem is in the method isWon()
i modified it several times, and there are the problems
isWon can only check first row or column,
must place the 5 symbol sequentially,
0
 
LVL 3

Author Comment

by:Barca
ID: 10958574
umm,,,the problems are solved

now i am going to try mmuruganandam concept
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10958969
What you need to be saying (in case your current solution is problematic again) is something like:

  StringBuffer winBuf = new StringBuffer("000000000");

  for(int rowpointer = 0;rowpointer<9;rowpointer++){

    for (int colpointer =0; colpointer <8;colpointer++){

       if((slot[rowpointer][colpointer++]!=0)&&((slot[rowpointer][colpointer+1])==slot[rowpointer][colpointer])){

          winBuf.setCharAt(colpointer+1,'1');
       
       }  

         else{

          winBuf = "000000000";

         }

     }

  }


   if(Integer.parseInt((winBuf.toString.trim())).intValue()=11111){System.out.println("You Won!");}
   else {System.out.println("Try again :((");}

Then you can adapt this to vertical lookups by reversing the col and row pointers, and also to the diagonal dilemma. Hope this helps.

0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10959020
Sorry ... remove the "++" from the inline colpointer incrementor as it's already in the for loop.
0
 
LVL 3

Author Comment

by:Barca
ID: 10959410
krakatoa,i solved the row and colmun checking  problem by this:

            for(int r = 0;r < row_Size -1; r++){
                if((slot[r][y1] == slot[r + 1][y1]) && (slot[r + 1][y1] != 0)){
                    hor++;
                    if(hor >=4 ){win = true;}
                }else{
                    hor = 0;
                }
            }
            for(int c = 0;c < col_Size -1; c++){
                if((slot[x1][c] == slot[x1][c + 1]) && (slot[x1][c + 1] != 0)){
                    ver++;
                    if(ver >=4 ){win = true;}
                }else{
                    ver = 0;
                }
            }
0
 
LVL 3

Author Comment

by:Barca
ID: 10959435
dear krakatoa,

i will try your code
thx
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10959939
Barca:

Yes, I think your algorithm is correct now .. it is the same principle as mine, and is even better because you are not using the graphic analog of my winBuf record - although to be fair to myself, I would have boiled it down to a simple int increment with a bit more careful thought. ;)

But great - it seems a solid algo. now, what you have come up with. Have you tested it enough, and what are you using for the diagonal search?

0
 
LVL 3

Author Comment

by:Barca
ID: 10960179
for diagonal , i just list out all possible, totally is 10.
something like:
(2x2,1x3,3x1,0x4,4x0) x2(because  reversing  the right hand side angle and left side angel)

i am stupid...
is there a smart way to check it?
0
 
LVL 3

Author Comment

by:Barca
ID: 10960298
how can i solve the problem of array[x1][y1] that either x1 or y1 is negative number...

        if(((slot[x1][y1] == slot[x1 - 2][y1 - 2]) && (slot[x1][y1] == slot[x1 - 1][y1 - 1]) && (slot[x1][y1] == slot[x1 + 1][y1 + 1]) && (slot[x1][y1] == slot[x1 + 2][y1 + 2]))||
            ((slot[x1][y1] == slot[x1 + 3][y1 + 3]) && (slot[x1][y1] == slot[x1 - 1][y1 - 1]) && (slot[x1][y1] == slot[x1 + 1][y1 + 1]) && (slot[x1][y1] == slot[x1 + 2][y1 + 2]))||
            ((slot[x1][y1] == slot[x1 - 2][y1 - 2]) && (slot[x1][y1] == slot[x1 - 1][y1 - 1]) && (slot[x1][y1] == slot[x1 + 1][y1 + 1]) && (slot[x1][y1] == slot[x1 - 3][y1 - 3]))||
            ((slot[x1][y1] == slot[x1 + 4][y1 + 4]) && (slot[x1][y1] == slot[x1 + 3][y1 + 3]) && (slot[x1][y1] == slot[x1 + 1][y1 + 1]) && (slot[x1][y1] == slot[x1 + 2][y1 + 2]))||
            ((slot[x1][y1] == slot[x1 - 2][y1 - 2]) && (slot[x1][y1] == slot[x1 - 1][y1 - 1]) && (slot[x1][y1] == slot[x1 - 3][y1 - 3]) && (slot[x1][y1] == slot[x1 - 4][y1 - 4]))||
            ((slot[x1][y1] == slot[x1 - 2][y1 + 2]) && (slot[x1][y1] == slot[x1 - 1][y1 + 1]) && (slot[x1][y1] == slot[x1 + 1][y1 - 1]) && (slot[x1][y1] == slot[x1 + 2][y1 - 2]))||
            ((slot[x1][y1] == slot[x1 + 3][y1 - 3]) && (slot[x1][y1] == slot[x1 - 1][y1 + 1]) && (slot[x1][y1] == slot[x1 + 1][y1 - 1]) && (slot[x1][y1] == slot[x1 + 2][y1 - 2]))||
            ((slot[x1][y1] == slot[x1 - 3][y1 + 3]) && (slot[x1][y1] == slot[x1 - 1][y1 + 1]) && (slot[x1][y1] == slot[x1 + 1][y1 - 1]) && (slot[x1][y1] == slot[x1 - 2][y1 + 2]))||
            ((slot[x1][y1] == slot[x1 - 3][y1 + 3]) && (slot[x1][y1] == slot[x1 - 4][y1 + 4]) && (slot[x1][y1] == slot[x1 - 1][y1 + 1]) && (slot[x1][y1] == slot[x1 - 2][y1 + 2]))||
            ((slot[x1][y1] == slot[x1 + 3][y1 - 3]) && (slot[x1][y1] == slot[x1 + 4][y1 - 4]) && (slot[x1][y1] == slot[x1 + 1][y1 - 1]) && (slot[x1][y1] == slot[x1 + 2][y1 - 2]))
        ){win = true;}
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10960625
>>is there a smart way to check it? <<

I would imagine that the answer to the question is there is usually a better way, but at this point you'd better know that I've never played tic tac toe, and so I dont know what the diagonal is meant to look like! ;) Is it just a line of squares touching at their bottom right / top left corners, or what? If I know that, then the sky's the limit! lol!
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10960791
And I better add in for completeness that in my code, the test for the winning line should be incorporated earlier in the looping, once per row - but you know this anyway. ;)

Let us know how the diagonal should look.
0
 
LVL 3

Author Comment

by:Barca
ID: 10960835
in 9x9 square.

X                                              X
  X                                          X
    X                                      X
      X                                  X
        X               or            X

something like this
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10961517
:).

How many squares in any one row can there be "selected" - I mean in any given row, how many is the most marked squares that you can have in one game?
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10961645
Additionally I dont know this :

Is it possible to have a winning line from grid indexes for example : col 1 + row 5 through to col 5 + row 9? (There are many others, but exclusing mini-diagonals less than length 5).
0
 
LVL 3

Author Comment

by:Barca
ID: 10963638
hard to describe TicTacToe there...

if you installed SDK , there is a demo about TicTacToe

C:\j2sdk1.4.2_04\demo\applets\TicTacToe
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10964083
Is this what we want (although it could need de-analogising to use int counters) :

StringBuffer winBufHo = new StringBuffer("000000000");
StringBuffer winBufVe = new StringBuffer("000000000");
StringBuffer winBufDi = new StringBuffer("000000000");

boolean won = false;

 for(int rowpointer = 0;rowpointer<9;rowpointer++){

   for (int colpointer =0; colpointer <9;colpointer++){

        if((slot[rowpointer][colpointer]!=0)&&((slot[rowpointer][colpointer+1])==slot[rowpointer][colpointer])){

            winBufHo.setCharAt(colpointer+1,'1');
     
          if(Integer.parseInt((winBufHo.toString.trim())).intValue()=11111){won=true;}
 
     
       }  

        else{

         winBufHo = "000000000";

        } //end else

     }//colpointer for


          if((slot[rowpointer][colpointer]!=0)&&((slot[rowpointer+1][colpointer])==slot[rowpointer][colpointer])){

              winBufVe.setCharAt(colpointer+1,'1');
     
              if(Integer.parseInt((winBufVe.toString.trim())).intValue()=11111){won=true;}

          }        


          else{

         winBufVe = "000000000";

        } //end else


         if((slot[rowpointer][colpointer]!=0)&&((slot[rowpointer+1][colpointer+1])==slot[rowpointer][colpointer])){

              winBufDi.setCharAt(colpointer+1,'1');
     
             if(Integer.parseInt((winBufDi.toString.trim())).intValue()=11111){won=true;}

         }

       
        else{

         winBufDi = "000000000";

        } //end else



 }//rowpointer for

0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10964526
Although on second thoughts, it needs a bit more I think:

StringBuffer winBufHo = new StringBuffer("000000000");
StringBuffer winBufVe = new StringBuffer("000000000");
StringBuffer winBufDi = new StringBuffer("000000000");

boolean won = false;

 for(int rowpointer = 0;rowpointer<9;rowpointer++){

   for (int colpointer =0; colpointer <9;colpointer++){

        if((slot[rowpointer][colpointer]!=0)&&((slot[rowpointer][colpointer+1])==slot[rowpointer][colpointer])){

            winBufHo.setCharAt(colpointer+1,'1');
     
          if(Integer.parseInt((winBufHo.toString.trim())).intValue()=11111){won=true;}
 
     
       }  

        else{

         winBufHo = "000000000";

        } //end else

     }//colpointer for


         colpointer = rowpointer;    

          if((slot[rowpointer][colpointer]!=0)&&((slot[rowpointer+1][colpointer])==slot[rowpointer][colpointer])){

              winBufVe.setCharAt(colpointer,'1');
     
              if(Integer.parseInt((winBufVe.toString.trim())).intValue()=11111){won=true;}

          }        


          else{

         winBufVe = "000000000";

        } //end else


         colpointer = rowpointer+1;


         if((slot[rowpointer][colpointer]!=0)&&((slot[rowpointer+1][colpointer])==slot[rowpointer][colpointer])){

              winBufDi.setCharAt(colpointer,'1');
     
             if(Integer.parseInt((winBufDi.toString.trim())).intValue()=11111){won=true;}

         }

       
        else{

         winBufDi = "000000000";

        } //end else



 }//rowpointer for
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10964804
LOL! - It's a slippery fish, but *hopefully * this does it :

StringBuffer winBufHo = new StringBuffer("000000000");
StringBuffer winBufVe = new StringBuffer("000000000");
StringBuffer winBufDi = new StringBuffer("000000000");

boolean extraHori = false;
boolean extraVert = false;
boolean extraDiag = false;

boolean won = false;

for(int rowpointer = 0;rowpointer<9;rowpointer++){

  for (int colpointer =0; colpointer <9;colpointer++){

       if((slot[rowpointer][colpointer]!=0)&&((slot[rowpointer][colpointer+1])==slot[rowpointer][colpointer])){

           winBufHo.setCharAt(colpointer+1,'1');

           if(!extraHoriUsed){if(!extraHoriUsed){extraHoriUsed=true;winBufHo.setCharAt(colpointer,'1');}}
     

          if(Integer.parseInt((winBufHo.toString.trim())).intValue()=11111){won=true;}

     
      }  

       
       else{

        winBufHo = "000000000";

       } //end else

    }//colpointer for


        colpointer = rowpointer;    

          if((slot[rowpointer][colpointer]!=0)&&((slot[rowpointer+1][colpointer])==slot[rowpointer][colpointer])){

             winBufVe.setCharAt(rowpointer+1,'1');
   
            if(!extraVertUsed){extraVertUsed=true;winBufVe.setCharAt(rowpointer,'1');}



              if(Integer.parseInt((winBufVe.toString.trim())).intValue()=11111){won=true;}

         }        


          else{

        winBufVe = "000000000";

       } //end else


        colpointer = rowpointer+1;


        if((slot[rowpointer][colpointer]!=0)&&((slot[rowpointer+1][colpointer])==slot[rowpointer][colpointer])){

             winBufDi.setCharAt(rowpointer+1,'1');
   
             if(!extraDiagUsed){extraDiagUsed=true;winBufDi.setCharAt(rowpointer,'1');}


             if(Integer.parseInt((winBufDi.toString.trim())).intValue()=11111){won=true;}

        }

     
        else{

        winBufDi = "000000000";

       } //end else



}//rowpointer for
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10964967
I think that could work, but the main loop counters need to be 8, not 9 AFAICS. Fingers crossed, and I haven't tested it at all. ;)
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10964972
And then you can replace my gothic string driven counter thing, with a simple integer incrementation, as you already have!. G'night!
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10966377
The above approach is not good on reflection. The nesting of the for loops* makes for a lot of redundant checking over the same squares, and also there would need to be some handlers for OutOfBoundsExceptions which would occur when dealing with the last row and last column. If these provisions were made in the code, then it would still only handle the horiz and diag checks properly, but there would need to be an additional outer (third) loop as well, since the above would only check all horizontals, and the first column only! And finally, the code cannot handle the diagonal situation at all, which is another reason for abandoning it.

For these reasons it is clear that the best approach is for separate, sequential for loops, one for the horiz., and one for vertical. This is also the least complicated and fastest I believe.

As for the diagonal checking, this I think can be handled in nested loops, both of which decrement one per cycle to handle the ever shorter diagonals. We can still look at this diagonal question if you like, but do you agree with this present analysis of first

*By nested loops here, I dont mean the two loops that are used for iterating either the whole row set or the loops for iterating the whole column set, I mean the two loops that were meant to incorporate both these two operations in one routine. For the reasons I've mentioned, I don't think this is the way to go.
0
 
LVL 3

Author Comment

by:Barca
ID: 10966654
i have other idea for checking 5 consecutive symbols  diagonally, but there is a problem that the first row or column must have a symbol

        if(x1 > y1){
            x2 = x1 -y1;
            y2 = 0;
            for(int a1 = 0;a1 < (9 - x2);a1++ ){
                if((slot[x2][y2] == slot[x2 + a1][y2 + a1]) && (slot[x2 + a1][y2 + a1] != 0)){
                    dia++;
                    if(dia >= 4 ){win = true;}
                }else{dia = 0;}
            }
        }else if(y1 > x1){
            x2 = 0;
            y2 = y1 - x1;
            for(int a2 = 0 ;a2 < (9 - y2);a2++ ){
                if((slot[x2][y2] == slot[x2 + a2][y2 + a2]) && (slot[x2 + a2][y2 + a2] != 0)){
                    dia++;
                    if(dia >= 4 ){win = true;}
                }else{dia = 0;}
            }
        }else if(x1 == y1){
            for(int a3 = 0;a3 < 9;a3++ ){
                if((slot[x1][y1] == slot[x1 + a3][y1 + a3]) && (slot[x1 + a3][y1 + a3] != 0)){
                    dia++;
                    if(dia >= 4 ){win = true;}
                }else{dia = 0;}
            }
        }
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10967027
Wouldn't it be enough to treat the diagonals more or less the same way as the straights, except that you move both the row and the column indexes in the inner loop, and need to reduce the diagonal search space as well, once per 'row' search?

int innerReducer = 9;

    for (int c = 0; c<9 ; c++){

        for (int r = 0; r<innerReducer ; r++){

           if slot[row+r][col+r] !=0 && slot[row+r][col+r] == slot[row+r+1][col+r+1]{
                  if (!usedExtraDiag){
                      usedExtraDiag=true;
                      increment the hitcounter twice;
                      test for win and exit if it is
                  }
                  else{
                  increment the hitcounter once
                  test for win and exit if it is
                  }
                   
           }//end if      

        }//end for

        innerReducer --;

    }//end for


This would search the squares with diagonals "starting" along the top row, and running like this "\" I think in the right hand half of the entire square.

Then you'd need a similar routine for the left hand half of the entire square, again "\", and then two more, each complementary to these for the "/" searches.
0
 
LVL 3

Author Comment

by:Barca
ID: 10967069
dear all,
i have already solved all problem

thx

and share it to you

    static boolean checkDia(int start, int end){
        int a1,x2,y2,dia = 0;
        for(a1 = start; a1 <= end; a1++){
            x2 = x1 + a1;
            y2 = y1 + a1;
            if((x2 >= 0 && x2 <= 8 ) && (y2 >= 0 && y2 <= 8 )){
                if((slot[x1][y1] == slot[x2][y2]) && (slot[x2][y2] != 0)){
                    dia++;
                    if(dia == 5 ){return true;}
                }else{return false;}
            }else{return false;}
        }
        return false;
    }
        static boolean checkRefDia(int start, int end){
        int a1,x2,y2,dia = 0;
        for(a1 = start; a1 <= end; a1++){
            x2 = x1 - a1;
            y2 = y1 + a1;
            if((x2 >= 0 && x2 <= 8 ) && (y2 >= 0 && y2 <= 8 )){
                if((slot[x1][y1] == slot[x2][y2]) && (slot[x2][y2] != 0)){
                    dia++;
                    if(dia == 5 ){return true;}
                }else{return false;}
            }else{return false;}
        }
        return false;
    }
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 10967281
Glad to see you've got there finally. Hope we helped somehow. ;)

krakatoa.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question