教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 外语考试 >

java程序设计五子棋

来源:网络收集 时间:2026-09-05
导读: 自己用java语言开发的五子棋小游戏其中实现了双人对战和人机对战功能,供大家参考 package FiveChess; import java.awt.Color; import java.awt.Graphics; public class Chess { int id; int chess_x; int chess_y; int state; int radius=13; public Chess()

自己用java语言开发的五子棋小游戏其中实现了双人对战和人机对战功能,供大家参考

package FiveChess;

import java.awt.Color;

import java.awt.Graphics;

public class Chess {

int id;

int chess_x;

int chess_y;

int state;

int radius=13;

public Chess(){}

public Chess(int id,int chess_x,int chess_y,int state){

this.id=id;

this.chess_x=chess_x;

this.chess_y=chess_y;

this.state=state;

}

public int getChess_x() {

return chess_x;

}

public void setChess_x(int chess_x) {

this.chess_x = chess_x;

}

public int getChess_y() {

return chess_y;

}

public void setChess_y(int chess_y) {

this.chess_y = chess_y;

}

public int getState() {

return state;

}

public void setState(int state) {

this.state = state;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public void drawChess(Graphics g){

自己用java语言开发的五子棋小游戏其中实现了双人对战和人机对战功能,供大家参考

Color color=null;

if(state==1){

color=new Color(255,255,255);

g.setColor(color);

g.fillOval(chess_x*30-radius, chess_y*30-radius, radius*2, radius*2);

}else if(state==-1){

color=new Color(0,0,0);

g.setColor(color);

g.fillOval(chess_x*30-radius, chess_y*30-radius, radius*2, radius*2);

}

}

}

package FiveChess;

import java.awt.Graphics;

public class ChessBoard {

int [][]board=new int[16][16];

public ChessBoard(){

initialBoard();

}

public void initialBoard(){

for(int i=0;i<board.length;i++){

for(int j=0;j<board[i].length;j++){

board[i][j]=0;//0无棋1白棋-1黑棋

}

}

}

public void draw(Graphics g){

g.drawLine(25, 25, 455, 25);

g.drawLine(455, 25, 455, 455);

g.drawLine(25, 455, 455, 455);

g.drawLine(25, 25, 25, 455);

int i=1,j=1;

while(i<board.length){

g.drawLine(i*30, 30, i*30, 15*30);

i++;

}

while(j<board.length){

g.drawLine(30,j*30 , 15*30, j*30);

j++;

}

自己用java语言开发的五子棋小游戏其中实现了双人对战和人机对战功能,供大家参考

}

public int getState(int x,int y){

if(x>0&y>0&x<16&y<16) return board[x][y];

else return -2;

}

public void setState(int x,int y,int state){

board[x][y]=state;

}

}

package FiveChess;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Image;

import java.awt.TextArea;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.IOException;

import http://doc.guandang.net.ServerSocket;

import http://doc.guandang.net.Socket;

import java.sql.Time;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JSplitPane;

import javax.swing.JTextField;

import javax.swing.border.Border;

public class ChessServer extends JFrame implements ActionListener{

public static final int frame_width=35;//窗体的宽度

public static final int frame_height=27;//窗体的高度

public static final int frame_opration=10;//操作区域的宽度

自己用java语言开发的五子棋小游戏其中实现了双人对战和人机对战功能,供大家参考

public static final int block_width=20;//单位宽度 public static final int block_height=20;//单位高度 ServerWar jpz=new ServerWar(this);//创建服务器端主面板 JMenuBar menubar=new JMenuBar();//创建菜单条 JMenu fileMenu=new JMenu("文件"); JMenuItem one = new JMenuItem("单人游戏"); JMenuItem two =new JMenuItem("双人游戏"); JMenuItem exit=new JMenuItem("退出"); JPanel jpy=new JPanel();//创建操作区的面板 JLabel jlPort=new JLabel("端口号");//创建提示用户输入端口号的标签 JTextField jtfPort=new JTextField("9999");//创建用于输入端口号的文本框,默认是9999 JLabel jlNickName=new JLabel("昵 称");//创建用于提示输入昵称的标签 JTextField jtfNickName=new JTextField("Player2");//创建用于输入昵称的文本框 JButton jbNew=new JButton("建主");//创建"建主","停止","开始"三个动作按钮 JButton jbStop=new JButton("停止"); JButton jbStart=new JButton("开始"); JButton jbSend=new JButton("发送"); TextArea jaChat=new TextArea("",5000,1,TextArea.SCROLLBARS_VERTICAL_ONLY); int textIndex=0; JTextField jtfMessage=new JTextField("你好"); JSplitPane jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jpz,jpy);//创建JSplitPane ServerSocket ss;//声明ServerSocket引用 ServerThread st;//声明服务器线程 ServerAgentThread sat;//声明服务器代理线程引用 public ChessServer(){ this.addComponent();//初始化窗体,将控件添加到窗体中 this.addListener();//为相应控件注册监听器 this.initialFrame();//初始化窗体 jpz.setFocusable(true); jpz.requestFocus(true);//使左边面板获得焦点 } public void addComponent(){//添加控件 的方法 jpy.setLayout(null);//设为空布局 jlPort.setBounds(10,20,40,25); jpy.add(jlPort);//添加"端口号"标签 jtfPort.setBounds(55,20,100,25); jpy.add(jtfPort);//添加输入端口号的文本框

自己用java语言开发的五子棋小游戏其中实现了双人对战和人机对战功能,供大家参考

…… 此处隐藏:15820字,全部文档内容请下载后查看。喜欢就下载吧 ……

java程序设计五子棋.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/1693159.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)