真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

最新java五子棋代碼 java五子棋代碼壓縮包

求一個(gè)簡(jiǎn)單的JAVA五子棋代碼??! 網(wǎng)上復(fù)制的別來(lái)了!

以下是現(xiàn)寫(xiě)的 實(shí)現(xiàn)了兩人對(duì)戰(zhàn) 自己復(fù)制后運(yùn)行把 沒(méi)什么難度 類名 Games

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括靜寧網(wǎng)站建設(shè)、靜寧網(wǎng)站制作、靜寧網(wǎng)頁(yè)制作以及靜寧網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,靜寧網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到靜寧省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

import java.util.Scanner;

public class Games {

private String board[][];

private static int SIZE = 17;

private static String roles = "A玩家";

//初始化數(shù)組

public void initBoard() {

board = new String[SIZE][SIZE];

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

// if(i==0){

// String str = "";

// str += j+" ";

// board[i][j]= str;

// }else if(i!=0j==0){

// String str = "";

// str += i+" ";

// board[i][j]= str;

// }else{

board[i][j] = "╋";

// }

}

}

}

//輸出棋盤(pán)

public void printBoard() {

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

System.out.print(board[i][j]);

}

System.out.println();

}

}

//判斷所下棋子位置是否合理

public boolean isOk(int x, int y) {

boolean isRight = true;

if (x = 16 || x 1 || y = 16 | y 1) {

//System.out.println("輸入錯(cuò)誤,請(qǐng)從新輸入");

isRight = false;

}

if (board[x][y].equals("●") || board[x][y].equals("○")) {

isRight = false;

}

return isRight;

}

//判斷誰(shuí)贏了

public void whoWin(Games wz) {

// 從數(shù)組挨個(gè)查找找到某個(gè)類型的棋子就從該棋子位置向右,向下,斜向右下 各查找5連續(xù)的位置看是否為5個(gè)相同的

int xlabel;// 記錄第一次找到某個(gè)棋子的x坐標(biāo)

int ylabel;// 記錄第一次找到某個(gè)棋子的y坐標(biāo)

// ●○╋

// 判斷人是否贏了

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

if (board[i][j].equals("○")) {

xlabel = i;

ylabel = j;

// 橫向找 x坐標(biāo)不變 y坐標(biāo)以此加1連成字符串

String heng = "";

if (i + 5 SIZE j + 5 SIZE) {

for (int k = j; k j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("○○○○○")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

// 向下判斷y不變 x逐增5 連成字符串

String xia = "";

for (int l = j; l i + 5; l++) {

xia += board[l][j];

// System.out.println(xia);

}

if (xia.equals("○○○○○")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

// 斜向右下判斷

String youxia = "";

for (int a = 1; a = 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("○○○○○")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

}

}

}

}

// 判斷電腦是否贏了

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

if (board[i][j].equals("●")) {

xlabel = i;

ylabel = j;

// 橫向找 x坐標(biāo)不變 y坐標(biāo)以此加1連成字符串

String heng = "";

if (j + 5 SIZE i + 5 SIZE) {

for (int k = j; k j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("●●●●●")) {

System.out.println(roles+"贏輸了!您輸了!");

System.exit(0);

}

// 向下判斷y不變 x逐增5 連成字符串

String xia = "";

for (int l = i; l i + 5; l++) {

xia += board[l][ylabel];

// System.out.println(xia);

}

if (xia.equals("●●●●●")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

// 斜向右下判斷

String youxia = "";

for (int a = 1; a = 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("●●●●●")) {

System.out.println(roles+"贏了!您輸了!");

System.exit(0);

}

}

}

}

}

}

public static void main(String[] args) {

Games wz = new Games();

Scanner sc = new Scanner(System.in);

wz.initBoard();

wz.printBoard();

while (true) {

System.out.print("請(qǐng)"+roles+"輸入X,Y坐標(biāo),必須在0-15范圍內(nèi),xy以空格隔開(kāi),輸入16 16結(jié)束程序");

int x = sc.nextInt();

int y = sc.nextInt();

if (x == SIZE y == SIZE) {

System.out.println("程序結(jié)束");

System.exit(0);

}

if (x SIZE || x 0 || y SIZE | y 0) {

System.out.println("輸入錯(cuò)誤,請(qǐng)從新輸入");

continue;

}

//如果roles是A玩家 就讓A玩家下棋,否則就讓B玩家下棋。

if (wz.board[x][y].equals("╋")roles.equals("A玩家")) {

wz.board[x][y] = "○";

wz.printBoard();

//判斷輸贏

wz.whoWin(wz);

}else if(wz.board[x][y].equals("╋")roles.equals("B玩家")){

wz.board[x][y] = "●";

wz.printBoard();

//判斷輸贏

wz.whoWin(wz);

} else {

System.out.println("此處已經(jīng)有棋子,從新輸入");

continue;

}

if(roles.equals("A玩家")){

roles = "B玩家";

}else if(roles.equals("B玩家")){

roles = "A玩家";

}

}

}

}

