java五子棋人機的代碼 java五子棋人機的代碼是多少

java五子棋人機對戰(zhàn)的一段代碼幫我具體分析下是怎么運算的!

shape是三維數(shù)組,前兩維是位置,第三維開始,0-3放著4個方向的連著的同顏色子的數(shù)目(個人估計應該排序過),4放著評估值

成都創(chuàng)新互聯(lián)網(wǎng)站建設由有經(jīng)驗的網(wǎng)站設計師、開發(fā)人員和項目經(jīng)理組成的專業(yè)建站團隊,負責網(wǎng)站視覺設計、用戶體驗優(yōu)化、交互設計和前端開發(fā)等方面的工作,以確保網(wǎng)站外觀精美、網(wǎng)站設計制作、成都網(wǎng)站建設易于使用并且具有良好的響應性。

下面的一堆循環(huán)是這樣的:

如果已經(jīng)有5個連一起,評估值為最高(200),跳出

如果是4個,則看下一個連著的棋子數(shù),4個150分,3個100分,其他50分

如果是3個,則看下一個連著的棋子數(shù),3個75分,其他20分

如果是2個,10分

如果是1個,0分

最后的循環(huán)是找出評估值最高的位置

max_x,max_y放這個位置,max是放評估值

個人認為這樣的評估算法下電腦AI不會很高

因為情況分太粗

java五子棋源代碼

我這有算法 不過沒做swing界面 DOS下可以直接運行 要不你拿去改改

import java.io.BufferedReader;

import java.io.InputStreamReader;

/*

* 五子棋源碼

* 所用的符號標識 ○ ● ┼

* 在dos界面下運行效果最佳

* 黑白雙方交叉輸入落子點坐標 以逗號隔開如 1,1

* 輸入空 或者一方勝出 程序停止

*/

public class Chess {

// 定義棋盤大小

private static int SIZE = 15;

private String[][] board;

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

Chess chess = new Chess();

// 初始化棋盤

chess.initBoard();

// 畫出棋盤

chess.paintBoard();

// 根據(jù)who的奇偶性 判斷該誰落子

int who = 0;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String str = null;

while ((str = br.readLine()) != null) {

// 提取輸入的 以","分開的數(shù) 分別對應x y坐標

String[] posStr = str.split(",");

int x = Integer.parseInt(posStr[0]);

int y = Integer.parseInt(posStr[1]);

// 判斷落子點是否合法

if (!"┼".equals(chess.board[x][y])) {

System.out.println("這里不允許落子,請重下..");

continue;

}

if (who % 2 == 0) {

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

chess.paintBoard();

// 判斷是否勝出

if (chess.isWin("○")) {

System.out.println("○獲勝");

return;

}

} else {

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

chess.paintBoard();

// 判斷是否勝出

if (chess.isWin("●")) {

System.out.println("●獲勝");

return;

}

}

who++;

}

}

// 以 "┼" 初始化棋盤

public void initBoard() {

board = new String[SIZE][SIZE];

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

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

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

}

}

}

// 描繪出當前棋盤

public void paintBoard() {

// 以下代碼 這里為了使得棋盤坐標看的清楚 加入了坐標值

System.out.print(" ");

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

if (i 10) {

System.out.print(i + " ");

} else {

System.out.print((i - 10) + " ");

}

}

System.out.println();

// 以上代碼 這里為了使得棋盤坐標看的清楚 加入了坐標值

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

if (i 10) {

System.out.print(" " + i);

} else {

System.out.print(i);

}

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

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

}

System.out.println();

}

}

// 判斷是否獲勝

public boolean isWin(String sign) {

int count = 0;

// 橫向掃描各行

// 有一個sign的子 計數(shù)器+1

// 碰到不是sign的子 計數(shù)器置零

// 計數(shù)器到達5時 返回true 勝出

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

count = 0;

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

if (board[i][j].equals(sign)) {

count++;

if (count == 5) {

return true;

}

} else {

count = 0;

}

}

}

// 縱向掃描各列

// 方法同上

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

count = 0;

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

if (board[j][i].equals(sign)) {

count++;

if (count == 5) {

return true;

}

} else {

count = 0;

}

}

}

// 掃描斜右下

// 在橫向掃描基礎上 外層套一個循環(huán) 以k為標識

// 坐標x-y的范圍在-SIZE+1到SIZE-1之間

// 當x-y的值相等時 在同一右下斜線上

for (int k = -SIZE + 1; k = SIZE - 1; k++) {

count = 0;

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

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

if (i - j == k) {

if (board[i][j].equals(sign)) {

count++;

if (count == 5) {

return true;

}

} else {

count = 0;

}

}

}

}

}

// 掃描斜左邊上

// 方法同上 坐標x+y的值相等時 在同一左上斜線上

for (int k = -SIZE + 1; k = SIZE - 1; k++) {

count = 0;

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

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

if (i + j == k) {

if (board[i][j].equals(sign)) {

count++;

if (count == 5) {

return true;

}

} else {

count = 0;

}

}

}

}

}

