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

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

java人機(jī)會(huì)話代碼 java人機(jī)交互代碼

求JAVA人機(jī)猜拳的代碼,類似一下界面。

自己純手打,老半天才弄出來(lái)啊

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比沾益網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式沾益網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋沾益地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;

import java.util.Random;

import javax.swing.AbstractAction;

import javax.swing.Box;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Demo2 extends JFrame {

private JLabel lb1, lb2, lb3, lb4; // 提示標(biāo)簽

private JTextField ta1, ta2;// 兩個(gè)文本框

private JButton b1, b2, b3; // 三個(gè)按鈕

private JPanel p1, p2; // 兩個(gè)JPanel面板

public Demo2() {

// 初始化所有組件

lb1 = new JLabel("歡迎使用人機(jī)猜拳程序");

lb2 = new JLabel("你出拳: ");

lb3 = new JLabel("電腦出拳:");

lb4 = new JLabel("結(jié)果");

ta1 = new JTextField();

ta1.setPreferredSize(new Dimension(60, 60)); // 設(shè)置大小

ta1.setEditable(false);//設(shè)置不可編輯

ta2 = new JTextField();

ta2.setPreferredSize(new Dimension(60, 60));

ta2.setEditable(false);//設(shè)置不可編輯

b1 = new JButton("剪刀");

b2 = new JButton("石頭");

b3 = new JButton("布");

p1 = new JPanel();

p2 = new JPanel();

// 設(shè)置第一個(gè)面板內(nèi)容

Box box = Box.createVerticalBox();

Box box1 = Box.createHorizontalBox();

box1.add(lb2);

box1.add(ta1);

box1.add(lb3);

box1.add(ta2);

box.add(lb1);

box.add(Box.createVerticalStrut(40));

box.add(box1);

box.add(Box.createVerticalStrut(10));

box.add(lb4);

box.add(new JLabel());

p1.add(box);

// 設(shè)置第二個(gè)面板

p2.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器

p2.setPreferredSize(new Dimension(0, 60));

GridBagConstraints g2 = new GridBagConstraints();

g2.fill = GridBagConstraints.BOTH;

g2.weightx = 1.0;

g2.weighty = 1.0;

g2.gridx = 0;

g2.gridy = 0;

p2.add(b1, g2);

g2.gridx = 1;

p2.add(b2, g2);

g2.gridx = 2;

p2.add(b3, g2);

//為3個(gè)按鈕添加事件

b1.addActionListener(new buttonAction());

b2.addActionListener(new buttonAction());

b3.addActionListener(new buttonAction());

this.getContentPane().add(p1);

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

this.setTitle("機(jī)器人猜拳游戲");

this.setSize(300, 300);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

//事件類

class buttonAction extends AbstractAction{

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==b1){

ta1.setText("剪刀");

init(ta1.getText());

}else if(e.getSource()==b2){

ta1.setText("石頭");

init(ta1.getText());

}else if(e.getSource()==b3){

ta1.setText("布");

init(ta1.getText());

}

}

// 模擬電腦出拳,產(chǎn)生三個(gè)隨機(jī)數(shù)。0代表剪刀,1代表石頭,2代表布

public String getQuan(){

String str="";

int num=new Random().nextInt(3) ;

if(num==0){

str="剪刀";

}else if(num==1){

str="石頭";

}else if(num==2){

str="布";

}

return str;

}

// 判斷輸贏方法

public String isying(String s1,String s2){

String s="";

if(s1.equals(s2)){

s="平局";

}else if(s1.equals("剪刀")s2.equals("布")){

s="你贏";

}else if(s1.equals("石頭")s2.equals("剪刀")){

s="你贏";

}else if(s1.equals("布")s2.equals("石頭")){

s="你贏";

}else{

s="電腦贏";

}

return s;

}

public void init(String wo){

String sy=""; // 保存輸贏結(jié)果

String dncq=getQuan(); //電腦出拳

if(wo.equals(dncq)){

sy="平局";

}else if(wo.equals("剪刀")dncq.equals("布")){

sy="你贏";

}else if(wo.equals("石頭")dncq.equals("剪刀")){

sy="你贏";

}else if(wo.equals("布")dncq.equals("石頭")){

sy="你贏";

}else{

sy="電腦贏";

}

ta2.setText(dncq);// 電腦出拳

lb4.setText("結(jié)果:"+sy);

}

}

public static void main(String[] args) {

new Demo2();

}

}

求java編寫的五子棋代碼,要有電腦AI的

java網(wǎng)絡(luò)五子棋

下面的源代碼分為4個(gè)文件;

chessClient.java:客戶端主程序。