java五子棋代碼帶詳細(xì)解釋

浩大的工程 你有五子棋程序 如果你水平還行的話你參照這個(gè)聊天室程序應(yīng)該也比較容易寫(xiě)出人人對(duì)戰(zhàn)的

package Chat;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowEvent;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintStream;

import java點(diǎn)虐 .InetAddress;

import java點(diǎn)虐 .Socket;

import java.util.StringTokenizer;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import javax.swing.border.TitledBorder;

/**

* 聊天室的客戶端程序,GUI界面。

*/

public class ChatClient extends JFrame implements ActionListener{

// 登陸聊天室的名字標(biāo)簽和輸入框

JLabel nameLabel = new JLabel();

JTextField nameTextField = new JTextField(15);

// 連接和斷開(kāi)連接的按鈕

JButton connectButton = new JButton();

JButton disConnectButton = new JButton();

// 聊天室內(nèi)容的文本域

JTextArea chatContentTextArea = new JTextArea(9, 30);

// 發(fā)送消息的按鈕

JButton sendMsgButton = new JButton();

// 消息輸入框

JTextField msgTextField = new JTextField(20);

JLabel msglabel = new JLabel();

// 聊天室用戶列表

java.awt.List peopleList = new java.awt.List(10);

/*以下定義數(shù)據(jù)流和網(wǎng)絡(luò)變量*/

Socket soc = null;

PrintStream ps = null;

// 客戶端偵聽(tīng)服務(wù)器消息的線程

ClentListener listener = null;

public ChatClient() {

init();

}

// 初始化圖形界面

public void init() {

this.setTitle("聊天室客戶端");

// 初始化按鈕和標(biāo)簽

nameLabel.setText("姓名:");

connectButton.setText("連 接");

connectButton.addActionListener(this);

disConnectButton.setText("斷 開(kāi)");

disConnectButton.addActionListener(this);

// 設(shè)置聊天內(nèi)容不可編輯

chatContentTextArea.setEditable(false);

sendMsgButton.setText("發(fā) 送");

sendMsgButton.addActionListener(this);

msgTextField.setText("請(qǐng)輸入聊天信息");

//panel1放置輸入姓名和連接兩個(gè)按鈕

JPanel panel1 = new JPanel();

panel1.setLayout(new FlowLayout());

panel1.add(nameLabel);

panel1.add(nameTextField);

panel1.add(connectButton);

panel1.add(disConnectButton);

//用于放置聊天信息顯示和聊天人員列表

JPanel panel2 = new JPanel();

panel2.setLayout(new FlowLayout());

JScrollPane pane1 = new JScrollPane(chatContentTextArea);

pane1.setBorder(new TitledBorder(BorderFactory.createEtchedBorder(

Color.white, new Color(134, 134, 134)), "聊天內(nèi)容"));

panel2.add(pane1);

JScrollPane pane2 = new JScrollPane(peopleList);

pane2.setBorder(new TitledBorder(BorderFactory.createEtchedBorder(

Color.white, new Color(134, 134, 134)), "用戶列表"));

panel2.add(pane2);

//用于放置發(fā)送信息區(qū)域

JPanel panel3 = new JPanel();

panel3.setLayout(new FlowLayout());

panel3.add(msglabel);

panel3.add(msgTextField);

panel3.add(sendMsgButton);

// 將組件添加到界面

this.getContentPane().setLayout(new BorderLayout());

this.getContentPane().add(panel1, BorderLayout.NORTH);

this.getContentPane().add(panel2, BorderLayout.CENTER);

this.getContentPane().add(panel3, BorderLayout.SOUTH);

this.pack();

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

SwingUtilities.updateComponentTreeUI(this);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 關(guān)閉聊天室客戶端事件

*/

protected void processWindowEvent(WindowEvent e){

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

// 如果是關(guān)閉聊天室客戶端,則斷開(kāi)連接

disconnect();

dispose();

System.exit(0);

}

}

/**

* 處理按鈕事件

*/

public void actionPerformed(ActionEvent event) {

Object source = event.getSource();

if (source == connectButton){

// 如果點(diǎn)擊連接按鈕

if (soc == null) {

try {

// 使用端口2525實(shí)例化一個(gè)本地套接字

soc = new Socket(InetAddress.getLocalHost(), Constants.SERVER_PORT);

// 在控制臺(tái)打印實(shí)例化的結(jié)果

System.out.println(soc);

//將ps指向soc的輸出流

ps = new PrintStream(soc.getOutputStream());

//定義一個(gè)字符緩沖存儲(chǔ)發(fā)送信息

StringBuffer info = new StringBuffer(Constants.CONNECT_IDENTIFER).append(Constants.SEPERATOR);

//其中INFO為關(guān)鍵字讓服務(wù)器識(shí)別為連接信息

//并將name和ip用":"分開(kāi),在服務(wù)器端將用一個(gè)

//StringTokenizer類來(lái)讀取數(shù)據(jù)

String userinfo = nameTextField.getText() + Constants.SEPERATOR

+ InetAddress.getLocalHost().getHostAddress();

ps.println(info.append(userinfo));

ps.flush();

//將客戶端線程實(shí)例化,并啟動(dòng)

listener = new ClentListener(this, nameTextField.getText(), soc);

listener.start();

} catch (IOException e) {

System.out.println("Error:" + e);

disconnect();

}

}

} else if (source == disConnectButton){

// 如果點(diǎn)擊斷開(kāi)連接按鈕

disconnect();

} else if (source == sendMsgButton) {

//如果點(diǎn)擊發(fā)送按鈕

if (soc != null) {

//定義并實(shí)例化一個(gè)字符緩沖存儲(chǔ)發(fā)送的聊天信息

StringBuffer msg = new StringBuffer(Constants.MSG_IDENTIFER).append(Constants.SEPERATOR);

//用打印流發(fā)送聊天信息

ps.println(msg.append(msgTextField.getText()));

ps.flush();

}

}

}

/**

* 斷開(kāi)與服務(wù)器的連接

*/

public void disconnect(){

if (soc != null) {

try {

// 用打印流發(fā)送QUIT信息通知服務(wù)器斷開(kāi)此次通信

ps.println(Constants.QUIT_IDENTIFER);

ps.flush();

soc.close(); //關(guān)閉套接字

listener.toStop();

soc = null;

} catch (IOException e) {

System.out.println("Error:" + e);

}

}

}

public static void main(String[] args){

ChatClient client = new ChatClient();

client.setVisible(true);

}

/**

* 客戶端線程類用來(lái)監(jiān)聽(tīng)服務(wù)器傳來(lái)的信息

*/

class ClentListener extends Thread {

//存儲(chǔ)客戶端連接后的name信息

String name = null;

//客戶端接受服務(wù)器數(shù)據(jù)的輸入流

BufferedReader br = null;

//實(shí)現(xiàn)從客戶端發(fā)送數(shù)據(jù)到服務(wù)器的打印流

PrintStream ps = null;

//存儲(chǔ)客戶端的socket信息

Socket socket = null;

//存儲(chǔ)當(dāng)前運(yùn)行的ChatClient實(shí)例

ChatClient parent = null;

boolean running = true;

//構(gòu)造方法

public ClentListener(ChatClient p, String n, Socket s) {

//接受參數(shù)

parent = p;

name = n;

socket = s;

try {

//實(shí)例化兩個(gè)數(shù)據(jù)流

br = new BufferedReader(new InputStreamReader(s

.getInputStream()));

ps = new PrintStream(s.getOutputStream());

} catch (IOException e) {

System.out.println("Error:" + e);

parent.disconnect();

}

}

// 停止偵聽(tīng)

public void toStop(){

this.running = false;

}

//線程運(yùn)行方法

public void run(){

String msg = null;

while (running) {

msg = null;

try {

// 讀取從服務(wù)器傳來(lái)的信息

msg = br.readLine();

System.out.println("receive msg: " + msg);

} catch (IOException e) {

System.out.println("Error:" + e);

parent.disconnect();

}

// 如果從服務(wù)器傳來(lái)的信息為空則斷開(kāi)此次連接

if (msg == null) {

parent.listener = null;

parent.soc = null;

parent.peopleList.removeAll();

running = false;

return;

}

//用StringTokenizer類來(lái)實(shí)現(xiàn)讀取分段字符

StringTokenizer st = new StringTokenizer(msg, Constants.SEPERATOR);

//讀取信息頭即關(guān)鍵字用來(lái)識(shí)別是何種信息

String keyword = st.nextToken();

if (keyword.equals(Constants.PEOPLE_IDENTIFER)) {

//如果是PEOPLE則是服務(wù)器發(fā)來(lái)的客戶連接信息

//主要用來(lái)刷新客戶端的用戶列表

parent.peopleList.removeAll();

//遍歷st取得目前所連接的客戶

while (st.hasMoreTokens()) {

String str = st.nextToken();

parent.peopleList.add(str);

}

} else if (keyword.equals(Constants.MSG_IDENTIFER)) {

//如果關(guān)鍵字是MSG則是服務(wù)器傳來(lái)的聊天信息,

//主要用來(lái)刷新客戶端聊天信息區(qū)將每個(gè)客戶的聊天內(nèi)容顯示出來(lái)

String usr = st.nextToken();

parent.chatContentTextArea.append(usr);

parent.chatContentTextArea.append(st.nextToken("\0"));

parent.chatContentTextArea.append("\r\n");

} else if (keyword.equals(Constants.QUIT_IDENTIFER)) {

//如果關(guān)鍵字是QUIT則是服務(wù)器關(guān)閉的信息, 切斷此次連接

System.out.println("Quit");

try {

running = false;

parent.listener = null;

parent.soc.close();

parent.soc = null;

} catch (IOException e) {

System.out.println("Error:" + e);

} finally {

parent.soc = null;

parent.peopleList.removeAll();

}

break;

}

}

//清除用戶列表

parent.peopleList.removeAll();

}

}

}

