0

This is the test file.

while(count<52){

    System.out.println("the times:"+panel.getCount());

    if(panel.getCount()==3){
    for(int i=0;i<2;i++){
    try{
        int temp=number.nextInt(51);
        panel.simulationClick(temp);
    }
    catch(AWTException e){
        e.printStackTrace();
    }
    }}
    }

This is the method that lets computer to click on the buttons.

public void simulationClick(int dex) throws AWTException
{

    Point p1 = bt[dex].getLocation();

    System.out.println(p1.x + "," + p1.y);

    Point p = MouseInfo.getPointerInfo().getLocation();
    int x = p.x;
    int y = p.y;
    robot = new Robot();
    System.out.println(x + "," + y);
    robot.mouseMove(p1.x+100, p1.y+100);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.mouseMove(x, y);

}

I want the user and the simulationClick to take turns, which means that the method works after the user click the buttons. Then method works again! How do I do that? Thank you!

Here are all the codes if you need them!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.AWTException;
import java.awt.BorderLayout;
 import javax.swing.*;
import java.util.Random;
public class cardsTest extends JFrame  {



JButton k=new JButton();
static Cards panel=new Cards();
public static void main(String[] args){

    JFrame frame=new JFrame();
    Random number=new Random();


    restart but=new restart(frame,panel);
    frame.add(panel,BorderLayout.CENTER);
    frame.add(but,BorderLayout.NORTH);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1400,700);
    frame.setVisible(true);

    int count=0;



    System.out.println("the times:"+panel.getCount());


    for(int i=0;i<2;i++){
    try{
        int temp=number.nextInt(51);
        panel.simulationClick(temp);
    }
    catch(AWTException e){
        e.printStackTrace();
    }
    }




}


}


import java.awt.*;
import java.awt.event.*;
import java.util.Collections;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
public class Cards extends JPanel {
private FlowLayout grid1;
private JTextField text1;
private JTextField text2;
private JTextField text3;
 private Robot robot;
 private JButton k;
 ArrayList<Integer> rn= new ArrayList<Integer>();


String score1,score2,turn;
int count=0;
int countS=0;
int record=0;
int p1=0;
int p2=0;
Timer time=new Timer();

JButton []bt=new JButton[52];
ImageIcon tail=new ImageIcon(getClass().getResource("b1fv.png"));

ImageIcon ori;
public Cards(){
    for(int i=0;i<52;i++){
        rn.add(i);
    }
    Random number=new Random();

    grid1=new FlowLayout();
    setLayout(grid1);
    for(int i=0;i<bt.length;i++){
        ImageIcon c=new ImageIcon(getClass().getResource(i+1+".png"));

        bt[i]=new JButton(c);
        bt[i].addActionListener(new ACListener(i));
        bt[i].addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent event)
            {
                if (event.getButton() == MouseEvent.BUTTON1)
                    System.out.println(bt[1].getText() + "鼠标左键点击");
                if (event.getButton() == MouseEvent.BUTTON3)
                    System.out.println(bt[1].getText() + "鼠标右键点击");
            }
        });
        bt[i].setSize(5,5);
        //flip(i);
        flip(i);

    }



    Collections.shuffle(rn);

    for(int i=0;i<52;i++){

        add( bt[rn.get(i)]);
    }


    text1=new JTextField(score1);
    text1.setText("Player 1: 0");
    text2=new JTextField(score2);
    text2.setText("Player 2: 0");
    text3=new JTextField();
    text3.setText("Player 1's turn.\n"+"This is turn 0.");


    add(text1);
    add(text2);
    add(text3);








    addMouseMotionListener(new MouseMotionAdapter()
    {
        public void mouseMoved(MouseEvent event)
        {
        }
    });


}



private class ACListener implements ActionListener {
    private  final int index;


    public ACListener(int index) {
        this.index = index;

    }




