This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2014/5/15

Tic Tac Toe Random Corner Method not Random

JasonZhu JasonZhu

2014/5/15

#
I'm coding a Tic Tac Toe game. I have this int method that returns the position of a random open corner. However, it keeps returning the bottom left corner. It's not random.
    private int openCorner()
    {
        boolean topLeft = board[0][0].isAvailable();
        boolean topRight = board[0][2].isAvailable();
        boolean bottomLeft = board[2][0].isAvailable();
        boolean bottomRight = board[2][2].isAvailable();

        ArrayList<Integer> pos = new ArrayList<Integer>();
        if(topLeft) pos.add(0);        
        if(topRight) pos.add(2);
        if(bottomLeft) pos.add(6);
        if(bottomRight) pos.add(8);

        Random gen = new Random();
        if(pos.size()==0){
            return -1;
        }else{
            return pos.get(gen.nextInt(pos.size()));
        }
    }
Can someone point the problem please? Thanks.
davmac davmac

2014/5/15

#
You're generating a new Random() sequence generator every time you want a random number. Each time you call 'new Random()' you get a new generator which returns the same sequence.
JasonZhu JasonZhu

2014/5/15

#
This method is only called once from start to finish though. I don't think I get what you mean exactly.
danpost danpost

2014/5/15

#
If you insist on creating a new Random number generator each time 'openCorner' is called, then create it with a different seed each time:
Random gen = new Random(System.currentTimeMillis());
JasonZhu JasonZhu

2014/5/15

#
I just need a random number of the size of the arraylist. What's the best fix?
davmac davmac

2014/5/15

#
Hmm. Actually creating a new Random() should use a pseudo-random seed anyway, so I was wrong; I thought you would get the same sequence each time, but you won't. Actually, I think what you have is correct. There's probably an issue with other parts of your code, or perhaps with the check for which corners are available. Why don't you try some debug prints:
    private int openCorner()  
    {  
        boolean topLeft = board[0][0].isAvailable();  
        boolean topRight = board[0][2].isAvailable();  
        boolean bottomLeft = board[2][0].isAvailable();  
        boolean bottomRight = board[2][2].isAvailable();
       
        System.out.println("topLeft = " + topLeft);
        System.out.println("topRight = " + topRight);
        System.out.println("bottomLeft = " + bottomLeft);
        System.out.println("bottomLeft = " + bottomRight);
      
        ArrayList<Integer> pos = new ArrayList<Integer>();  
        if(topLeft) pos.add(0);          
        if(topRight) pos.add(2);  
        if(bottomLeft) pos.add(6);  
        if(bottomRight) pos.add(8);  

        System.out.println("pos = " + pos);
      
        Random gen = new Random();  
        if(pos.size()==0){  
            return -1;  
        }else{  
            return pos.get(gen.nextInt(pos.size()));  
        }  
    }  
JasonZhu JasonZhu

2014/5/15

#
I will. Thanks davmac. I know that its more of an internal error rather than wrong coding on that specific practice.
You need to login to post a reply.