<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • 單機五子棋java代碼 java五子棋

            java五子棋源代碼

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

            專注于為中小企業(yè)提供成都網(wǎng)站建設、網(wǎng)站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)唐河免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

            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;

            }

            }

            }

            // 掃描斜右下

            // 在橫向掃描基礎(chǔ)上 外層套一個循環(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五子棋代碼!! 網(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結(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語言編寫五子棋

            import java.awt.*;

            import java.awt.event.*;

            import javax.swing.*;

            class mypanel extends Panel implements MouseListener

            {

            int chess[][] = new int[11][11];

            boolean Is_Black_True;

            mypanel()

            {

            Is_Black_True = true;

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

            {

            for(int j = 0;j 11;j++)

            {

            chess[i][j] = 0;

            }

            }

            addMouseListener(this);

            setBackground(Color.BLUE);

            setBounds(0, 0, 360, 360);

            setVisible(true);

            }

            public void mousePressed(MouseEvent e)

            {

            int x = e.getX();

            int y = e.getY();

            if(x 25 || x 330 + 25 ||y 25 || y 330+25)

            {

            return;

            }

            if(chess[x/30-1][y/30-1] != 0)

            {

            return;

            }

            if(Is_Black_True == true)

            {

            chess[x/30-1][y/30-1] = 1;

            Is_Black_True = false;

            repaint();

            Justisewiner();

            return;

            }

            if(Is_Black_True == false)

            {

            chess[x/30-1][y/30-1] = 2;

            Is_Black_True = true;

            repaint();

            Justisewiner();

            return;

            }

            }

            void Drawline(Graphics g)

            {

            for(int i = 30;i = 330;i += 30)

            {

            for(int j = 30;j = 330; j+= 30)

            {

            g.setColor(Color.WHITE);

            g.drawLine(i, j, i, 330);

            }

            }

            for(int j = 30;j = 330;j += 30)

            {

            g.setColor(Color.WHITE);

            g.drawLine(30, j, 330, j);

            }

            }

            void Drawchess(Graphics g)

            {

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

            {

            for(int j = 0;j 11;j++)

            {

            if(chess[i][j] == 1)

            {

            g.setColor(Color.BLACK);

            g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);

            }

            if(chess[i][j] == 2)

            {

            g.setColor(Color.WHITE);

            g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);

            }

            }

            }

            }

            void Justisewiner()

            {

            int black_count = 0;

            int white_count = 0;

            int i = 0;

            for(i = 0;i 11;i++)//橫向判斷

            {

            for(int j = 0;j 11;j++)

            {

            if(chess[i][j] == 1)

            {

            black_count++;

            if(black_count == 5)

            {

            JOptionPane.showMessageDialog(this, "黑棋勝利");

            Clear_Chess();

            return;

            }

            }

            else

            {

            black_count = 0;

            }

            if(chess[i][j] == 2)

            {

            white_count++;

            if(white_count == 5)

            {

            JOptionPane.showMessageDialog(this, "白棋勝利");

            Clear_Chess();

            return;

            }

            }

            else

            {

            white_count = 0;

            }

            }

            }

            for(i = 0;i 11;i++)//豎向判斷

            {

            for(int j = 0;j 11;j++)

            {

            if(chess[j][i] == 1)

            {

            black_count++;

            if(black_count == 5)

            {

            JOptionPane.showMessageDialog(this, "黑棋勝利");

            Clear_Chess();

            return;

            }

            }

            else

            {

            black_count = 0;

            }

            if(chess[j][i] == 2)

            {

            white_count++;

            if(white_count == 5)

            {

            JOptionPane.showMessageDialog(this, "白棋勝利");

            Clear_Chess();

            return;

            }

            }

            else

            {

            white_count = 0;

            }

            }

            }

            for(i = 0;i 7;i++)//左向右斜判斷

            {

            for(int j = 0;j 7;j++)

            {

            for(int k = 0;k 5;k++)

            {

            if(chess[i + k][j + k] == 1)

            {

            black_count++;

            if(black_count == 5)

            {

            JOptionPane.showMessageDialog(this, "黑棋勝利");

            Clear_Chess();

            return;

            }

            }

            else

            {

            black_count = 0;

            }

            if(chess[i + k][j + k] == 2)

            {

            white_count++;

            if(white_count == 5)

            {

            JOptionPane.showMessageDialog(this, "白棋勝利");

            Clear_Chess();

            return;

            }

            }

            else

            {

            white_count = 0;

            }

            }

            }

            }

            for(i = 4;i 11;i++)//右向左斜判斷

            {

            for(int j = 6;j = 0;j--)

            {

            for(int k = 0;k 5;k++)

            {

            if(chess[i - k][j + k] == 1)

            {

            black_count++;

            if(black_count == 5)

            {

            JOptionPane.showMessageDialog(this, "黑棋勝利");

            Clear_Chess();

            return;

            }

            }

            else

            {

            black_count = 0;

            }

            if(chess[i - k][j + k] == 2)

            {

            white_count++;

            if(white_count == 5)

            {

            JOptionPane.showMessageDialog(this, "白棋勝利");

            Clear_Chess();

            return;

            }

            }

            else

            {

            white_count = 0;

            }

            }

            }

            }

            }

            void Clear_Chess()

            {

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

            {

            for(int j=0;j11;j++)

            {

            chess[i][j]=0;

            }

            }

            repaint();

            }

            public void paint(Graphics g)

            {

            Drawline(g);

            Drawchess(g);

            }

            public void mouseExited(MouseEvent e){}

            public void mouseEntered(MouseEvent e){}

            public void mouseReleased(MouseEvent e){}

            public void mouseClicked(MouseEvent e){}

            }

            class myframe extends Frame implements WindowListener

            {

            mypanel panel;

            myframe()

            {

            setLayout(null);

            panel = new mypanel();

            add(panel);

            panel.setBounds(0,23, 360, 360);

            setTitle("單人版五子棋");

            setBounds(200, 200, 360, 383);

            setVisible(true);

            addWindowListener(this);

            }

            public void windowClosing(WindowEvent e)

            {

            System.exit(0);

            }

            public void windowDeactivated(WindowEvent e){}

            public void windowActivated(WindowEvent e){}

            public void windowOpened(WindowEvent e){}

            public void windowClosed(WindowEvent e){}

            public void windowIconified(WindowEvent e){}

            public void windowDeiconified(WindowEvent e){}

            }

            public class mywindow

            {

            public static void main(String argc [])

            {

            myframe f = new myframe();

            }

            }

            分享標題:單機五子棋java代碼 java五子棋
            標題來源:http://www.jbt999.com/article22/doosdcc.html

            成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設網(wǎng)站導航建站公司定制網(wǎng)站云服務器網(wǎng)站收錄

            廣告

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

            成都定制網(wǎng)站網(wǎng)頁設計

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 天天操夜夜添 | 大香蕉操操网 | 久伊人狠狠干 | 婷婷五月丁香在线 | 手机看片一区二区三区 |