Java-written Gobang small program __ static function

Source: Internet
Author: User
Tags gety

Import java.applet.*;
Import java.awt.*;
Import java.awt.event.*;
Import Java.applet.Applet;
Import Java.awt.Color;

public class Wu7ziqi extends Applet implements Actionlistener,mouselistener,mousemotionlistener,itemlistener
{
int color_qizi=0;//flag Color logo 0: White 1: Sunspots
int intgame_start=0;//game Start logo 0 not started in 1 game
int intgame_body[][]=new int[16][16]; Set board Pawn Status 0 No Child 1 White 2 sunspots

Button B1=new button ("game start");
Button B2=new button ("Reset Game");
Label Lblwin=new label ("");
Checkbox Ckbhb[]=new checkbox[2];
CheckboxGroup ckghb=new CheckboxGroup ();
public void Init ()
{
SetLayout (NULL);

Addmouselistener (this);
Add (B1);
B1.setbounds (330,50,80,30);
B1.addactionlistener (this);
Add (B2);
B2.setbounds (330,90,80,30);
B2.addactionlistener (this);
Ckbhb[0]=new Checkbox ("White son first", ckghb,false);
Ckbhb[0].setbounds (320,20,60,30);
Ckbhb[1]=new Checkbox ("Sunspot First", Ckghb,false);
Ckbhb[1].setbounds (380,20,60,30);
Add (ckbhb[0]);
Add (ckbhb[1]);
Ckbhb[0].additemlistener (this);
Ckbhb[1].additemlistener (this);
Add (Lblwin);
Lblwin.setbounds (330,130,80,30);

GAME_START_CSH ();
}

public void itemstatechanged (ItemEvent e)
{
if (Ckbhb[0].getstate ())//Select Sunspot first or White first
{
color_qizi=0;
}
Else
{
Color_qizi=1;
}
}

public void actionperformed (ActionEvent e)
{
Graphics G=getgraphics ();
if (E.getsource () ==b1)
{
Game_start ();
}
Else
{
Game_re ();
}
}

public void mousepressed (MouseEvent e) {}

public void mouseclicked (MouseEvent e)
{
Graphics G=getgraphics ();
int x1,y1;
X1=e.getx ();
Y1=e.gety ();
if (E.getx () <20 | | e.getx () >300 | | e.gety () <20 | | e.gety () >300)
{
Return
}

if (x1%20>10)
{
x1+=20;
}

if (y1%20>10)
{
y1+=20;
}

x1=x1/20*20;
y1=y1/20*20;
Set_qizi (X1,Y1);

}

public void mouseentered (MouseEvent e) {}
public void mouseexited (MouseEvent e) {}
public void mousereleased (MouseEvent e) {}
public void mousedragged (MouseEvent e) {}
public void mousemoved (MouseEvent e) {}

public void Paint (Graphics g)
{
Draw_qipan (g);
}

public void Set_qizi (int x,int y)//Drop
{
if (intgame_start==0)//judgment game not started
{
Return
}

if (intgame_body[x/20][y/20]!=0)
{
Return
}
Graphics G=getgraphics ();

if (color_qizi==1)//To judge whether the sunspot or the white child
{
G.setcolor (Color.Black);
color_qizi=0;
}
Else
{
G.setcolor (Color.White);
Color_qizi=1;
}

G.filloval (x-10,y-10,20,20);

intgame_body[x/20][y/20]=color_qizi+1;


if (game_win_1 (X/20,Y/20))//judging win or lose
{
Lblwin.settext (Get_qizi_color (Color_qizi) + "Win!");
intgame_start=0;
}

if (Game_win_2 (X/20,Y/20))//judging win or lose
{
Lblwin.settext (Get_qizi_color (Color_qizi) + "Win!");
intgame_start=0;
}

if (Game_win_3 (X/20,Y/20))//judging win or lose
{
Lblwin.settext (Get_qizi_color (Color_qizi) + "Win!");
intgame_start=0;
}

if (Game_win_4 (X/20,Y/20))//judging win or lose
{
Lblwin.settext (Get_qizi_color (Color_qizi) + "Win!");
intgame_start=0;
}
}

Public String get_qizi_color (int x)
{
if (x==0)
{
return "sunspots";
}
Else
{
Return "White child";
}
}

public void Draw_qipan (Graphics G)/Draw a chessboard 15*15
{
G.setcolor (Color.lightgray);
G.fill3drect (10,10,300,300,true);
G.setcolor (Color.Black);
for (int i=1;i<16;i++)
{
G.drawline (20,20*i,300,20*i);
G.drawline (20*i,20,20*i,300);
}
}

public void Game_start ()//game start
{
Intgame_start=1;
Game_btn_enable (FALSE);
B2.setenabled (TRUE);
}

public void game_start_csh ()//game start initialization
{

intgame_start=0;
Game_btn_enable (TRUE);
B2.setenabled (FALSE);
Ckbhb[0].setstate (TRUE);

for (int i=0;i<16; i++)
{
for (int j=0;j<16; j + +)
{
intgame_body[i][j]=0;
}
}
Lblwin.settext ("");
}

public void Game_re ()//game Start again
{
Repaint ();
GAME_START_CSH ();
}

public void Game_btn_enable (Boolean e)//Set component state
{
B1.setenabled (e);
B2.setenabled (e);
Ckbhb[0].setenabled (e);
Ckbhb[1].setenabled (e);
}

public boolean game_win_1 (int x,int y)//Judging winning and losing cross
{
int x1,y1,t=1;
X1=x;
Y1=y;

for (int i=1;i<5; i++)
{
if (x1>15)
{
Break
}
if (Intgame_body[x1+i][y1]==intgame_body[x][y])
{
T+=1;
}
Else
{
Break
}

}

for (int i=1;i<5; i++)
{
if (x1<1)
{
Break
}

if (Intgame_body[x1-i][y1]==intgame_body[x][y])
{
T+=1;
}
Else
{
Break
}
}

if (t>4)
{
return true;
}
Else
{
return false;
}
}

public boolean game_win_2 (int x,int y)//Judging winning and losing vertical
{
int x1,y1,t=1;
X1=x;
Y1=y;

for (int i=1;i<5; i++)
{
if (x1>15)
{
Break
}
if (Intgame_body[x1][y1+i]==intgame_body[x][y])
{
T+=1;
}
Else
{
Break
}

}

for (int i=1;i<5; i++)
{
if (x1<1)
{
Break
}

if (Intgame_body[x1][y1-i]==intgame_body[x][y])
{
T+=1;
}
Else
{
Break
}
}

if (t>4)
{
return true;
}
Else
{
return false;
}
}

public boolean game_win_3 (int x,int y)//Judgment win/lose left oblique
{
int x1,y1,t=1;
X1=x;
Y1=y;

for (int i=1;i<5; i++)
{
if (x1>15)
{
Break
}
if (Intgame_body[x1+i][y1-i]==intgame_body[x][y])
{
T+=1;
}
Else
{
Break
}

}

for (int i=1;i<5; i++)
{
if (x1<1)
{
Break
}

if (Intgame_body[x1-i][y1+i]==intgame_body[x][y])
{
T+=1;
}
Else
{
Break
}
}

if (t>4)
{
return true;
}
Else
{
return false;
}
}

public boolean game_win_4 (int x,int y)//Judgment win/lose left oblique
{
int x1,y1,t=1;
X1=x;
Y1=y;

for (int i=1;i<5; i++)
{
if (x1>15)
{
Break
}
if (Intgame_body[x1+i][y1+i]==intgame_body[x][y])
{
T+=1;
}
Else
{
Break
}

}

for (int i=1;i<5; i++)
{
if (x1<1)
{
Break
}

if (Intgame_body[x1-i][y1-i]==intgame_body[x][y])
{
T+=1;
}
Else
{
Break
}
}


if (t>4)
{
return true;
}
Else
{
return false;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.