JAVA 黑白棋

界面实现 文件读写 

主要是要判断放置一个棋子是否符合要求 

比如放置了一个黑色棋子 在水平、竖直、斜线方向上 找到一个黑色棋子  判断这两个棋子之间是否都是白色棋子

如果符合要求 则要将其中的白色棋子都转换为黑色棋子

package src;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.*;


/**
 * @author Lenovo
 * 构造方法  实现界面 为按钮添加事件监听器
 */
public class Othello extends JFrame{
	
	private boolean isBlackPlay=true;//是否是黑棋先手
	boolean gameover=false;
	private int blackChess=0,whiteChess=0;
	private int[][] data=new int[4][4];
	
	private JPanel p1=new JPanel();
	private JButton renewB=new JButton("重新开始");
	private JButton saveB=new JButton("保存");
	private JButton openB=new JButton("打开");
	private JButton exitB=new JButton("退出");
	
	private JPanel p2=new JPanel();
	private JButton button[][]=new JButton[4][4];
	
	public Othello(){
		Container c=this.getContentPane();
		c.add(p1, BorderLayout.NORTH);
		p1.add(renewB);
		p1.add(saveB);
		p1.add(openB);
		p1.add(exitB);
		
		c.add(p2, BorderLayout.CENTER);
		p2.setLayout(new GridLayout(4, 4));
		
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				button[i][j]=new JButton("");
			}
		}
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				p2.add(button[i][j]);
				button[i][j].setBackground(Color.GREEN);
				button[i][j].addActionListener(new Handler(i, j));
			}
		}
		
		button[1][1].setBackground(Color.WHITE);
		button[1][2].setBackground(Color.BLACK);
		button[2][1].setBackground(Color.BLACK);
		button[2][2].setBackground(Color.WHITE);
		
		renewB.addActionListener(new renewHandler());		
		saveB.addActionListener(new saveHandler());
		openB.addActionListener(new openHandler());
		exitB.addActionListener(new exitHandler());
	}
	
	/**
	 * @author Lenovo
	 * 实现重新开始按钮功能 初始化界面参数
	 */
	class renewHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			for(int i=0;i<4;i++){
				for(int j=0;j<4;j++){
					button[i][j].setBackground(Color.GREEN);
				}
			}
			button[1][1].setBackground(Color.WHITE);
			button[1][2].setBackground(Color.BLACK);
			button[2][1].setBackground(Color.BLACK);
			button[2][2].setBackground(Color.WHITE);
			
			isBlackPlay=true;
			blackChess=0;
			whiteChess=0;
		}
		
	}
	
	/**
	 * @author Lenovo
	 * 实现保存按钮的功能  将数据保存到文件
	 */
	class saveHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			for(int i=0;i<4;i++){
				for(int j=0;j<4;j++){
					if(button[i][j].getBackground()==Color.BLACK)
						data[i][j]=1;
					else if(button[i][j].getBackground()==Color.WHITE)
						data[i][j]=-1;
					else data[i][j]=0;//空白棋盘为0
				}
			}
			File file=new File("output.txt");
			try{
				BufferedWriter bw=new BufferedWriter(new FileWriter(file));
				for(int i=0;i<4;i++){
					for(int j=0;j<4;j++){
						if(j!=0)
							bw.write(",");
						bw.write(String.valueOf(data[i][j]));
					}
					bw.write("\r\n");
				}
				bw.write(isBlackPlay+"\r\n");
				bw.write("0表示空白,1表示黑棋,-1表示白棋。true表示该黑棋走了");
				bw.flush();
				bw.close();
			}catch (Exception e1) {
				// TODO: handle exception
				e1.printStackTrace();
			}
			
		}
		
	}
	
	/**
	 * @author Lenovo
	 * 实现打开按钮的功能 恢复文件中棋盘中的状态
	 */
	class openHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			try{
				File file=new File("output.txt");
				BufferedReader br=new BufferedReader(new FileReader(file));
				String line=br.readLine();
				int num=0;
				while(line!=null&&num<4){
					String[] temp=line.split(",");
					for(int i=0;i<temp.length;i++){
						data[num][i]=Integer.parseInt(temp[i]);
					}
					line=br.readLine();
					num++;
				}
				isBlackPlay=Boolean.parseBoolean(line);
				for(int i=0;i<4;i++){
					for(int j=0;j<4;j++){
						if(data[i][j]==1){
							button[i][j].setBackground(Color.BLACK);
						}else if(data[i][j]==-1){
							button[i][j].setBackground(Color.WHITE);
						}else{
							button[i][j].setBackground(Color.GREEN);
						}
					}
				}
				String s=new String();
				if(isBlackPlay)
					s="黑";
				else s="白";
				JOptionPane.showMessageDialog(null, "打开成功 后面到"+s+"方下");
			}catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}
	
	class exitHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			System.exit(0);
		}
		
	}
	
	class Handler implements ActionListener{
		
		int row=-1,col=-1;
		
		public Handler(int x,int y){
			row=x;
			col=y;
		}
		
		private boolean isValidPosition(int x,int y){
			if(button[x][y].getBackground()!=Color.GREEN)
				return false;
			if(isBlackPlay==true){//黑方下
				for(int i=0;i<4;i++){//垂直方向判断
					if(button[i][y].getBackground()==Color.BLACK){
						if((i-x)>=2){
							int count=0;
							for(int k=x;k<i;k++){
								if(button[k][y].getBackground()==Color.white){
									count++;
								}
							}
							if(count==(i-x-1)){//如果两个黑子之间夹得都是白子
								return true;
							}
						}
						if((x-i)>=2){
							int count=0;
							for(int k=x;k>i;k--){
								if(button[k][y].getBackground()==Color.WHITE){
									count++;
								}
							}
							if(count==(x-i-1))
								return true;
						}
					}
				}
				//水平方向判断
				for(int j=0;j<4;j++){
					if(button[x][j].getBackground()==Color.BLACK){
						if((j-y)>=2){
							int count=0;
							for(int k=y;k<j;k++){
								if(button[x][k].getBackground()==Color.WHITE)
									count++;
							}
							if(count==(j-y-1))
								return true;
						}
						if((y-j)>=2){
							int count=0;
							for(int k=y;k>j;k--){
								if(button[x][k].getBackground()==Color.white)
									count++;
							}
							if(count==(y-j-1))
								return true;
						}
					}
				}
				//斜线方向上判断
				for(int i=0;i<4;i++){
					 for(int j=0;j<4;j++){
						 if(button[i][j].getBackground()==Color.BLACK){
							 if((x-i)==(y-j)&&(x-i)>=2){
								 int yy=y;
								 int count=0;
								 for(int k=x;k>i;k--){
									 if(button[k][yy].getBackground()==Color.WHITE)
										 count++;
									 yy--;
								 }
								 if(count==(x-i-1))
									 return true;
							 }
							 if((x-i)==(j-y)&&(x-i)>=2){
								 int yy=y;
								 int count=0;
								 for(int k=x;k>i;k--){
									 if(button[k][yy].getBackground()==Color.WHITE){
										 count++;
									 }
									 yy++;
								 }
								 if(count==(x-i-1))
									 return true;
							 }
							 if((i-x)==(y-j)&&(i-x)>=2){
								 int yy=y;
								 int count=0;
								 for(int k=x;k<i;k++){
									 if(button[k][yy].getBackground()==Color.WHITE)
										 count++;
									 yy--;
								 }
								 if(count==(i-x-1))
									 return true;
							 }
							 if((i-x)==(j-y)&&(i-x)>=2){
								 int yy=y;
								 int count=0;
								 for(int k=x;k<i;k++){
									 if(button[k][yy].getBackground()==Color.WHITE)
										 count++;
									 yy++;
								 }
								 if(count==(i-x-1))
									 return true;
							 }
						 }
					 }
				}
				return false;
			}
			else{
				for (int i = 0; i < 4; i++)
				{
					if (button[i][y].getBackground() == Color.WHITE)
					{
						if ((i - x) >= 2)
						{
							int count = 0;
							for (int k = x; k < i; k++)
							{
								if (button[k][y].getBackground() == Color.BLACK)
									count++;
							}
							if (count == (i - x - 1))
								return true;
						}
						if ((x - i) >= 2)
						{
							int count = 0;
							for (int k = x; k > i; k--)
							{
								if (button[k][y].getBackground() == Color.BLACK)
									count++;
							}
							if (count == (x - i - 1))
								return true;
						}
					}
				}

				for (int j = 0; j < 4; j++)
				{
					if (button[x][j].getBackground() == Color.WHITE)
					{
						if ((j - y) >= 2)
						{
							int count = 0;
							for (int k = y; k < j; k++)
							{
								if (button[x][k].getBackground() == Color.BLACK)
									count++;
							}
							if (count == (j - y - 1))
								return true;
						}

						if ((y - j) >= 2)
						{
							int count = 0;
							for (int k = y; k > j; k--)
							{
								if (button[x][k].getBackground() == Color.BLACK)
									count++;
							}
							if (count == (y - j - 1))
								return true;
						}

					}
				}

				for (int i = 0; i < 4; i++)
				{
					for (int j = 0; j < 4; j++)
					{
						if (button[i][j].getBackground() == Color.WHITE)
						{
							if ((x - i) == (y - j) && (x - i) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k > i; k--)
								{
									if (button[k][yy].getBackground() == Color.BLACK)
									{
										count++;
									}
									yy--;

								}
								if (count == (x - i - 1))
									return true;
							}

							if ((x - i) == (j - y) && (x - i) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k > i; k--)
								{
									if (button[k][yy].getBackground() == Color.BLACK)
									{
										count++;
									}
									yy++;

								}
								if (count == (x - i - 1))
									return true;
							}

							if ((i - x) == (y - j) && (i - x) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k < i; k++)
								{
									if (button[k][yy].getBackground() == Color.BLACK)
									{
										count++;
									}
									yy--;
								}
								if (count == (i - x - 1))
									return true;
							}

							if ((i - x) == (j - y) && (i - x) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k < i; k++)
								{
									if (button[k][yy].getBackground() == Color.BLACK)
									{
										count++;
									}
									yy++;
								}
								if (count == (i - x - 1))
									return true;
							}

						}
					}
				}

				return false;
			}
		}
		
		
		private void refresh(int x, int y)
		{
			if (isBlackPlay == true)// 若是黑方下
			{
				for (int i = 0; i < 4; i++)
				{
					if (button[i][y].getBackground() == Color.BLACK)
					{
						if ((i - x) >= 2)
						{
							int count = 0;
							for (int k = x; k < i; k++)
							{
								if (button[k][y].getBackground() == Color.WHITE)
									count++;
							}

							if (count == (i - x - 1))
							{
								for (int k = x; k < i; k++)
									button[k][y].setBackground(Color.BLACK);
							}

						}

						if ((x - i) >= 2)
						{
							int count = 0;// /
							for (int k = x; k > i; k--)
							{
								if (button[k][y].getBackground() == Color.WHITE)
									count++;
							}

							if (count == (x - i - 1))
							{
								for (int k = x; k > i; k--)
									button[k][y].setBackground(Color.BLACK);
							}
						}
					}
				}

				for (int j = 0; j < 4; j++)
				{
					if (button[x][j].getBackground() == Color.BLACK)
					{
						if ((j - y) >= 2)
						{
							int count = 0;
							for (int k = y; k < j; k++)
							{
								if (button[x][k].getBackground() == Color.WHITE)
									count++;
							}

							if (count == (j - y - 1))
							{
								for (int k = y; k < j; k++)
									button[x][k].setBackground(Color.BLACK);
							}
						}

						if ((y - j) >= 2)
						{
							int count = 0;
							for (int k = y; k > j; k--)
							{
								if (button[x][k].getBackground() == Color.WHITE)
									count++;
							}
							if (count == (y - j - 1))
							{
								for (int k = y; k > j; k--)
									button[x][k].setBackground(Color.BLACK);
							}
						}
					}
				}

				for (int i = 0; i < 4; i++)
				{
					for (int j = 0; j < 4; j++)
					{
						if (button[i][j].getBackground() == Color.BLACK)
						{
							if ((x - i) == (y - j) && (x - i) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k > i; k--)
								{
									if (button[k][yy].getBackground() == Color.WHITE)
									{
										count++;
									}
									yy--;

								}
								if (count == (x - i - 1))
								{
									yy = y;
									for (int k = x; k > i; k--)
									{
										button[k][yy].setBackground(Color.BLACK);
										yy--;
									}
								}
							}

							if ((x - i) == (j - y) && (x - i) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k > i; k--)
								{
									if (button[k][yy].getBackground() == Color.WHITE)
									{
										count++;
									}
									yy++;

								}
								if (count == (x - i - 1))
								{
									yy = y;
									for (int k = x; k > i; k--)
									{
										button[k][yy].setBackground(Color.BLACK);
										yy++;
									}
								}
							}

							if ((i - x) == (y - j) && (i - x) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k < i; k++)
								{
									if (button[k][yy].getBackground() == Color.WHITE)
									{
										count++;
									}
									yy--;

								}
								if (count == (i - x - 1))
								{
									yy = y;
									for (int k = x; k < i; k++)
									{
										button[k][yy].setBackground(Color.BLACK);
										yy--;
									}
								}
							}

							if ((i - x) == (j - y) && (i - x) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k < i; k++)
								{
									if (button[k][yy].getBackground() == Color.WHITE)
									{
										count++;
									}
									yy++;

								}
								if (count == (i - x - 1))
								{
									yy = y;
									for (int k = x; k < i; k++)
									{
										button[k][yy].setBackground(Color.BLACK);
										yy++;
									}
								}
							}

						}
					}
				}

			}
			else
			// 若是白方下
			{
				for (int i = 0; i < 4; i++)
				{
					if (button[i][y].getBackground() == Color.WHITE)
					{
						if ((i - x) >= 2)
						{
							int count = 0;
							for (int k = x; k < i; k++)
							{
								if (button[k][y].getBackground() == Color.BLACK)
									count++;
							}

							if (count == (i - x - 1))
							{
								for (int k = x; k < i; k++)
									button[k][y].setBackground(Color.WHITE);
							}

						}
						if ((x - i) >= 2)
						{
							int count = 0;
							for (int k = x; k > i; k--)
							{
								if (button[k][y].getBackground() == Color.BLACK)
									count++;
							}

							if (count == (x - i - 1))
							{
								for (int k = x; k > i; k--)
									button[k][y].setBackground(Color.WHITE);
							}
						}
					}
				}

				for (int j = 0; j < 4; j++)
				{
					if (button[x][j].getBackground() == Color.WHITE)
					{
						if ((j - y) >= 2)
						{
							int count = 0;
							for (int k = y; k < j; k++)
							{
								if (button[x][k].getBackground() == Color.BLACK)
									count++;
							}

							if (count == (j - y - 1))
							{
								for (int k = y; k < j; k++)
									button[x][k].setBackground(Color.WHITE);
							}
						}
						if ((y - j) >= 2)
						{
							int count = 0;
							for (int k = y; k > j; k--)
							{
								if (button[x][k].getBackground() == Color.BLACK)
									count++;
							}
							if (count == (y - j - 1))
							{
								for (int k = y; k > j; k--)
									button[x][k].setBackground(Color.WHITE);
							}
						}
					}
				}

				for (int i = 0; i < 4; i++)
				{
					for (int j = 0; j < 4; j++)
					{
						if (button[i][j].getBackground() == Color.WHITE)
						{
							if ((x - i) == (y - j) && (x - i) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k > i; k--)
								{
									if (button[k][yy].getBackground() == Color.BLACK)
									{
										count++;
									}
									yy--;
								}
								if (count == (x - i - 1))
								{
									yy = y;
									for (int k = x; k > i; k--)
									{
										button[k][yy].setBackground(Color.WHITE);
										yy--;
									}
								}
							}

							if ((x - i) == (j - y) && (x - i) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k > i; k--)
								{
									if (button[k][yy].getBackground() == Color.BLACK)
									{
										count++;
									}
									yy++;

								}
								if (count == (x - i - 1))
								{
									yy = y;
									for (int k = x; k > i; k--)
									{
										button[k][yy].setBackground(Color.WHITE);
										yy++;
									}
								}
							}
							if ((i - x) == (y - j) && (i - x) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k < i; k++)
								{
									if (button[k][yy].getBackground() == Color.BLACK)
									{
										count++;
									}
									yy--;

								}
								if (count == (i - x - 1))
								{
									yy = y;
									for (int k = x; k < i; k++)
									{
										button[k][yy].setBackground(Color.WHITE);
										yy--;
									}
								}
							}
							if ((i - x) == (j - y) && (i - x) >= 2)
							{
								int yy = y;
								int count = 0;
								for (int k = x; k < i; k++)
								{
									if (button[k][yy].getBackground() == Color.BLACK)
									{
										count++;
									}
									yy++;

								}
								if (count == (i - x - 1))
								{
									yy = y;
									for (int k = x; k < i; k++)
									{
										button[k][yy].setBackground(Color.WHITE);
										yy++;

									}
								}
							}

						}
					}
				}

			}

		}
		
		/**
		 * @return
		 * 判断是否还有位置可下
		 */
		private boolean hasValidPosition(){
			for(int i=0;i<4;i++){
				for(int j=0;j<4;j++){
					if(isValidPosition(i, j))
						return true;
				}
			}
			return false;
		}
		
		public void changePlayer(){
			if(isBlackPlay==true)
				isBlackPlay=false;
			else isBlackPlay=true;
		}
		
		private void whoWin(){
			for(int i=0;i<4;i++){
				for(int j=0;j<4;j++){
					if(button[i][j].getBackground()==Color.BLACK)
						blackChess++;
					if(button[i][j].getBackground()==Color.WHITE)
						whiteChess++;
				}
			}
			if(blackChess>whiteChess)
				JOptionPane.showMessageDialog(null, "恭喜黑方获胜!");
			else if(blackChess<whiteChess)
				JOptionPane.showMessageDialog(null, "恭喜白方获胜!");
			else 
				JOptionPane.showMessageDialog(null, "平局了!");
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(gameover==true){
				JOptionPane.showMessageDialog(null, "棋局已经结束 请重新开始");
				return;
			}else{
				if(!isValidPosition(row, col)){
					JOptionPane.showMessageDialog(null, "非法位置 请重新放置");
					return;
				}else{
					refresh(row, col);
					changePlayer();
					if(!hasValidPosition()){
						changePlayer();
						{
							if(!hasValidPosition()){
								gameover=true;
								whoWin();
							}else{
								JOptionPane.showMessageDialog(null, "对方已无处可下 己方继续");
								return;
							}
						}
					}
					else 
						return;
				}
			}
		}
		
	}
	
	
	public static void main(String[] args){
		Othello othello=new Othello();
		othello.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		othello.setSize(350,400);
		othello.setVisible(true);
	}
	
}


效果图:





  

  • 1
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值