package Chat;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowEvent;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintStream;

import java點(diǎn)虐 .ServerSocket;

import java點(diǎn)虐 .Socket;

import java.util.StringTokenizer;

import java.util.Vector;

import javax.swing.BorderFactory;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import javax.swing.border.TitledBorder;

/**

* 聊天室的服務(wù)器端程序,GUI界面

*/

public class ChatServer extends JFrame {

// 狀態(tài)欄標(biāo)簽

static JLabel statusBar = new JLabel();

// 顯示客戶端的連接信息的列表

static java.awt.List connectInfoList = new java.awt.List(10);

// 保存當(dāng)前處理客戶端請(qǐng)求的處理器線程

static Vector clientProcessors = new Vector(10);

// 當(dāng)前的連接數(shù)

static int activeConnects = 0;

// 構(gòu)造方法

public ChatServer() {

init();

try {

// 設(shè)置界面為系統(tǒng)默認(rèn)外觀

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

SwingUtilities.updateComponentTreeUI(this);

} catch (Exception e) {

e.printStackTrace();

}

}

private void init(){

this.setTitle("聊天室服務(wù)器");

statusBar.setText("");

// 初始化菜單

JMenu fileMenu = new JMenu();

fileMenu.setText("文件");

JMenuItem exitMenuItem = new JMenuItem();

exitMenuItem.setText("退出");

exitMenuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

exitActionPerformed(e);

}

});

