创建一个简单的五子棋游戏,带GUI界面。不多说,上代码。首先上的是五子棋模型。这个模型提供简单的放置棋子,判断是否五子相连,也就是是否赢了。代码如下所示。
/**game five in a row
* */
public class FiveInARowModel {
public static final int EMPTY=0;//no chessman
public static final int BLACK=1;//black chessman
public static final int WHITE=2;//white chessman
private int[][]board;
private int lastPutRow,lastPutCol;
public FiveInARowModel(int dimension){
board=new int[dimension][dimension];
clear();
}
public int getDimension(){
return board.length;
}
public int getChessman(int row,int col){
return board[row][col];
}
public boolean isEmpty(int row,int col){
return EMPTY==getChessman(row, col);
}
public boolean isFull(){
for(int k=0;kright
int chessman=getChessman(row, col);
int selfChessManNum=1;
for(int left=col-1;
left>=0&&getChessman(row, left)==chessman;
--left,++selfChessManNum);
for(int right=col+1;
right=5)
return true;
//up->down
selfChessManNum=1;
for(int up=row-1;
up>=0&&getChessman(up,col)==chessman;
--up,++selfChessManNum);
for(int down=row+1;
down=5)
return true;
//left-up->right-down
selfChessManNum=1;
for(int left=col-1,up=row-1;
left>=0&&up>=0&&getChessman(up, left)==chessman;
--left,--up,++selfChessManNum);
for(int right=col+1,down=row+1;
right=5)
return true;
//right-up->left-down
selfChessManNum=1;
for(int left=col-1,down=row+1;
left>=0&&down=0&&getChessman(up, right)==chessman;
++right,--up,++selfChessManNum);
if(selfChessManNum>=5)
return true;
return false;
}
}
接着是计算机玩家的代码。这里只是个简单的框架,只做最简单的判断,首先看是否有四子相连,则直接赢棋,否则就是下子堵另一方。
import java.util.Random;
//
public class ComputerPlayer {
private FiveInARowModel board;
private int chessman;
public ComputerPlayer(FiveInARowModel board,int chessman){
this.board=board;
this.chessman=chessman;
}
/**
* put a chessman on board
* @return true if computer wins
* */
public boolean play(){
int[]anotherPlayerPut=board.getLastPut();
if(anotherPlayerPut[0]<0){
try {
board.setChessman(board.getDimension()/2, board.getDimension()/2, chessman);
} catch (Exception e) {
}
return false;
}
int row=anotherPlayerPut[0],col=anotherPlayerPut[1];
int nearRow=-1,nearCol=-1,dis=2*board.getDimension();
//search a position if player can win
for(int k=0;kright
//check whether another player has more than 3 chessman in a row
if(checkLeftToRight(row, col))
return false;
if(checkUpToDown(row, col))
return false;
if(checkLeftUpToRightDown(row, col))
return false;
if(checkLeftDownToRightUp(row, col))
return false;
//choose a position randomly
try {
board.setChessman(nearRow, nearCol, chessman);
} catch (Exception e) {
}
return false;
}
private boolean checkLeftToRight(int row,int col){
return check(row,col,0,1);
}
private boolean checkUpToDown(int row,int col){
return check(row,col,1,0);
}
private boolean checkLeftUpToRightDown(int row,int col){
return check(row,col,1,1);
}
private boolean checkLeftDownToRightUp(int row,int col){
return check(row,col,-1,1);
}
//check chessmans in a line. If more than 3 chessmans in a row, then blocked it
private boolean check(int row,int col,int drow,int dcol){
int startRow=row-drow,startCol=col-dcol,
endRow=row+drow,endCol=col+dcol;
int chessman=board.getChessman(row, col);
int sameChessNum=1;
while(startRow>=0&&startCol>=0&&board.getChessman(startRow, startCol)==chessman){
startRow-=drow;
startCol-=dcol;
++sameChessNum;
}
while(endRow=3){//more than 3 chessman in a row
if(startRow>=0&&startCol>=0&&board.isEmpty(startRow, startCol)){
try {
board.setChessman(startRow, startCol, this.chessman);
return true;
} catch (Exception e) {
}
}else if(endRow
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class BoardPanel extends JPanel {
public static final int WIDTH=600;
public static final int HEIGHT=600;
private static int MARGIN=20;
private FiveInARowModel board;//chessman board
private ComputerPlayer computerPlayer;
public BoardPanel(FiveInARowModel board){
this.board=board;
computerPlayer=new ComputerPlayer(board, FiveInARowModel.BLACK);
this.setBackground(Color.WHITE);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Point p=e.getPoint();
int row,col;
int dim=BoardPanel.this.board.getDimension();
int cellWidth=(getWidth()-2*MARGIN)/dim;
if(p.y>=MARGIN&&p.y=MARGIN&&p.x
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
public MainFrame() {
FiveInARowModel board=new FiveInARowModel(19);
BoardPanel panel=new BoardPanel(board);
getContentPane().add(panel,BorderLayout.CENTER);
setSize(panel.getWidth(),10+panel.getHeight());
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Main {
public static void main(String[]args){
MainFrame mf=new MainFrame();
mf.setVisible(true);
}
}
好了。看看结果吧。这是初始界面。
接着是下棋的界面。
好了,最后是用户赢了。
总体就是这样了,代码很简单。主要就是计算机的智能太低了,不过显然这是可以改进的。当然了,五子棋模型类也需要改进,毕竟现在不支持退回。