chessInterface.java:客戶端的界面。

chessPad.java:棋盤的繪制。

chessServer.java:服務(wù)器端。

可同時(shí)容納50個(gè)人同時(shí)在線下棋,聊天。

沒有加上詳細(xì)注釋,不過(guò)絕對(duì)可以運(yùn)行,j2sdk1.4下通過(guò)。

/*********************************************************************************************

1.chessClient.java

**********************************************************************************************/

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.util.*;

class clientThread extends Thread

{

chessClient chessclient;

clientThread(chessClient chessclient)

{

this.chessclient=chessclient;

}

public void acceptMessage(String recMessage)

{

if(recMessage.startsWith("/userlist "))

{

StringTokenizer userToken=new StringTokenizer(recMessage," ");

int userNumber=0;

chessclient.userpad.userList.removeAll();

chessclient.inputpad.userChoice.removeAll();

chessclient.inputpad.userChoice.addItem("所有人");

while(userToken.hasMoreTokens())

{

String user=(String)userToken.nextToken(" ");

if(userNumber0 !user.startsWith("[inchess]"))

{

chessclient.userpad.userList.add(user);

chessclient.inputpad.userChoice.addItem(user);

}

userNumber++;

}

chessclient.inputpad.userChoice.select("所有人");

}

else if(recMessage.startsWith("/yourname "))

{

chessclient.chessClientName=recMessage.substring(10);

chessclient.setTitle("Java五子棋客戶端 "+"用戶名:"+chessclient.chessClientName);

}

else if(recMessage.equals("/reject"))

{

try

{

chessclient.chesspad.statusText.setText("不能加入游戲");

chessclient.controlpad.cancelGameButton.setEnabled(false);

chessclient.controlpad.joinGameButton.setEnabled(true);

chessclient.controlpad.creatGameButton.setEnabled(true);

}

catch(Exception ef)

{

chessclient.chatpad.chatLineArea.setText("chessclient.chesspad.chessSocket.close無(wú)法關(guān)閉");

}

chessclient.controlpad.joinGameButton.setEnabled(true);

}

else if(recMessage.startsWith("/peer "))

{

chessclient.chesspad.chessPeerName=recMessage.substring(6);

if(chessclient.isServer)

{

chessclient.chesspad.chessColor=1;

chessclient.chesspad.isMouseEnabled=true;

chessclient.chesspad.statusText.setText("請(qǐng)黑棋下子");

}

else if(chessclient.isClient)

{

chessclient.chesspad.chessColor=-1;

chessclient.chesspad.statusText.setText("已加入游戲,等待對(duì)方下子...");

}

}

else if(recMessage.equals("/youwin"))

{

chessclient.isOnChess=false;

chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);

chessclient.chesspad.statusText.setText("對(duì)方退出,請(qǐng)點(diǎn)放棄游戲退出連接");

chessclient.chesspad.isMouseEnabled=false;

}

else if(recMessage.equals("/OK"))

{

chessclient.chesspad.statusText.setText("創(chuàng)建游戲成功,等待別人加入...");

}

else if(recMessage.equals("/error"))

{

chessclient.chatpad.chatLineArea.append("傳輸錯(cuò)誤:請(qǐng)退出程序,重新加入 \n");

}

else

{

chessclient.chatpad.chatLineArea.append(recMessage+"\n");

chessclient.chatpad.chatLineArea.setCaretPosition(

chessclient.chatpad.chatLineArea.getText().length());

}

}

public void run()

{

String message="";

try

{

while(true)

{

message=chessclient.in.readUTF();

acceptMessage(message);

}

}

catch(IOException es)

{

}

}

}

public class chessClient extends Frame implements ActionListener,KeyListener