return false;

}

}

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

java網(wǎng)絡五子棋

下面的源代碼分為4個文件;

chessClient.java:客戶端主程序。

chessInterface.java:客戶端的界面。

chessPad.java:棋盤的繪制。

chessServer.java:服務器端。

可同時容納50個人同時在線下棋,聊天。

沒有加上詳細注釋,不過絕對可以運行,j2sdk1.4下通過。

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

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無法關閉");

}

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("請黑棋下子");

}

else if(chessclient.isClient)

{

chessclient.chesspad.chessColor=-1;

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

}

}

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

{

chessclient.isOnChess=false;

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

chessclient.chesspad.statusText.setText("對方退出,請點放棄游戲退出連接");

chessclient.chesspad.isMouseEnabled=false;

}

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

{

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

}

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

{

chessclient.chatpad.chatLineArea.append("傳輸錯誤:請退出程序,重新加入 \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; //如果是下棋的主機

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:無法連接,建議重新啟動程序 \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("連接成功,請創(chuàng)建游戲或加入游戲");

}

}

catch(Exception ei)

{

chatpad.chatLineArea.setText("controlpad.connectButton:無法連接,建議重新啟動程序 \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("必須先選定一個有效用戶");

}

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無法連接 \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無法連接 \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("請建立游戲或者加入游戲");

}

if(!isOnChess)

{

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

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

}

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無法連接,建議重新連接 \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無法連接,建議重新連接 \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("連接主機");

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

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

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

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

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("錯誤:沒有這個用戶,請退出程序,重新加入");

}

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("請先連接服務器");

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:無法連接 \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 五子棋 人機

