黑白棋(Othello)

     其实这篇学习总结应该早就要写的,但是对技术博客确实把握的不到位,总觉得语言不够生动,表达也略显牵强,除了贴几行代码,中间的技术点就透漏的不太多了,经过一个多月的沉沦之后,这篇本来要被毙掉的心得又被我拿出来炒一炒剩饭了。(源代码已在下面分享)

     老师曾说过,学习还真的不是越学越进步的,现在回头想想,之前所熟练的知识现在还真的慢慢的生疏起来了,我们在公司学习了如何制作java五子棋,经过反反复复的锤磨打炼,我也算是好汉一条了,但是之前由于不够自信就一直在找借口不给自己寻找问题,就像老师说的,我们看到身边有越来越多的牛人,相反我们却变得更加懒惰了。

    这个问题是我女朋友留给我的,我在去公司学习了不到一个月java的时间里,就大言不谗的答应她:“寒假我写个游戏给你玩。”新手都是这样的,我们在成长的过程中,最奇怪的事情就是,明明比以前要厉害了,胆子却越来越小了,只有小时候是什么都不怕的。作为一个新手,刚写了一个登陆界面就说自己要写一个QQ程序,路漫漫其修远兮,绝知此事要躬行(PS:这句话是出自两首诗哦)。

    刚一开始也没有什么想法,看了下书上有一章介绍连连看的,心里想着,女生一般都比较喜欢玩连连看(我曾经就与她一起在网吧玩了两个多小时的连连看),心里不经的也乐了一下,不过在那天玩五子棋的时候,突然间想起了很小的时候在妈妈手机上玩的一款游戏,依稀的还记得名字好像是,没错,是黑白棋,在百度上面一搜索,还真的有,下面百科一下。

    黑白棋,又叫Othello棋,反棋(Reversi)、苹果棋或翻转棋,借用莎士比亚名剧奥赛罗为其重命名。

    下子的方法:

    把自己颜色的棋子放在空格上,当上下左右或者斜方向上有一个自己的棋子时,夹在中间的棋子都会翻转变成自己的棋子,并且,只有可以翻转的地方才能下棋子。

棋盘如图所示:
黑白棋(Othello)_第1张图片


    起初觉得这游戏跟五子棋应该差不了多少,反正都是黑白色的,熟不知,同样的包装下,却是不一样的内核,就像你去商店买一个蛋黄派,包装上面明明说好了两个的,结果打开一看,却有四个。好的,闲话就不扯这么多了,下面分享一下人人对战的代码,由于是专门为女朋友准备的,所以界面也做了很大的改革哦,代码有点长,解释我尽量的添加详细了,中间被注释掉的内容是当初建模的时候用的,大家有兴趣的慢慢的看一下吧,如有错误,还请指点:

