这是本人最近一段时间写的斗地主的java代码,大体框架都实现了,一些细节还需要后续优化。
package basegame;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import msg.RestartGameMsg;
import socket.GameServer;
import socket.NetClient;
/***************************************************************************
* 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();
dialog.setVisible(true);
}
// 初始化窗体
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() {
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);
if(null!=chessBoard){
this.remove(chessBoard);
}
chessBoard = new ChessBoard(this);
add(chessBoard);
chessBoard.repaint();
// 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());
}
}
public void setWinById(int id){
chessBoard.removeMouseListener(chessBoard);
chessBoard.removeMouseListener(chessBoard);
String s = "恭喜电脑" + id + ",赢了! 你的智商有待提高哦";
JOptionPane.showMessageDialog(this, s);
RestartGameMsg msg = new RestartGameMsg();
gameThread.send(msg);
}
public NetClient gameThread = new NetClient(this);
ConnDialog dialog = new ConnDialog();
class ConnDialog extends Dialog {
Button b = new Button("确定");
TextField tfIP = new TextField("127.0.0.1", 12);
TextField tfPort = new TextField("" + GameServer.TCP_PORT, 4);
TextField tfMyUDPPort = new TextField("2223", 4);
public ConnDialog() {
super(GameFrame.this, true);
this.setLayout(new FlowLayout());
this.add(new Label("IP:"));
this.add(tfIP);
this.add(new Label("Port:"));
this.add(tfPort);
this.add(new Label("My UDP Port:"));
this.add(tfMyUDPPort);
this.add(b);
this.setLocation(300, 300);
this.pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String IP = tfIP.getText().trim();
int port = Integer.parseInt(tfPort.getText().trim());
int myUDPPort = Integer.parseInt(tfMyUDPPort.getText()
.trim());
gameThread.setUdpPort(myUDPPort);
gameThread.connect(IP, port);
setVisible(false);
}
});
}
}
}
package basegame;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import msg.SendMoveMsg;
import msg.YourTurnMsg;
/***************************************************************************
* TODO
* <br>Created on 2013-7-3 下午8:26:59<br>
* @author daicy
***************************************************************************/
public class ChessBoard extends JPanel implements MouseListener,MouseMotionListener{
private Image chessBoard ;
private Image white ;
private Image black ;
public Image toClickFlag ;
public Image clickedFlag ;
public static int chessSize = 35; //棋子宽度
//棋盘
private static final int GRIDNUM = 15;
public static final int GRID = 35; //棋盘格子的边长
public static int BORDER = 20; //棋盘边框宽度
private int width,height;
private GameFrame mainFrame;
public int[][] chesses = new int[15][15]; //用于记录棋盘上那些位置已落子 0 空,1黑子,2白子
int mousex ;
int mousey ;
public ChessBoard(GameFrame mainFrame){
initImage();
this.mainFrame = mainFrame;
initOther();
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
}
// 初始化窗体
private void initOther() {
width = chessBoard.getWidth(this);
height = chessBoard.getHeight(this);
setSize(width,height);
BORDER = (width - (GRIDNUM-1)* GRID)/2;
}
private void initImage(){
//Loading Images
MediaTracker tracker = new MediaTracker(this);//Used to track loading of image
Toolkit toolkit = Toolkit.getDefaultToolkit();
//loading images from jar file
chessBoard = toolkit.getImage("Res//FiveChessBoard.jpg");
white = toolkit.getImage("Res//White.gif");
black = toolkit.getImage("Res//Black.gif");
toClickFlag = toolkit.getImage("Res//Selected.gif");
clickedFlag = toolkit.getImage("Res//Clicked.gif");
tracker.addImage(chessBoard, 1);
tracker.addImage(white, 1);
tracker.addImage(black, 1);
tracker.addImage(toClickFlag, 1);
tracker.addImage(clickedFlag, 1);
try {
//Waiting until image loaded.
tracker.waitForAll();
} catch (InterruptedException e) {
}
chessSize = white.getHeight(this);
}
@Override
/**
* 重写父类的重画方法
*/
public void paint(Graphics g) {
g.drawImage(chessBoard, 0, 0, this);
for (int i = 0; i < mainFrame.getPlayers().length; i++) {
drawMyChess(g);
}
if(this.mainFrame.turnIndex==mainFrame.id){
g.drawImage(toClickFlag, mousex*GRID+BORDER-chessSize/2, mousey*GRID+BORDER-chessSize/2, null);
}else if(this.mainFrame.turnIndex>-1){
if(null!= mainFrame.getPlayers()[mainFrame.turnIndex].getCurrentChesse()){
int mousex = mainFrame.getPlayers()[mainFrame.turnIndex].getCurrentChesse().x;
int mousey = mainFrame.getPlayers()[mainFrame.turnIndex].getCurrentChesse().y;
g.drawImage(clickedFlag, mousex*GRID+BORDER-5, mousey*GRID+BORDER-5, null);
}
}
}
public void drawMyChess(Graphics g) {
// GRID = 35; //棋盘格子的边长
//
// public static int BORDER = 20; //棋盘边框宽度
for (int i = 0; i < chesses.length; i++) {
for (int j = 0; j < chesses[i].length; j++) {
if(chesses[i][j]==1){
g.drawImage(black, i*GRID+BORDER-chessSize/2, j*GRID+BORDER-chessSize/2, null);
}else if(chesses[i][j]==2){
g.drawImage(white, i*GRID+BORDER-chessSize/2, j*GRID+BORDER-chessSize/2, null);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(this.mainFrame.turnIndex==mainFrame.id){
int x = e.getX()/GRID;
int y = e.getY()/GRID;
if(chesses[x][y]==0){
Point point = new Point(x,y);
SendMoveMsg msg = new SendMoveMsg(this.mainFrame.turnIndex,point);
mainFrame.gameThread.send(msg);
YourTurnMsg msgs = new YourTurnMsg((mainFrame.turnIndex+1)%2);
mainFrame.gameThread.send(msgs);
// mainFrame.getPlayers()[mainFrame.turnIndex].setCurrentChesse(new Point(x,y));
// chesses[x][y] = mainFrame.getPlayers()[mainFrame.turnIndex].getChessNum();
// mainFrame.turnIndex = (mainFrame.turnIndex+1)%2;
// mainFrame.repaint();
}
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent e) {
}
//移动
public void mouseMoved(MouseEvent e) {
if(this.mainFrame.id==mainFrame.turnIndex){
mousex = e.getX()/GRID;
mousey = e.getY()/GRID;
repaint();//重画
}else{
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}
}
}
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;
}
}