这是本人最近一段时间写的五子棋的java代码,大体框架都实现了,一些细节还需要优化。
package basegame;
import java.awt.Color;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/***************************************************************************
* TODO
* <br>Created on 2013-6-28 下午7:45:13<br>
* @author daicy
***************************************************************************/
public class GameFrame extends JFrame {
private Player[] players = new Player[2];
private GameThread gameThread;
private ChessBoard chessBoard;
public int lordIndex = -1;
public int turnIndex = -1;
public int id;
public GameFrame(String name){
initMain();// 初始化
//this.setlord(1);
reStart();
setVisible(true);
//new Thread(new PaintThread()).start();
}
// 初始化窗体
private void initMain() {
{
setBackground(new Color(0, 112, 26)); // 背景为绿色
setTitle("五子棋");
setSize(536,546);
setResizable(false);
setLocationRelativeTo(getOwner()); // 屏幕居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
// 初始化窗体
public void reStart() {
if(null!=chessBoard){
this.remove(chessBoard);
}
chessBoard = new ChessBoard(this);
add(chessBoard);
players[0] = new Player(0+"",0);
players[0].userState = 0;
players[0].setChessNum(1);
players[1] = new Player(1+"",1);
players[1].userState = 0;
players[1].setChessNum(2);
gameThread = new GameThread(this);
gameThread.start();
gameThread.gameState = 2;
turnIndex = 1;
}
public static void main(String args[]) {
GameFrame GamePanel = new GameFrame("daicy");
}
public Player[] getPlayers() {
return players;
}
public void setPlayers(Player[] players) {
this.players = players;
}
public ChessBoard getChessBoard() {
return chessBoard;
}
public void setChessBoard(ChessBoard chessBoard) {
this.chessBoard = chessBoard;
}
public void sendXY(int id,Point point){
getPlayers()[id].setCurrentChesse(point);
getChessBoard().chesses[point.x][point.y] = getPlayers()[id].getChessNum();
getChessBoard().repaint();
if (new FiveChessAI(getChessBoard().chesses,2,1).
showWin(point.x, point.y, getPlayers()[id].getChessNum())) {
setWinById(getPlayers()[id].getChessNum());
}
turnIndex = (id+1)%2;
}
public void setWinById(int id){
gameThread.gameState = -1;
chessBoard.removeMouseListener(chessBoard);
chessBoard.removeMouseListener(chessBoard);
String s = "恭喜电脑" + id + ",赢了! 你的智商有待提高哦";
JOptionPane.showMessageDialog(this, s);
this.reStart();
}
}
package basegame;
import java.awt.Point;
public class GameThread extends Thread {
private GameFrame mainPanel ;
public int gameState; //0准备 1 抢地主2出牌; -1 结束
public GameThread(GameFrame mainPanel) {
this.mainPanel = mainPanel;
}
@Override
public void run() {
while (true) {
if(gameState==-1){
break;
}
switch (gameState) {
case 0:
break;
case 1:
break;
case 2:
if(mainPanel.turnIndex==0){
}else if(mainPanel.turnIndex>0){
// Integer x = (int) (Math.random() * 14);
// Integer y = (int) (Math.random() * 14);
int num = new FiveChessAI(mainPanel.getChessBoard().chesses,2,1).makeResultPoint();
Point point = new Point();
point.x = (num / 15);
point.y = (num % 15);
mainPanel.sendXY(mainPanel.turnIndex,point);
}
break;
}
}
}
}
package basegame;
import java.awt.Point;
public class Player {
public Point currentChesse ; //用于记录自己刚走过的棋子
private String name ;
private Integer id ;
private Integer chessNum ;
public int userState = -1; // 0 准备 1 抢地主 2 出牌;
public Player(String name, Integer id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Point getCurrentChesse() {
return currentChesse;
}
public void setCurrentChesse(Point currentChesse) {
this.currentChesse = currentChesse;
}
public Integer getChessNum() {
return chessNum;
}
public void setChessNum(Integer chessNum) {
this.chessNum = chessNum;
}
}