import java.util.Random;
十余年的成华网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整成华建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“成华网站设计”,“成华网站推广”以来,每个客户项目都认真落实执行。
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.*;
public class SmallGame extends JFrame {
private Random r;
private String[] box = { "剪刀", "石头", "布" };
private JComboBox choice;
private JTextArea ta;
private JLabel lb;
private int win = 0;
private int loss = 0;
private int equal = 0;
public SmallGame() {
initial();//调用initial方法,就是下面定义的那个.该方法主要是初始界面.
pack();
setTitle("游戏主界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(400, 300);
setVisible(true);
}
public static void main(String[] args) {
new SmallGame();
}
public void initial() {
r = new Random(); // 生成随机数
choice = new JComboBox();//初始化choice这个下拉框.也就是你选择出剪子还是石头什么的那个下拉框..
for (int i = 0; i box.length; i++) {//为那个下拉框赋值.用前面定义的private String[] box = { "剪刀", "石头", "布" };附值.这样下拉框就有三个选项了..
choice.addItem(box[i]);
}
ta = new JTextArea(3, 15);//初始化那个文本域3行15列
ta.setEditable(false);//让用户不能编辑那个文本域即不能在里面写东西
JButton okBut = new JButton("出招");//新建一个出招的按钮
okBut.addActionListener(new ActionListener() {//给出招按钮加个监听.意思就是监听着这个按钮看用户有没有点击它..如果点击就执行下面这个方法
public void actionPerformed(ActionEvent e) {//就是这个方法
ta.setText(getResult());//给那个文本域赋值..就是显示结果 赋值的是通过getResult()这个方法得到的返回值 getResult()这个方法下面会讲
lb.setText(getTotal());//给分数那个LABEL赋值..就是显示分数..赋值的是通过getTotal()这个方法得到的返回值
}
});
JButton clearBut = new JButton("清除分数");//新建一个清楚分数的按钮
clearBut.addActionListener(new ActionListener() {//同上给他加个监听
public void actionPerformed(ActionEvent e) {//如果用户点击了就执行这个方法
ta.setText("");//给文本域赋值为""..其实就是清楚他的内容
win = 0;//win赋值为0
loss = 0;//同上
equal = 0;//同上
lb.setText(getTotal());//给显示分数那个赋值..因为前面已经都赋值为0了..所以这句就是让显示分数那都为0
}
});
lb = new JLabel(getTotal());//初始化那个显示分数的东西
JPanel choicePanel = new JPanel();//定义一个面板..面板就相当于一个画图用的东西..可以在上面加按钮啊文本域什么的..
choicePanel.add(choice);//把下拉框加到面板里
choicePanel.add(okBut);//把出招按钮加到面板里
choicePanel.add(clearBut);//把清楚分数按钮加到面板里
JScrollPane resultPanel = new JScrollPane(ta);//把文本域加到一个可滚动的面板里面..JScrollPane就是可滚动的面板..这样如果那个文本域内容太多就会出现滚动条..而不是变大
JPanel totalPanel = new JPanel();//再定义个面板..用来显示分数的..
totalPanel.add(lb);//把那个显示分数的label加到里面去
Container contentPane = getContentPane();//下面就是布局了..
contentPane.setLayout(new BorderLayout());
contentPane.add(choicePanel, BorderLayout.NORTH);
contentPane.add(resultPanel, BorderLayout.CENTER);
contentPane.add(totalPanel, BorderLayout.SOUTH);
}
public String getResult() {//获得结果的方法 返回值是一个String..这个返回值会用来显示在文本域里面
String tmp = "";
int boxPeop = choice.getSelectedIndex();//获得你选择的那个的索引..从0开始的..
int boxComp = getBoxComp();//获得电脑出的索引..就是随机的0-2的数
tmp += "你出:\t" + box[boxPeop];//下面你应该明白了..
tmp += "\n电脑出:\t" + box[boxComp];
tmp += "\n结果:\t" + check(boxPeop, boxComp);
return tmp;
}
public int getBoxComp() {//就是产生一个0-2的随机数..
return r.nextInt(3);//Random的nextInt(int i)方法就是产生一个[0-i)的随机整数 所以nextInt(3)就是[0-2]的随机数
}
public String check(int boxPeop, int boxComp) {//这个就是判断你选择的和电脑选择的比较结果..是输是赢还是平..boxPeop就是你选择的..boxComp就是电脑选择的..
String result = "";
if (boxPeop == (boxComp + 1) % 3) {//(boxComp + 1) % 3 电脑选择的加上1加除以3取余..也就是如果电脑选0这个就为1..这个判断的意思就是如果电脑选0并且你选1..那么就是电脑选了
//private String[] box = { "剪刀", "石头", "布" };这里面下标为0的..你选了下标为1的..就是电脑选剪刀你选石头..所以你赢了..如果电脑选1..(boxComp + 1) % 3就为2..意思就是
//电脑选了石头你选了布..所以你赢了..另外一种情况你明白了撒..只有三种情况你赢..所以这里都包含了..也只包含了那三种..
result = "你赢了!";
win++;//赢了就让记你赢的次数的那个变量加1
} else if (boxPeop == boxComp) {//相等当然平手了
result = "平";
equal++;//同上了
} else {//除了赢和平当然就是输了..
result = "你输了!";
loss++;//同上
}
return result;
}
public int getPoint() {
return (win - loss) * 10;
}
public String getTotal() {
return "赢:" + win + " 平:" + equal + " 输:" + loss + " 得分:"
+ getPoint();
}
}
希望你能明白哈。。
就是这个程序 把他变成五子棋程序
import java.awt.*;
import java.awt.event.*;
//创建棋盘的类
class ChessPad extends Panel implements MouseListener,ActionListener
{
int x=-1,y=-1,棋子颜色=-1;
Button button=new Button("重新开局");
TextField text_1=new TextField("请黑棋下子");
TextField text_2=new TextField();
ChessPad()
{ setSize(440,440);
setLayout(null); setBackground(Color.orange);
addMouseListener(this); add(button);button.setBounds(10,5,60,26);
button.addActionListener(this);
add(text_1);text_1.setBounds(90,5,90,24);
add(text_2);text_2.setBounds(290,5,90,24);
text_1.setEditable(false);text_2.setEditable(false);
}
public void paint(Graphics g)//绘制棋盘外观
{ for(int i=40;i=400;i=i+20)//两种划线的方式,分用于行和列
{ g.drawLine(40,i,400,i);
}
//g.drawLine(40,400,400,400);//最后一行
for(int j=40;j=380;j=j+20)
{ g.drawLine(j,40,j,400);
}
g.drawLine(400,40,400,400);//最后一列
g.fillOval(97,97,6,6);g.fillOval(337,97,6,6);//oval表示椭圆,这里是指棋盘中的五个圆点
g.fillOval(97,337,6,6);g.fillOval(337,337,6,6);
g.fillOval(217,217,6,6);
}
public void mousePressed(MouseEvent e)//当按下鼠标左键时下棋子
{ if(e.getModifiers()==InputEvent.BUTTON1_MASK)
{ x=(int)e.getX();y=(int)e.getY();//鼠标所在位置的XY坐标
ChessPoint_black chesspoint_black=new ChessPoint_black(this);
ChessPoint_white chesspoint_white=new ChessPoint_white(this);
int a=(x+10)/20,b=(y+10)/20;
if(x/202||y/202||x/2019||y/2019)//棋盘以外不下棋子
{ }
else
{
if(棋子颜色==1)
{ this.add(chesspoint_black);
chesspoint_black.setBounds(a*20-10,b*20-10,20,20);
棋子颜色=棋子颜色*(-1);
text_2.setText("请白棋下子");
text_1.setText("");
}
else if(棋子颜色==-1)
{ this.add(chesspoint_white);
chesspoint_white.setBounds(a*20-10,b*20-10,20,20);
棋子颜色=棋子颜色*(-1);
text_2.setText("请黑棋下子");
text_1.setText("");
}
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{
this.removeAll();棋子颜色=-1;
add(button);button.setBounds(10,5,60,26);
add(text_1);text_1.setBounds(90,5,90,24);
text_2.setText("");text_1.setText("请黑棋下子");
add(text_2);text_2.setBounds(290,5,90,24);
}
}
//负责创建黑色棋子的类
class ChessPoint_black extends Canvas implements MouseListener
{
ChessPad chesspad=null;
ChessPoint_black(ChessPad p)
{ //setSize(20,20);
chesspad=p;addMouseListener(this);
}
public void paint(Graphics g)//绘制棋子的大小
{ g.setColor(Color.black);g.fillOval(0,0,20,20);
}
public void mousePressed(MouseEvent e)
{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)//标记
{ chesspad.remove(this);
chesspad.棋子颜色=1;
chesspad.text_2.setText("");chesspad.text_1.setText("请黑棋下子");
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{ if(e.getClickCount()=2)
chesspad.remove(this);//双击时吃掉该棋子
}
}
//负责创建白色棋子的类
class ChessPoint_white extends Canvas implements MouseListener
{ ChessPad chesspad=null;
ChessPoint_white(ChessPad p)
{ //setSize(20,20);
addMouseListener(this);
chesspad=p;
}
public void paint(Graphics g)
{ g.setColor(Color.white);g.fillOval(0,0,20,20);
}
public void mousePressed(MouseEvent e)
{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{ chesspad.remove(this);chesspad.棋子颜色=-1;
chesspad.text_2.setText("请白棋下子");
chesspad.text_1.setText("");
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()=2)
chesspad.remove(this);
}
}
public class Chess extends Frame//添加棋盘窗口
{ ChessPad chesspad=new ChessPad();
Chess()
{
setVisible(true);
setLayout(null);
Label label=new Label("单击左键下棋子,双击吃棋子,用右键单击棋子悔棋",Label.CENTER);
add(label);label.setBounds(70,55,440,26);
add(chesspad);chesspad.setBounds(70,90,440,440);
addWindowListener(new WindowAdapter()
{ public void windowClosing (WindowEvent e)
{ System.exit(0);
}
});
//pack();
setSize(600,550);
}
public static void main(String args[])
{ Chess chess=new Chess();
}
}
applet可以调用另一个和。
一个简单例子
//Applet-同一页面的两applet通信.txt
1、第一个Applet
//: Applet1.java
// Very simple applet
package xjtu.citi;
import java.awt.*;
import java.applet.*;
public class Applet1 extends Applet {
public void paint(Graphics g) {
g.drawString("First applet,What can I do?", 20, 30);
//获得同一页面的applet
Applet2 theOtherApplet =(Applet2)getAppletContext().getApplet("app2");
//使用另一个applet的方法
g.drawString(theOtherApplet.whatidoallforyou(),20,60);
//获得同一页面的所有applet
Enumeration allAppletsOnSamePage = getAppletContext().getApplets();
while(allAppletsOnSamePage.hasMoreElements()) {
Applet appl = (Applet) allAppletsOnSamePage.nextElement();
appl.paint(g); //调用任一个公用方法
}
}
} ///:~
2、第二个Applet
//: Applet2.java
// Easy graphics
package xjtu.citi;
import java.awt.*;
import java.applet.*;
public class Applet2 extends Applet {
public void paint(Graphics g) {
g.drawString("Second applet", 10, 15);
g.draw3DRect(0, 0, 100, 20, false);
}
public String whatidoallforyou(){
return "what I do all for you,zly";
}
} ///:~
3、Html文件:twoapplets.html
html
applet codebase = "." code="xjtu.citi.Applet2" width="400" height="200" name="app2"
/applet
br
applet codebase = "." code="xjtu.citi.Applet1" width="400" height="200" name="app1"
/applet
br
/html
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JApplet
{
private JTextField rows=new JTextField("3");
private JTextField cols=new JTextField("3");
private static final int BLANK=0,XX=1,OO=2;
class ToeDialog extends JDialog
{
private int turn=XX;
ToeDialog(int cellsWide,int cellsHigh)
{
setTitle("The game itself");
Container cp=getContentPane();
cp.setLayout(new GridLayout(cellsWide,cellsHigh));
for(int i=0;icellsWide*cellsHigh;i++)
cp.add(new ToeButton());
setSize(cellsWide*50,cellsHigh*50);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
class ToeButton extends JPanel
{
private int state=BLANK;
public ToeButton()
{
addMouseListener(new ML());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int x1=0,y1=0;
int x2=getSize().width-1;
int y2=getSize().height-1;
g.drawRect(x1,y1,x2,y2);
x1=x2/4;
y1=y2/4;
int wide=x2/2,high=y2/2;
if(state==XX)
{
g.drawLine(x1,y1,x1+wide,y1+high);
g.drawLine(x1,y1+high,x1+wide,y1);
}
if(state==OO)
g.drawOval(x1,y1,x1+wide/2,y1+high/2);
}
class ML extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if(state==BLANK)
{
state=turn;
turn=(turn==XX?OO:XX);
}
else
state=(state==XX?OO:XX);
repaint();
}
}
}
}
class BL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JDialog d=new ToeDialog(Integer.parseInt(rows.getText()),Integer.parseInt(cols.getText()));
d.setVisible(true);
}
}
public void init()
{
JPanel p=new JPanel();
p.setLayout(new GridLayout(2,2));
p.add(new JLabel("Rows",JLabel.CENTER));
p.add(rows);
p.add(new JLabel("Columns",JLabel.CENTER));
p.add(cols);
Container cp=getContentPane();
cp.add(p,BorderLayout.NORTH);
JButton b=new JButton("go");
b.addActionListener(new BL());
cp.add(b,BorderLayout.SOUTH);
}
public static void main(String[] args)
{
TicTacToe test=new TicTacToe();
JFrame frame=new JFrame("TicTacToe");
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.getContentPane().add(test);
frame.setSize(100,100);
test.init();
test.start();
frame.setVisible(true);
}
}
#includestdio.h
#includectype.h
#includeconio.h
int n=9, z=0, qp[10]={0};
int chkwin(int t[], int w)
{if (t[1]==w t[1]==t[2] t[2]==t[3]) return(w);
if (t[4]==w t[4]==t[5] t[5]==t[6]) return(w);
if (t[7]==w t[7]==t[8] t[8]==t[9]) return(w);
if (t[1]==w t[1]==t[4] t[4]==t[7]) return(w);
if (t[2]==w t[2]==t[5] t[5]==t[8]) return(w);
if (t[3]==w t[3]==t[6] t[6]==t[9]) return(w);
if (t[1]==w t[1]==t[5] t[5]==t[9]) return(w);
if (t[3]==w t[3]==t[5] t[5]==t[7]) return(w);
return(0);
}
long search(int n, int k, int t[])
{int i, h, f, g;
long j;
if (n==0) return(chkwin(t,k));
for (f=0, j=0,i=1; i10; i++)
if (t[i]==0)
{t[i]=k; h=chkwin(t,k);
if (h==k) f++;
else j+=search(n-1,k*-1,t);
t[i]=0;
}
if (fz) for (j=k, g=n; g0; j*=g--);
return(j);
}
/* 电脑选位置走棋,择位原则1.当前位置必须为空 */
/* 2.若走当前位置电脑能赢,则走这一步,否则转(3) */
/* 3.若当前位置被人走电脑会输,则走这一步,否则转(4) */
/* 4.试走当前位置并调用 search()函数,求走当前位置电脑的有利程度 */
/* 5.找对电脑有利程度最大的位置走 */
void cgo()
{int i, ti=0;
long j=-8000000, t=0;
for (ti=1; ti-2; ti-=2) /*ti=1,先看自己能否赢;ti=-1,看对受能否赢*/
for (i=1; i10; i++)
if (qp[i]==0)
{qp[i]=ti;
if (chkwin(qp, ti)!=0) {n--; qp[i]=1; return;}
qp[i]=0;
}
for (i=1; i10;i++)
if (qp[i]==0)
{qp[i]=1; t=search(n-1,-1,qp);
if (tj) {j=t; ti=i;}
qp[i]=0;
}
n--; qp[ti]=1;
}
/* 函数mgo(),人输入走棋位置 */
void mgo()
{int c=0;
printf ("\nPlease enter the Num to go: ");
for (c=getche(); ; printf("\n"), c=getche() )
if (isdigit(c) c!='0' qp[c-48]==0)
{n--; qp[c-48]=-1; return;
} }
/* 屏幕输出函数display,在屏幕上输出当前的棋盘 */
void display(int x)
{int i;
char t[10]={0};
for (i=1; i10; i++)
{if (qp[i]0) t[i]=88;
if (qp[i]0) t[i]=79;
}
printf ("\n%c|%c|%c\n-----\n%c|%c", t[1], t[2], t[3], t[4], t[5]);
printf ("|%c\n-----\n%c|%c|%c\n", t[6], t[7], t[8], t[9]);
if (x==0) printf("\ndraw! \n");
if (x==1) printf("\ncomputer win!\n");
if (x==2) printf("\ncontinue \n");
}
main()
{char c;
printf ("\nGo first? [Y/N]:"); /*选择谁先走*/
for (c=getche(); c!='Y'c!='y'c!='N'c!='n'; c=getche());
if (c=='N'||c=='n') {cgo(); z=1; display(2);}
while (1)
{mgo(); if (!n) {display(0); break;} /*人走,若不是最后一步,继续;否则跳出*/
cgo(); if (chkwin(qp,1)) {display(1); break;} /*电脑走,若没赢,继续;否则跳出*/
if (n) display(2); /*还没走到最后一步,继续;否则跳出*/
else {display(0); break;}
}
getchar();
return 0;
}