其實這篇學習總結應該早就要寫的,但是對技術博客確實把握的不到位,總覺得語言不夠生動,表達也略顯牽強,除了貼幾行代碼,中間的技術點就透漏的不太多了,經過一個多月的沉淪之後,這篇本來要被斃掉的心得又被我拿出來炒一炒剩飯了。(源代碼已在下面分享)
老師曾說過,學習還真的不是越學越進步的,現在回頭想想,之前所熟練的知識現在還真的慢慢的生疏起來了,我們在公司學習了如何制作java五子棋,經過反反復復的錘磨打煉,我也算是好漢一條了,但是之前由於不夠自信就一直在找借口不給自己尋找問題,就像老師說的,我們看到身邊有越來越多的牛人,相反我們卻變得更加懶惰了。
這個問題是我女朋友留給我的,我在去公司學習了不到一個月java的時間裡,就大言不讒的答應她:“寒假我寫個游戲給你玩。”新手都是這樣的,我們在成長的過程中,最奇怪的事情就是,明明比以前要厲害了,膽子卻越來越小了,只有小時候是什麼都不怕的。作為一個新手,剛寫了一個登陸界面就說自己要寫一個QQ程序,路漫漫其修遠兮,絕知此事要躬行(PS:這句話是出自兩首詩哦)。
剛一開始也沒有什麼想法,看了下書上有一章介紹連連看的,心裡想著,女生一般都比較喜歡玩連連看(我曾經就與她一起在網吧玩了兩個多小時的連連看),心裡不經的也樂了一下,不過在那天玩五子棋的時候,突然間想起了很小的時候在媽媽手機上玩的一款游戲,依稀的還記得名字好像是,沒錯,是黑白棋,在百度上面一搜索,還真的有,下面百科一下。
黑白棋,又叫Othello棋,反棋(Reversi)、蘋果棋或翻轉棋,借用莎士比亞名劇奧賽羅為其重命名。
下子的方法:
把自己顏色的棋子放在空格上,當上下左右或者斜方向上有一個自己的棋子時,夾在中間的棋子都會翻轉變成自己的棋子,並且,只有可以翻轉的地方才能下棋子。
棋盤如圖所示:

