汇智学堂-JAVA桌面游戏开发(五子棋之四:落子及判断输赢)
之四:落子及判断输赢
“程序主界面”上落子,判断输赢,效果见下图:
准备工作:
Images文件夹下,放置black.png及white.png图片一张。
代码实现:
package com.fivechess.view;
import javax.swing.JOptionPane;
public class WhichWin extends ChessBoard2{
//判断上下输赢
public void upOrDown(int x1,int y1,int[][] position,int whichColor) {
int sum=1;
//向上判断
for(int i=0;i<5;i++) {
if(position[x1][y1]==0) {
return;
}
if(position[x1][y1-i]==position[x1][y1-i-1]) {
sum+=1;
}
else {
break;
}
}
//向下判断
for(int i=0;i<5;i++) {
if(position[x1][y1]==0) {
return;
}
if(position[x1][y1+i]==position[x1][y1+i+1]) {
sum+=1;
}
else {
break;
}
}
if(sum>=5) {
if(whichColor==1) {
JOptionPane.showMessageDialog(null,"白棋胜利,游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(whichColor==0) {
JOptionPane.showMessageDialog(null,"黑棋胜利,游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
PCChessBoard2.isClose=1;
initStart();
}
}
//判断左右输赢
public void leftOrRight(int x1,int y1,int[][] position,int whichColor) {
int sum=1;
//向左判断
for(int i=0;i<5;i++) {
if(position[x1][y1]==0) {
return;
}
if(position[x1-i][y1]==position[x1-i-1][y1]) {
sum+=1;
}
else {
break;
}
}
//向右判断
for(int i=0;i<5;i++) {
if(position[x1][y1]==0) {
return;
}
if(position[x1+i][y1]==position[x1+i+1][y1]) {
sum+=1;
}
else {
break;
}
}
if(sum>=5) {
if(whichColor==1) {
JOptionPane.showMessageDialog(null,"白棋胜利,游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(whichColor==0) {
JOptionPane.showMessageDialog(null,"黑棋胜利,游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
PCChessBoard2.isClose=1;
}
}
//判断左上及右下输赢
public void Obliquedirection(int x1,int y1,int[][] position,int whichColor) {
int sum=1;
//向左上判断
for(int i=0;i<5;i++) {
if(position[x1][y1]==0) {
return;
}
if(position[x1-i][y1-i]==position[x1-i-1][y1-i-1]) {
sum+=1;
}
else {
break;
}
}
//向右判断
for(int i=0;i<5;i++) {
if(position[x1][y1]==0) {
return;
}
if(position[x1+i][y1+i]==position[x1+i+1][y1+i+1]) {
sum+=1;
}
else {
break;
}
}
if(sum>=5) {
if(whichColor==1) {
JOptionPane.showMessageDialog(null,"白棋胜利,游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(whichColor==0) {
JOptionPane.showMessageDialog(null,"黑棋胜利,游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
PCChessBoard2.isClose=1;
}
}
//判断右上及左下输赢
public void Obliquedirection2(int x1,int y1,int[][] position,int whichColor) {
int sum=1;
//向右上判断
for(int i=0;i<5;i++) {
if(position[x1][y1]==0) {
return;
}
if(position[x1+i][y1-i]==position[x1+i+1][y1-i-1]) {
sum+=1;
}
else {
break;
}
}
//向左下判断
for(int i=0;i<5;i++) {
if(position[x1][y1]==0) {
return;
}
if(position[x1-i][y1+i]==position[x1-i-1][y1+i+1]) {
sum+=1;
}
else {
break;
}
}
if(sum>=5) {
if(whichColor==1) {
JOptionPane.showMessageDialog(null,"白棋胜利,游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(whichColor==0) {
JOptionPane.showMessageDialog(null,"黑棋胜利,游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
PCChessBoard2.isClose=1;
}
}
}
修改后PCChessBoard2 类的代码
package com.fivechess.view;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
public class PCChessBoard2 extends ChessBoard2{
public int whichColor=0;
private int ex;
private int ey;
public static int isClose=0;
WhichWin whichwin=new WhichWin();
//落下棋子
@Override
public void mouseMoved(MouseEvent e) {
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
if(isClose==1) {
return;
}
ex=e.getX();
ey=e.getY();
float x = (ex - 25) / 25;
float y = (ey - 25) / 25;
int x1 = (int) x;
int y1 = (int) y;
if(whichColor==0&&position[x1][y1]==0) {
position[x1][y1]=1;//落下白棋子
whichColor=1;
}
else if(whichColor==1&&position[x1][y1]==0) {
position[x1][y1]=2;//落下黑色棋子
whichColor=0;
}
//判断各方向输赢
whichwin.upOrDown(x1,y1,position,whichColor);
whichwin.leftOrRight(x1,y1,position,whichColor);
whichwin.Obliquedirection(x1,y1,position,whichColor);
whichwin.Obliquedirection2(x1,y1,position,whichColor);
}
}
ChessBoard2类
package com.fivechess.view;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
/*
-
汇制棋盘的代码
-
*/
public class ChessBoard2 extends JPanel implements MouseMotionListener,MouseListener{public Image chessBoardImage;
public Image blackChess;
public Image whiteChess;public static final int COLS=19;
public static final int RAWS=19;//棋盘的行数和列数
public int position[][]=new int[COLS][RAWS];public ChessBoard2() {
chessBoardImage = Toolkit.getDefaultToolkit().getImage(“images/chessBoard.jpg”);
blackChess= Toolkit.getDefaultToolkit().getImage(“images/black.png”);
whiteChess= Toolkit.getDefaultToolkit().getImage(“images/white.png”);initStart(); addMouseListener(this);
}
/**
* 初始化数组为BLANK
*/
public void initStart()
{
for(int i=0;i<19;i++)
{
for(int j=0;j<19;j++)
{
position[i][j]= 0;//“0”代表当前无棋子
}
}
}//汇制棋盘
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(chessBoardImage, 0, 0,null);for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { if (position[i][j] ==2) { g.drawImage(blackChess, 13+i * 25 , 13+j * 25 , null); } else if (position[i][j] ==1) { g.drawImage(whiteChess, 13+i * 25 , 13+j * 25 , null); } } }
}
@Override
public void mouseClicked(MouseEvent e) {
}
/**
* 按下鼠标时,记录鼠标的位置,并改写数组的数值,重新绘制图形
* @param e
*/
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}@Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub }
}