이것은 테스트 파일입니다.사용자와 컴퓨터가 순서대로 번갈아 가도록하는 방법은 무엇입니까?
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();
}
}}
}
이것은 컴퓨터가 버튼을 클릭 할 수있게하는 방법입니다.
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);
}
나는 방법이 버튼을 클릭하여 사용자 후 작동 즉, 교대로하는 사용자와 simulationClick을 원한다. 그런 다음 방법이 다시 작동합니다! 어떻게해야합니까? 고맙습니다!
필요한 경우 여기에 모두 코드가 있습니다!
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;
}
}
}
내 말은 사용자가 첫 번째 이동을하고 컴퓨터가 다음 이동을 수행 한 다음 사용자가 세 번째 이동을한다는 것입니다. –
이제 메서드는 사용자가 클릭 할 때까지 기다리지 않습니다. 컴퓨터와 사용자가 버튼을 클릭하는 루프를 유지하고 싶습니다. 코드를 업데이트했습니다. –
마치 사람들이 코드를 작성하도록하는 것처럼 보입니까? 1. 코드를 작성하지 않았고 당신과 같은 생각 패턴을 가지고 있지 않습니다. 2. 코드가 빽빽하고 누구도 간과 할 수없는 코드이기 때문에 코드에서 필요한 것을 알아내는 것이 어렵습니다. 그것을 통해 읽고 3. 귀하의 질문은 분명하지 않습니다. 그러나!! 부울을 사용하여 컴퓨터가 돌아 갔는지 확인하는 것을 멈추게하는 것은 무엇입니까? 만약 그렇다면 그것은 플레이어의 차례입니까? 그리고 당신이 루프를 필요에 관해 말했던 것에서. .. 당신은 벌써해야하는지에 관해 안다! 해 :) – Savlon