الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف استخدم call method

بدأه قمر الامارات في 24 أكتوبر 2011 · 2 رد · 615 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

صباح الورد

انا كتبت method اللي تحدد حركة الكمبيوتر ... بس مب عارفه اسوي لهم call في main method

اتمنى حد يوضحهم لي

هذي الmethod :

public int[] nextMove(String player) {
        int counter = 0;
        int pos[] = new int[18];
        for (int i = 0; i < 18; i++) {
            pos = -1;
        }

        /* lucky position in the center of board*/
        if (board[1][1] == " ") {
            return new int[]{1, 1};
        }

        /* choose available move */
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[j] == " ") {
                    pos[counter] = i;
                }
                pos[counter + 1] = j;
                counter = counter + 2;
            }
        }
        /* no move is available */

        return pos;
    }


public String preventAll() {
         String block=preventdiagonalsLeft();

        if (block.equals("")) {
            block=preventdiagonalsRight();
            if (block.equals("")) {
                block=preventhorizontal();
                if (block.equals("")) {
                    block=preventvertical();
                }
            }
        }

        return block;

    }

public String preventdiagonalsLeft() {
        String player = "x";
        boolean found = false;
        int i = 0;
        int j = 0;
        int count = 0;
        String block="";
        while (i < ROWS) {
            if (board[j] == player) {
                count++;
            } else if (board[j] == " ") {
                block = "Block: "+i+" "+j;
            }
            i++;
            j++;
        }
        if (count == 2) {
            found = true;
            return block;
        }

        return "";
    }

    public String preventdiagonalsRight() {
        String player="x";
        boolean found = false;
        int i = 0;
        int j = 2;
        int count = 0;
        String block="";
        while (i < ROWS) {
            if (board[j] == player) {
                count++;
            } else if (board[j] == " ") {
                block = "Block: "+i+" "+j;
            }
            i++;
            j--;
        }
        if (count == 2) {
            found = true;
            return block;
        }

        return "";
    }

    public String preventhorizontal() {
        String player="x";
        boolean found = false;
        int count = 0;
        String block="";
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                if (board[j] == player) {
                    count++;
                } else if (board[j] == " ") {
                    block = "Block: "+i+" "+j;
                }
            }
            if (count == 2) {
                found = true;
                return block;
            } else {
                count = 0;
            }

        }
        return "";
    }

    public String preventvertical() {
        String player="x";
        boolean found = false;
        int count = 0;
        String block="";
        for (int j = 0; j < COLUMNS; j++) {
            for (int i = 0; i < ROWS; i++) {
                if (board[j] == player) {
                    count++;
                } else if (board[j] == " ") {
                    block = "Block: "+i+" "+j;
                }
            }
            if (count == 2) {
                found = true;
                return block;
            } else {
                count = 0;
            }

        }
        return "";
    }

و هذا كود الmain method :

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String player = "x";
        Tictactoe_cpu game = new Tictactoe_cpu();
        boolean done = false;
        System.out.println(game.toString());
        while (!done) {

            int row = 0, column = 0;
            if (player.equals("o")) {

             /// هني لازم اسوي call لل method 


                System.out.println(game.preventAll());
            } else {
            System.out.print("Row for " + player + " (-1 to exit): ");
            row = in.nextInt();
            if (row < 0) {
                done = true;
                break;
            } else {
                System.out.print("Column for " + player + ": ");
                column = in.nextInt();
            }}
            if (game.set(row, column, player).equals(player)) {
                System.out.println(game.toString());

                if (game.checkAll(player)) {
                    System.out.println("Result: The Winner is " + player);
                    break;
                }

                if (player.equals("x")) {
                    player = "o";
                } else {
                    player = "x";
                }
            } else {
                System.out.println("Position selected!");
            }

        }//while
       }
}

تم تعديل هذه المشاركة بواسطة قمر الامارات في 24 أكتوبر 2011 في 11:24

#2

use object to call the method

1
#3

عرفت المشكلة كان المفروض اكتب static في الmethod ... و الحين اشتغلت الحمد الله ... بس طلعت مشكلة يديده :

اذا سويت call ل nextMove("o") ...... تنزل تلقائيا ل "Position selected!" و تكررر الي مالا نهاية !!

import java.util.*;

public class Tictactoe_cpu {

    private static final int ROWS = 3;
    private static final int COLUMNS = 3;
    private static String[][] board;
    //construct an empty board



    public Tictactoe_cpu() {
        board = new String[ROWS][COLUMNS];
        //Fill with spaces
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                board[j] = " ";
            }
        }
    }
    //set the fields in the board

    public String set(int i, int j, String player) {
        if (board[j].equals(" ")) {
            board[j] = player;
            return player;
        } else {
            return board[j];
        }
    }

    public String getLocation(int row, int column) {
	return board[row][column];
    }
