⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gobangout.java

📁 java applet程序
💻 JAVA
字号:
package mygobang;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Gobangout extends Applet implements MouseListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2122629424609821274L;
   
	private Button button1 = null;

	private Button button2 = null;

	private Button button3 = null;
	
	private Button button4 = null;

	private Button Buttonexit = null;
	
	Image picture;

	Panel p = new Panel();

	//记录鼠标位置
	public int mousex, mousey;

	//当前落子情况
	public int table[][] = new int[15][15];
	
	public int luozitable[][]=new int [15][15];
	
    //电脑棋型,电脑先手表
	public int computer[][][] = new int[15][15][8];

	// 玩家棋型
	public int player[][][] = new int[15][15][8];

	// 判断电脑下子
	private Gobangcomturn comt = new Gobangcomturn();

	// true当前可下子
	private boolean start = false;

	private boolean again = false;

	private boolean first = false;
	
	private boolean twoplayers =false;
	
	private boolean blackplayer = true;
	
	//显示输赢状况
	private int win = 0;

	private int lose = 0;
	
	private int win1 = 0;
	
	private int win2 = 0;
	
	//声音播放
	AudioClip clip,sound;
	
	private Button play;
	
	private Button loop;
	
	private Button stop;
	
	//设置字体
	private Font font;
	
	
	public void init() {
        //注册鼠标事件监听程序
		addMouseListener(this);
        
		resize(600, 600);

		picture = getImage(getCodeBase(), "ff.gif");

		Panel p = new Panel();
		p.add(button4 = new Button("Two players??"));
		p.add(button2 = new Button("(人机)先手"));
		p.add(button1 = new Button("开始"));
		p.add(button3 = new Button("重新开始"));
		p.add(Buttonexit = new Button("退出"));
		add("South", p);
		button3.disable();
        
		//实现“退出键”功能
		Buttonexit.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				System.exit(0);
			}
		});
		
		//播放音乐
		clip=getAudioClip(getCodeBase(),"ff.mid");
		sound=getAudioClip(getCodeBase(),"ff.wav");
		//开始播放
		play=new Button("Music Play");
		//循环播放
		loop=new Button("Loop");
		//停止播放
		stop=new Button("Stop");
		add(play);
		add(loop);
		add(stop);
		
		//设置字体
		font=new Font("Courier",Font.ITALIC,24);

	}

	public boolean action(Event e, Object o) {
		if (e.target instanceof Button)
		{
			if (e.target == button1) {
				start = true;
				//开始后,其它选择键不能再选
				button1.disable();
				button2.disable();
				button4.disable();
				//重新开始键可以使用
				button3.enable();
				win1=0;
				win2=0;
				// 初始化三个数组
				new Gobanginitarray().qinglintable(table,luozitable);
				new Gobanginitarray().qinglincap(computer, player);

				if (!first) {
					luozitable[7][7]=1;
					table[7][7] = 2;
					repaint();
				}
			} 
			else if (e.target == button2) {
				//人机,且player先开始
				first = true;
				twoplayers=false;
				button2.disable();
				button4.disable();
			} 
			else if (e.target == button3) {
				again = true;
				start = false;
				first = false;
				twoplayers=false;
                //重新开始,其它选择键可用
				button1.enable();
				button2.enable();
				button4.enable();
				button3.disable();
				// 初始化三个数组
				new Gobanginitarray().qinglintable(table,luozitable);
				new Gobanginitarray().qinglincap(computer, player);
				repaint();
			}
			else if(e.target ==button4){
				//两人对战 
				twoplayers=true;
				first=true;
				button2.disable();
				button4.disable();
			}
			else if(e.target==play)
				clip.play();
			else if(e.target==loop)
				clip.loop();
			else if(e.target==stop)
				clip.stop();
		}
		 
		return again;

	}
	
	public void paint(Graphics g) {
		
		// 画棋盘
		g.drawImage(picture, 50, 50, this);
		g.drawRect(50, 50, 490, 490);
		
		//棋盘外框
		
		//格距35
		g.drawLine(50, 85, 540, 85);
		g.drawLine(50, 120, 540, 120);
		g.drawLine(50, 155, 540, 155);
		g.drawLine(50, 190, 540, 190);
		g.drawLine(50, 225, 540, 225);
		g.drawLine(50, 260, 540, 260);
		g.drawLine(50, 295, 540, 295);
		g.drawLine(50, 330, 540, 330);
		g.drawLine(50, 365, 540, 365);
		g.drawLine(50, 400, 540, 400);
		g.drawLine(50, 435, 540, 435);
		g.drawLine(50, 470, 540, 470);
		g.drawLine(50, 505, 540, 505);
		//棋盘横格
		g.drawLine(85, 50, 85, 540);
		g.drawLine(120, 50, 120, 540);
		g.drawLine(155, 50, 155, 540);
		g.drawLine(190, 50, 190, 540);
		g.drawLine(225, 50, 225, 540);
		g.drawLine(260, 50, 260, 540);
		g.drawLine(295, 50, 295, 540);
		g.drawLine(330, 50, 330, 540);
		g.drawLine(365, 50, 365, 540);
		g.drawLine(400, 50, 400, 540);
		g.drawLine(435, 50, 435, 540);
		g.drawLine(470, 50, 470, 540);
		g.drawLine(505, 50, 505, 540);
		
		//画子
		for(int i=0;i<15;i++)
		      for(int j=0;j<15;j++)
		      {
		       if(luozitable[i][j]==1)
		       {
		           g.setColor(Color.black);
		           g.fillOval(35+i*35,35+j*35,30,30);
		           sound.play();
		       }
		       else if(luozitable[i][j]==2)
		       {
		           g.setColor(Color.white);
		           g.fillOval(35+i*35,35+j*35,30,30);
		           sound.play();
		       }         
		      } 
		
		//显示输赢状况(人机)
		if (win == 1) {
			g.setColor(Color.red);
			g.setFont(font);
			g.drawString("恭喜!你赢了!", 300, 580);
			win = 0;
		}
		if (lose == 1) {
			g.setColor(Color.red);
			g.setFont(font);
			g.drawString("你输了", 300, 580);
			lose = 0;
		}
		
		//显示输赢状况(two players)
		if(win1 == 1)
		{
			g.setColor(Color.red);
			g.setFont(font);
			g.drawString("黑棋胜!", 250, 580);
			win1=0;
		}
		if(win2 == 1)
		{
			g.setColor(Color.red);
			g.setFont(font);
			g.drawString("白棋胜!", 250, 580);
			win2=0;
		}
		
		
	}
	
	public void mouseClicked(MouseEvent e) {
		mousex = e.getX();
		mousey = e.getY();
		
		//one player and the computer,computer first
		if (start &&!twoplayers&& mousex >= 35 && mousex <= 555 
					&&mousey >= 35 && mousey <= 555&&!first) 
		{
			//首先判断该位置是否已经落子
			if(luozitable[(mousex - 35) / 35][(mousey - 35) / 35] ==0)
			{
			    luozitable[(mousex - 35) / 35][(mousey - 35) / 35] = 2;
				table[(mousex - 35) / 35][(mousey - 35) / 35]=1;
			    repaint();
				
				if (new Gobangwin().whowin(table, 1, (mousex - 35) / 35,
						(mousey - 35) / 35) == true) {
					win = 1;
					repaint();
					start = false;
				}
				
                //清零 并建立电脑和玩家棋型表

				new Gobanginitarray().qinglincap(computer, player);

				new Gobangcomqixing().ai(table, computer);
				new Gobangplaqixing().ai(table, player);

				/*
				 * 继续调用核心算法电脑下子
				 * 
				 */
				comt.comturn(table, computer, player);
				mousex = comt.getnextx();
				mousey = comt.getnexty();

				// 此处mousex,mousey为table中的点坐标
				table[mousex][mousey] = 2;
                luozitable[mousex][mousey]=1;
				repaint();
				
				if (new Gobangwin().whowin(table, 2, mousex ,mousey) == true) {
					lose = 1;
					repaint();
					start = false;
				}
			}
		}
		
        //one player and the computer,player first
		else if (start &&!twoplayers&& mousex >= 35 && mousex <= 555 
					&&mousey >= 35 && mousey <= 555&&first) 
		{
			//首先判断该位置是否已经落子
			if(luozitable[(mousex - 35) / 35][(mousey - 35) / 35] ==0)
			{
				//相应数组置数
				luozitable[(mousex - 35) / 35][(mousey - 35) / 35] = 1;
				table[(mousex - 35) / 35][(mousey - 35) / 35] = 1;
				repaint();
				
				if (new Gobangwin().whowin(table, 1, (mousex - 35) / 35,
						(mousey - 35) / 35) == true)
				{
					win = 1;
					repaint();
					start = false;
				}
				
                //	清零 并建立电脑和玩家棋型表
				new Gobanginitarray().qinglincap(computer, player);
				new Gobangcomqixing().ai(table, computer);
				new Gobangplaqixing().ai(table, player);
				
				/*
				 * 继续调用核心算法电脑下子
				 * 
				 */
				comt.comturn(table, computer, player);
				mousex = comt.getnextx();
				mousey = comt.getnexty();

				// 此处mousex,mousey为table中的点坐标
				table[mousex][mousey] = 2;
				luozitable[mousex][mousey] =2;
					
				repaint();
				
				if (new Gobangwin().whowin(table, 2,mousex ,mousey) == true) {
					lose = 1;
					repaint();
					start = false;
				}
			}
		}
		
		//two players
		else if(start&&twoplayers&&mousex >= 35 && mousex <= 555 
					&&mousey >= 35 && mousey <= 555)
		{
			if(luozitable[(mousex - 35) / 35][(mousey - 35) / 35] ==0)
			{
				//判断应是哪一方落子
				if(blackplayer)
				{
                    //相应数组置数
					luozitable[(mousex - 35) / 35][(mousey - 35) / 35] = 1;
					blackplayer=false;
				}	
				else 
				{
					luozitable[(mousex - 35) / 35][(mousey - 35) / 35] = 2;
					blackplayer=true;
				}
			    repaint();
			}
			
			//判断胜负
			//横向
			for(int i=0;i<15;i++)
		         for(int j=0;j<15;j++)
		         {
		            if(luozitable[i][j]==1&&luozitable[i][j]==luozitable[i][j+1]&&
		            	luozitable[i][j]==luozitable[i][j+2]&&luozitable[i][j]
		            ==luozitable[i][j+3]&&luozitable[i][j]==luozitable[i][j+4])
		            	{
		            	   win1=1;
		            	   start=false;
		            	   blackplayer=true;
		            	}
		            if(luozitable[i][j]==2&&luozitable[i][j]==luozitable[i][j+1]&&
		                luozitable[i][j]==luozitable[i][j+2]&&luozitable[i][j]
		               ==luozitable[i][j+3]&&luozitable[i][j]==luozitable[i][j+4])
		            	{
		            	   win2=1;
		            	   start=false;
		            	   blackplayer=true;
		            	}
		         }
			
			//纵向
			for(int i=0;i<15;i++)
		         for(int j=0;j<15;j++)
		         {
		            if(luozitable[i][j]==1&&luozitable[i][j]==luozitable[i+1][j]&&
		            	luozitable[i][j]==luozitable[i+2][j]&&luozitable[i][j]==
		            		luozitable[i+3][j]&&luozitable[i][j]==luozitable[i+4][j])
		            	{
		            	   win1=1;
		            	   start=false;
		            	   blackplayer=true;
		            	}
		            if(luozitable[i][j]==2&&luozitable[i][j]==luozitable[i+1][j]&&
		            	luozitable[i][j]==luozitable[i+2][j]&&luozitable[i][j]==
		            		luozitable[i+3][j]&&luozitable[i][j]==luozitable[i+4][j])
		            	{
		            	   win2=1;
		            	   start=false;
		            	   blackplayer=true;
		            	}
		         }
			
			//斜向,右
			for(int i=0;i<15;i++)
		         for(int j=0;j<15;j++)            
		         {
		            if(luozitable[i][j]==1&&luozitable[i][j]==luozitable[i+1][j+1]&&
		            luozitable[i][j]==luozitable[i+2][j+2]&&luozitable[i][j]==
		            	luozitable[i+3][j+3]&&luozitable[i][j]==luozitable[i+4][j+4])
		            {
		            	win1=1;
		            	start=false;
		            	blackplayer=true;
		            }
		            if(luozitable[i][j]==2&&luozitable[i][j]==luozitable[i+1][j+1]&&
		            	luozitable[i][j]== luozitable[i+2][j+2]&&luozitable[i][j]==
		            		luozitable[i+3][j+3]&&luozitable[i][j]==luozitable[i+4][j+4])
		            {
		            	win2=1;
		            	start=false;
		            	blackplayer=true;
		            }
		         }
			
			//斜向,左
			for(int i=0;i<15;i++)
		         for(int j=14;j>0;j--)
		         {
		            if(luozitable[i][j]==1&&luozitable[i][j]==luozitable[i+1][j-1]
		               &&luozitable[i][j]==luozitable[i+2][j-2]&&luozitable[i][j]==
		            	   luozitable[i+3][j-3]&&luozitable[i][j]==luozitable[i+4][j-4])
		            	{
		            	   win1=1;
		            	   start=false;
		            	   blackplayer=true;
		            	}
		            if(luozitable[i][j]==2&&luozitable[i][j]==luozitable[i+1][j-1]
		                &&luozitable[i][j]==luozitable[i+2][j-2] &&luozitable[i][j]==
		                	luozitable[i+3][j-3]&&luozitable[i][j]==luozitable[i+4][j-4])
		            {
		            	win2=1;
		            	start=false;
		            	blackplayer=true;
		            }
		         }
			
		            
			
		}
	}
	

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -