JAVA之翻转棋游戏

这是我写的另一个小游戏,界面什么的不美观,先来看下效果神马的。

曾经看过一部动漫,叫做秦时明月,里面就有这样一种棋,里面的叫法是墨攻棋局,突然想到,我也就想写写看了,花了些小时写好了这货。。。

看下源码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class Reversi extends JFrame implements ActionListener{

	JButton jbStart = new JButton("开始游戏");
	JButton jbStop = new JButton("结束游戏");
	MyPanel panel = new MyPanel();
	Container c = this.getContentPane();
	
	int width = Toolkit.getDefaultToolkit().getScreenSize().width;
	int height = Toolkit.getDefaultToolkit().getScreenSize().height;
	boolean canPlay = false;
	boolean isBlack = false;
	int count = 0;
	int f = 1;
	int cntBlack = 0;
	int cntWhite = 0;
	int reversi[][] = new int[9][9];
	
	public Reversi() {
		Init();
		addListener();
	}
	
	public void Init() {
		this.setTitle("Reversi");
		this.setSize(500, 500);
		this.setResizable(false);
		this.setLayout(null);
		this.setVisible(true);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setLocation((width - 500)/2, (height - 500)/2);
		jbStart.setBounds(135, 430, 100, 30);
		jbStop.setBounds(265, 430, 100, 30);
		panel.add(jbStart);
		panel.add(jbStop);
		
		panel.setLayout(null);
		panel.setSize(500, 500);
		c.add(panel);
	}
	
	public void addListener() {
		this.jbStart.addActionListener(this);
		this.jbStop.addActionListener(this);
		this.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
				if(canPlay) {
					if(e.getX() >= 50 && e.getX() <= 450 && e.getY() >= 50 && e.getY() <= 450) {
						//System.out.println(e.getX() + "---" + e.getY());
						int x = e.getX();
						int y = e.getY();
						x = (x - 50) / 50;
						y = (y - 50) / 50;
//						System.out.println(x + " - " + y);
						if(reversi[x][y] == 0) {
							if(isBlack) {
								if(!isOK(x, y, 1))
									return;
								reversi[x][y] = 1;
								//System.out.println(x + "-" + y + reversi[x][y]);
								//System.out.println(reversi[4][3]);
								isEated(x, y);
								repaint();
								//System.out.println(reversi[4][3]);
								isBlack = false;
								if(Check(2)) {
									if(cntBlack > cntWhite)
										JOptionPane.showMessageDialog(null, "黑子多于白子 ,黑方赢");
									else if(cntBlack < cntWhite)
										JOptionPane.showMessageDialog(null, "白子多余黑子,白方赢");
									else
										JOptionPane.showMessageDialog(null, "双方不分胜负!");
								}
								System.out.println(cntBlack + "---" + cntWhite);
							}
							else {
								if(!isOK(x, y, 2))
									return;
								reversi[x][y] = 2;
								isEated(x, y);
								repaint();
								
								isBlack = true;
								if(Check(1)) {
									if(cntBlack > cntWhite)
										JOptionPane.showMessageDialog(null, "黑子多于白子 ,黑方赢");
									else if(cntBlack < cntWhite)
										JOptionPane.showMessageDialog(null, "白子多余黑子,白方赢");
									else
										JOptionPane.showMessageDialog(null, "双方不分胜负!");
								}
								System.out.println(cntBlack + "---" + cntWhite);
							}
						}
						
						
						repaint();
					}
				}
			}
			
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == jbStart) {
			canPlay = true;
			jbStart.setEnabled(false);
			InitFrame();
			repaint();
		}
		if(e.getSource() == jbStop) {
			System.exit(0);
		}
	}
	
	public class MyPanel extends JPanel {
		public void paintComponent(Graphics scr){
			
			BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
			Graphics g = bi.createGraphics();
			
			g.setColor(Color.BLACK);
			
			for(int i = 1; i <= 9; i++) {
				g.drawLine(i*50, 20, i*50, 420);
			}
			for(int i = 1; i <= 9; i++) {
				g.drawLine(50, i*50-30, 450, i*50-30);
			}
			
			for(int i = 0; i <= 7; i++) {
				for(int j = 0; j <= 7; j++) {
					int x;
					int y;
					x = i * 50 + 50;
					y = j * 50 + 20;
					if(reversi[i][j] == 1) {
						g.fillOval(x, y, 50, 50);
					}
					else if(reversi[i][j] == 2) {
						g.setColor(Color.WHITE);
						g.fillOval(x, y, 50, 50);
						g.setColor(Color.BLACK);
						g.drawOval(x, y, 50, 50);
					}
				}
			}
			scr.drawImage(bi, 0, 0, this);
		}
	}
	
	public void isEated(int x, int y) {
		
		int color;
		
		if(isBlack)
			color = 1;
		else
			color = 2;
		
		int flag = 0;
		int tempx = x;
		int tempy = y;
		while(tempx > 0) { //左
			tempx--;
			if(reversi[tempx][tempy] == color) {
				for(int i = tempx + 1; i <= x; i++) {
					if(reversi[i][tempy] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = tempx + 1; i <= x; i++) {
					reversi[i][tempy] = color;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx < 7) { //右
			tempx++;
			if(reversi[tempx][tempy] == color) {
				for(int i = x + 1; i <= tempx; i++) {
					if(reversi[i][tempy] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = x + 1; i <= tempx; i++) {
					reversi[i][tempy] = color;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempy > 0) { //上
			tempy--;
			if(reversi[tempx][tempy] == color) {
				for(int i = tempy + 1; i <= y; i++) {
					if(reversi[tempx][i] == 0) 
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = tempy + 1; i <= y; i++) {
					reversi[tempx][i] = color;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempy < 7) { //下
			tempy++;
			if(reversi[tempx][tempy] == color) {
				for(int i = y + 1; i <= tempy; i++) {
					if(reversi[tempx][i] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = y + 1; i <= tempy; i++) {
					reversi[tempx][i] = color;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx > 0 && tempy > 0) { //左上
			tempx--;
			tempy--;
			if(reversi[tempx][tempy] == color) {
				for(int i = tempx + 1, j  = tempy + 1; i <= x && j <= y; i++, j++) {
					if(reversi[i][j] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = tempx + 1, j  = tempy + 1; i <= x && j <= y; i++, j++) {
					reversi[i][j] = color;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx > 0 && tempy < 7) { //左下
			tempx--;
			tempy++;
			if(reversi[tempx][tempy] == color) {
				for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {
					if(reversi[i][j] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {
					reversi[i][j] = color;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx < 7 && tempy > 0) { //右上
			tempx++;
			tempy--;
			if(reversi[tempx][tempy] == color) {
				//System.out.println("11");
				for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {
					//System.out.println(i + "--" + j + reversi[i][j]);
					if(reversi[i][j] == 0) {
						flag = 1;
						System.out.println(i + "----" + j + reversi[i][j]);
					}
				}
				if(flag == 1)
					break;
				for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {
					reversi[i][j] = color;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx < 7 && tempy < 7) { //右下
			tempx++;
			tempy++;
			if(reversi[tempx][tempy] == color) {
				for(int i = x + 1, j  = y + 1; i <= tempx && j <= tempy; i++, j++) {
					if(reversi[i][j] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = x + 1, j  = y + 1; i <= tempx && j <= tempy; i++, j++) {
					reversi[i][j] = color;
				}
				break;
			}
		}
	}
	
public boolean isOK(int x, int y, int color) {
		
		f = 0;
		int flag = 0;
		int tempx = x;
		int tempy = y;
		while(tempx > 0) { //左
			tempx--;
			if(reversi[tempx][tempy] == color) {
				for(int i = tempx + 1; i < x; i++) {
					if(reversi[i][tempy] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = tempx + 1; i < x; i++) {
					//reversi[i][tempy] = color;
					f = 1;
				}
				
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx < 7) { //右
			tempx++;
			if(reversi[tempx][tempy] == color) {
				for(int i = x + 1; i < tempx; i++) {
					if(reversi[i][tempy] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = x + 1; i < tempx; i++) {
					//reversi[i][tempy] = color;
					f = 1;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempy > 0) { //上
			tempy--;
			if(reversi[tempx][tempy] == color) {
				for(int i = tempy + 1; i < y; i++) {
					if(reversi[tempx][i] == 0) 
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = tempy + 1; i < y; i++) {
					//reversi[tempx][i] = color;
					f = 1;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempy < 7) { //下
			tempy++;
			if(reversi[tempx][tempy] == color) {
				for(int i = y + 1; i < tempy; i++) {
					if(reversi[tempx][i] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = y + 1; i < tempy; i++) {
					//reversi[tempx][i] = color;
					f = 1;
				}
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx > 0 && tempy > 0) { //左上
			tempx--;
			tempy--;
			if(reversi[tempx][tempy] == color) {
				for(int i = tempx + 1, j  = tempy + 1; i < x && j < y; i++, j++) {
					if(reversi[i][j] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = tempx + 1, j  = tempy + 1; i < x && j < y; i++, j++) {
					//reversi[i][j] = color;
					f = 1;
				}
				
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx > 0 && tempy < 7) { //左下
			tempx--;
			tempy++;
			if(reversi[tempx][tempy] == color) {
				for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {
					if(reversi[i][j] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {
				//	reversi[i][j] = color;
					f = 1;
				}
				
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx < 7 && tempy > 0) { //右上
			tempx++;
			tempy--;
			if(reversi[tempx][tempy] == color) {
				//System.out.println("11");
				for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {
					//System.out.println(i + "--" + j + reversi[i][j]);
					if(reversi[i][j] == 0) {
						flag = 1;
						//System.out.println(i + "----" + j + reversi[i][j]);
					}
				}
				if(flag == 1)
					break;
				for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {
					//reversi[i][j] = color;
					f = 1;
				}
				
				break;
			}
		}
		flag = 0;
		tempx = x;
		tempy = y;
		while(tempx < 7 && tempy < 7) { //右下
			tempx++;
			tempy++;
			if(reversi[tempx][tempy] == color) {
				for(int i = x + 1, j  = y + 1; i < tempx && j < tempy; i++, j++) {
					if(reversi[i][j] == 0)
						flag = 1;
				}
				if(flag == 1)
					break;
				for(int i = x + 1, j  = y + 1; i < tempx && j < tempy; i++, j++) {
					//reversi[i][j] = color;
					f = 1;
				}
				
				break;
			}
		}
		if(f == 1)
			return true;
		else
			return false;
	}
	
	public boolean Check(int color) {
		
//		for(int i = 0; i <= 7; i++) {
//			for(int j = 0; j <= 7; j++) {
//				if(reversi[i][j] == color) 
//					return false;
//				else if(reversi[i][j] == color % 2 + 1)
//					count++;
//			}
//		}
//		return true;
		cntBlack = 0;
		cntWhite = 0;
		int cnt = 0;
		for(int i = 0; i <= 7; i++) {
			for(int j = 0; j <= 7; j++) {
				
				
				if(reversi[i][j] == 1) {
					cntBlack++;
				}
				else if(reversi[i][j] == 2) {
					cntWhite++;
				}
				else {
					cnt++;
				}
			}
		}
		
		for(int i = 0; i <= 7; i++) {
			for(int j = 0; j <= 7; j++) {
				if(reversi[i][j] == 0 && isOK(i, j, color))
					return false;
			}
		}
		JOptionPane.showMessageDialog(null, color == 1?"黑子无处可下,换白子行动":"白子无处可下,换黑子行动");
		color = color % 2 + 1;
		
		for(int i = 0; i <= 7; i++) {
			for(int j = 0; j <= 7; j++) {
				if(reversi[i][j] == 0 && isOK(i, j, color)) {
					if(color == 1) {
						isBlack = true;
					}
					else {
						isBlack = false;
					}
					return false;
				}
					
			}
		}
		JOptionPane.showMessageDialog(null, "双方均无处可下,游戏结束");
		return true;
	}
	
	public void InitFrame() {
		reversi[3][3] = reversi[4][3] = 1;
		reversi[3][4] = reversi[4][4] = 2;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Reversi();
	}
}

游戏基本没什么BUG了,我改了好久T T,注释什么的也没写,哎呀,好失败-。-算了,就那样了~ 

有什么问题欢迎指出。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值