fileMenu.add(exitMenuItem);

JMenuBar menuBar = new JMenuBar();

menuBar.add(fileMenu);

this.setJMenuBar(menuBar);

// 將組件進(jìn)行布局

JPanel jPanel1 = new JPanel();

jPanel1.setLayout(new BorderLayout());

JScrollPane pane = new JScrollPane(connectInfoList);

pane.setBorder(new TitledBorder(BorderFactory.createEtchedBorder(

Color.white, new Color(134, 134, 134)), "客戶端連接信息"));

jPanel1.add(new JScrollPane(pane), BorderLayout.CENTER);

this.getContentPane().setLayout(new BorderLayout());

this.getContentPane().add(statusBar, BorderLayout.SOUTH);

this.getContentPane().add(jPanel1, BorderLayout.CENTER);

this.pack();

}

/**

* 退出菜單項(xiàng)事件

* @param e

*/

public void exitActionPerformed(ActionEvent e){

// 向客戶端發(fā)送斷開(kāi)連接信息

sendMsgToClients(new StringBuffer(Constants.QUIT_IDENTIFER));

// 關(guān)閉所有的連接

closeAll();

System.exit(0);

}

/**

* 處理窗口關(guān)閉事件

*/

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

exitActionPerformed(null);

}

}

/**

* 刷新聊天室,不斷刷新clientProcessors,制造最新的用戶列表

*/

public static void notifyRoomPeople(){

StringBuffer people = new StringBuffer(Constants.PEOPLE_IDENTIFER);

for (int i = 0; i clientProcessors.size(); i++) {

ClientProcessor c = (ClientProcessor) clientProcessors.elementAt(i);

people.append(Constants.SEPERATOR).append(c.clientName);

}

// 用sendClients方法向客戶端發(fā)送用戶列表的信息

sendMsgToClients(people);

}

/**

* 向所有客戶端群發(fā)消息

* @param msg

*/

public static synchronized void sendMsgToClients(StringBuffer msg) {

for (int i = 0; i clientProcessors.size(); i++) {

ClientProcessor c = (ClientProcessor) clientProcessors.elementAt(i);

System.out.println("send msg: " + msg);

c.send(msg);

}

}

/**

* 關(guān)閉所有連接

*/

public static void closeAll(){

while (clientProcessors.size() 0) {

ClientProcessor c = (ClientProcessor) clientProcessors.firstElement();

try {

// 關(guān)閉socket連接和處理線程

c.socket.close();

c.toStop();

} catch (IOException e) {

System.out.println("Error:" + e);

} finally {

clientProcessors.removeElement(c);

}

}

}

