永发信息网

java初学者写五子棋游戏

答案:4  悬赏:20  手机版
解决时间 2021-04-01 21:24
java初学者写五子棋游戏
最佳答案
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ChessGame extends JFrame implements MouseListener{
private int blackcount=0;
private int whitecount=0;
private boolean flag=true;
private boolean whitewin;
private boolean blackwin;
private boolean flash=false;
public Timer time;
private ArrayListblacklist;
private ArrayListwhitelist;
public ChessGame(){

setSize(450,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
blacklist=new ArrayList();
whitelist=new ArrayList();
this.addMouseListener(this);

setVisible(true);

}

@Override

public void paint(Graphics g) {
if(flash==true){
super.paint(g);
}
flash=false;
g.setColor(Color.WHITE);
g.fillRect(0, 0, 420, 420);
g.setColor(Color.DARK_GRAY);
g.drawLine(5, 5, 420, 5);
g.drawLine(5, 5, 5, 420);
g.drawLine(420, 5, 420, 420);
g.drawLine(5, 420, 420, 420);
for(int i=1;i<=21;i++){
g.drawLine(5, 20*i, 420, 20*i);
g.drawLine(20*i, 5, 20*i, 420);
}

g.setColor(Color.BLACK);

for(int i=0;i g.fillOval(blacklist.get(i).getX()*20+12,blacklist.get(i).getY()*20+32, 16, 16);
}
g.setColor(Color.RED);

for(int i=0;i g.fillOval(whitelist.get(i).getX()*20+12,whitelist.get(i).getY()*20+32, 16, 16);
}
this.checkwin(blacklist, 0, 0, 0,0);
this.checkwin(whitelist, 0, 0, 0,1);
g.setColor(Color.black);
String str1 = "黑方胜利局数为:" + blackcount;
g.drawString(str1, 10, 450);
String str2 = "红方胜利局数为:" + whitecount;
g.drawString(str2, 150, 450);

}
public static void main(String[] args){
ChessGame snake=new ChessGame();

}
public boolean checkExist(mypoint a){
for(int i=0;i if(whitelist.get(i).getX()==a.getX()&&whitelist.get(i).getY()==a.getY()){
return true;
}
}
for(int i=0;i if(blacklist.get(i).getX()==a.getX()&&blacklist.get(i).getY()==a.getY()){
return true;
}
}
return false;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
double x=e.getX();
double y=e.getY();

if(x>=5&&x<=410&&y>=5&&y<=410){
double k=20;
x=x/k-1;
y=y/k-2;
int ax=(int) Math.round(x);
int ay=(int) Math.round(y);
if(checkExist(new mypoint(ax,ay))){
JOptionPane.showMessageDialog(null, "该位置已有棋子!");
repaint();
}else{
if(flag==true){
blacklist.add(new mypoint(ax,ay));
}
if(flag==false){
whitelist.add(new mypoint(ax,ay));
}
flag=!flag;

repaint();
}
}
}
public void checkwin(ArrayList list,int pos,int direct,int count,int type){
if(count==0){
for(int i=0;i for(int j=0;j
if(list.get(i).getY()==list.get(j).getY()&&(list.get(j).getX()-list.get(i).getX()==-1)){
direct=2;
count++;
checkwin(list,j,2,count,type);
count=0;
}

if(list.get(i).getX()==list.get(j).getX()&&(list.get(j).getY()-list.get(i).getY()==-1)){
direct=0;
count++;
checkwin(list,j,0,count,type);
count=0;
}
if(list.get(j).getY()==list.get(i).getY()-1&&(list.get(j).getX()-list.get(i).getX()==1)){
direct=4;
count++;
checkwin(list,j,4,count,type);
count=0;
}
if(list.get(j).getY()==list.get(i).getY()+1&&(list.get(j).getX()-list.get(i).getX()==1)){
direct=5;
count++;

checkwin(list,j,5,count,type);
count=0;
}

}
}

}

else{
if(direct==4){
for(int i=0;i if(i!=pos){
if(list.get(i).getX()==list.get(pos).getX()+1&&(list.get(i).getY()-list.get(pos).getY()==-1)){

count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}
if(direct==5){
for(int i=0;i if(i!=pos){
if(list.get(i).getX()==list.get(pos).getX()+1&&(list.get(i).getY()-list.get(pos).getY()==1)){

count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}

if(direct==0){
for(int i=0;i if(i!=pos){
if(list.get(pos).getX()==list.get(i).getX()&&(list.get(i).getY()-list.get(pos).getY()==-1)){

count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}

if(direct==2){
for(int i=0;i if(i!=pos){
if(list.get(pos).getY()==list.get(i).getY()&&(list.get(i).getX()-list.get(pos).getX()==-1)){

count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}

}

}

public void restart(){
whitelist=new ArrayList();
blacklist=new ArrayList();
flash=true;
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}
}
class mypoint{
int x;
int y;
public mypoint(int a,int b){
this.x=a;
this.y=b;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
全部回答

正好有个现成的。



书店多看JAVA教程,游戏开发建议学习unityc# ue4蓝图
初学者写不了,还是先好好学习吧
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
压力怎么转化为动力
10.4-9.6x0.5巧算
优之贡茶(黔灵东路店)在哪里啊,我有事要去这
高中作文拆除与保留怎样拟题
傻儿传奇之抗战到底李成谁演的
齐河县德州巧街坊饺子(迎宾路店)在什么地方啊
凤梨花叶片发黄怎么办
兰花泡酒可以喝吗我泡了兰花酒不知
西安ip地址113.200.106.141具体是哪
H6 V6 L6性能上有什么区别?
高级会计师评审如何填写申报资料
棠香托管中心地址在什么地方,想过去办事,
巴东哪里有助听器卖?
离歌原唱是谁?
我手机有高德地图为什么滴滴拼车不能用啊
推荐资讯
什么是人文学,主要研究目标是什么?
2011g联赛dota冠军是?
我要写给伯父帮我调到好班的感谢信。 我开
90度弯头 dn50 smls sh/t3408 r=1.5d bw
十至+三万大众有哪些车?
字谜:1.木字多一撇 2.木字少一撇 ,因为太简
改性沥青混凝土摊铺多少一个平方米,
萧和箫能组什么词?
我长的这个是什么
五年级下册暑假日记七十字30篇
试简述钢铁工业发展会给武汉带来的有利影响
55的的百分之二十是多少?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?