public void pcPK(){

if (type) {

if (jLabels[i][j].getIcon() == null) {

jLabels[i][j].setIcon(new ImageIcon("src/imagess/hei.gif"));

type = false;

initializationSum();

success(i, j);

} else {

JOptionPane.showMessageDialog(null, "已有棋子");

}

}

while (!type) {

int k = new Random().nextInt(8);

if (k == 1) {

if (jLabels[this.i + 1][this.j].getIcon() == null) {

jLabels[this.i + 1][this.j].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i + 1][this.j]);

success(this.i + 1, j);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 2) {

if (jLabels[this.i - 1][j].getIcon() == null) {

jLabels[this.i - 1][j].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i - 1][j]);

success(this.i - 1, j);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 3) {

if (jLabels[i][j + 1].getIcon() == null) {

jLabels[this.i][j + 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[i][j + 1]);

success(this.i, j + 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 4) {

if (jLabels[this.i][j - 1].getIcon() == null) {

jLabels[this.i][j - 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i][j - 1]);

success(this.i, j - 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 5) {

if (jLabels[this.i + 1][j + 1].getIcon() == null) {

jLabels[this.i + 1][j + 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[i + 1][j + 1]);

success(this.i + 1, j + 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 6) {

if (jLabels[this.i - 1][j - 1].getIcon() == null) {

jLabels[this.i - 1][j - 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i - 1][j - 1]);

success(this.i - 1, j - 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 7) {

if (jLabels[this.i + 1][j - 1].getIcon() == null) {

jLabels[this.i + 1][j - 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[i + 1][j - 1]);

success(this.i + 1, j - 1);

type = true;

break;

} else {

type = false;

continue;

}

} else if (k == 0) {

if (jLabels[this.i - 1][j + 1].getIcon() == null) {

jLabels[this.i - 1][j + 1].setIcon(new ImageIcon(

"src/imagess/bai.gif"));

arrayList.add(jLabels[this.i - 1][j + 1]);

success(this.i - 1, j + 1);

type = true;

break;

} else {

type = false;

continue;

}

}

}

}

JAVA五子棋代碼

import java.util.Arrays;

public class GoBangGame {

public static final char BLANK='*';

public static final char BLACK='@';

public static final char WHITE='O';

public static final int MAX = 16;

private static final int COUNT = 5;

//棋盤

private char[][] board;

public GoBangGame() {

}

//開始游戲

public void start() {

board = new char[MAX][MAX];

//把二維數(shù)組都填充‘*’

for(char[] ary: board){

Arrays.fill(ary, BLANK);

}

}

public char[][] getChessBoard(){

return board;

}

public void addBlack(int x, int y) throws ChessExistException{

//@

//char blank = '*';

//System.out.println( x +"," + y + ":" + board[y][x] + "," + BLANK);

if(board[y][x] == BLANK){// x, y 位置上必須是空的才可以添棋子

board[y][x] = BLACK;

return;

}

throw new ChessExistException("已經(jīng)有棋子了!");

}

public void addWhite(int x, int y)

throws ChessExistException{

if(board[y][x] == BLANK){// x, y 位置上必須是空的才可以添棋子

board[y][x] = WHITE;

return;

}

throw new ChessExistException("已經(jīng)有棋子了!");

}

//chess 棋子:'@'/'O'

public boolean winOnY(char chess, int x, int y){

//先找到y(tǒng)方向第一個不是 blank的棋子

int top = y;

while(true){

if(y==0 || board[y-1][x]!=chess){

//如果y已經(jīng)是棋盤的邊緣, 或者的前一個不是chess

//就不再繼續(xù)查找了

break;

}

y--;

top = y;

}

//向回統(tǒng)計所有chess的個數(shù),如果是COUNT個就贏了

int count = 0;

y = top;

while(true){

if(y==MAX || board[y][x]!=chess){

//如果找到頭 或者 下一個子不是chess 就不再繼續(xù)統(tǒng)計了

break;

}

count++;

y++;

}

return count==COUNT;

}

//chess 棋子:'@'/'O'

public boolean winOnX(char chess, int x, int y){

//先找到x方向第一個不是 blank的棋子

int top = x;

while(true){

if(x==0 || board[y][x-1]!=chess){

//如果x已經(jīng)是棋盤的邊緣, 或者的前一個不是chess

//就不再繼續(xù)查找了

break;

}

x--;

top = x;

}

//向回統(tǒng)計所有chess的個數(shù),如果是COUNT個就贏了

int count = 0;

x = top;

while(true){

if(x==MAX || board[y][x]!=chess){

//如果找到頭 或者 下一個子不是chess 就不再繼續(xù)統(tǒng)計了

break;

}

count++;

x++;

}

return count==COUNT;

}

//chess 棋子:'@'/'O'

public boolean winOnXY(char chess, int x, int y){

//先找MAX向第一個不是 blank的棋子

int top = y;

int left = x;

while(true){

if(x==0 || y==0 || board[y-1][x-1]!=chess){

//如果x已經(jīng)是棋盤的邊緣, 或者的前一個不是chess

//就不再繼續(xù)查找了

break;

}

x--;

y--;

top = y;

left=x;

}

//向回統(tǒng)計所有chess的個數(shù),如果是COUNT個就贏了

int count = 0;

x = left;

y = top;

while(true){

if(x==MAX || y==MAX || board[y][x]!=chess){

//如果找到頭 或者 下一個子不是chess 就不再繼續(xù)統(tǒng)計了

break;

}

count++;

x++;

y++;

}

return count==COUNT;

}

//chess 棋子:'@'/'O'

public boolean winOnYX(char chess, int x, int y){

//先找到x方向第一個不是 blank的棋子

int top = y;

int left = x;

while(true){

if(x==MAX-1 || y==0 || board[y-1][x+1]!=chess){

//如果x已經(jīng)是棋盤的邊緣, 或者的前一個不是chess

//就不再繼續(xù)查找了

break;

}

x++;

y--;

top = y;

left=x;

}

//向回統(tǒng)計所有chess的個數(shù),如果是COUNT個就贏了

int count = 0;

x = left;

y = top;

while(true){

if(x==0 || y==MAX || board[y][x]!=chess){

//如果找到頭 或者 下一個子不是chess 就不再繼續(xù)統(tǒng)計了

break;

}

count++;

x--;

y++;

}

return count==COUNT;

}

public boolean whiteIsWin(int x, int y) {

//在任何一個方向上贏了,都算贏

return winOnY(WHITE, x, y) ||

winOnX(WHITE, x, y) ||

winOnXY(WHITE, x, y) ||

winOnYX(WHITE, x, y);

}

public boolean blackIsWin(int x, int y) {

return winOnY(BLACK, x, y) ||

winOnX(BLACK, x, y) ||

winOnXY(BLACK, x, y) ||

winOnYX(BLACK, x, y);

}

}

自己看看這個行不行,應該能滿足你的要求了

求一個簡單的JAVA五子棋代碼??! 網(wǎng)上復制的別來了!

以下是現(xiàn)寫的 實現(xiàn)了兩人對戰(zhàn) 自己復制后運行把 沒什么難度 類名 Games

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] = "╋";

// }

}

}

}

//輸出棋盤

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("輸入錯誤,請從新輸入");

isRight = false;

}

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

isRight = false;

}

return isRight;

}

//判斷誰贏了

public void whoWin(Games wz) {

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

int xlabel;// 記錄第一次找到某個棋子的x坐標

int ylabel;// 記錄第一次找到某個棋子的y坐標

// ●○╋

// 判斷人是否贏了

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

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

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

xlabel = i;

ylabel = j;

// 橫向找 x坐標不變 y坐標以此加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坐標不變 y坐標以此加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("請"+roles+"輸入X,Y坐標,必須在0-15范圍內(nèi),xy以空格隔開,輸入16 16結束程序");

int x = sc.nextInt();

int y = sc.nextInt();

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

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

System.exit(0);

}

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

System.out.println("輸入錯誤,請從新輸入");

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玩家";

}

}

}

}

網(wǎng)頁名稱:java五子棋人機的代碼 java五子棋人機的代碼是多少
文章分享:http://muchs.cn/article30/dossiso.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站網(wǎng)站設計公司、手機網(wǎng)站建設、虛擬主機、網(wǎng)站建設、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名