起初覺得這游戲跟五子棋應該差不了多少,反正都是黑白色的,熟不知,同樣的包裝下,卻是不一樣的內核,就像你去商店買一個蛋黃派,包裝上面明明說好了兩個的,結果打開一看,卻有四個。好的,閒話就不扯這麼多了,下面分享一下人人對戰的代碼,由於是專門為女朋友准備的,所以界面也做了很大的改革哦,代碼有點長,解釋我盡量的添加詳細了,中間被注釋掉的內容是當初建模的時候用的,大家有興趣的慢慢的看一下吧,如有錯誤,還請指點:
以下是界面類:
Java代碼
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<chess.length;i++){
for(int j=0;j<chess[i].length;j++){
chess[i][j]=0;
}
}
}
/**
* 重寫父類的paint方法
*/
public void paint(Graphics g){
super.paint(g);
Image back=new ImageIcon("image/棋盤.jpg").getImage();
g.drawImage(back, 10,30,800,600, null);
//重繪棋盤
//劃橫線
// for(int i=0;i<rows;i++){
// g.setColor(Color.BLUE);
// g.drawLine(x,y+i*size,x+size*(rows-1,y+i*size);
// }
// //劃豎線
// for(int j=0;j<cols;j++){
// g.setColor(Color.BLUE);
// g.drawLine(x+j*size,y,x+j*size,y+size*(cols-1));
// }
//繪制棋子
for(int i=0;i<rows-1;i++){
for(int j=0;j<cols-1;j++){
int X=x+size/2+size*i;//棋子的橫坐標
int Y=y+size/2+size*j;//棋子的縱坐標
if(chess[i][j]==1){
//畫黑棋
Image b=new ImageIcon("image/ali.jpg").getImage();
g.drawImage(b, X-Chess_size/2, Y-Chess_size/2, Chess_size, Chess_size, null);
//g.fillOval(X-size/2,Y-size/2,Chess_size,Chess_size);
}
else if(chess[i][j]==-1){
//畫白棋
Image w=new ImageIcon("image/taozi.jpg").getImage();
g.drawImage(w, X-Chess_size/2, Y-Chess_size/2, Chess_size, Chess_size, null);
//g.fillOval(X-size/2,Y-size/2,Chess_size,Chess_size);
}
}
}
}
public final static int x=79; //棋盤初始點橫坐標
public final static int y=83; //棋盤初始點縱坐標
public final static int rows=9; //棋盤行數
public final static int cols=9; //棋盤列數
public final static int size=61;//棋盤格子大小
public final static int Chess_size=56;//棋子大小
public final static int chess[][]=new int[rows-1][cols-1];//定義一個8*8的數組,用來保存棋子
}
下面監聽類,主要方法都在這裡面:
查看本欄目
Java代碼
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;i<Chess.rows-1;i++){
for(int j=0;j<Chess.cols-1;j++){
int x2=Chess.x+Chess.size/2+Chess.size*i;//得到棋盤中棋子所在點的橫坐標
int y2=Chess.y+Chess.size/2+Chess.size*j;//得到棋盤中棋子所在點的縱坐標
//將棋子下在棋盤格子的正中央
if(Math.abs(x1-x2)<Chess.size/3&&Math.abs(y1-y2)<Chess.size/3){
if(Chess.chess[i][j]==0){
if(stata==1){
//Image b=new ImageIcon("image/ali.jpg").getImage();
//g.drawImage(b, x2-Chess.Chess_size/2, y2-Chess.Chess_size/2, Chess.Chess_size, Chess.Chess_size, null);
//g.setColor(Color.BLACK);
stata=-1;
}
else if(stata==-1){
//Image w=new ImageIcon("image/ali.jpg").getImage();
//g.drawImage(w, x2-Chess.Chess_size/2, y2-Chess.Chess_size/2, Chess.Chess_size, Chess.Chess_size, null);
//g.setColor(Color.WHITE);
stata=1;
}
Chess.chess[i][j]=-stata;//將棋子存入棋盤
//g.fillOval(x2-Chess.size/2, y2-Chess.size/2, Chess.Chess_size, Chess.Chess_size);
//--------------如果8個方向都不能改變棋子則不改變棋盤狀態--------------
if (hengYou(i, j) == null && hengZuo(i, j) == null
&& hengShang(i, j) == null
&& hengXia(i, j) == null
&& xieyouS(i, j) == null
&& xieyouX(i, j) == null
&& xiezuoS(i, j) == null
&& xiezuoX(i, j) == null){
Chess.chess[i][j]=0;
if(stata==1){
stata=-1;
}
else if(stata==-1){
stata=1;
}
//刷新棋盤
ch.update(g);
JOptionPane.showMessageDialog(null, "不能放子");
}
else{
// 改變中間子顏色
hengYou1(i, j);
hengZuo1(i, j);
hengShang1(i, j);
hengXia1(i, j);
xieyou1(i, j);
xieyou2(i, j);
xiezuo1(i, j);
xiezuo2(i, j);
// 以坐標為圓心畫圓
//g.fillOval(x2-Chess.size/2, y2-Chess.size/2, Chess.Chess_size, Chess.Chess_size);
//g.setColor(Color.BLACK);
ch.update(g);
//改變棋子記錄框裡面的數字
alila.setText(judge()[0]+"");
taozila.setText(judge()[1]+"");
System.out.println("改變了"+change+"個子");
change=0;
//判斷當前下棋的人
if(stata==1){
jilu.setText(" 阿狸下 ");
a=1;
s="阿狸";
}
else if(stata==-1){
jilu.setText(" 桃子下 ");
a=-1;
s="桃子";
}
xiafa=Check(a);
//輸出當前下棋的人還有幾種下法
System.out.println(s+"有"+xiafa+"種下法");
if(xiafa==0&&full()==false){//如果不能下子並且棋盤未滿
te++;
JOptionPane.showMessageDialog(null, s+"不能下子,跳過!");
if(stata==1){//如果阿狸不能下子則跳過,桃子下
stata=-1;
jilu.setText(" 桃子下 ");
a=-1;
s="桃子";
}
else if(stata==-1){//如果桃子不能下子則跳過,阿狸下
stata=1;
jilu.setText(" 阿狸下 ");
a=1;
s="阿狸";
}
xiafa=Check(a);
System.out.println(s+"有"+xiafa+"種下法");
if(xiafa==0){
te++;//如果無子可下,則特殊情況記錄加1
}else
te=0;
}
//----------------勝負判斷----------------
if(judge()[0]==0){//如果阿狸沒子了則桃子獲勝
JOptionPane.showMessageDialog(null,"游戲結束,桃子獲勝");
}
else if(judge()[1]==0){//如果桃子沒子了則阿狸獲勝
JOptionPane.showMessageDialog(null, "游戲結束,阿狸獲勝");
}
if(full()){
if(judge()[0]>judge()[1]){//如果阿狸的子較多,則阿狸獲勝
JOptionPane.showMessageDialog(null, "游戲結束,阿狸獲勝");
}
else if(judge()[0]<judge()[1]){//如果桃子的子較多,則桃子獲勝
JOptionPane.showMessageDialog(null, "游戲結束,桃子獲勝");
}
else if(judge()[0]==judge()[1]){
JOptionPane.showMessageDialog(null, "平局");
}
}
}
return;
}
}
}
}
}
/**
* 判斷輸贏的方法,如果黑棋多則黑棋獲勝,否則白棋獲勝
* @return 黑棋和白棋的數目
*/
public int[] judge(){
int count[]=new int[2];
for(int i=0;i<Chess.rows-1;i++){
for(int j=0;j<Chess.cols-1;j++){
if(Chess.chess[i][j]==1){
count[0]++;
}
else if(Chess.chess[i][j]==-1){
count[1]++;
}
}
}
return count;
}
/**
* 判斷棋盤是否已滿的方法
*/
public boolean full(){
if(te==2){ //如果雙方都不能下子,則游戲結束
return true;
}
else{
for(int i=0;i<Chess.rows-1;i++){
for(int j=0;j<Chess.cols-1;j++){
//如果有一個地方是空的則返回false
if(Chess.chess[i][j]==0){
return false;
}
}
}
}
return true;
}
/**
* 判斷當前棋子的下法還有多少種
* @param return 返回下法的總數
*/
public int Check(int a){
int n=0;
for(int i=0;i<Chess.chess.length;i++){
for(int j=0;j<Chess.chess[i].length;j++){
if(Chess.chess[i][j]!=0){
continue;
}
else{
Chess.chess[i][j]=a;
if(hengYou(i, j) != null || hengZuo(i, j) != null
|| hengShang(i, j) != null || hengXia(i, j) != null
|| xieyouS(i, j) != null || xieyouX(i, j) != null
|| xiezuoS(i, j) != null || xiezuoX(i, j) != null)
{
Chess.chess[i][j]=0;
n++; //如果有一個地方可以下子,則n+1
}
else{
Chess.chess[i][j]=0;
}
}
}
}
return n;
}
/*********************************************** 檢測相同顏色 *******************************/
/**
* 改變兩棋子之間的棋子的顏色——直線
*/
public void paintChess(int r1, int c1, int r2, int c2) {
// 橫向
if (c1 == c2) {
for (int k = Math.min(r1, r2) + 1; k < Math.max(r1, r2); k++) {
Chess.chess[k][c1] = Chess.chess[r1][c1];
change++;
}
}
// 縱向
if (r1 == r2) {
for (int k = Math.min(c1, c2) + 1; k < Math.max(c1, c2); k++) {
Chess.chess[r1][k] = Chess.chess[r1][c1];
change++;
}
}
}
/**
* 改變兩棋子之間的棋子的顏色——斜線——右上
*/
public void paintChess1(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++;
}
}
/**
* 改變兩棋子之間的棋子的顏色——斜線——右下
*/
public void paintChess2(int r1, int c1, int r2, int c2) {
for (int k = Math.min(r1, r2) + 1, v = Math.min(c1, c2) + 1; k < Math
.max(r1, r2); k++, v++) {
Chess.chess[k][v] = Chess.chess[r1][c1];
change++;
}
}
/**
* 改變兩棋子之間的棋子的顏色——斜線——左上
*/
public void paintChess3(int r1, int c1, int r2, int c2) {
for (int k = Math.max(r1, r2) - 1, v = Math.max(c1, c2) - 1; k > 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;
}
}
}
image.zip (461.4 KB)