
本源代码由五个java源程序文件组成,Game.java、GameFrame.java、GamePanel.java、 ChessMan.java、Player.java
源代码下载
源代码:
Game.java
import javax.swing.*;
public class Game {
public static void main(String[] args) {
GameFrame GFrame=new GameFrame();
GFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GFrame.setVisible(true);
}
}
GameFrame.java
import java.awt.*;
import javax.swing.*;
class GameFrame extends JFrame {
private static final int Width=570;
private static final int Height=470;
GameFrame(){
setTitle("五子棋游戏");
CenteredFrame();
setSize(Width,Height);
GamePanel Gpanel=new GamePanel();
add(Gpanel);
}
void CenteredFrame(){
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;
int Xposition=(screenWidth-Width)/2;
int Yposition=(screenHeight-Height)/2;
setLocation(Xposition,Yposition);
}
}
GamePanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GamePanel extends JPanel {
private Point cursor=new Point(40,60); //棋盘坐标
private int[][] ChessState=new int[18][18];//棋盘状态
private int i=0;//横坐标
private int j=0;//纵坐标
private final static int testnum=5;//五子棋的规定棋子数
private Player Black=new Player(1,Color.BLACK,"黑方");//黑方棋子
private Player White=new Player(2,Color.WHITE,"白方");//白方棋子
private Player Cplayer=null;//当前用户的引用
private JTextField textBlack=new JTextField("
rea gInstruction=new JTextArea();
gInstruction.setSelectedTextColor(new Color(238,238,238));
String gSInstruction=
"Play1(黑方) Key \
up--------W \
down----S \
left--------A \
right------F \
\
"+
"Play2(白方) Key \
up--------up \
down----down \
left--------left \
right------right \
\
"+
"Exit game: \
Press Esc";
gInstruction.setText(gSInstruction);
gInstruction.setEditable(false);
gInstruction.setBounds(440,60,100,340);
add(gInstruction);
ChessManPerformListener Perform=new ChessManPerformListener();
addKeyListener(Perform);
}
// 设置第一个JTextField输出"请黑方下棋
String[] options = {"继续
int keyCode=event.getKeyCode();
if(keyCode==KeyEvent.VK_ESCAPE)
System.exit(0);
if(Cplayer.getCurrentIdentify()==2){//判别当前玩家
if(keyCode==KeyEvent.VK_LEFT){
if(cursor.x>40)
cursor.x-=20;
}
else if(keyCode==KeyEvent.VK_RIGHT){
if(cursor.x<380)
cursor.x+=20;
}
else if(keyCode==KeyEvent.VK_UP){
if(cursor.y>60)
cursor.y-=20;
}
else if(keyCode==KeyEvent.VK_DOWN){
if(cursor.y<400)
cursor.y+=20;
}
else if(keyCode==KeyEvent.VK_ENTER){
if(!isChessState()){
Cplayer.PerformChessMan();
RecordChessState();
repaint();
JudgeWin();//判定当前落子后是否赢棋
}
}
repaint();
}
}
public void keyReleased(KeyEvent event) {}
//玩家1的按键
public void keyTyped(KeyEvent event) {
char keyChar=event.getKeyChar();
if(Cplayer.getCurrentIdentify()==1){//判别当前玩家
if(keyChar=='a'){
if(cursor.x>40)//对移动光标超界现象做判别
cursor.x-=20;
}
else if(keyChar=='d'){
if(cursor.x<380)
cursor.x+=20;
}
else if(keyChar=='w'){
if(cursor.y>60)
cursor.y-=20;
}
else if(keyChar=='s'){
if(cursor.y<400)
cursor.y+=20;
}
else if(keyChar==' '){
if(!isChessState()){ //落子前先判断当前位置上是否已有棋子
Cplayer.PerformChessMan();//落子
RecordChessState();//记录当前落子后棋盘状态
repaint();
JudgeWin();//判定当前落子后是否赢棋
}
}
}
repaint();
}
}
}
ChessMan.java
import java.awt.Color;
class Ch
essMan {
private static final int ChessManSize=20;
private Color ChessManColor;
ChessMan(Color c){
ChessManColor=c;
}
static int getChessManSize(){
return ChessManSize;
}
Color getChessManColor(){
return ChessManColor;
}
}
Player.java
import java.awt.Color;
class Player {
private int identify;
private ChessMan pChessMan;
private String PlayerStringIdentify;
Player(int identify,Color c,String sIdentify){
this.identify=identify;
pChessMan=new ChessMan(c);
this.PlayerStringIdentify=sIdentify;
}
int getCurrentIdentify(){
return identify;
}
String getsIdentify(){
return PlayerStringIdentify;
}
void PerformChessMan(){}
Color getplayerChessManColor(){
return pChessMan.getChessManColor();
}
}
