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=2){
							int count=0;
							for(int k=x;k=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=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=2){
								 int yy=y;
								 int count=0;
								 for(int k=x;k= 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


效果图:

JAVA 黑白棋_第1张图片




  

你可能感兴趣的:(JAVA)