/**

* 判斷客戶端是否合法。

* 不允許同一客戶端重復(fù)登陸,所謂同一客戶端是指IP和名字都相同。

* @param newclient

* @return

*/

public static boolean checkClient(ClientProcessor newclient){

if (clientProcessors.contains(newclient)){

return false;

} else {

return true;

}

}

/**

* 斷開(kāi)某個(gè)連接,并且從連接列表中刪除

* @param client

*/

public static void disconnect(ClientProcessor client){

disconnect(client, true);

}

/**

* 斷開(kāi)某個(gè)連接,根據(jù)要求決定是否從連接列表中刪除

* @param client

* @param toRemoveFromList

*/

public static synchronized void disconnect(ClientProcessor client, boolean toRemoveFromList){

try {

//在服務(wù)器端程序的list框中顯示斷開(kāi)信息

connectInfoList.add(client.clientIP + "斷開(kāi)連接");

ChatServer.activeConnects--; //將連接數(shù)減1

String constr = "目前有" + ChatServer.activeConnects + "客戶相連";

statusBar.setText(constr);

//向客戶發(fā)送斷開(kāi)連接信息

client.send(new StringBuffer(Constants.QUIT_IDENTIFER));

client.socket.close();

} catch (IOException e) {

System.out.println("Error:" + e);

} finally {

//從clients數(shù)組中刪除此客戶的相關(guān)socket等信息, 并停止線程。

if (toRemoveFromList) {

clientProcessors.removeElement(client);

client.toStop();

}

}

}

public static void main(String[] args) {

ChatServer chatServer1 = new ChatServer();

chatServer1.setVisible(true);

System.out.println("Server starting ...");

ServerSocket server = null;

try {

// 服務(wù)器端開(kāi)始偵聽(tīng)

server = new ServerSocket(Constants.SERVER_PORT);

} catch (IOException e) {

System.out.println("Error:" + e);

System.exit(1);

}

while (true) {

// 如果當(dāng)前客戶端數(shù)小于MAX_CLIENT個(gè)時(shí)接受連接請(qǐng)求

if (clientProcessors.size() Constants.MAX_CLIENT) {

Socket socket = null;

try {

// 收到客戶端的請(qǐng)求

socket = server.accept();

if (socket != null) {

System.out.println(socket + "連接");

}

} catch (IOException e) {

System.out.println("Error:" + e);

}

// 定義并實(shí)例化一個(gè)ClientProcessor線程類,用于處理客戶端的消息

ClientProcessor c = new ClientProcessor(socket);

if (checkClient(c)) {

// 添加到列表

clientProcessors.addElement(c);

// 如果客戶端合法,則繼續(xù)

int connum = ++ChatServer.activeConnects;

// 在狀態(tài)欄里顯示連接數(shù)

String constr = "目前有" + connum + "客戶相連";

ChatServer.statusBar.setText(constr);

// 將客戶socket信息寫(xiě)入list框

ChatServer.connectInfoList.add(c.clientIP + "連接");

c.start();

// 通知所有客戶端用戶列表發(fā)生變化

notifyRoomPeople();

} else {

//如果客戶端不合法

c.ps.println("不允許重復(fù)登陸");

disconnect(c, false);

}

} else {

//如果客戶端超過(guò)了MAX_CLIENT個(gè),則等待一段時(shí)間再嘗試接受請(qǐng)求

try {

Thread.sleep(200);

} catch (InterruptedException e) {

}

}

}

}

}

/**

* 處理客戶端發(fā)送的請(qǐng)求的線程

*/