{

userPad userpad=new userPad();

chatPad chatpad=new chatPad();

controlPad controlpad=new controlPad();

chessPad chesspad=new chessPad();

inputPad inputpad=new inputPad();

Socket chatSocket;

DataInputStream in;

DataOutputStream out;

String chessClientName=null;

String host=null;

int port=4331;

boolean isOnChat=false; //在聊天?

boolean isOnChess=false; //在下棋?

boolean isGameConnected=false; //下棋的客戶端連接?

boolean isServer=false; //如果是下棋的主機(jī)

boolean isClient=false; //如果是下棋的客戶端

Panel southPanel=new Panel();

Panel northPanel=new Panel();

Panel centerPanel=new Panel();

Panel westPanel=new Panel();

Panel eastPanel=new Panel();

chessClient()

{

super("Java五子棋客戶端");

setLayout(new BorderLayout());

host=controlpad.inputIP.getText();

westPanel.setLayout(new BorderLayout());

westPanel.add(userpad,BorderLayout.NORTH);

westPanel.add(chatpad,BorderLayout.CENTER);

westPanel.setBackground(Color.pink);

inputpad.inputWords.addKeyListener(this);

chesspad.host=controlpad.inputIP.getText();

centerPanel.add(chesspad,BorderLayout.CENTER);

centerPanel.add(inputpad,BorderLayout.SOUTH);

centerPanel.setBackground(Color.pink);

controlpad.connectButton.addActionListener(this);

controlpad.creatGameButton.addActionListener(this);

controlpad.joinGameButton.addActionListener(this);

controlpad.cancelGameButton.addActionListener(this);

controlpad.exitGameButton.addActionListener(this);

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(false);

southPanel.add(controlpad,BorderLayout.CENTER);

southPanel.setBackground(Color.pink);

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

if(isOnChat)

{

try

{

chatSocket.close();

}

catch(Exception ed)

{

}

}

if(isOnChess || isGameConnected)

{

try

{

chesspad.chessSocket.close();

}

catch(Exception ee)

{

}

}

System.exit(0);

}

public void windowActivated(WindowEvent ea)

{

}

});

add(westPanel,BorderLayout.WEST);

add(centerPanel,BorderLayout.CENTER);

add(southPanel,BorderLayout.SOUTH);

pack();

setSize(670,548);

setVisible(true);

setResizable(false);

validate();

}

public boolean connectServer(String serverIP,int serverPort) throws Exception

{

try

{

chatSocket=new Socket(serverIP,serverPort);

in=new DataInputStream(chatSocket.getInputStream());

out=new DataOutputStream(chatSocket.getOutputStream());

clientThread clientthread=new clientThread(this);

clientthread.start();

isOnChat=true;

return true;

}

catch(IOException ex)

{

chatpad.chatLineArea.setText("chessClient:connectServer:無(wú)法連接,建議重新啟動(dòng)程序 \n");

}

return false;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==controlpad.connectButton)

{

host=chesspad.host=controlpad.inputIP.getText();

try

{

if(connectServer(host,port))

{

chatpad.chatLineArea.setText("");

controlpad.connectButton.setEnabled(false);

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

chesspad.statusText.setText("連接成功,請(qǐng)創(chuàng)建游戲或加入游戲");

}

}

catch(Exception ei)

{

chatpad.chatLineArea.setText("controlpad.connectButton:無(wú)法連接,建議重新啟動(dòng)程序 \n");

}

}

if(e.getSource()==controlpad.exitGameButton)

{

if(isOnChat)

{

try

{

chatSocket.close();

}

catch(Exception ed)

{

}

}

if(isOnChess || isGameConnected)

{

try

{

chesspad.chessSocket.close();

}

catch(Exception ee)

{

}

}

System.exit(0);

}

if(e.getSource()==controlpad.joinGameButton)

{

String selectedUser=userpad.userList.getSelectedItem();

if(selectedUser==null || selectedUser.startsWith("[inchess]") ||

selectedUser.equals(chessClientName))

{

chesspad.statusText.setText("必須先選定一個(gè)有效用戶");

}

else

{

try

{

if(!isGameConnected)

{

if(chesspad.connectServer(chesspad.host,chesspad.port))

{

isGameConnected=true;

isOnChess=true;

isClient=true;

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName);

}

}

else

{

isOnChess=true;

isClient=true;

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName);

}

}

catch(Exception ee)

{

isGameConnected=false;

isOnChess=false;

isClient=false;

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

chatpad.chatLineArea.setText("chesspad.connectServer無(wú)法連接 \n"+ee);

}

}

}

if(e.getSource()==controlpad.creatGameButton)

{

try

{

if(!isGameConnected)

{

if(chesspad.connectServer(chesspad.host,chesspad.port))

{

isGameConnected=true;

isOnChess=true;

isServer=true;

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName);

}

}

else

{

isOnChess=true;

isServer=true;

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName);

}

}

catch(Exception ec)

{

isGameConnected=false;

isOnChess=false;

isServer=false;

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

ec.printStackTrace();

chatpad.chatLineArea.setText("chesspad.connectServer無(wú)法連接 \n"+ec);

}

}

if(e.getSource()==controlpad.cancelGameButton)

{

if(isOnChess)

{

chesspad.chessthread.sendMessage("/giveup "+chessClientName);

chesspad.chessVictory(-1*chesspad.chessColor);

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

chesspad.statusText.setText("請(qǐng)建立游戲或者加入游戲");

}

if(!isOnChess)

{

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

chesspad.statusText.setText("請(qǐng)建立游戲或者加入游戲");

}

isClient=isServer=false;

}

}