//    public int getLocation(int i, int j) {
//	return board[j];
//    }
//    
//    


    public boolean checkAll(String player) {

        if (checkdiagonalsLeft(player)) {
            return true;
        } else if (checkdiagonalsRight(player)) {
            return true;
        } else if (checkcheckhorizontal(player)) {
            return true;
        } else if (checkvertical(player)) {
            return true;

        } else {
            return false;
        }

    }

    public boolean checkdiagonalsLeft(String player) {
        boolean found = false;
        int i = 0;
        int j = 0;
        int count = 0;
        while (i < ROWS) {
            if (board[j] == player) {
                i++;
                j++;
                count++;
            } else {
                break;
            }
        }
        if (count == 3) {
            found = true;
        }


        return found;
    }

    public boolean checkdiagonalsRight(String player) {
        boolean found = false;
        int i = 0;
        int j = 2;
        int count = 0;
        while (i < ROWS) {
            if (board[j] == player) {
                i++;
                j--;
                count++;
            } else {
                break;
            }
        }
        if (count == 3) {
            found = true;
        }


        return found;
    }

    public boolean checkcheckhorizontal(String player) {
        boolean found = false;
        int count = 0;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                if (board[j] == player) {
                    count++;
                } else {
                    break;
                }
            }
            if (count == 3) {
                found = true;
                break;
            } else {
                count = 0;
            }

        }
        return found;
    }

    public boolean checkvertical(String player) {
        boolean found = false;
        int count = 0;
        for (int j = 0; j < COLUMNS; j++) {
            for (int i = 0; i < ROWS; i++) {
                if (board[j] == player) {
                    count++;
                } else {
                    break;
                }
            }
            if (count == 3) {
                found = true;
                break;
            } else {
                count = 0;
            }

        }
        return found;
    }

   /* choose the move that prevent enemy to win */ 
     public static String preventAll() {
         String block=preventdiagonalsLeft();

        if (block.equals("")) {
            block=preventdiagonalsRight();
            if (block.equals("")) {
                block=preventhorizontal();
                if (block.equals("")) {
                    block=preventvertical();
                }
            }
        }

        return block;

    }



      public static int[] nextMove(String player) {
        int counter = 0;
        int pos[] = new int[18];
        for (int i = 0; i < 18; i++) {
            pos = -1;
        }

        /* lucky position in the center of board*/
        if (board[1][1] == " ") {
            return new int[]{1, 1};
        }

        /* choose available move */
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[j] == " ") {
                    pos[counter] = i;
                }
                pos[counter + 1] = j;
                counter = counter + 2;
            }
        }
        /* no move is available */

        return pos;
    }




    public static String preventdiagonalsLeft() {
        String player = "x";
        boolean found = false;
        int i = 0;
        int j = 0;
        int count = 0;
        String block="";
        while (i < ROWS) {
            if (board[j] == player) {
                count++;
            } else if (board[j] == " ") {
                block = "Block: "+i+" "+j;
            }
            i++;
            j++;
        }
        if (count == 2) {
            found = true;
            return block;
        }

        return "";
    }

    public  static String preventdiagonalsRight() {
        String player="x";
        boolean found = false;
        int i = 0;
        int j = 2;
        int count = 0;
        String block="";
        while (i < ROWS) {
            if (board[j] == player) {
                count++;
            } else if (board[j] == " ") {
                block = "Block: "+i+" "+j;
            }
            i++;
            j--;
        }
        if (count == 2) {
            found = true;
            return block;
        }

        return "";
    }

    public static String preventhorizontal() {
        String player="x";
        boolean found = false;
        int count = 0;
        String block="";
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                if (board[j] == player) {
                    count++;
                } else if (board[j] == " ") {
                    block = "Block: "+i+" "+j;
                }
            }
            if (count == 2) {
                found = true;
                return block;
            } else {
                count = 0;
            }

        }
        return "";
    }

    public  static String preventvertical() {
        String player="x";
        boolean found = false;
        int count = 0;
        String block="";
        for (int j = 0; j < COLUMNS; j++) {
            for (int i = 0; i < ROWS; i++) {
                if (board[j] == player) {
                    count++;
                } else if (board[j] == " ") {
                    block = "Block: "+i+" "+j;
                }
            }
            if (count == 2) {
                found = true;
                return block;
            } else {
                count = 0;
            }

        }
        return "";
    }


    /**creates a string representation of the board
     * @return the string representation
     */
    public String toString() {
        String r = "";
        for (int i = 0; i < ROWS; i++) {
            r += "|";
            for (int j = 0; j < COLUMNS; j++) {
                r += board[j] + "|";
            }
            r += "\n";
        }
        return r;
    }



    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String player = "x";
        Tictactoe_cpu game = new Tictactoe_cpu();
        boolean done = false;
        System.out.println(game.toString());
        while (!done) {

            int row = 0, column = 0;
            if (player.equals("o")) {
                nextMove("o");
                preventAll();    
           System.out.println(game.preventAll());

            } else {
            System.out.print("Row for " + player + " (-1 to exit): ");
            row = in.nextInt();
            if (row < 0) {
                done = true;
                break;
            } else {
                System.out.print("Column for " + player + ": ");
                column = in.nextInt();
            }}
            if (game.set(row, column, player).equals(player)) {
                System.out.println(game.toString());

                if (game.checkAll(player)) {
                    System.out.println("Result: The Winner is " + player);
                    break;
                }

                if (player.equals("x")) {
                    player = "o";
                } else {
                    player = "x";
                }
            } else {
                System.out.println("Position selected!");
            }

        }//while
       }
}

اي حد عنده فكرة ؟؟

مواضيع مشابهة