class ClientProcessor extends Thread {

//存儲(chǔ)一個(gè)連接客戶端的socket信息

Socket socket;

//存儲(chǔ)客戶端的連接姓名

String clientName;

//存儲(chǔ)客戶端的ip信息

String clientIP;

//用來(lái)實(shí)現(xiàn)接受從客戶端發(fā)來(lái)的數(shù)據(jù)流

BufferedReader br;

//用來(lái)實(shí)現(xiàn)向客戶端發(fā)送信息的打印流

PrintStream ps;

boolean running = true;

/**

* 構(gòu)造方法

* @param s

*/

public ClientProcessor(Socket s) {

socket = s;

try {

// 初始化輸入輸出流

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

ps = new PrintStream(socket.getOutputStream());

// 讀取收到的信息,第一條信息是客戶端的名字、IP信息

String clientInfo = br.readLine();

// 讀取信息,根據(jù)消息分隔符解析消息

StringTokenizer stinfo = new StringTokenizer(clientInfo, Constants.SEPERATOR);

String head = stinfo.nextToken();

if (head.equals(Constants.CONNECT_IDENTIFER)){

if (stinfo.hasMoreTokens()){

//關(guān)鍵字后的第二段數(shù)據(jù)是客戶名信息

clientName = stinfo.nextToken();

}

if (stinfo.hasMoreTokens()){

//關(guān)鍵字后的第三段數(shù)據(jù)是客戶ip信息

clientIP = stinfo.nextToken();

}

System.out.println(head); //在控制臺(tái)打印頭信息

}

} catch (IOException e) {

System.out.println("Error:" + e);

}

}

/**

* 向客戶端發(fā)送消息

* @param msg

*/

public void send(StringBuffer msg) {

ps.println(msg);

ps.flush();

}

//線程運(yùn)行方法

public void run() {

while (running) {

String line = null;

try {

//讀取客戶端發(fā)來(lái)的數(shù)據(jù)流

line = br.readLine();

} catch (IOException e) {

System.out.println("Error" + e);

ChatServer.disconnect(this);

ChatServer.notifyRoomPeople();

return;

}

//客戶已離開(kāi)

if (line == null){

ChatServer.disconnect(this);

ChatServer.notifyRoomPeople();

return;

}

StringTokenizer st = new StringTokenizer(line, Constants.SEPERATOR);

String keyword = st.nextToken();

// 如果關(guān)鍵字是MSG則是客戶端發(fā)來(lái)的聊天信息

if (keyword.equals(Constants.MSG_IDENTIFER)){

StringBuffer msg = new StringBuffer(Constants.MSG_IDENTIFER).append(Constants.SEPERATOR);

msg.append(clientName);

msg.append(st.nextToken("\0"));

// 再將某個(gè)客戶發(fā)來(lái)的聊天信息發(fā)送到每個(gè)連接客戶的聊天欄中

ChatServer.sendMsgToClients(msg);

} else if (keyword.equals(Constants.QUIT_IDENTIFER)) {

// 如果關(guān)鍵字是QUIT則是客戶端發(fā)來(lái)斷開(kāi)連接的信息

// 服務(wù)器斷開(kāi)與這個(gè)客戶的連接

ChatServer.disconnect(this);

// 繼續(xù)監(jiān)聽(tīng)聊天室并刷新其他客戶的聊天人名list

ChatServer.notifyRoomPeople();

running = false;

}

}

}

public void toStop(){

running = false;

}

// 覆蓋Object類的equals方法

public boolean equals(Object obj){

if (obj instanceof ClientProcessor){

ClientProcessor obj1 = (ClientProcessor)obj;

if (obj1.clientIP.equals(this.clientIP)

(obj1.clientName.equals(this.clientName))){

return true;

}

}

return false;

}

// 覆蓋Object類的hashCode方法

public int hashCode(){

return (this.clientIP + Constants.SEPERATOR + this.clientName).hashCode();

}

}

package Chat;

/**

* 定義聊天室程序中用到的常量

*/

public class Constants {

// 服務(wù)器的端口號(hào)

public static final int SERVER_PORT = 2525;

public static final int MAX_CLIENT = 10;

// 消息標(biāo)識(shí)符與消息體之間的分隔符

public static final String SEPERATOR = ":";

// 消息信息的標(biāo)識(shí)符

public static final String MSG_IDENTIFER = "MSG";

// 用戶列表信息的標(biāo)識(shí)符

public static final String PEOPLE_IDENTIFER = "PEOPLE";

// 連接服務(wù)器信息的標(biāo)識(shí)符

public static final String CONNECT_IDENTIFER = "INFO";

// 退出信息標(biāo)識(shí)符

public static final String QUIT_IDENTIFER = "QUIT";

}

用eclipse編寫(xiě)一個(gè)基于java的五子棋(與電腦對(duì)弈)

基于java的五子棋:

主要功能代碼如下:

import?java.awt.*;

import?javax.swing.*;

import?java.awt.event.*;

