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("對方離開.");
uj5u.com熱心網友回復:

不會連一句代碼都看不明白吧?
哪段程式看不明白說出來可以幫你看看解釋說明。要全部解釋,那就等待時間充裕的人現身吧。
uj5u.com熱心網友回復:
樓上言之有理
uj5u.com熱心網友回復:
+1
uj5u.com熱心網友回復:
+1
uj5u.com熱心網友回復:
你先玩幾把,然后在除錯,花點時間就會了uj5u.com熱心網友回復:
比如哪句話不懂,打在評論區,自然就有人幫你啦,剛好還可以鍛煉自己動手能力。uj5u.com熱心網友回復:
我注釋一般這么寫
/**
* 1.拿到設備上生產的第一個工單資訊
* 2.判斷狀態(下發和中斷)的工單更新開始時間為當前系統時間,記錄工單結束時間
* 3.判斷是否是同組工單然后更新
* 4.根據設備id去查詢設備上所有的工單(除去已經更新過的) list
* 5.使用filter過濾同組工單
* 6.選擇最小序值更新開始時間為上一工單結束時間,記錄工單結束時間
* 7.移除該map避免重復6步時重新遍歷到
*/
public void updateByDevOrder(Map devOrder,String lastOrderEndTime) {
/**/
}
uj5u.com熱心網友回復:
10樓編程好習慣uj5u.com熱心網友回復:
10樓可還行,不過要是游戲類這么寫,就跟寫文章一樣了,好幾千行代碼,寫注釋也要累死,哈哈
uj5u.com熱心網友回復:
單行注釋單獨寫一行。不要寫在代碼后面。uj5u.com熱心網友回復:
10樓編程好習慣uj5u.com熱心網友回復:
方法有注釋,關鍵環節有注釋uj5u.com熱心網友回復:
10樓可還行,不過要是游戲類這么寫,就跟寫文章一樣了,好幾千行代碼,寫注釋也要累死,哈哈
代碼即注釋嘛,我覺得注釋一般要加在特別難懂的代碼上,還有背景關系的介紹上,比如setXXX,我一看就知道賦值,就沒必要寫個注釋設定xxx值了
uj5u.com熱心網友回復:
10樓可還行,不過要是游戲類這么寫,就跟寫文章一樣了,好幾千行代碼,寫注釋也要累死,哈哈
代碼即注釋嘛,我覺得注釋一般要加在特別難懂的代碼上,還有背景關系的介紹上,比如setXXX,我一看就知道賦值,就沒必要寫個注釋設定xxx值了
是呀,注釋是對難于理解或有演算法的地方說明
uj5u.com熱心網友回復:
被前任程式員坑了幾次之后我現在寫代碼根本沒有注釋
,有仇不報非君子
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/58320.html
標籤:J2ME
