在代码后面加注释解释

weixin_44295111 2019-12-09 11:04:25
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.geom.*;
class OmokBoard extends Canvas{
public static final int BLACK=1, WHITE=-1;//定义黑白颜色初始值
private int[][] map;//定义二维数组用于记录地图坐标
private int size, cell;//定义大小和表格
private String info="游戏终止";//定于提示信息

private int color=BLACK;//定义颜色初始值
private boolean enable=false;//定义布尔类型值
private boolean running=false;//定义布尔类型值
private PrintWriter writer;
private Graphics gboard, gbuff;//定义图形抽象类
private Image buff;
OmokBoard(int s, int c){
this.size=s;this.cell=c;
map=new int[size+2][];//实例化地图
for(int i=0;i<map.length;i++)//for循环生成地图
map[i]=new int[size+2];//设置地图元素
setBackground(new Color(200,200,100));//设置背景颜色及大小
setSize(size*(cell+1)+size, size*(cell+1)+size);//设置大小
addMouseListener(new MouseAdapter(){//鼠标监听事件
public void mousePressed(MouseEvent me){//鼠标经过事件
if(!enable)return;//判断enable的值
int x=(int)Math.round(me.getX()/(double)cell);//四舍五入得到值
int y=(int)Math.round(me.getY()/(double)cell);
if(x==0 || y==0 || x==size+1 || y==size+1)return;//判断并返回
if(map[x][y]==BLACK || map[x][y]==WHITE)return;//判断并返回
writer.println("[STONE]"+x+" "+y);//打印出石子
map[x][y]=color;//设置指定位置的颜色
if(check(new Point(x, y), color)){
info="获胜.";//设置消息
writer.println("[WIN]");//打印信息
}
else info="等待对方落子.";//设置消息
repaint();
enable=false;//设置enable的值
}
});
}
public boolean isRunning(){
return running;
}
public void startGame(String col){
running=true;
if(col.equals("BLACK")){//判断颜色
enable=true; color=BLACK;
info="开始游戏...请落子.";
}
else{
enable=false; color=WHITE;
info="开始游戏...请等待.";
}
}
public void stopGame(){
reset();//重值默认为初始值
writer.println("[STOPGAME]");
enable=false;
running=false;
}
public void putOpponent(int x, int y){
map[x][y]=-color;
info="对手已落子...请落子.";
repaint();
}
public void setEnable(boolean enable){
this.enable=enable;
}
public void setWriter(PrintWriter writer){
this.writer=writer;
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(gbuff==null){
buff=createImage(getWidth(),getHeight());
gbuff=buff.getGraphics();
}
drawBoard(g);
}
public void reset(){
for(int i=0;i<map.length;i++)
for(int j=0;j<map[i].length;j++)
map[i][j]=0;
info="游戏终止";
repaint();
}
private void drawLine(){
gbuff.setColor(Color.black);
for(int i=1; i<=size;i++){
gbuff.drawLine(cell, i*cell, cell*size, i*cell);
gbuff.drawLine(i*cell, cell, i*cell , cell*size);
}
}
private void drawBlack(int x, int y){
Graphics2D gbuff=(Graphics2D)this.gbuff;
gbuff.setColor(Color.black);
gbuff.fillOval(x*cell-cell/2, y*cell-cell/2, cell, cell);
gbuff.setColor(Color.white);
gbuff.drawOval(x*cell-cell/2, y*cell-cell/2, cell, cell);
}
private void drawWhite(int x, int y){
gbuff.setColor(Color.white);
gbuff.fillOval(x*cell-cell/2, y*cell-cell/2, cell, cell);
gbuff.setColor(Color.black);
gbuff.drawOval(x*cell-cell/2, y*cell-cell/2, cell, cell);
}
private void drawStones(){
for(int x=1; x<=size;x++)
for(int y=1; y<=size;y++){
if(map[x][y]==BLACK)
drawBlack(x, y);
else if(map[x][y]==WHITE)
drawWhite(x, y);
}
}
synchronized private void drawBoard(Graphics g){
gbuff.clearRect(0, 0, getWidth(), getHeight());
drawLine();
drawStones();
gbuff.setColor(Color.red);
gbuff.drawString(info, 20, 15);
g.drawImage(buff, 0, 0, this);
}
private boolean check(Point p, int col){
if(count(p, 1, 0, col)+count(p, -1, 0, col)==4)
return true;
if(count(p, 0, 1, col)+count(p, 0, -1, col)==4)
return true;
if(count(p, -1, -1, col)+count(p, 1, 1, col)==4)
return true;
if(count(p, 1, -1, col)+count(p, -1, 1, col)==4)
return true;
return false;
}
private int count(Point p, int dx, int dy, int col){
int i=0;
for(; map[p.x+(i+1)*dx][p.y+(i+1)*dy]==col ;i++);
return i;
}
}
public class OmokClient extends Frame implements Runnable, ActionListener{
private TextArea msgView=new TextArea("", 1,1,1);
private TextField sendBox=new TextField("");
private TextField nameBox=new TextField();
private TextField roomBox=new TextField("0");
private Label pInfo=new Label("等待室: 名");
private java.awt.List pList=new java.awt.List();
private Button startButton=new Button("开始对决");
private Button stopButton=new Button("弃权");
private Button enterButton=new Button("入场");
private Button exitButton=new Button("去待机室");
private Label infoView=new Label("< 2019电子商务大作业 >", 1);
private OmokBoard board=new OmokBoard(15,30);
private BufferedReader reader;
private PrintWriter writer;
private Socket socket;
private int roomNumber=-1;
private String userName=null;
public OmokClient(String title){
super(title);
setLayout(null);
msgView.setEditable(false);
infoView.setBounds(10,30,480,30);
infoView.setBackground(new Color(200,200,255));
board.setLocation(10,70);
add(infoView);
add(board);
Panel p=new Panel();
p.setBackground(new Color(200,255,255));
p.setLayout(new GridLayout(3,3));
p.add(new Label("名 子:", 2));p.add(nameBox);
p.add(new Label("房间号:", 2)); p.add(roomBox);
p.add(enterButton); p.add(exitButton);
enterButton.setEnabled(false);
p.setBounds(500,30, 250,70);

Panel p2=new Panel();
p2.setBackground(new Color(255,255,100));
p2.setLayout(new BorderLayout());
Panel p2_1=new Panel();
p2_1.add(startButton); p2_1.add(stopButton);
p2.add(pInfo,"North"); p2.add(pList,"Center"); p2.add(p2_1,"South");
startButton.setEnabled(false); stopButton.setEnabled(false);
p2.setBounds(500,110,250,180);

Panel p3=new Panel();
p3.setLayout(new BorderLayout());
p3.add(msgView,"Center");
p3.add(sendBox, "South");
p3.setBounds(500, 300, 250,250);

add(p); add(p2); add(p3);
sendBox.addActionListener(this);
enterButton.addActionListener(this);
exitButton.addActionListener(this);
startButton.addActionListener(this);
stopButton.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==sendBox){
String msg=sendBox.getText();
if(msg.length()==0)return;
if(msg.length()>=30)msg=msg.substring(0,30);
try{
writer.println("[MSG]"+msg);
sendBox.setText("");
}catch(Exception ie){}
}
else if(ae.getSource()==enterButton){
try{

if(Integer.parseInt(roomBox.getText())<1){
infoView.setText("房间号错误,大于1");
return;
}
writer.println("[ROOM]"+Integer.parseInt(roomBox.getText()));
msgView.setText("");
}catch(Exception ie){
infoView.setText("输入的事项发生错误.");
}
}
else if(ae.getSource()==exitButton){
try{
goToWaitRoom();
startButton.setEnabled(false);
stopButton.setEnabled(false);
}catch(Exception e){}
}
else if(ae.getSource()==startButton){
try{
writer.println("[START]");
infoView.setText("等待对方决定.");
startButton.setEnabled(false);
}catch(Exception e){}
}
else if(ae.getSource()==stopButton){
try{
writer.println("[DROPGAME]");
endGame("已弃权.");
}catch(Exception e){}
}
}
void goToWaitRoom(){
if(userName==null){
String name=nameBox.getText().trim();
if(name.length()<=2 || name.length()>10){
infoView.setText("房间号错误. 3~10个数");
nameBox.requestFocus();
return;
}
userName=name;
writer.println("[NAME]"+userName);
nameBox.setText(userName);
nameBox.setEditable(false);
}
msgView.setText("");
writer.println("[ROOM]0");
infoView.setText("已进待机室.");
roomBox.setText("0");
enterButton.setEnabled(true);
exitButton.setEnabled(false);
}
public void run(){
String msg;
try{
while((msg=reader.readLine())!=null){
if(msg.startsWith("[STONE]")){
String temp=msg.substring(7);
int x=Integer.parseInt(temp.substring(0,temp.indexOf(" ")));
int y=Integer.parseInt(temp.substring(temp.indexOf(" ")+1));
board.putOpponent(x, y);
board.setEnable(true);
}
else if(msg.startsWith("[ROOM]")){
if(!msg.equals("[ROOM]0")){
enterButton.setEnabled(false);
exitButton.setEnabled(true);
infoView.setText(msg.substring(6)+"号房间已被进入.");
}
else infoView.setText("已进入待机室.");
roomNumber=Integer.parseInt(msg.substring(6));
if(board.isRunning()){
board.stopGame();
}
}
else if(msg.startsWith("[FULL]")){
infoView.setText("房间满员,禁止入内.");
}
else if(msg.startsWith("[PLAYERS]")){
nameList(msg.substring(9));
}
else if(msg.startsWith("[ENTER]")){
pList.add(msg.substring(7));
playersInfo();
msgView.append("["+ msg.substring(7)+"]入场.\n");
}
else if(msg.startsWith("[EXIT]")){
pList.remove(msg.substring(6));
playersInfo();
msgView.append("["+msg.substring(6)+"]进入其它房间.\n");
if(roomNumber!=0)
endGame("对方已离开.");
}
else if(msg.startsWith("[DISCONNECT]")){
pList.remove(msg.substring(12));
playersInfo();
msgView.append("["+msg.substring(12)+"]中断连接.\n");
if(roomNumber!=0)
endGame("对方离开.");
...全文
11293 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gemini_Kanon 2020-06-08
  • 打赏
  • 举报
回复
被前任程序员坑了几次之后我现在写代码根本没有注释,有仇不报非君子
小灯塔 2020-05-04
  • 打赏
  • 举报
回复
引用 16 楼 maradona1984 的回复:
[quote=引用 12 楼 来自底层程序猿的仰望 的回复:] 10楼可还行,不过要是游戏类这么写,就跟写文章一样了,好几千行代码,写注释也要累死,哈哈
代码即注释嘛,我觉得注释一般要加在特别难懂的代码上,还有上下文的介绍上,比如setXXX,我一看就知道赋值,就没必要写个注释设置xxx值了[/quote] 是呀,注释是对难于理解或有算法的地方说明
maradona1984 2020-03-19
  • 打赏
  • 举报
回复
引用 12 楼 来自底层程序猿的仰望 的回复:
10楼可还行,不过要是游戏类这么写,就跟写文章一样了,好几千行代码,写注释也要累死,哈哈
代码即注释嘛,我觉得注释一般要加在特别难懂的代码上,还有上下文的介绍上,比如setXXX,我一看就知道赋值,就没必要写个注释设置xxx值了
聪头 2020-03-19
  • 打赏
  • 举报
回复
方法有注释,关键环节有注释
weixin_42614237 2020-03-18
  • 打赏
  • 举报
回复
10楼编程好习惯
juno_393 2020-01-10
  • 打赏
  • 举报
回复
单行注释单独写一行。不要写在代码后面。
你看见了吗° 2020-01-10
  • 打赏
  • 举报
回复
10楼可还行,不过要是游戏类这么写,就跟写文章一样了,好几千行代码,写注释也要累死,哈哈
zwolfe 2020-01-10
  • 打赏
  • 举报
回复
10楼编程好习惯
胖到没有朋友 2020-01-10
  • 打赏
  • 举报
回复
我注释一般这么写

/**
     * 1.拿到设备上生产的第一个工单信息
     * 2.判断状态(下发和中断)的工单更新开始时间为当前系统时间,记录工单结束时间
     * 3.判断是否是同组工单然后更新
     * 4.根据设备id去查询设备上所有的工单(除去已经更新过的)     list
     * 5.使用filter过滤同组工单
     * 6.选择最小序值更新开始时间为上一工单结束时间,记录工单结束时间
     * 7.移除该map避免重复6步时重新遍历到
     */
    public void updateByDevOrder(Map devOrder,String lastOrderEndTime) {
        /**/
    }
zzzzzzzzzzzw___ 2020-01-10
  • 打赏
  • 举报
回复
比如哪句话不懂,打在评论区,自然就有人帮你啦,刚好还可以锻炼自己动手能力。
牧歌ing 2019-12-20
  • 打赏
  • 举报
回复
你先玩几把,然后在调试,花点时间就会了
Mister_X 2019-12-10
  • 打赏
  • 举报
回复
引用 3 楼 sun0322 的回复:
[quote=引用 1 楼 qybao 的回复:] 不会连一句代码都看不明白吧? 哪段程序看不明白说出来可以帮你看看解释说明。要全部解释,那就等待时间充裕的人现身吧。
+1[/quote] +1
sun0322 2019-12-09
  • 打赏
  • 举报
回复
引用 1 楼 qybao 的回复:

不会连一句代码都看不明白吧?
哪段程序看不明白说出来可以帮你看看解释说明。要全部解释,那就等待时间充裕的人现身吧。

+1
程序yang 2019-12-09
  • 打赏
  • 举报
回复
楼上言之有理
qybao 2019-12-09
  • 打赏
  • 举报
回复

不会连一句代码都看不明白吧?
哪段程序看不明白说出来可以帮你看看解释说明。要全部解释,那就等待时间充裕的人现身吧。

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