    public void actionPerformed(ActionEvent e) {
          System.out.println("Mouse entered for rating " + index);
        ori=new ImageIcon(getClass().getResource(index+1+".png"));

        ori.setDescription("ori");
        tail.setDescription("tail");

        if (((ImageIcon) bt[index].getIcon()).getDescription()=="ori")
            bt[index].setIcon(tail);
         else
            bt[index].setIcon(ori);
       count++;

       System.out.printf("Action Performed %d times \n",count);

       TimerTask taskFlip1=new TimerTask(){
        public void run(){
            flip(index);
        }
       };

       TimerTask taskFlip2=new TimerTask(){
            public void run(){
                flip(record);
            }
          };

       if(count==1){
           record=index;
           countS++;

       }
       String turnS=Integer.toString(countS);
       text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
       if(count==2){
           int match1=record/4;
           int match2=index/4;
          if(match1==match2&&record!=index){
              p1++;
              score1=Integer.toString(p1);
              text1.setText("Player 1: "+score1);
              text3.setText("Player 2's turn.\n"+"This is turn "+turnS);
              bt[index].setEnabled(false);
              bt[record].setEnabled(false);
          }
          if(record==index){
              text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
          }
          if(match1!=match2){
           time.schedule(taskFlip1,1000);
           time.schedule(taskFlip2,1000);}
          text3.setText("Player 2's turn.\n"+"This is turn "+turnS);


       }

       if(count==3){
           record=index;
       }
       if(count==4){
           int match1=record/4;
           int match2=index/4;
          if(match1==match2&&record!=index){
              p2++;
              score2=Integer.toString(p2);
              text2.setText("Player 2: "+score2);
              text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
             bt[index].setEnabled(false);
              bt[record].setEnabled(false);
          }
          if(record==index){
              text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
          }
          if(match1!=match2){
           time.schedule(taskFlip1,1000);
           time.schedule(taskFlip2,1000);}
          text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
           count=0;
       }

    }

}

public void flip(int i){
    ori=new ImageIcon(getClass().getResource(i+1+".png"));
    ori.setDescription("ori");
    tail.setDescription("tail");

    if (((ImageIcon) bt[i].getIcon()).getDescription()=="ori")
        bt[i].setIcon(tail);
     else
        bt[i].setIcon(ori);
}

public void simulationClick(int dex) throws AWTException
{

    Point p1 = bt[dex].getLocation();

    System.out.println(p1.x + "," + p1.y);

    Point p = MouseInfo.getPointerInfo().getLocation();
    int x = p.x;
    int y = p.y;
    robot = new Robot();
    System.out.println(x + "," + y);
    robot.mouseMove(p1.x+100, p1.y+100);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.mouseMove(x, y);

}

public  int getCount(){
    return count;
}
}



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class restart extends JPanel {
private JButton start;
private int count=0;
public restart(JFrame fr,JPanel pa){
    AListener act=new AListener(fr,pa);
    start=new JButton("Restart");
    start.addActionListener(act);
    add(start);
}

public class AListener implements ActionListener{
    private JFrame f;
    private JPanel p;
    public AListener(JFrame f,JPanel p){
        this.f=f;
        this.p=p;
    }
public void actionPerformed(ActionEvent event){

    count++;
         f.remove(p);  
        f.setVisible(false);
        Cards panel=new Cards();
        f.add(panel,BorderLayout.CENTER);

        for(int i=0;i<10;i++){
            try
            {panel.simulationClick(i);
            }
            catch(AWTException e){
                e.printStackTrace();
            }
        }
        f.setVisible(true);

        p=panel;

    }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Yuhan Su
  • 59
  • 5

1 Answers1

0

I am guessing you have a handler for user click event. You can call simulationClick() in the end of the user click handler(listener).

However if the first move is random you'll have to call the simulationClick() once after determining if its computer's turn.

You'll need to explain your question with more code for specific help.

EDIT: After looking into your code it seems the best place to call simulationClick() is inside the method actionPerformed() of class ACListener.

I am not sure on your logic but if you can call it inside those if conditions (if needed). For example, call it only if the cards match etc. or you can add a check for specific action event as well.

Hope that helps.

PCM
  • 873
  • 8
  • 23
  • What I mean is that the user takes the first move, the computer takes the next move, and then the user takes the third move. – Yuhan Su Nov 27 '12 at 03:01
  • Now the method does not wait for the user to click. I want something like a loop that keeps both the computer and the user clicking the buttons. I have updated the codes. – Yuhan Su Nov 27 '12 at 03:03
  • It seems as though you are trying to get people to write code for you? It is hard for us to figure out what you need in your code because 1. We didn't write it and don't have the same thinking pattern as you, 2. It is a shit load of code and barely anyone will want to read through it, and 3. Your question isn't quite clear. But!! What is stopping you from using a Boolean to check if the computer has had a turn? If it has then it is the players' turn? And from what you said about needing a loop... You already know what to do!! Do it :) – Savlon Jan 30 '13 at 06:19