public void keyPressed(KeyEvent e)

{

TextField inputWords=(TextField)e.getSource();

if(e.getKeyCode()==KeyEvent.VK_ENTER)

{

if(inputpad.userChoice.getSelectedItem().equals("所有人"))

{

try

{

out.writeUTF(inputWords.getText());

inputWords.setText("");

}

catch(Exception ea)

{

chatpad.chatLineArea.setText("chessClient:KeyPressed無(wú)法連接,建議重新連接 \n");

userpad.userList.removeAll();

inputpad.userChoice.removeAll();

inputWords.setText("");

controlpad.connectButton.setEnabled(true);

}

}

else

{

try

{

out.writeUTF("/"+inputpad.userChoice.getSelectedItem()+" "+inputWords.getText());

inputWords.setText("");

}

catch(Exception ea)

{

chatpad.chatLineArea.setText("chessClient:KeyPressed無(wú)法連接,建議重新連接 \n");

userpad.userList.removeAll();

inputpad.userChoice.removeAll();

inputWords.setText("");

controlpad.connectButton.setEnabled(true);

}

}

}

}

public void keyTyped(KeyEvent e)

{

}

public void keyReleased(KeyEvent e)

{

}

public static void main(String args[])

{

chessClient chessClient=new chessClient();

}

}

/******************************************************************************************

下面是:chessInteface.java

******************************************************************************************/

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

class userPad extends Panel

{

List userList=new List(10);

userPad()

{

setLayout(new BorderLayout());

for(int i=0;i50;i++)

{

userList.add(i+"."+"沒有用戶");

}

add(userList,BorderLayout.CENTER);

}

}

class chatPad extends Panel

{

TextArea chatLineArea=new TextArea("",18,30,TextArea.SCROLLBARS_VERTICAL_ONLY);

chatPad()

{

setLayout(new BorderLayout());

add(chatLineArea,BorderLayout.CENTER);

}

}

class controlPad extends Panel

{

Label IPlabel=new Label("IP",Label.LEFT);

TextField inputIP=new TextField("localhost",10);

Button connectButton=new Button("連接主機(jī)");

Button creatGameButton=new Button("建立游戲");

Button joinGameButton=new Button("加入游戲");

Button cancelGameButton=new Button("放棄游戲");

Button exitGameButton=new Button("關(guān)閉程序");

controlPad()

{

setLayout(new FlowLayout(FlowLayout.LEFT));

setBackground(Color.pink);

add(IPlabel);

add(inputIP);

add(connectButton);

add(creatGameButton);

add(joinGameButton);

add(cancelGameButton);

add(exitGameButton);

}

}

class inputPad extends Panel

{

TextField inputWords=new TextField("",40);

Choice userChoice=new Choice();

inputPad()

{

setLayout(new FlowLayout(FlowLayout.LEFT));

for(int i=0;i50;i++)

{

userChoice.addItem(i+"."+"沒有用戶");

}

userChoice.setSize(60,24);

add(userChoice);

add(inputWords);

}

}

/**********************************************************************************************

下面是:chessPad.java

**********************************************************************************************/

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.util.*;

class chessThread extends Thread

{

chessPad chesspad;

chessThread(chessPad chesspad)

{

this.chesspad=chesspad;

}

public void sendMessage(String sndMessage)

{

try

{

chesspad.outData.writeUTF(sndMessage);

}

catch(Exception ea)

{

System.out.println("chessThread.sendMessage:"+ea);

}

}

public void acceptMessage(String recMessage)

{

if(recMessage.startsWith("/chess "))

{

StringTokenizer userToken=new StringTokenizer(recMessage," ");

String chessToken;

String[] chessOpt={"-1","-1","0"};

int chessOptNum=0;

while(userToken.hasMoreTokens())

{

chessToken=(String)userToken.nextToken(" ");

if(chessOptNum=1 chessOptNum=3)

{

chessOpt[chessOptNum-1]=chessToken;

}

chessOptNum++;

}

chesspad.netChessPaint(Integer.parseInt(chessOpt[0]),Integer.parseInt(chessOpt[1]),Integer.parseInt(chessOpt[2]));

}

else if(recMessage.startsWith("/yourname "))

{

chesspad.chessSelfName=recMessage.substring(10);

}

else if(recMessage.equals("/error"))

{

chesspad.statusText.setText("錯(cuò)誤:沒有這個(gè)用戶,請(qǐng)退出程序,重新加入");

}

else

{

//System.out.println(recMessage);

}

}

public void run()

{

String message="";

try

{

while(true)

{

message=chesspad.inData.readUTF();

acceptMessage(message);

}

}

catch(IOException es)

{

}

}

}