以下是界面类:

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Chess extends JFrame {
	public static void main(String args[]){
		Chess ch=new Chess();
		ch.showframe();
	}
	//显示游戏界面
	public void showframe(){
		//--------------------游戏界面----------------------
		JPanel gamejp=new JPanel();
		//--------------------组件界面----------------------
		JPanel buttonjp=new JPanel();
		//按钮图标
		ImageIcon ali=new ImageIcon("image/阿狸.jpg");
		ImageIcon taozi=new ImageIcon("image/桃子.jpg");
		ImageIcon ali1=new ImageIcon("image/阿狸1.jpg");
		ImageIcon taozi1=new ImageIcon("image/桃子1.jpg");
		JButton alibu=new JButton(ali);
		JButton taozibu=new JButton(taozi);
		alibu.setRolloverIcon(ali1);
		taozibu.setRolloverIcon(taozi1);
		alibu.setPressedIcon(ali1);
		taozibu.setPressedIcon(taozi1);
		Dimension di=new Dimension(100,100);
		alibu.setPreferredSize(di);
		taozibu.setPreferredSize(di);
		//-----------------------当前下棋的人---------------------
		final JLabel jilu=new JLabel("  阿狸下  ");
		//设置字体
		Font jilufont=new Font("黑体",Font.BOLD,30);
		jilu.setFont(jilufont);
		//用来记录阿狸与桃子的数目
		final JLabel alila=new JLabel("2");
		final JLabel taozila=new JLabel("2");
		//设置Label的字体和大小
		Font font=new Font("宋体",Font.BOLD,42);
		alila.setFont(font);
		taozila.setFont(font);
		//-----------------重新开局的方法------------------
		ImageIcon img=new ImageIcon("image/restart.jpg");
		JButton bu=new JButton(img);
		bu.setPreferredSize(new Dimension(100,40));
		buttonjp.add(jilu);
		buttonjp.add(alibu);
		buttonjp.add(alila);
		buttonjp.add(taozibu);
		buttonjp.add(taozila);
		buttonjp.add(bu);
		this.setLayout(new GridLayout(1,2,600,0));
		this.add(gamejp);
		this.add(buttonjp);
		this.setTitle("阿狸&桃子");
		this.setSize(1000,650);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
		//显示窗口
		this.setVisible(true);
		//获取画布权限
		Graphics g=this.getGraphics();
		chess[3][3]=1;
		chess[4][4]=1;
		chess[3][4]=-1;
		chess[4][3]=-1;
		ChessListener lis=new ChessListener(g,this,jilu,alila,taozila);
		this.addMouseListener(lis);
		//按钮监听器----------------重新开始
		ActionListener bulis=new ActionListener(){
			public void actionPerformed(ActionEvent e){
				clear();
				//重绘的方法
				repaint();
				//绘制初始的四个图片
				chess[3][3]=1;
				chess[4][4]=1;
				chess[3][4]=-1;
				chess[4][3]=-1;
				//将状态改为初始状态
				ChessListener.stata=1;
				jilu.setText("  阿狸下  ");
				alila.setText("2");
				taozila.setText("2");
			}
		};
		//如果点击阿狸则阿狸下
		ActionListener alilis=new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ChessListener.stata=1;
				jilu.setText("  阿狸下  ");
			}
		};
		//如果点击桃子则桃子下
		ActionListener taozilis=new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ChessListener.stata=-1;
				jilu.setText("  桃子下  ");
			}
		};
		alibu.addActionListener(alilis);
		taozibu.addActionListener(taozilis);
		bu.addActionListener(bulis);
	}
	/**
	 * 清空棋盘的方法
	 */
	public void clear(){
		for(int i=0;i

 下面监听类,主要方法都在这里面:

 

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class ChessListener extends MouseAdapter{
	private Graphics g;        //将画布对象传过来
	private int xiafa;         //记录当前棋子的下法
	public  static int stata=1;//判断下黑棋还是白棋(黑棋阿狸,白棋桃子)
	private int x1,y1;         //记录点击处的横坐标和纵坐标
	private JLabel jilu;       //当前下棋子的人
	private JLabel alila;      //阿狸棋子数
	private JLabel taozila;    //桃子棋子数
	private Chess ch;          //用来刷新棋盘
	private int   te=0;        //特殊情况
	private int   change;      //记录改变的棋子数目
	public ChessListener(Graphics g,Chess ch,JLabel jilu,JLabel alila,JLabel taozila){
		this.g=g;
		this.ch=ch;
		this.jilu=jilu;
		this.alila=alila;
		this.taozila=taozila;
	}
	
	//点击下棋子
	public void mouseClicked(MouseEvent e){
		int a=1;			//记录所下的子
		String s = null;       
		x1=e.getX();
		y1=e.getY();
		for(int i=0;ijudge()[1]){//如果阿狸的子较多,则阿狸获胜
									JOptionPane.showMessageDialog(null, "游戏结束,阿狸获胜");
								}
								else if(judge()[0] Math
				.min(r1, r2); k--, v--) {
			Chess.chess[k][v] = Chess.chess[r1][c1];
			change++;
		}
	}

	/**
	 * 改变两棋子之间的棋子的颜色——斜线——左下
	 */
	public void paintChess4(int r1, int c1, int r2, int c2) {
		for (int k = Math.min(r1, r2) + 1, v = Math.max(c1, c2) - 1; k <= Math
				.max(r1, r2); k++, v--) {
			Chess.chess[k][v] = Chess.chess[r1][c1];
			change++;
		}
	}
	
	/**
	 * 向右检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] hengYou(int x, int y) {
		int r = -2;
		int i;
		// 向右
		for (i = x + 1; i < Chess.rows-1; i++) {
			if (Chess.chess[i][y] != 1 && Chess.chess[i][y] != -1) {
				break;
			}
			if (Chess.chess[i][y] == Chess.chess[x][y]) {
				r = i;
				break;
			}
		}
		if (r != -2 && Chess.chess[x + 1][y] != Chess.chess[i][y]) {
			return new int[] { r, y };
		} else {
			// System.out.println("向右不能");
			return null;
		}
	}
	
	/**
	 * 向左检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL
	 */
	public int[] hengZuo(int x, int y) {
		int r = -2;
		int i;
		// 向左
		for (i = x - 1; i >= 0; i--) {
			if (Chess.chess[i][y] != 1 && Chess.chess[i][y] != -1) {
				break;
			}
			if (Chess.chess[i][y] == Chess.chess[x][y]) {
				r = i;
				break;
			}
		}
		if (r != -2 && Chess.chess[x - 1][y] != Chess.chess[i][y]) {

			return new int[] { r, y };
		} else {
			// System.out.println("向左不能");
			return null;
		}
	}

	/**
	 * 向上检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL
	 */
	public int[] hengShang(int x, int y) {
		int r = -2;
		int i;
		// 向上
		for (i = y - 1; i >= 0; i--) {
			if (Chess.chess[x][i] == 0) {
				break;
			}
			if (Chess.chess[x][i] == Chess.chess[x][y]) {
				r = i;
				break;
			}
		}
		if (r != -2 && Chess.chess[x][y - 1] != Chess.chess[x][i]) {

			return new int[] { x, r };
		} else {
			// System.out.println("向上不能");
			return null;
		}
	}

	/**
	 * 向上检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL
	 */
	public int[] hengShang1(int x, int y) {
		int r = -2;
		int i;
		// 向上
		for (i = y - 1; i >= 0; i--) {
			if (Chess.chess[x][i] == 0) {
				break;
			}
			if (Chess.chess[x][i] == Chess.chess[x][y]) {
				r = i;
				break;
			}
		}
		if (r != -2 && Chess.chess[x][y - 1] != Chess.chess[x][i]) {
			// 改变中间的子
			paintChess(x, y, x, r);
			return new int[] { x, r };
		} else {
			return null;
		}
	}

	/**
	 * 向下检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL
	 */
	public int[] hengXia(int x, int y) {
		int r = -2;
		int i;
		// 向下
		for (i = y + 1; i < Chess.rows-1; i++) {
			if (Chess.chess[x][i] == 0) {
				break;
			}
			if (Chess.chess[x][i] == Chess.chess[x][y]) {
				r = i;
				break;
			}
		}

		if (r != -2 && Chess.chess[x][y + 1] != Chess.chess[x][i]) {
			return new int[] { x, r };
		} else {
			// System.out.println("向下不能");
			return null;
		}
	}

	/**
	 * 向下检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL
	 */
	public int[] hengXia1(int x, int y) {
		int r = -2;
		int i;
		// 向下
		for (i = y + 1; i < Chess.rows-1; i++) {
			if (Chess.chess[x][i] == 0) {
				break;
			}
			if (Chess.chess[x][i] == Chess.chess[x][y]) {
				r = i;
				break;
			}
		}
		if (r != -2 && Chess.chess[x][y + 1] != Chess.chess[x][i]) {
			// 改变中间的子
			paintChess(x, y, x, r);
			return new int[] { x, r };
		} else {
			return null;
		}
	}

	/**
	 * 斜右上方向
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] xieyouS(int x, int y) {
		// 向上
		int r = -2, s = -2;
		int i, j;
		for (i = x + 1, j = y - 1; i < Chess.rows-1 && j >= 0; i++, j--) {
			if (Chess.chess[i][j] == 0) {
				break;
			}
			if (Chess.chess[i][j] == Chess.chess[x][y]) {
				r = i;
				s = j;
				break;
			}
		}
		if (r != -2 && s != -2
				&& Chess.chess[x + 1][y - 1] != Chess.chess[i][j]) {
			return new int[] { r, s };
		} else {
			// System.out.println("向右上不能");
			return null;
		}
	}
	
	/**
	 * 斜右下方向
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] xieyouX(int x, int y) {
		// 向下
		int r = -2, s = -2;
		int i, j;
		for (i = x + 1, j = y + 1; i < Chess.rows-1 && j < Chess.cols-1; i++, j++) {
			if (Chess.chess[i][j] == 0) {
				break;
			}
			if (Chess.chess[i][j] == Chess.chess[x][y]) {
				r = i;
				s = j;
				break;
			}
		}
		if (r != -2 && s != -2
				&& Chess.chess[x + 1][y + 1] != Chess.chess[i][j]) {
			return new int[] { r, s };
		} else {
			// System.out.println("向右下不能");
			return null;
		}
	}

	/**
	 * 斜左上方向
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] xiezuoS(int x, int y) {
		// 向上
		int r = -2, s = -2;
		int i, j;
		for (i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
			if (Chess.chess[i][j] == 0) {
				break;
			}
			if (Chess.chess[i][j] == Chess.chess[x][y]) {
				r = i;
				s = j;
				break;
			}
		}
		if (r != -2 && s != -2
				&& Chess.chess[x - 1][y - 1] != Chess.chess[i][j]) {
			return new int[] { r, s };
		} else {
			return null;
		}
	}

	/**
	 * 斜左下方向
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] xiezuoX(int x, int y) {
		// 向下
		int r = -2, s = -2;
		int i, j;
		for (i = x - 1, j = y + 1; i >= 0 && j < Chess.cols-1; i--, j++) {
			if (Chess.chess[i][j] == 0) {
				break;
			}
			if (Chess.chess[i][j] == Chess.chess[x][y]) {
				r = i;
				s = j;
				break;
			}
		}
		if (r != -2 && s != -2
				&& Chess.chess[x - 1][y + 1] != Chess.chess[i][j]) {
			return new int[] { r, s };
		} else {
			return null;
		}
	}
	
	/**
	 * 向右检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL
	 */
	public int[] hengYou1(int x, int y) {
		int r = -2;
		int i;
		// 向右
		for (i = x + 1; i < Chess.cols-1; i++) {
			if (Chess.chess[i][y] != 1 && Chess.chess[i][y] != -1) {
				break;
			}
			if (Chess.chess[i][y] == Chess.chess[x][y]) {
				r = i;
				break;
			}
		}

		if (r != -2 && Chess.chess[x + 1][y] != Chess.chess[i][y]) {
			// 改变中间的子
			paintChess(x, y, r, y);
			return new int[] { r, y };
		} else {
			return null;
		}
	}

	/**
	 * 向左检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL
	 */
	public int[] hengZuo1(int x, int y) {
		int r = -2;
		int i;
		// 向左
		for (i = x - 1; i >= 0; i--) {
			if (Chess.chess[i][y] != 1 && Chess.chess[i][y] != -1) {
				break;
			}
			if (Chess.chess[i][y] == Chess.chess[x][y]) {
				r = i;
				break;
			}
		}

		if (r != -2 && Chess.chess[x - 1][y] != Chess.chess[i][y]) {
			// 改变中间的子
			paintChess(x, y, r, y);
			return new int[] { r, y };
		} else {
			return null;
		}
	}

	/**
	 * 斜右上方向 
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] xieyou1(int x, int y) {
		// 向上
		int r = -2, s = -2;
		int i, j;
		for (i = x + 1, j = y - 1; i < Chess.rows-1 && j >= 0; i++, j--) {
			if (Chess.chess[i][j] == 0) {
				break;
			}
			if (Chess.chess[i][j] == Chess.chess[x][y]) {
				r = i;
				s = j;
				break;
			}
		}
		if (r != -2 && s != -2
				&& Chess.chess[x + 1][y - 1] != Chess.chess[i][j]) {
			// 改变中间的子
			paintChess1(x, y, i, j);
			return new int[] { r, s };
		} else {
			return null;
		}
	}

	/**
	 * 斜右下方向
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] xieyou2(int x, int y) {
		// 向下
		int r = -2, s = -2;
		int i, j;
		for (i = x + 1, j = y + 1; i < Chess.rows-1 && j < Chess.cols-1; i++, j++) {
			if (Chess.chess[i][j] == 0) {
				break;
			}
			if (Chess.chess[i][j] == Chess.chess[x][y]) {
				r = i;
				s = j;
				break;
			}
		}
		if (r != -2 && s != -2
				&& Chess.chess[x + 1][y + 1] != Chess.chess[i][j]) {
			// 改变中间的子
			paintChess2(x, y, i, j);
			return new int[] { r, s };
		} else {
			return null;
		}
	}

	/**
	 * 斜左上方向
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] xiezuo1(int x, int y) {
		// 向上
		int r = -2, s = -2;
		int i, j;
		for (i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
			if (Chess.chess[i][j] == 0) {
				break;
			}
			if (Chess.chess[i][j] == Chess.chess[x][y]) {
				r = i;
				s = j;
				break;
			}
		}
		if (r != -2 && s != -2
				&& Chess.chess[x - 1][y - 1] != Chess.chess[i][j]) {
			// 改变中间的子
			paintChess3(x, y, i, j);
			return new int[] { r, s };
		} else {
			return null;
		}
	}

	/**
	 * 斜左下方向 
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] xiezuo2(int x, int y) {
		// 向下
		int r = -2, s = -2;
		int i, j;
		for (i = x - 1, j = y + 1; i >= 0 && j < Chess.cols-1; i--, j++) {
			if (Chess.chess[i][j] == 0) {
				break;
			}
			if (Chess.chess[i][j] == Chess.chess[x][y]) {
				r = i;
				s = j;
				break;
			}
		}
		if (r != -2 && s != -2
				&& Chess.chess[x - 1][y + 1] != Chess.chess[i][j]) {
			// 改变中间的子
			paintChess4(x, y, i, j);
			return new int[] { r, s };
		} else {
			return null;
		}
	}
}

 

 所使用到的图片素材都在附件中提供,喜欢黑白棋的朋友们不妨自己来做一做吧。

你可能感兴趣的:(java,编程应用)