📄 ycgdiamonds.java
字号:
LinkedList picture3=new LinkedList();//缓存图片
JLabel result=new JLabel("");
JLabel net_state=new JLabel(" ");
JLabel num1=new JLabel("");//记录成绩
JLabel num2=new JLabel("");
JLabel num3=new JLabel("");
JPanel pp1=new JPanel();//显示下一个的参数
JPanel pp2=new JPanel();
JPanel pp3=new JPanel();
ChessButton pp_cb1[][]=new ChessButton[5][5];
ChessButton pp_cb2[][]=new ChessButton[5][5];
ChessButton pp_cb3[][]=new ChessButton[5][5];
boolean have_three=false;
//construct
public NetGame(int number,int port,InetAddress address){
System.out.println(address);
setSize(800,600);
this.setLocation(100,100);
if(number==3)
have_three=true;
JOptionPane.showMessageDialog(null,"回车键开始\n空格键到底,上下左右移动,p暂停");
this.setVisible(true);
this.address=address;
this.port=port;
if(number==3)
this.port++;
Container cph=getContentPane();
cph.setBackground(Backcolor);
Container cp=new JPanel();
cp.setLayout(new GridLayout(1,3));
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
jp2.setLayout(new GridLayout());
jp3.setLayout(new GridLayout());
jp4.setLayout(new GridLayout());
jp2.add(user1.chessView);
jp3.add(user2.chessView);
jp4.add(user3.chessView);
cp.add(jp2);
cp.add(jp3);
cp.add(jp4);
pp1.setLayout(new GridLayout(4,4));//小窗口
pp2.setLayout(new GridLayout(4,4));
pp3.setLayout(new GridLayout(4,4));
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
pp1.add(pp_cb1[i][j]=new ChessButton(i,j,BLANK));
}
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
pp2.add(pp_cb2[i][j]=new ChessButton(i,j,BLANK));
}
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
pp3.add(pp_cb3[i][j]=new ChessButton(i,j,BLANK));
}
JPanel north=new JPanel();
north.setLayout(new GridLayout(1,3));
JPanel tt1=new JPanel();
JPanel tt2=new JPanel();
JPanel tt3=new JPanel();
tt1.setBackground(Backcolor);
tt2.setBackground(Backcolor);
tt3.setBackground(Backcolor);
tt1.add(pp1);
tt2.add(pp2);
tt3.add(pp3);
north.add(tt1);
north.add(tt2);
north.add(tt3);
north.setBackground(Backcolor);
num1=new JLabel("1消去"+user1.win_number+"分数:"+user1.mark);
num2=new JLabel("2消去"+user2.win_number+"分数:"+user2.mark);
num3=new JLabel("3消去"+user3.win_number+"分数:"+user3.mark);
cph.add(new JLabel(" "),BorderLayout.WEST);
cph.add(new JLabel(" "),BorderLayout.EAST);
cph.add(north,BorderLayout.NORTH);
JPanel newPanel=new JPanel();
newPanel.setBackground(Backcolor);
newPanel.setLayout(new GridLayout());
newPanel.add(net_state);
newPanel.add(result);
newPanel.add(num1);
newPanel.add(num2);
newPanel.add(num3);
cph.add(newPanel,BorderLayout.SOUTH);
cph.add(cp,BorderLayout.CENTER);
OpenKey();//打开键盘事件
}//end of NetGame()
class User{
int begin_key,pause_key,up_key,down_key,left_key,right_key,bottom_key;
ChessButton cb[][]=new ChessButton[SIZEI+1][SIZEJ+1];//棋子
JPanel chessView=new MyJPanel();//棋盘面版
boolean flag=true;//记录有无暂停
int speed=SPEED;
int win_number=0;
int mark=0;
int diamonds_panel[][]=new int[SIZEI+2][SIZEJ+2];
Diamonds diamonds_thread;
int win_num;//当前成的行数
boolean flag_random=false;//是否请求缓存
MyImage tempMyImage=new MyImage(0,0,0,0,0,0,0,0,0);//临时存放小图
public User(int begin,int pause,int up,int bottom,int left,int right,int down)
{
begin_key=begin;
pause_key=pause;
up_key=up;
down_key=down;
left_key=left;
right_key=right;
bottom_key=bottom;
chessView.setLayout(new GridLayout(SIZEI-3,SIZEJ));
for(int i=1;i<=SIZEI;i++)
for(int j=1;j<=SIZEJ;j++)
{
cb[i][j]=new ChessButton(i,j,BLANK);
}
for(int i=1;i<=SIZEI-3;i++)
for(int j=1;j<=SIZEJ;j++)
{
chessView.add(cb[i+3][j]);
}
}
//清屏
public void ClearChess(){
for(int i=1;i<=SIZEI;i++)
for(int j=1;j<=SIZEJ;j++)
{
cb[i][j].color=0;
}
chessView.repaint();
}
//重绘棋盘
public void ReDraw(){
for(int i=1;i<=SIZEI;i++)
for(int j=1;j<=SIZEJ;j++)
{
cb[i][j].color=diamonds_panel[i][j];
}
chessView.repaint();
}//end of ReDraw
//初始化棋盘
void CreatDiamondsPanel()
{
for(int i=0;i<=SIZEI+1;i++)
diamonds_panel[i][0]=diamonds_panel[i][SIZEJ+1]=1;
for(int i=0;i<=SIZEJ+1;i++)
diamonds_panel[0][i]=diamonds_panel[SIZEI+1][i]=1;
for(int i=1;i<=SIZEI;i++)
for(int j=1;j<=SIZEJ;j++)
diamonds_panel[i][j]=0;
}
public void RandomMyImage()
{
flag_random=true;
AskRandom();//请求缓存//外部函数
}
//判断是否有冲突
boolean Clash(MyImage temp){
if(diamonds_panel[temp.x1][temp.y1]!=0||
diamonds_panel[temp.x2][temp.y2]!=0||
diamonds_panel[temp.x3][temp.y3]!=0||
diamonds_panel[temp.x4][temp.y4]!=0)
return false;
else
return true;
}
MyImage temp=new MyImage(0,0,0,0,0,0,0,0,0);
boolean CanDown(){
temp.SetMyImage(tempMyImage.x1+1,tempMyImage.y1,tempMyImage.x2+1,tempMyImage.y2,
tempMyImage.x3+1,tempMyImage.y3,tempMyImage.x4+1,tempMyImage.y4,tempMyImage.color);
return Clash(temp);
}
boolean CanLeft(){
temp.SetMyImage(tempMyImage.x1,tempMyImage.y1-1,tempMyImage.x2,tempMyImage.y2-1,
tempMyImage.x3,tempMyImage.y3-1,tempMyImage.x4,tempMyImage.y4-1,tempMyImage.color);
return Clash(temp);
}
boolean CanRight(){
temp.SetMyImage(tempMyImage.x1,tempMyImage.y1+1,tempMyImage.x2,tempMyImage.y2+1,
tempMyImage.x3,tempMyImage.y3+1,tempMyImage.x4,tempMyImage.y4+1,tempMyImage.color);
return Clash(temp);
}
boolean CanRound(){
if(tempMyImage.color==YELLOW)//田字格,不变形
return false;
int x1=tempMyImage.x1-tempMyImage.x3;
int x2=tempMyImage.x2-tempMyImage.x3;
int x4=tempMyImage.x4-tempMyImage.x3;
int y1=tempMyImage.y1-tempMyImage.y3;
int y2=tempMyImage.y2-tempMyImage.y3;
int y4=tempMyImage.y4-tempMyImage.y3;
temp.SetMyImage(tempMyImage.x3+y1,tempMyImage.y3-x1,tempMyImage.x3+y2,tempMyImage.y3-x2,
tempMyImage.x3,tempMyImage.y3,tempMyImage.x3+y4,tempMyImage.y3-x4,tempMyImage.color);
return Clash(temp);
}
//擦除下落图
void ClearTemp(){
cb[tempMyImage.x1][tempMyImage.y1].color=0;
cb[tempMyImage.x2][tempMyImage.y2].color=0;
cb[tempMyImage.x3][tempMyImage.y3].color=0;
cb[tempMyImage.x4][tempMyImage.y4].color=0;
cb[tempMyImage.x4][tempMyImage.y4].repaint();
cb[tempMyImage.x1][tempMyImage.y1].repaint();
cb[tempMyImage.x2][tempMyImage.y2].repaint();
cb[tempMyImage.x3][tempMyImage.y3].repaint();
}
//显示下落图
void PrintTemp(){
cb[tempMyImage.x1][tempMyImage.y1].color=tempMyImage.color;
cb[tempMyImage.x2][tempMyImage.y2].color=tempMyImage.color;
cb[tempMyImage.x3][tempMyImage.y3].color=tempMyImage.color;
cb[tempMyImage.x4][tempMyImage.y4].color=tempMyImage.color;
cb[tempMyImage.x4][tempMyImage.y4].repaint();
cb[tempMyImage.x1][tempMyImage.y1].repaint();
cb[tempMyImage.x2][tempMyImage.y2].repaint();
cb[tempMyImage.x3][tempMyImage.y3].repaint();
}
void DealWin(){
int i=SIZEI;
int flag=1;
win_num=0;
while(i>0)
{
flag=1;
for(int j=1;j<=SIZEJ;j++)
if(diamonds_panel[i][j]==0)
flag=0;
if(flag==1)
{
win_number++;
win_num++;
mark+=10;
for(int k=i;k>=2;k--)
for(int j=1;j<=SIZEJ;j++)
diamonds_panel[k][j]=diamonds_panel[k-1][j];
}
else
i--;
}
if(win_num>1&&!have_three)
{
mark+=(win_num-1)*10;
Destroy(win_num);
}
if(win_num>2&&have_three)
{
mark+=(win_num-1)*10;
Destroy(win_num);
}
ReDraw();
}
//判断是否有行可消
boolean HaveWin(int x1,int x2,int x3,int x4){
int i=1;
for(int j=1;j<=SIZEJ;j++)
if(diamonds_panel[x1][j]==0)
i=0;
if(i==1)
return true;
i=1;
for(int j=1;j<=SIZEJ;j++)
if(diamonds_panel[x2][j]==0)
i=0;
if(i==1)
return true;
i=1;
for(int j=1;j<=SIZEJ;j++)
if(diamonds_panel[x3][j]==0)
i=0;
if(i==1)
return true;
i=1;
for(int j=1;j<=SIZEJ;j++)
if(diamonds_panel[x4][j]==0)
i=0;
if(i==1)
return true;
return false;
}
public void DealWith()
{
try{
output.write(down_key);
output.flush();
}catch(IOException e){
JOptionPane.showMessageDialog(null,"信息发送失败,游戏会不同步了");
System.exit(0);};
}
class Diamonds extends Thread{
Diamonds(){
win_number=0;
mark=0;
CreatDiamondsPanel();
ReDraw();
RandomMyImage();
speed=SPEED;
}
public void run(){
while(true)
{
{
try{
sleep(speed);
}catch(Exception e){};
DealWith();
}
}
}
}
}//end of user()
int key_num=0;
public void DealWithKey()
{
if(list_key!=0)
{
key_num=list_key;
list_key=0;
if(key_num==user1.left_key)//<
{
{
if(user1.CanLeft())
{
user1.ClearTemp();
user1.tempMyImage.y1--;
user1.tempMyImage.y2--;
user1.tempMyImage.y3--;
user1.tempMyImage.y4--;
user1.PrintTemp();
}
}
}
else if(key_num==user1.up_key)//^
{
{
if(user1.CanRound())
{
user1.ClearTemp();
int x1=user1.tempMyImage.x1-user1.tempMyImage.x3;
int x2=user1.tempMyImage.x2-user1.tempMyImage.x3;
int x4=user1.tempMyImage.x4-user1.tempMyImage.x3;
int y1=user1.tempMyImage.y1-user1.tempMyImage.y3;
int y2=user1.tempMyImage.y2-user1.tempMyImage.y3;
int y4=user1.tempMyImage.y4-user1.tempMyImage.y3;
user1.tempMyImage.x1=user1.tempMyImage.x3+y1;
user1.tempMyImage.y1=user1.tempMyImage.y3-x1;
user1.tempMyImage.x2=user1.tempMyImage.x3+y2;
user1.tempMyImage.y2=user1.tempMyImage.y3-x2;
user1.tempMyImage.x4=user1.tempMyImage.x3+y4;
user1.tempMyImage.y4=user1.tempMyImage.y3-x4;
user1.PrintTemp();
}
}
}
else if(key_num==user1.right_key)//>
{
{
if(user1.CanRight())
{
user1.ClearTemp();
user1.tempMyImage.y1++;
user1.tempMyImage.y2++;
user1.tempMyImage.y3++;
user1.tempMyImage.y4++;
user1.PrintTemp();
}
}
}
else if(key_num==user1.down_key)//V
{
{
if(user1.CanDown())
{
user1.ClearTemp();
user1.tempMyImage.x1++;
user1.tempMyImage.x2++;
user1.tempMyImage.x3++;
user1.tempMyImage.x4++;
user1.PrintTemp();
}
else
{
user1.diamonds_panel[user1.tempMyImage.x1][user1.tempMyImage.y1]=user1.tempMyImage.color;
user1.diamonds_panel[user1.tempMyImage.x2][user1.tempMyImage.y2]=user1.tempMyImage.color;
user1.diamonds_panel[user1.tempMyImage.x3][user1.tempMyImage.y3]=user1.tempMyImage.color;
user1.diamonds_panel[user1.tempMyImage.x4][user1.tempMyImage.y4]=user1.tempMyImage.color;
user1.PrintTemp();
if(user1.HaveWin(user1.tempMyImage.x1,user1.tempMyImage.x2,user1.tempMyImage.x3,user1.tempMyImage.x4))
{
//System.out.println("have win");
user1.DealWin();
}
user1.RandomMyImage();
}
}
}
else if(key_num==user1.bottom_key)//到底
{
num1.setText("1消去"+user1.win_number+"分数:"+user1.mark);
num2.setText("2消去"+user2.win_number+"分数:"+user2.mark);
num3.setText("3消去"+user3.win_number+"分数:"+user3.mark);
{
while(user1.CanDown())
{
user1.ClearTemp();
user1.tempMyImage.x1++;
user1.tempMyImage.x2++;
user1.tempMyImage.x3++;
user1.tempMyImage.x4++;
//user1.PrintTemp();
}
user1.diamonds_panel[user1.tempMyImage.x1][user1.tempMyImage.y1]=user1.tempMyImage.color;
user1.diamonds_panel[user1.tempMyImage.x2][user1.tempMyImage.y2]=user1.tempMyImage.color;
user1.diamonds_panel[user1.tempMyImage.x3][user1.tempMyImage.y3]=user1.tempMyImage.color;
user1.diamonds_panel[user1.tempMyImage.x4][user1.tempMyImage.y4]=user1.tempMyImage.color;
user1.PrintTemp();
if(user1.HaveWin(user1.tempMyImage.x1,user1.tempMyImage.x2,user1.tempMyImage.x3,user1.tempMyImage.x4))
{
//System.out.println("have win");
user1.DealWin();
}
user1.RandomMyImage();
}
}
else if(key_num==user2.left_key)//<
{
{
if(user2.CanLeft())
{
user2.ClearTemp();
user2.tempMyImage.y1--;
user2.tempMyImage.y2--;
user2.tempMyImage.y3--;
user2.tempMyImage.y4--;
user2.PrintTemp();
}
}
}
else if(key_num==user2.up_key)//^
{
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -