java_小小五子棋小游戏


package Gobang;

import javax.swing.JOptionPane;

public class Text {
	public static void main(String[] args)
	{
		FiveChessFrame fc = new FiveChessFrame();
	}
}

package Gobang;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

 
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageFilter;
  
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FiveChessFrame extends JFrame implements MouseListener,Runnable
{
	int wight = Toolkit.getDefaultToolkit().getScreenSize().width;//当前屏幕的宽度
	int height = Toolkit.getDefaultToolkit().getScreenSize().height;
	
	BufferedImage bgImage = null;
	
	//保存棋子的坐标
	int x = 0;
	int y = 0;
	
	//保存之前下过的所以棋子的坐标
	//其中数据内容 0 表示这个点并没有棋子,1表示这个点黑子,2表示这个点是白子
	int [][]allChess = new int[19][19];
	//标示是黑棋下 还是白棋下
	boolean isBlack = true;
	
	//标示游戏是否已经结束
	boolean canPlay = true;
	
	//保存显示的提示信息
	String message = "黑方先行";
	
	//保存最多拥有多少时间(秒);
	int maxTime = 0;
	
	//倒计时线程类
	Thread t = new Thread(this);
	
	//用来保存黑方 白方的剩余时间
	int blackTime = 0;
	int whiteTime = 0;
	
	//保存双方剩余时间的信息
	String blackMessage = "无限制";
	String whiteMessage = "无限制";
	
	public FiveChessFrame()
	{
		this.setTitle("五子棋小游戏");
		this.setSize(500,500);
		this.setLocation((wight-500)/2,(height-500)/2);//当前窗口的坐标
		this.setFocusable(false);//窗体大小是否能改变
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//和程序一起关闭
		this.setVisible(true);
		this.addMouseListener(this);
		
		t.start();
		t.suspend();
		//刷新屏幕,防止开始游戏时出现无法显示的情况
		this.repaint();
		try
		{
			bgImage = ImageIO.read(new File("e://image//1.png"));
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	
	public void paint(Graphics g)
	{
		//双缓冲技术,防止屏幕闪烁。
		BufferedImage bi = new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB);
		Graphics g2 = bi.createGraphics();
		
		// 画出背景图片
		g2.drawImage(bgImage,4,20,this);
		
		//画出棋盘线
		Color c=g2.getColor();
	    g2.setColor(Color.BLACK);
	    
		g2.setFont(new Font("楷体",Font.BOLD,20));
		g2.drawString("五子棋游戏信息: "+message,20,60);	
		
		g2.setFont(new Font("宋体",0,12));
		g2.drawString("黑方时间 "+blackMessage,30,460);
		g2.drawString("白方时间 "+whiteMessage,220,460);
		
		g2.drawString("重新开始",400,85);
		g2.drawString("游戏设置",400,135);	
		g2.drawString("游戏说明",400,185);
		g2.drawString("认输",400,285);		
		g2.drawString("关于",400,340);
		g2.drawString("退出",400,395);
		//绘制棋盘
		for(int i=0;i<19;i++)
		{
			g2.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
			g2.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
		}
		//标注点位
		g2.fillOval(68, 128, 4, 4);//上下左右点
		g2.fillOval(308, 128, 4, 4);
		g2.fillOval(308, 368, 4, 4);
		g2.fillOval(68, 368, 4, 4);
		
		g2.fillOval(308, 248, 4, 4);//四周点
		g2.fillOval(188, 128, 4, 4);
		g2.fillOval(68, 248, 4, 4);
		g2.fillOval(188, 368, 4, 4);
		
		g2.fillOval(188, 248, 4, 4);//中心点
		
		//绘制棋子
		g2.fillOval(x, y, 10, 10);
		
		for(int i=0;i<19;i++)
		{
			for(int j=0;j<19;j++)
			{
				if(allChess[i][j] == 1)
				{
					int tempx = i * 20 + 10;
					int tempy = j * 20 + 70;
					g2.fillOval(tempx - 7, tempy - 7, 14, 14);
				}
				if(allChess[i][j] == 2)
				{
					int tempx = i * 20 + 10;
					int tempy = j * 20 + 70;
					g2.setColor(Color.WHITE);
					g2.fillOval(tempx - 7, tempy - 7, 14, 14);
					g2.setColor(Color.BLACK);
					g2.drawOval(tempx - 7, tempy - 7, 14, 14);
				}
			}
		}
		g.drawImage(bi, 0, 0, this);
	}
	@Override
	public void mouseClicked(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
		
	}

	@Override
	public void mousePressed(MouseEvent e) 
	{
		if(canPlay == true)
		{
			x = e.getX();
			y = e.getY();
			if(x >= 10 && x <= 370 && y >= 70 && y <= 430)
			{
				x = (x - 10) / 20;
				y = (y - 70) / 20;
				if(allChess[x][y] == 0)
				{
					//判断当前下的是什么颜色的棋子
					if(isBlack == true)
					{
						allChess[x][y] = 1;
						isBlack = false;
						message = "轮到白方";
					}
					else
					{
						allChess[x][y] = 2;
						isBlack = true;
						message = "轮到黑方";
					}
					//判断这个棋子是否和其他棋子连成5颗,同时判断游戏是否结束
					boolean winFlag = this.checkWin();
					if(winFlag == true)
					{
						JOptionPane.showMessageDialog(this, "游戏结束 "+(allChess[x][y]==1 ? "黑方获胜" : "白方获胜"));
						canPlay = false;
					}
				}
				else
				{
					JOptionPane.showMessageDialog(this, "当前位置已经有棋子,请重新落子");
				}
				
				this.repaint();
			}
		}
		//点击开始游戏按钮。
		if(e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 70 && e.getY() <= 100)
		{
			int result = JOptionPane.showConfirmDialog(this, "是否重新开始游戏");
			if(result == 0)
			{
				canPlay = true;
				for(int i=0;i<19;i++)
				{
					for(int j=0;j<19;j++)
					{
						allChess[i][j] = 0;
					}
				}
				message = "黑方先行";
				isBlack = true;
				blackTime = maxTime;
				whiteTime = maxTime;
				if(maxTime > 0)
				{
					blackMessage = maxTime/3000+":"+( maxTime/60-maxTime/3600*60)+":"+( maxTime-maxTime/60*60 );
					whiteMessage = maxTime/3000+":"+( maxTime/60-maxTime/3600*60)+":"+( maxTime-maxTime/60*60 );
					t.resume();
				}
				else
				{
					blackMessage = "无限制";
					whiteMessage = "无限制";
				}
				this.repaint();
			}
			//JOptionPane.showMessageDialog(this, "开始游戏");
		}
		//点击,游戏设置按钮
		if(e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 120 && e.getY() <= 150)
		{
			String input = JOptionPane.showInputDialog("请输入游戏的最大限制时间(分钟),如果输入0表示没有时间限制");
			try
			{
				maxTime = Integer.parseInt(input) * 60;
				if(maxTime<0)
				{
					JOptionPane.showMessageDialog(this, "请正确输入信息,不予许输入负数");
				}
				else if(maxTime == 0)
				{
					int result = JOptionPane.showConfirmDialog(this, "设置完成,是否重新开始游戏");
					if(result == 0)
					{
						for(int i=0;i<19;i++)
						{
							for(int j=0;j<19;j++)
							{
								allChess[i][j] = 0;
							}
						}
						message = "黑方先行";
						isBlack = true;
						blackTime = maxTime;
						whiteTime = maxTime;
						blackMessage = "无限制";
						whiteMessage = "无限制";
						this.repaint();
					}
				}
				else
				{
					int result = JOptionPane.showConfirmDialog(this, "设置完成,是否重新开始游戏");
					if(result == 0)
					{
						for(int i=0;i<19;i++)
						{
							for(int j=0;j<19;j++)
							{
								allChess[i][j] = 0;
							}
						}
						message = "黑方先行";
						isBlack = true;
						blackTime = maxTime;
						whiteTime = maxTime;
						blackMessage = maxTime/3000+":"+( maxTime/60-maxTime/3600*60)+":"+( maxTime-maxTime/60*60 );
						whiteMessage = maxTime/3000+":"+( maxTime/60-maxTime/3600*60)+":"+( maxTime-maxTime/60*60 );
						t.resume();
						this.repaint();
					}
				}
			}
			catch(Exception e1)
			{
				JOptionPane.showMessageDialog(this, "请正确输入信息");
			}
		}
		//点击,游戏说明按钮
		if(e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 170 && e.getY() <= 200)
		{
			JOptionPane.showMessageDialog(this, "这是一个五子棋游戏,黑白双方轮流下棋,当某一方连到5个棋子时,判赢");
		}
		//点击,认输按钮
		if(e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 270 && e.getY() <= 300)
		{
			int result = JOptionPane.showConfirmDialog(this, "是否确定认输 ? ");
			if(result ==0)
			{
				if(isBlack)
				{
					JOptionPane.showMessageDialog(this, "黑方已经认输,游戏结束");
				}
				else
				{
					JOptionPane.showMessageDialog(this, "白方已经认输,游戏结束");
				}
				canPlay = false;
			}	
		}
		//点击,关于按钮
		if(e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 320 && e.getY() <= 350)
		{
			JOptionPane.showMessageDialog(this, "本程序是有,++++++写的。");
		}
		//点击,游戏退出
		if(e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 370 && e.getY() <= 400)
		{
			JOptionPane.showMessageDialog(this, "游戏退出");
			System.exit(0);
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public boolean checkWin()
	{
		boolean flag = false;
		//报存共同颜色的棋子共有多少个
		
		//判断当前时候有五个棋子相连,特点:纵坐标是相同的,即allChess[x][y] 中y 值是相同的。
		int color = allChess[x][y];
		//通过循环来判断 棋子相连的情况
		
		//横方向的判断
//		int count = 1;
//		int i = 1;
//		while(color == allChess[x+i][y])
//		{
//			count ++ ; i ++ ; 
//		}
//		i = 1;
//		while(color == allChess[x-i][y])
//		{	
//			count++;
//			i ++ ;
//		}
//		if(count >= 5)
//		{
//			flag = true;
//		}
//		//return flag;
//		
//		//纵向的判断
//		int i2 = 1;
//		int count2 = 1;
//		while(color == allChess[x][y+i2])
//		{
//			count2 ++;
//			i2 ++;
//		}
//		i2 = 1;
//		while(color == allChess[x][y-i2])
//		{
//			count2 ++ ;
//			i2 ++ ;
//		}
//		if(count2 >= 5)
//		{
//			flag = true;
//		}
//		
//		//斜方向的判断(右上 + 左下)
//		int i3 = 1;
//		int count3 = 1;
//		while(color == allChess[x + i3][y - i3])
//		{
//			count3 ++;
//			i3 ++;
//		}
//		i3 = 1;
//		while(color == allChess[x - i3][y + i3])
//		{
//			count3 ++ ;
//			i3 ++ ;
//		}
//		if(count3 >= 5)
//		{
//			flag = true;
//		}
//		
//		//斜方向的判断(右下 + 左上)
//		int i4 = 1;
//		int count4 = 1;
//		while(color == allChess[x + i4][y + i4])
//		{
//			count4 ++;
//			i4 ++;
//		}
//		i4 = 1;
//		while(color == allChess[x - i4][y - i4])
//		{
//			count4 ++ ;
//			i4 ++ ;
//		}
//		if(count4 >= 5)
//		{
//			flag = true;
//		}
		int count = this.CheckCount(1, 0, color);
		if(count >= 5)
		{
			flag = true;
		}
		else
		{
			count = this.CheckCount(0, 1, color);
			if(count>=5)
			{
				flag = true;
			}
			else
			{
				count = this.CheckCount(1, -1, color);
				if(count>=5)
				{
					flag = true;
				}
				else
				{
					count = this.CheckCount(1, 1, color);
					if(count>=5)
					{
						flag = true;
					}
				}
			}
		}
		return flag;
	}
	private int CheckCount(int xchenge,int ychenge,int color)
	{
		int count = 1;
		int tempx = xchenge;
		int tempy = ychenge;
		while(x + xchenge>=0 && x + xchenge<=18 && y + ychenge>=0 && y + ychenge<=18 && color==allChess[x+xchenge][y+ychenge])
		{
			count++;
			if(xchenge!=0)
			{
				xchenge++;
			}
			if(ychenge!=0)
			{
				if(ychenge>0)
				{
					ychenge++;	
				}
				else
				{
					ychenge--;
				}
			}
		}
		xchenge = tempx;
		ychenge = tempy;
		while( x - xchenge>=0 && x - xchenge<=18 && y - ychenge>=0 && y - ychenge<=18 &&color==allChess[x-xchenge][y-ychenge])
		{
			count++;
			if(xchenge!=0)
			{
				xchenge++;
			}
			if(ychenge!=0)
			{
				if(ychenge>0)
				{
					ychenge++;	
				}
				else
				{
					ychenge--;
				}
			}	
		}
		return count;
	}


	@Override
	public void run() 
	{
		//判断是否有时间的限制
		if(maxTime > 0)
		{
			while(true)
			{
				if(isBlack)
				{
					blackTime -- ;
					if(blackTime ==0 )
					{
						JOptionPane.showMessageDialog(this, "黑方超时,游戏结束");
					}
				}
				else
				{
					whiteTime-- ;
					if(whiteTime ==0 )
					{
						JOptionPane.showMessageDialog(this, "白方超时,游戏结束");
					}
				}
				blackMessage = blackTime/3000+":"+( blackTime/60-blackTime/3600*60)+":"+( blackTime-blackTime/60*60 );
				whiteMessage = whiteTime/3000+":"+( whiteTime/60-whiteTime/3600*60)+":"+( whiteTime-whiteTime/60*60 );
				this.repaint();
				try
				{
					Thread.sleep(1000);
				}catch(InterruptedException e)
				{
					e.printStackTrace();
				}
				//System.out.println(blackTime+whiteTime);
				
			}
		}
	}
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值