五子棋代碼java教程 簡單五子棋編程java

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

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

創(chuàng)新互聯(lián)建站是一家專業(yè)提供長清企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計制作、成都網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為長清眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計公司優(yōu)惠進(jìn)行中。

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坐標(biāo)

int ylabel;// 記錄第一次找到某個棋子的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("請"+roles+"輸入X,Y坐標(biāo),必須在0-15范圍內(nèi),xy以空格隔開,輸入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("輸入錯誤,請從新輸入");

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怎么編寫??

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

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

chessClient.java:客戶端主程序。

chessInterface.java:客戶端的界面。

chessPad.java:棋盤的繪制。

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

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

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

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

1.chessClient.java

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

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java點虐 .*;

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

}

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點虐 .*;

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("關(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點虐 .*;

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點虐 ChessPaint(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("請先連接服務(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:無法連接 \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五子棋源代碼

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

import java.io.BufferedReader;

import java.io.InputStreamReader;

/*

* 五子棋源碼

* 所用的符號標(biāo)識 ○ ● ┼

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

* 黑白雙方交叉輸入落子點坐標(biāo) 以逗號隔開如 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ù) 分別對應(yīng)x y坐標(biāo)

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

}

}

}

// 描繪出當(dāng)前棋盤

public void paintBoard() {

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

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();

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

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ù)器到達(dá)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;

}

}

}

// 掃描斜右下

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

// 坐標(biāo)x-y的范圍在-SIZE+1到SIZE-1之間

// 當(dāng)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;

}

}

}

}

}

// 掃描斜左邊上

// 方法同上 坐標(biāo)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代碼旁邊就注釋 讓我看的清楚明白一些,要詳細(xì)的解釋和思路

import java.applet.*;

import t.*;

import t.event.*;

import java.applet.Applet;

import t.Color; //這一段import就不說了,下面要用到的就import進(jìn)來

public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener

//繼承Applet表明是個applet,后面的接口必須要實現(xiàn)每個接口的所有方法

{

int color_Qizi=0;//旗子的顏色標(biāo)識 0:白子 1:黑子

int intGame_Start=0;//游戲開始標(biāo)志 0未開始 1游戲中

int intGame_Body[][]=new int[16][16]; //設(shè)置棋盤棋子狀態(tài) 0 無子 1 白子 2 黑子

網(wǎng)站標(biāo)題:五子棋代碼java教程 簡單五子棋編程java
當(dāng)前路徑:http://bm7419.com/article30/ddehoso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、自適應(yīng)網(wǎng)站、商城網(wǎng)站、移動網(wǎng)站建設(shè)、App開發(fā)微信小程序

廣告

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

搜索引擎優(yōu)化