public?class?Main?extends?JFrame?implements?ActionListener{

private?static?final?long?serialVersionUID?=?1L;

JButton[][]?point=new?JButton[40][40];

int[][]?state=new?int[40][40];

int[][]?value=new?int[40][40];

int[]?place=new?int[2];

public?Main(){

this.setTitle("五子棋");

this.setBounds(100,10,1000,1000);

this.setLayout(new?GridLayout(30,30));

int?i,j;

for(i=0;i=39;i++){

for(j=0;j=39;j++){

state[i][j]=0;

value[i][j]=0;

}

}

for(i=5;i=34;i++){

for?(j=5;j=34;j++){

point[i][j]=new?JButton("");

this.add(point[i][j]);

point[i][j].addActionListener(this);

}

}

this.setVisible(true);

}

public?void?actionPerformed(ActionEvent?e)

{

int?i,j;

for(i=5;i=34;i++){

for(j=5;j=34;j++){

if(e.getSource()==point[i][j]){

point[i][j].setBackground(Color.RED);

state[i][j]=1;

point[i][j].setEnabled(false);

value[i][j]=0;

}

}

}

public?static?void?main(String[]?args)throws?NullPointerException{

new?Main();

}

else?if(((state[i][j]==0)(state[i][j+1]==1)(state[i][j+2]==0))||

((state[i][j]==0)(state[i][j-1]==1)(state[i][j-2]==0))||

((state[i][j]==0)(state[i+1][j]==1)(state[i+2][j]==0))||

((state[i][j]==0)(state[i-1][j]==1)(state[i-2][j]==0))||???????????????????????????????//空紅空

((state[i][j]==0)(state[i+1][j+1]==1)(state[i+2][j+2]==0))||

((state[i][j]==0)(state[i+1][j-1]==1)(state[i+2][j-2]==0))||

((state[i][j]==0)(state[i-1][j+1]==1)(state[i-2][j+2]==0))||

((state[i][j]==0)(state[i-1][j-1]==1)(state[i-2][j-2]==0))||

((state[i][j]==0)(state[i][j+1]==1)(state[i][j+2]==1)(state[i][j+3]==2))||

((state[i][j]==0)(state[i][j-1]==1)(state[i][j-2]==1)(state[i][j-3]==2))||

((state[i][j]==0)(state[i+1][j]==1)(state[i+2][j]==1)(state[i+3][j]==2))||

((state[i][j]==0)(state[i-1][j]==1)(state[i-2][j]==1)(state[i-3][j]==2))||????????????????????????????//空紅紅藍(lán)型

((state[i][j]==0)(state[i+1][j+1]==1)(state[i+2][j+2]==1)(state[i+3][j+3]==2))||

((state[i][j]==0)(state[i+1][j-1]==1)(state[i+2][j-2]==1)(state[i+3][j-3]==2))||

((state[i][j]==0)(state[i-1][j+1]==1)(state[i-2][j+2]==1)(state[i-3][j+3]==2))||

((state[i][j]==0)(state[i-1][j-1]==1)(state[i-2][j-2]==1)(state[i-3][j-3]==2))){

value[i][j]=2;

return?value[i][j];

}

else?if(((state[i][j]==0)(state[i][j+1]==2)(state[i][j+2]==0))||

((state[i][j]==0)(state[i][j-1]==2)(state[i][j-2]==0))||

((state[i][j]==0)(state[i+1][j]==2)(state[i+2][j]==0))||

((state[i][j]==0)(state[i-1][j]==2)(state[i-2][j]==0))||

((state[i][j]==0)(state[i+1][j+1]==2)(state[i+2][j+2]==0))||

((state[i][j]==0)(state[i+1][j-1]==2)(state[i+2][j-2]==0))||

((state[i][j]==0)(state[i-1][j+1]==2)(state[i-2][j+2]==0))||

((state[i][j]==0)(state[i-1][j-1]==2)(state[i-2][j-2]==0))||

((state[i][j]==0)(state[i][j+1]==2)(state[i][j+2]==2)(state[i][j+3]==1))||

((state[i][j]==0)(state[i][j-1]==2)(state[i][j-2]==2)(state[i][j-3]==1))||

((state[i][j]==0)(state[i+1][j]==2)(state[i+2][j]==2)(state[i+3][j]==1))||

((state[i][j]==0)(state[i-1][j]==2)(state[i-2][j]==2)(state[i-3][j]==1))||

((state[i][j]==0)(state[i+1][j+1]==2)(state[i+2][j+2]==2)(state[i+3][j+3]==1))||

((state[i][j]==0)(state[i+1][j-1]==2)(state[i+2][j-2]==2)(state[i+3][j-3]==1))||

((state[i][j]==0)(state[i-1][j+1]==2)(state[i-2][j+2]==2)(state[i-3][j+3]==1))||

((state[i][j]==0)(state[i-1][j-1]==2)(state[i-2][j-2]==2)(state[i-3][j-3]==1))){

value[i][j]=1;

return?value[i][j];

}

else{

value[i][j]=0;

return?value[i][j];

}

}

public?int[]?largestValue(int[][]?value){

int?max,i,j,l,k;

l=0;k=0;

max=0;

for(i=5;i34;i++){

for(j=5;j34;j++){

if(maxvalue[i][j]){

max=value[i][j];

place[0]=i;

place[1]=j;

}

}

}

return?place;

}

}

運(yùn)行結(jié)果如下圖所示:

JAVA五子棋程序代碼分析(1)