class chessPad extends Panel implements MouseListener,ActionListener

{

int chessPoint_x=-1,chessPoint_y=-1,chessColor=1;

int chessBlack_x[]=new int[200];

int chessBlack_y[]=new int[200];

int chessWhite_x[]=new int[200];

int chessWhite_y[]=new int[200];

int chessBlackCount=0,chessWhiteCount=0;

int chessBlackWin=0,chessWhiteWin=0;

boolean isMouseEnabled=false,isWin=false,isInGame=false;

TextField statusText=new TextField("請(qǐng)先連接服務(wù)器");

Socket chessSocket;

DataInputStream inData;

DataOutputStream outData;

String chessSelfName=null;

String chessPeerName=null;

String host=null;

int port=4331;

chessThread chessthread=new chessThread(this);

chessPad()

{

setSize(440,440);

setLayout(null);

setBackground(Color.pink);

addMouseListener(this);

add(statusText);

statusText.setBounds(40,5,360,24);

statusText.setEditable(false);

}

public boolean connectServer(String ServerIP,int ServerPort) throws Exception

{

try

{

chessSocket=new Socket(ServerIP,ServerPort);

inData=new DataInputStream(chessSocket.getInputStream());

outData=new DataOutputStream(chessSocket.getOutputStream());

chessthread.start();

return true;

}

catch(IOException ex)

{

statusText.setText("chessPad:connectServer:無(wú)法連接 \n");

}

return false;

}

public void chessVictory(int chessColorWin)

{

this.removeAll();

for(int i=0;i=chessBlackCount;i++)

{

chessBlack_x[i]=0;

chessBlack_y[i]=0;

}

for(int i=0;i=chessWhiteCount;i++)

{

chessWhite_x[i]=0;

chessWhite_y[i]=0;

}

chessBlackCount=0;

chessWhiteCount=0;

add(statusText);

statusText.setBounds(40,5,360,24);

if(chessColorWin==1)

{ chessBlackWin++;

statusText.setText("黑棋勝,黑:白為"+chessBlackWin+":"+chessWhiteWin+",重新開局,等待白棋下子...");

}

else if(chessColorWin==-1)

{

chessWhiteWin++;

statusText.setText("白棋勝,黑:白為"+chessBlackWin+":"+chessWhiteWin+",重新開局,等待黑棋下子...");

}

}

public void getLocation(int a,int b,int color)

{

if(color==1)

{

chessBlack_x[chessBlackCount]=a*20;

chessBlack_y[chessBlackCount]=b*20;

chessBlackCount++;

}

else if(color==-1)

{

chessWhite_x[chessWhiteCount]=a*20;

chessWhite_y[chessWhiteCount]=b*20;

chessWhiteCount++;

}

}

public boolean checkWin(int a,int b,int checkColor)

{

int step=1,chessLink=1,chessLinkTest=1,chessCompare=0;

if(checkColor==1)

{

chessLink=1;

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a+step)*20==chessBlack_x[chessCompare]) ((b*20)==chessBlack_y[chessCompare]))