樓主要是覺(jué)得看的不舒服可以拷到記事本里看~ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.awt.Color; //這一段import就不說(shuō)了,下面要用到的就import進(jìn)來(lái) public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener //繼承Applet表明是個(gè)applet,后面的接口必須要實(shí)現(xiàn)每個(gè)接口的所有方法 { int color_Qizi=0;//旗子的顏色標(biāo)識(shí) 0:白子 1:黑子 int intGame_Start=0;//游戲開(kāi)始標(biāo)志 0未開(kāi)始 1游戲中 int intGame_Body[][]=new int[16][16]; //設(shè)置棋盤(pán)棋子狀態(tài) 0 無(wú)子 1 白子 2 黑子 Button b1=new Button("游戲開(kāi)始"); Button b2=new Button("重置游戲"); //兩個(gè)按鈕 Label lblWin=new Label(" "); //這個(gè)label用來(lái)顯示最后輸贏信息的,先留空 Checkbox ckbHB[]=new Checkbox[2]; //用來(lái)表明選擇黑氣或白棋先走的checkbox CheckboxGroup ckgHB=new CheckboxGroup(); //兩個(gè)checkbox必須放在同一個(gè)checkboxgroup里才能做到單選 public void init() //初始化,堆砌界面 { setLayout(null); //不設(shè)布局管理器 addMouseListener(this); //將本類作為鼠標(biāo)事件的接口響應(yīng)鼠標(biāo)動(dòng)作 add(b1); //將事先定義好的第一個(gè)按鈕添加入界面 b1.setBounds(330,50,80,30); //設(shè)置第一個(gè)按鈕左上角的位置和大小 b1.addActionListener(this); //將本類作為按鈕事件的接口響應(yīng)按鈕動(dòng)作 add(b2); //將事先定義好的第二個(gè)按鈕添加進(jìn)去 b2.setBounds(330,90,80,30); /設(shè)置第二個(gè)按鈕左上角的位置和大小 b2.addActionListener(this); //將本類作為按鈕事件的接口響應(yīng)按鈕動(dòng)作 ckbHB[0]=new Checkbox("白子先",ckgHB,false); //new一個(gè)checkbox ckbHB[0].setBounds(320,20,60,30); //設(shè)置左上角位置和大小 ckbHB[1]=new Checkbox("黑子先",ckgHB,false); //new第二個(gè)checkbox ckbHB[1].setBounds(380,20,60,30); //設(shè)置左上角位置和大小 add(ckbHB[0]); //將第一個(gè)checkbox加入界面 add(ckbHB[1]); //將第二個(gè)checkbox加入界面 ckbHB[0].addItemListener(this); //將本類作為其事件接口來(lái)響應(yīng)選中動(dòng)作 ckbHB[1].addItemListener(this); //將本類作為其事件接口來(lái)響應(yīng)選中動(dòng)作 add(lblWin); //將標(biāo)簽加入界面 lblWin.setBounds(330,130,80,30); //設(shè)置標(biāo)簽的左上角位置和大小 Game_start_csh(); //調(diào)用游戲初始化 } public void itemStateChanged(ItemEvent e) //ItemListener接口中的方法,必須要有 { if (ckbHB[0].getState()) //選擇黑子先還是白子先 { color_Qizi=0; //白棋先 } else { color_Qizi=1; //黑棋先 } } public void actionPerformed(ActionEvent e) //ActionListener接口中的方法,也是必須的 { Graphics g=getGraphics(); //這句話貌似可以去掉,g是用來(lái)畫(huà)圖或者畫(huà)界面的 if (e.getSource()==b1) //如果動(dòng)作的來(lái)源是第一個(gè)按鈕 { Game_start(); //游戲開(kāi)始 } else //否則 { Game_re(); //游戲重新開(kāi)始 } } public void mousePressed(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseClicked(MouseEvent e) //鼠標(biāo)單擊時(shí) { Graphics g=getGraphics(); //獲得畫(huà)筆 int x1,y1; x1=e.getX(); //單擊處的x坐標(biāo) y1=e.getY(); //單擊處的y坐標(biāo) if (e.getX()20 || e.getX()300 || e.getY()20 || e.getY()300) //在棋盤(pán)范圍之外 { return; //則這是不能走棋的,直接返回 } //下面這兩個(gè)if和兩個(gè)賦值的作用是將x和y坐標(biāo)根據(jù)舍入原則修改成棋盤(pán)上格子的坐標(biāo) if (x1%2010) { x1+=20; } if(y1%2010) { y1+=20; } x1=x1/20*20; y1=y1/20*20; set_Qizi(x1,y1); //在棋盤(pán)上畫(huà)上一個(gè)棋子 } public void mouseEntered(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseExited(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseReleased(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseDragged(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void mouseMoved(MouseEvent e){} //MouseListener接口中的方法,用不到所以留個(gè)空,但一定要有 public void paint(Graphics g) //重繪和applet程序裝載的時(shí)候會(huì)調(diào)用這個(gè)繪制的過(guò)程 { draw_qipan(g); //畫(huà)棋盤(pán) }


當(dāng)前標(biāo)題:最新java五子棋代碼 java五子棋代碼壓縮包
URL網(wǎng)址:http://weahome.cn/article/ddipgsd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部