{

chessLink=chessLink+1;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a-step)*20==chessBlack_x[chessCompare]) (b*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

chessLink=1;

chessLinkTest=1;

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if((a*20==chessBlack_x[chessCompare]) ((b+step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if((a*20==chessBlack_x[chessCompare]) ((b-step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

chessLink=1;

chessLinkTest=1;

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a-step)*20==chessBlack_x[chessCompare]) ((b+step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a+step)*20==chessBlack_x[chessCompare]) ((b-step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

chessLink=1;

chessLinkTest=1;

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a+step)*20==chessBlack_x[chessCompare]) ((b+step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

}

if(chessLink==(chessLinkTest+1))

chessLinkTest++;

else

break;

}

for(step=1;step=4;step++)

{

for(chessCompare=0;chessCompare=chessBlackCount;chessCompare++)

{

if(((a-step)*20==chessBlack_x[chessCompare]) ((b-step)*20==chessBlack_y[chessCompare]))

{

chessLink++;

if(chessLink==5)

{

return(true);

}

}

求java人機(jī)猜拳游戲的程序

我之前寫了個(gè)猜拳游戲的源代碼,不過(guò)沒你想的這么精彩。你才給5分就給你你自己修改了,應(yīng)該很簡(jiǎn)單的。要多給點(diǎn)分我可以幫你修改。

import java.util.Scanner;

import java.util.Random;

public class caiquan

{

final int jiandao=0;

final int shitou=1;

final int bu=2;

public static void main(String[] args)

{

String yn=;y;;

while (yn.equals(;y;))

{

Scanner scanner = new Scanner(System.in);

System.out.println(;歡迎玩猜拳游戲。請(qǐng)輸入0,1,2:0表示剪刀,1表示石頭,2表示布;);

int a = scanner.nextInt();

Random rd = new Random();

int b = rd.nextInt(3);

switch (b)

{

case 0:

{

System.out.println(;系統(tǒng)出的是剪刀;);

switch(a)

{

case 0:System.out.println(;平;);break;

case 1:System.out.println(;贏;);break;

case 2:System.out.println(;輸;);break;

}

}

break;

case 1:

{

System.out.println(;系統(tǒng)出的是石頭;);

switch(a)

{

case 0:System.out.println(;輸;);break;

case 1:System.out.println(;平;);break;

case 2:System.out.println(;贏;);break;

}

}

break;

case 2:

{

System.out.println(;系統(tǒng)出的是布;);

switch(a)

{

case 0:System.out.println(;贏;);break;

case 1:System.out.println(;輸;);break;

case 2:System.out.println(;平;);break;

}

}

}

Scanner ynn = new Scanner(System.in);

System.out.println(;是否繼續(xù)?是請(qǐng)輸入y,否則輸入n。;);

yn=ynn.next();

}

}

}

java編程人機(jī)猜拳類和對(duì)象做求代碼

先建立個(gè)Game包

然后我做的是分了5個(gè)類來(lái)做的

TestStartGuess 類

package com.game.guess;

public class TestStartGuess {

/**

* 人機(jī)互動(dòng)版猜拳游戲

* 程序入口

*/

public static void main(String[] args) {

Game game=new Game();

game.initial();

game.startGame();

}

}

2.Person 類

package com.game.guess;

import java.util.Scanner;

/**

* 用戶類

*階段1完成

* @param Scanner

*/

public class Person {

String name ="匿名";//名字

int score =0;//積分

/**

* 出拳

*@return出拳結(jié)果:1.剪刀 2.石頭 3.布

*/

public int showFist(){

//接收用戶的選擇

Scanner input =new Scanner(System.in);

System.out.print("\n請(qǐng)出拳:1.剪刀 2.石頭 3.布 (輸入相應(yīng)數(shù)字):");

int show=input.nextInt();

//輸出出拳結(jié)果,并返回

switch(show){

case 1:

System.out.println("你出拳:剪刀");

break;

case 2:

System.out.println("你出拳:石頭");

break;

case 3:

System.out.println("你出拳:布");

break;

}

return show;

}

}

3.Computer 類

package com.game.guess;

/**

*計(jì)算機(jī)類

*階段2完成

*/

public class Computer{

String name="電腦";//名字

int score = 0;;//積分

/**

*出拳

*@return 出拳結(jié)果:1.剪刀 2.石頭 3.布

*/

public int showFist(){

? ? ? ?//產(chǎn)生隨機(jī)數(shù)

? ? ? ?int show =(int)(Math.random()*10)%3+1;//產(chǎn)生隨機(jī)數(shù),表示電腦出拳

? ? ? ?//輸出出拳結(jié)果并返回

? ? switch(show){

case 1:

System.out.println(name+"你出拳:剪刀");

break;

case 2:

System.out.println(name+"你出拳:石頭");

break;

case 3:

System.out.println(name+"你出拳:布");

break;

}

return show;

}

}

4.Game 類

package com.game.guess;

import java.util.Scanner;

/**

* 游戲類類完全版

* 階段7:功能擴(kuò)展

* @param computer

*

*/

public class Gamecomputer {

Person person; //甲方

Computer computer; ?//乙方

int count;//對(duì)戰(zhàn)次數(shù)

/**

* 初始化

*/

public void initial(){

person=new Person();

computer=new Computer();

count=0;

}

/**

* 開始游戲

*/

@SuppressWarnings("resource")

public void startGame(){

System.out.println("-------歡迎進(jìn)入游戲世界-------\n");

System.out.println("\n\t\t***************");

System.out.println("\t\t**猜拳,開始 **");

System.out.println("\t\t***************");

System.out.println("\n\n出拳規(guī)則:1.剪刀,2.石頭,3.布");

Scanner input=new Scanner(System.in);

String exit="n"; //退出系統(tǒng)

do{

initial();//初始化

/*選擇對(duì)方角色*/

System.out.print("請(qǐng)選擇對(duì)方角色:(1:劉備,2:孫權(quán),3:曹操):");

int role=input.nextInt();

if(role==1){

computer.name="劉備";

}else if(role==2){

computer.name="孫權(quán)";

}else if(role==3){

computer.name="曹操";

}

//擴(kuò)展功能1:輸入用戶姓名

/*輸入用戶姓名*/

System.out.print("請(qǐng)輸入你的姓名:");

person.name=input.next();

System.out.println(person.name+"VS"+computer.name+"對(duì)戰(zhàn)\n");

//擴(kuò)展功能1結(jié)束

System.out.print("要開始嗎?(y/n)");

String start=input.next();//開始每一局游戲

int perFist; //用戶出的拳

int compFist; //計(jì)算機(jī)出的拳

while(start.equals("y")){

/*出拳*/

? ?perFist=person.showFist();

? ?compFist=computer.showFist();

? ?/*裁決*/

? ?if((perFist==1compFist==1)||(perFist==2compFist==2)||(perFist==3compFist==3)){

? ? ? ?System.out.println("結(jié)果:和局,真衰!嘿嘿,等著瞧吧!\n"); ?//平局

? ? ? ? ? ? ? }else if((perFist==1compFist==3)||(perFist==2compFist==1)||(perFist==3compFist==2)){

? ? ? ? ? ?System.out.println("結(jié)果:恭喜,你贏了!"); ?//用戶贏

? ? ? ? ? ? ? ? ? ? person.score++;

? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? System.out.println("結(jié)果說(shuō):^_^,你輸了,真笨!\n"); ?//計(jì)算機(jī)贏

? ? ? ? ? ? ? ? computer.score++;

? ? ? ? ? ? ? }

? ? ? ? ? ? ? count++;

? ? ? ? ? ? ? System.out.println("\n是否開始下一輪(y/n):");

? ? ? ? ? ? ? start=input.next();

? ? ? ? ? ? ? }

/*顯示結(jié)果*/

showResult();

//擴(kuò)展功能3:循環(huán)游戲,知道退出系統(tǒng)

System.out.print("\n要開始下一局嗎?(y/n):");

exit=input.next();

? ? ? ?System.out.println();

? ? ? ?//擴(kuò)展功能3結(jié)束

}while(!exit.equals("n"));

System.out.println("系統(tǒng)退出!");

}

/**

* 顯示比賽結(jié)果

*/

public void showResult(){

/*顯示對(duì)戰(zhàn)次數(shù)*/

System.out.println("-------------------------------");

? ? ? System.out.println(computer.name+"VS"+person.name);

? ? ? System.out.println("對(duì)戰(zhàn)次數(shù):"+count);

? ?

? ? ? //擴(kuò)展功能2:顯示最終的得分

? ? ? System.out.println("\n姓名\t得分");

? ? ? System.out.println(person.name+"\t"+person.score);

System.out.println(computer.name+"\t"+computer.score+"\n");

? ?//擴(kuò)展功能2結(jié)束

? ? ? /*顯示對(duì)戰(zhàn)結(jié)果*/

? ? ? int result=calcResult();

? ? ? if(result==1){

? ? ? System.out.println("結(jié)果:打成平手,下次再和你一分高下!");

? ? ? }else if(result==2){

? ? ? System.out.println("結(jié)果:恭喜恭喜!"); ?//用戶獲勝

? ? ? }else{

? ? ? System.out.println("結(jié)果:呵呵,笨笨,下次加油??!"); ?//計(jì)算機(jī)獲勝

? ? ? }

System.out.println("--------------------------------");

}

/**

* 計(jì)算比賽結(jié)果

* @return1:戰(zhàn)平; 2:用戶贏; 3:電腦贏

*/

public int calcResult(){

if(person.score==computer.score){

return 1;//戰(zhàn)平

}else if(person.scorecomputer.score){

return 2;//用戶贏

}else{

return 3;//電腦贏

}

}

}

5.Start 類

package com.game.guess;

public class StartGuess {

public static void main (String[] args){

Game c = new Game();

c.initial();

c.startGame();

}

}

然后編譯執(zhí)行就OK了

希望能幫到你

java socket,我有客戶端和服務(wù)器的代碼,幫我添加廣播和能多人會(huì)話,加分?。〈a如下

根據(jù)你的改了個(gè)!不好意思,其中讀寫的思路稍微有點(diǎn)不同!不過(guò)可以做參考!

Server端代碼:

import java.net.*;

import java.io.*;

import java.util.*;

public class TestServer {

ServerSocket s = null;

boolean started = false;//用來(lái)監(jiān)聽服務(wù)器是否啟動(dòng)!

ListServerReaderWriter clients = new ArrayListServerReaderWriter();//用來(lái)存放啟動(dòng)的客服端

public static void main(String[] args) {

new TestServer().start();

}

public void start() {

try {

s = new ServerSocket(5050);

started = true;

} catch(SocketException e) {

System.out.println("5050端口正在使用中?。。≌?qǐng)關(guān)掉相關(guān)程序并重新運(yùn)行服務(wù)器!");

System.exit(0);

} catch (IOException e) {

e.printStackTrace();

}

int i = 1;

try {

while(started) {

Socket ss = s.accept();

ServerReaderWriter c = new ServerReaderWriter(ss);//建立客服端

System.out.println("第" + i + "個(gè)客服端啟動(dòng)!");

++i;

new Thread(c).start();//啟動(dòng)線程

clients.add(c);

}

} catch (EOFException e) {

System.out.println("客服端被關(guān)閉!");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

s.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

class ServerReaderWriter implements Runnable { //建議使用Runnable避免你重寫run方法麻煩!

private Socket s;

private DataInputStream dis = null;//注意賦值,養(yǎng)成好習(xí)慣!

private DataOutputStream dos = null;

private boolean bConnected = false;//用于調(diào)用連接成功后的run方法

public ServerReaderWriter(Socket s) {

this.s = s;

try {

dis = new DataInputStream(s.getInputStream());

dos = new DataOutputStream(s.getOutputStream());

bConnected = true;

} catch (IOException e) {

e.printStackTrace();

}

}

public void send(String str) {

try {

dos.writeUTF(str);

} catch (IOException e) {

clients.remove(this);

System.out.println("有客戶退出!");

}

}

public void run() {

try {

while (bConnected) {

String input = dis.readUTF();

System.out.println(input);

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

ServerReaderWriter c = clients.get(i);

c.send(input);

}

}

} catch(SocketException e) {

System.out.println("一個(gè)客服端已關(guān)閉,請(qǐng)勿再像他發(fā)送信息!");

} catch (EOFException e) {

System.out.println("謝謝使用!");

}catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (dis != null) {

dis.close();

}

if (dos != null) {

dos.close();

}

if (s != null) {

s.close();

s = null;

}

//clients.clear();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

}

Client端代碼:

import java.io.*;

import java.net.*;

import java.awt.*;

import java.awt.event.*;

public class TestClient extends Frame { //用到Frame生產(chǎn)界面比較直觀

Socket s = null;

DataOutputStream dos = null;

DataInputStream dis = null;

private boolean bConnected = false;

TextField tfText = new TextField();

TextArea taContent = new TextArea();

Thread tRecv = new Thread(new ClientReaderWriter());

public static void main(String[] args){

new TestClient().launchFrame();

}

public void launchFrame() {

this.setSize(300, 300); //設(shè)置客服端窗口格式

this.setLocation(400, 300);

add(tfText, BorderLayout.SOUTH);

add(taContent, BorderLayout.NORTH);

this.pack();

this.addWindowListener(new WindowAdapter() { //監(jiān)聽窗口關(guān)閉事件

public void windowClosing(WindowEvent arg0) {

disconnect();

System.exit(0);

}

});

tfText.addActionListener(new TFListener());

setVisible(true);

connect();

tRecv.start();

}

public void connect() {

try {

s = new Socket("127.0.0.1", 5050); //依據(jù)自己的服務(wù)器,我這里用的localhost

dos = new DataOutputStream(s.getOutputStream());

dis = new DataInputStream(s.getInputStream());

System.out.println("連接服務(wù)器!");

bConnected = true;

} catch(ConnectException e) {

System.out.println("請(qǐng)檢查服務(wù)器是否啟動(dòng)!");

try {

Thread.sleep(1000);

} catch (InterruptedException e1) {

e1.printStackTrace();

}

System.exit(0);

}

catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public void disconnect() {

try {

dos.close();

dis.close();

s.close();

} catch (IOException e) {

e.printStackTrace();

}

}

private class TFListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

String str = tfText.getText().trim();

tfText.setText("");

try {

dos.writeUTF(str);

if(str.equals("bye")){

System.exit(0);

}

dos.flush();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

class ClientReaderWriter implements Runnable {

public void run() {

try {

while(bConnected) {

String input = dis.readUTF();

taContent.setText(taContent.getText() + input +'\n');

}

} catch (SocketException e) {

System.out.println("輕輕的我走了!Bye-bye!");

} catch (EOFException e) {

System.out.println("我斷網(wǎng)了,再見!");

}

catch (IOException e) {

e.printStackTrace();

}

}

}

}


新聞標(biāo)題:java人機(jī)會(huì)話代碼 java人機(jī)交互代碼
本文URL:http://weahome.cn/article/hjeioi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部