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

مساعدة في لعبة X O

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

السلام عليكم و رحمه الله

عندي سوال في لعبة X O .... انا كتبت methods اللي تحدد الفايز ,,, يعني checkdiagonal و checkcheckhorizontal و checkvertical اذا اللاعب الاول كمل الخط المفروض اللعبة تلقائيا توقف و تتطلع نتيجة انه اللاعب الاول فاز

المشكلة ماشي خطأ واضح ... بس مع ذلك الmethods ما تشتغل يعني لو اللاعب الاول فاز اللعبة ماتوقف !!

و هذا الكود و انا استخدم برنامج ال NetBeans:

package Tictactoe;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author ituser
 */
import java.util.*;

public class Tictactoe {

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

    public Tictactoe() {
        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 void set(int i, int j, String player) {
        if (board[j].equals(" ")) {
            board[j] = player;
        }
    }
//set the fields in the board

    public void check(String player) {
        boolean found = false;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                if (board[j] == player) {
                    if (j == COLUMNS - 1) {
                        found = true;
                    }
                    continue;
                } else {
                    break;
                }
            }
        }
    }

    public boolean checkAll(String player) {

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

        }else {
            return false;
        }

    }

    public  boolean checkdiagonalsLeft(String player) {
        boolean found = false;
        int count = 0;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                if (board == player) {
                    count = i;
                    continue;

                } else {
                    break;
                }
            }
            if (count == ROWS - 1) {
                found = true;
           }

    }
         return found;
    }


     public  boolean checkdiagonalsRight(String player) {
        boolean found = false;
        int count = 0;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++) {
                if (board[2-i] == player) {
                    count = i;
                    continue;

                } else {
                    break;
                }
            }
            if (count == ROWS - 1) {
                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 = i;
                    continue;

                } else {
                    break;
                }
            }
            if (count == ROWS - 1) {
                found = true;
           }

    }
         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 = i;
                    continue;

                } else {
                    break;
                }
            }
            if (count == ROWS - 1) {
                found = true;
           }

    }
         return found;
    }




    /**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) {
        int counter = 0;
        Scanner in = new Scanner(System.in);
        String player = "x";
        Tictactoe game = new Tictactoe();
        boolean done = false;
        while (!done) {
            System.out.println(game.toString());
            System.out.print("Row for " + player + " (-1 to exit): ");
            int row = in.nextInt();
            if (row < 0) {
                done = true;
            } else {
                System.out.print("Column for " + player + ": ");
                int column = in.nextInt();
                game.set(row, column, player);
                counter++;
                game.check(player);
                if (player.equals("x")) {
                    player = "o";
                } else {
                    player = "x";
                }
            }

            if (game.checkAll(player)) {
                break;

            }



        }//while

        System.out.println("Result: ");
    }

}

أتمنى حد يقدر يساعدني و يحددلي الخطأ وين بالضبط

#2

هل الدالة لا تعمل في كل الحالات أم في حالة بعينها؟

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3

السلام عليكم ..

الــ methods التي تبدا بــ check فيها خطأ منطقي ... أي لا تقوم بالعمل بشكل صحيح : و هذا الشكل الصحيح لإحداها :

	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 = j;
                	if (count == COLUMNS - 1)
                	{
                    	                    	return true;
                	}

            	} else
            	{
                	break;
            	}
        	}

    	}
    	return found;
	}

قارن مع الشئ الذي كتبته أنت و ستجد الفرق ..

ثانيا أنت تقوم بتبديل الاعبين ثم تفحص هل انتهت اللعبة و هذا أيضا خطأ ... و بالتالي الكود الصحيح :

public static void main(String[] args)
	{
    	int counter = 0;
    	Scanner in = new Scanner(System.in);
    	String player = "x";
    	Tictactoe game = new Tictactoe();
    	game.board[0][0] = "x";
    	game.board[0][1] = "x";
    	game.board[2][0] = "o";
    	game.board[2][1] = "o";

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

            	}
            	if (player.equals("x"))
            	{
                	player = "o";
            	} else
            	{
                	player = "x";
            	}
        	}


    	}//while

    	System.out.println("Result: ");
	}

لاحظ اني استخدمت :

  game.board[0][0] = "x";

من أجل سرعة التنفيذ ..

ثالثا استخدم الــ debug الموجود في الــ Netbeans ...

1

لا إله إلا الله ... محمد رسول الله

لو كانت مشاركتي مفيدة و تريد تشجيعي على المزيد من العطاء , فضلا قم بتقييم المشاركة

المعرًف القديم : houssam11350_11350

من مواضيعي : ArabGenCode : مولد كود و إجراءات مخزنة و واجهات لجداول سيكوال سيرفر

#4

الغي اول سطر من الكود وإن شاء الله يشتغل معاك

امسح

package Tictactoe;
#5

اذا ما اشتغل معاك هذا الكود اخذ هذا شغال 100%

من برمجتي

/**
This Program is Done By Mohammad Riyad Alzaatary GUST ID: 3334
my email is mohmd.ryd@gmail.com and gust0003334@gust.edu.kw
student at Gulf University For Sciense And Technology
Instructer: Ms. Shiny Moncy
Assignment 1 Object Oriented Course

Its a game Tic Tac Toea Player1 Will Play First with 'X' Character
and Player2 Play second with 'O' character

each one of them will enter the number of the index in the cell
When one player has placed three tokens in a horizontal, vertical or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win.


 */
import java.util.*; 
public class Tic
{
	private char[][] board = new char [3][3]; // intialize the board with 3 X 3 size
	Scanner input = new Scanner(System.in); // create input object from scanner class to get input from the user	
	public Tic() // Constructer
	{
		char counter = '1'; // intialize the board with it
		for (int i = 0; i < 3; i++)
            	{
               		for (int j = 0; j < 3; j++){board[j] = counter++ ;} // put the numbers in the board
           	}


        }

	public void print() // print the board
        {  
            for ( int i = 0; i < 3; i++)
            {
                System.out.println("  ");   
                for( int j = 0; j < 3; j++)
                {
                    System.out.print("["+board[j] + "] "); // print each element with [ ]
                }
                System.out.println("\n------"); // seperate ROWS
          }
	}

	void play()
	{
		String c1;char index;

		while(true)
		{	
			print();//print board
			System.out.print("Player 1 You Are \'X\':\t");
			c1 = input.nextLine();//take input string from user
			index = c1.charAt(0);//convert String to char

			// enter to the board the first Character
			while(isChecked(index))	// check if already contain 'X' or 'O'
			{
				System.out.print("Player 1 You Are \'X\':");
				System.out.print("WRONG Input Enter Again:\t");
				c1 = input.nextLine();//take input string from user
				index = c1.charAt(0);//convert String to char	
			}
			inUser1(index); // put Char 'X' in the sepcified index
			if(isDraw()){
					System.out.println(((char) 27)+"[2J");//clear screen 
					print();
					System.out.println("ITS A DRAW TRY AGAIN BOTH OF YOU");
					break;}// Check if There is A Draw




			System.out.println(((char) 27)+"[2J");//clear screen 

			// check if any palayer Win
			if(isWin()){
				print();
				System.out.println("****************\nCONGRATULATION PLAYER 1 WON\n****************"); //prtint if player 1 WON
				break;
			}


			print();//print board

			System.out.print("Player 2 You Are \'O\':\t");
			c1 = input.nextLine();//take input string from user
			index = c1.charAt(0); //convert String to char

			// enter to the board the first Character
			while(isChecked(index))// check if already contain 'X' or 'O'
			{
				System.out.print("Player 2 You Are \'X\':");
				System.out.print("WRONG Input Enter Again:\t");
				c1 = input.nextLine();//take input string from user
				index = c1.charAt(0);//convert String to char	
			}
			inUser2(index);// put Char 'O' in the sepcified index
			if(isDraw())
			{
				System.out.println(((char) 27)+"[2J");
				print();System.out.println("ITS A DRAW TRY AGAIN BOTH OF YOU");break;}// Check if There is A Draw


			System.out.println(((char) 27)+"[2J");//clear screen 

			// check if any palayer Win
			if(isWin()){
				print();
				System.out.println("****************\nCONGRATULATION PLAYER 2 WON\n****************");//print if player 2 WON
				break;
			}




		}




	}

	boolean isDraw()
	{
		int counter=0;
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
			{
				if(board[j]=='X' || board[j]=='O')counter++;
			}
		}
		if(counter==9){return true;}
		else {return false;}
	}

	void inUser1(char index) // input for player 1 with 'X'
	{	
		switch(index)
		{
			case '1':board[0][0]='X';break;
			case '2':board[0][1]='X';break;
			case '3':board[0][2]='X';break;
			case '4':board[1][0]='X';break;
			case '5':board[1][1]='X';break;
			case '6':board[1][2]='X';break;
			case '7':board[2][0]='X';break;
			case '8':board[2][1]='X';break;
			case '9':board[2][2]='X';break;
		}

	}

	void inUser2(char index)	// input for player 2 with 'O'
	{	
		switch(index)
		{
			case '1':board[0][0]='O';break;
			case '2':board[0][1]='O';break;
			case '3':board[0][2]='O';break;
			case '4':board[1][0]='O';break;
			case '5':board[1][1]='O';break;
			case '6':board[1][2]='O';break;
			case '7':board[2][0]='O';break;
			case '8':board[2][1]='O';break;
			case '9':board[2][2]='O';break;
		}

	}

	boolean isWin()
	{
		if(board[0][1]==board[0][2] && board[0][1]==board[0][0]){return true;} //check first row
		else if(board[1][0]==board[1][1] && board[1][0]==board[1][2]){return true;}//check second row
		else if(board[2][0]==board[2][1] && board[2][0]==board[2][2]){return true;}//check third row
		else if(board[0][0]==board[1][0] && board[0][0]==board[2][0]){return true;}//check first colum
		else if(board[0][1]==board[1][1] && board[0][1]==board[2][1]){return true;}//check second colum
		else if(board[0][2]==board[1][2] && board[0][2]==board[2][2]){return true;}//check third colum
		else if(board[0][0]==board[1][1] && board[0][0]==board[2][2]){return true;}//check diagonal
		else if(board[0][2]==board[1][1] && board[0][2]==board[2][0]){return true;}//check subdiagonal
		else return false;
	}
	boolean isChecked(char index) // this method check whether the index already contain 'X' or 'O' character
	{

			boolean Flag=false;
			switch(index)
		{
			case '1':
				if(board[0][0] == 'X' || board[0][0] == 'O') Flag=true;
			break;
			case '2':
				if(board[0][1] == 'X' || board[0][1] == 'O') Flag=true;
			break;
			case '3':
				if(board[0][2] == 'X' || board[0][2] == 'O') Flag=true;
			break;
			case '4':
				if(board[1][0] == 'X' || board[1][0] == 'O') Flag=true;
			break;
			case '5':
				if(board[1][1] == 'X' || board[1][1] == 'O') Flag=true;

			break;
			case '6':
				if(board[1][2] == 'X' || board[1][2] == 'O') Flag=true;
			break;
			case '7':
				if(board[2][0] == 'X' || board[2][0] == 'O') Flag=true;
			break;
			case '8':
				if(board[2][1] == 'X' || board[2][1] == 'O') Flag=true;
			break;
			case '9':
				if(board[2][2] == 'X' || board[2][2] == 'O') Flag=true;
			break;
			default:
			Flag=false;
		}
		return Flag;


	}

	public static void main(String[] args)
    	{   
    		Tic ttt = new Tic();
    		ttt.play();
		System.out.println("THANK FOR PLAYING BYE BYE");
    	}




}
1
#6
bojwess كتب:

اذا ما اشتغل معاك هذا الكود اخذ هذا شغال 100%

من برمجتي

/**
This Program is Done By Mohammad Riyad Alzaatary GUST ID: 3334
my email is mohmd.ryd@gmail.com and gust0003334@gust.edu.kw
student at Gulf University For Sciense And Technology
Instructer: Ms. Shiny Moncy
Assignment 1 Object Oriented Course

Its a game Tic Tac Toea Player1 Will Play First with 'X' Character
and Player2 Play second with 'O' character

each one of them will enter the number of the index in the cell
When one player has placed three tokens in a horizontal, vertical or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win.


 */
import java.util.*; 
public class Tic
{
	private char[][] board = new char [3][3]; // intialize the board with 3 X 3 size
	Scanner input = new Scanner(System.in); // create input object from scanner class to get input from the user	
	public Tic() // Constructer
	{
		char counter = '1'; // intialize the board with it
		for (int i = 0; i < 3; i++)
            	{
               		for (int j = 0; j < 3; j++){board[j] = counter++ ;} // put the numbers in the board
           	}


        }

	public void print() // print the board
        {  
            for ( int i = 0; i < 3; i++)
            {
                System.out.println("  ");   
                for( int j = 0; j < 3; j++)
                {
                    System.out.print("["+board[j] + "] "); // print each element with [ ]
                }
                System.out.println("\n------"); // seperate ROWS
          }
	}

	void play()
	{
		String c1;char index;

		while(true)
		{	
			print();//print board
			System.out.print("Player 1 You Are \'X\':\t");
			c1 = input.nextLine();//take input string from user
			index = c1.charAt(0);//convert String to char

			// enter to the board the first Character
			while(isChecked(index))	// check if already contain 'X' or 'O'
			{
				System.out.print("Player 1 You Are \'X\':");
				System.out.print("WRONG Input Enter Again:\t");
				c1 = input.nextLine();//take input string from user
				index = c1.charAt(0);//convert String to char	
			}
			inUser1(index); // put Char 'X' in the sepcified index
			if(isDraw()){
					System.out.println(((char) 27)+"[2J");//clear screen 
					print();
					System.out.println("ITS A DRAW TRY AGAIN BOTH OF YOU");
					break;}// Check if There is A Draw




			System.out.println(((char) 27)+"[2J");//clear screen 

			// check if any palayer Win
			if(isWin()){
				print();
				System.out.println("****************\nCONGRATULATION PLAYER 1 WON\n****************"); //prtint if player 1 WON
				break;
			}


			print();//print board

			System.out.print("Player 2 You Are \'O\':\t");
			c1 = input.nextLine();//take input string from user
			index = c1.charAt(0); //convert String to char

			// enter to the board the first Character
			while(isChecked(index))// check if already contain 'X' or 'O'
			{
				System.out.print("Player 2 You Are \'X\':");
				System.out.print("WRONG Input Enter Again:\t");
				c1 = input.nextLine();//take input string from user
				index = c1.charAt(0);//convert String to char	
			}
			inUser2(index);// put Char 'O' in the sepcified index
			if(isDraw())
			{
				System.out.println(((char) 27)+"[2J");
				print();System.out.println("ITS A DRAW TRY AGAIN BOTH OF YOU");break;}// Check if There is A Draw


			System.out.println(((char) 27)+"[2J");//clear screen 

			// check if any palayer Win
			if(isWin()){
				print();
				System.out.println("****************\nCONGRATULATION PLAYER 2 WON\n****************");//print if player 2 WON
				break;
			}




		}




	}

	boolean isDraw()
	{
		int counter=0;
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
			{
				if(board[j]=='X' || board[j]=='O')counter++;
			}
		}
		if(counter==9){return true;}
		else {return false;}
	}

	void inUser1(char index) // input for player 1 with 'X'
	{	
		switch(index)
		{
			case '1':board[0][0]='X';break;
			case '2':board[0][1]='X';break;
			case '3':board[0][2]='X';break;
			case '4':board[1][0]='X';break;
			case '5':board[1][1]='X';break;
			case '6':board[1][2]='X';break;
			case '7':board[2][0]='X';break;
			case '8':board[2][1]='X';break;
			case '9':board[2][2]='X';break;
		}

	}

	void inUser2(char index)	// input for player 2 with 'O'
	{	
		switch(index)
		{
			case '1':board[0][0]='O';break;
			case '2':board[0][1]='O';break;
			case '3':board[0][2]='O';break;
			case '4':board[1][0]='O';break;
			case '5':board[1][1]='O';break;
			case '6':board[1][2]='O';break;
			case '7':board[2][0]='O';break;
			case '8':board[2][1]='O';break;
			case '9':board[2][2]='O';break;
		}

	}

	boolean isWin()
	{
		if(board[0][1]==board[0][2] && board[0][1]==board[0][0]){return true;} //check first row
		else if(board[1][0]==board[1][1] && board[1][0]==board[1][2]){return true;}//check second row
		else if(board[2][0]==board[2][1] && board[2][0]==board[2][2]){return true;}//check third row
		else if(board[0][0]==board[1][0] && board[0][0]==board[2][0]){return true;}//check first colum
		else if(board[0][1]==board[1][1] && board[0][1]==board[2][1]){return true;}//check second colum
		else if(board[0][2]==board[1][2] && board[0][2]==board[2][2]){return true;}//check third colum
		else if(board[0][0]==board[1][1] && board[0][0]==board[2][2]){return true;}//check diagonal
		else if(board[0][2]==board[1][1] && board[0][2]==board[2][0]){return true;}//check subdiagonal
		else return false;
	}
	boolean isChecked(char index) // this method check whether the index already contain 'X' or 'O' character
	{

			boolean Flag=false;
			switch(index)
		{
			case '1':
				if(board[0][0] == 'X' || board[0][0] == 'O') Flag=true;
			break;
			case '2':
				if(board[0][1] == 'X' || board[0][1] == 'O') Flag=true;
			break;
			case '3':
				if(board[0][2] == 'X' || board[0][2] == 'O') Flag=true;
			break;
			case '4':
				if(board[1][0] == 'X' || board[1][0] == 'O') Flag=true;
			break;
			case '5':
				if(board[1][1] == 'X' || board[1][1] == 'O') Flag=true;

			break;
			case '6':
				if(board[1][2] == 'X' || board[1][2] == 'O') Flag=true;
			break;
			case '7':
				if(board[2][0] == 'X' || board[2][0] == 'O') Flag=true;
			break;
			case '8':
				if(board[2][1] == 'X' || board[2][1] == 'O') Flag=true;
			break;
			case '9':
				if(board[2][2] == 'X' || board[2][2] == 'O') Flag=true;
			break;
			default:
			Flag=false;
		}
		return Flag;


	}

	public static void main(String[] args)
    	{   
    		Tic ttt = new Tic();
    		ttt.play();
		System.out.println("THANK FOR PLAYING BYE BYE");
    	}




}

يعطيك العافيه أخوي محمد.. برنامج منظم وجميل

انا بصراحه حاولت انو العمليه بأكملها تكون في لووب, مثلاً بعد مانتنهي اللعبه يكون فيه سؤال "هل تريد اللعب مره آخرى؟" إذا Yes يكمل, غير ذلك يخرج من اللوب..

لكن واجهتني مشكله بأني اعيد القيم اللي داخل المصفوفه كما كانت :/

تحيآتي ~

#7

مساء الورد

شكرا على المساعده .. و فعلا بعد ما استخدمت الdebug الموجود في الــ Netbeans وضحت معايه المشاكل و عدلتها و الحمد الله

و هذا الكود النهائي بعد التعديل حبيت انزله يمكن حد يستفيد منه :

package Tictactoe;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author ituser
 */
import java.util.*;

public class Tictactoe {

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

    public Tictactoe() {
        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 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;
    }

    /**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 game = new Tictactoe();
        boolean done = false;
        System.out.println(game.toString());
        while (!done) {

            int row = 0, column = 0;
            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

    }

}

bojwess كتب:

اذا ما اشتغل معاك هذا الكود اخذ هذا شغال 100%

من برمجتي

/**
This Program is Done By Mohammad Riyad Alzaatary GUST ID: 3334
my email is mohmd.ryd@gmail.com and gust0003334@gust.edu.kw
student at Gulf University For Sciense And Technology
Instructer: Ms. Shiny Moncy
Assignment 1 Object Oriented Course

Its a game Tic Tac Toea Player1 Will Play First with 'X' Character
and Player2 Play second with 'O' character

each one of them will enter the number of the index in the cell
When one player has placed three tokens in a horizontal, vertical or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win.


 */
import java.util.*; 
public class Tic
{
	private char[][] board = new char [3][3]; // intialize the board with 3 X 3 size
	Scanner input = new Scanner(System.in); // create input object from scanner class to get input from the user	
	public Tic() // Constructer
	{
		char counter = '1'; // intialize the board with it
		for (int i = 0; i < 3; i++)
            	{
               		for (int j = 0; j < 3; j++){board[j] = counter++ ;} // put the numbers in the board
           	}


        }

	public void print() // print the board
        {  
            for ( int i = 0; i < 3; i++)
            {
                System.out.println("  ");   
                for( int j = 0; j < 3; j++)
                {
                    System.out.print("["+board[j] + "] "); // print each element with [ ]
                }
                System.out.println("\n------"); // seperate ROWS
          }
	}

	void play()
	{
		String c1;char index;

		while(true)
		{	
			print();//print board
			System.out.print("Player 1 You Are \'X\':\t");
			c1 = input.nextLine();//take input string from user
			index = c1.charAt(0);//convert String to char

			// enter to the board the first Character
			while(isChecked(index))	// check if already contain 'X' or 'O'
			{
				System.out.print("Player 1 You Are \'X\':");
				System.out.print("WRONG Input Enter Again:\t");
				c1 = input.nextLine();//take input string from user
				index = c1.charAt(0);//convert String to char	
			}
			inUser1(index); // put Char 'X' in the sepcified index
			if(isDraw()){
					System.out.println(((char) 27)+"[2J");//clear screen 
					print();
					System.out.println("ITS A DRAW TRY AGAIN BOTH OF YOU");
					break;}// Check if There is A Draw




			System.out.println(((char) 27)+"[2J");//clear screen 

			// check if any palayer Win
			if(isWin()){
				print();
				System.out.println("****************\nCONGRATULATION PLAYER 1 WON\n****************"); //prtint if player 1 WON
				break;
			}


			print();//print board

			System.out.print("Player 2 You Are \'O\':\t");
			c1 = input.nextLine();//take input string from user
			index = c1.charAt(0); //convert String to char

			// enter to the board the first Character
			while(isChecked(index))// check if already contain 'X' or 'O'
			{
				System.out.print("Player 2 You Are \'X\':");
				System.out.print("WRONG Input Enter Again:\t");
				c1 = input.nextLine();//take input string from user
				index = c1.charAt(0);//convert String to char	
			}
			inUser2(index);// put Char 'O' in the sepcified index
			if(isDraw())
			{
				System.out.println(((char) 27)+"[2J");
				print();System.out.println("ITS A DRAW TRY AGAIN BOTH OF YOU");break;}// Check if There is A Draw


			System.out.println(((char) 27)+"[2J");//clear screen 

			// check if any palayer Win
			if(isWin()){
				print();
				System.out.println("****************\nCONGRATULATION PLAYER 2 WON\n****************");//print if player 2 WON
				break;
			}




		}




	}

	boolean isDraw()
	{
		int counter=0;
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
			{
				if(board[j]=='X' || board[j]=='O')counter++;
			}
		}
		if(counter==9){return true;}
		else {return false;}
	}

	void inUser1(char index) // input for player 1 with 'X'
	{	
		switch(index)
		{
			case '1':board[0][0]='X';break;
			case '2':board[0][1]='X';break;
			case '3':board[0][2]='X';break;
			case '4':board[1][0]='X';break;
			case '5':board[1][1]='X';break;
			case '6':board[1][2]='X';break;
			case '7':board[2][0]='X';break;
			case '8':board[2][1]='X';break;
			case '9':board[2][2]='X';break;
		}

	}

	void inUser2(char index)	// input for player 2 with 'O'
	{	
		switch(index)
		{
			case '1':board[0][0]='O';break;
			case '2':board[0][1]='O';break;
			case '3':board[0][2]='O';break;
			case '4':board[1][0]='O';break;
			case '5':board[1][1]='O';break;
			case '6':board[1][2]='O';break;
			case '7':board[2][0]='O';break;
			case '8':board[2][1]='O';break;
			case '9':board[2][2]='O';break;
		}

	}

	boolean isWin()
	{
		if(board[0][1]==board[0][2] && board[0][1]==board[0][0]){return true;} //check first row
		else if(board[1][0]==board[1][1] && board[1][0]==board[1][2]){return true;}//check second row
		else if(board[2][0]==board[2][1] && board[2][0]==board[2][2]){return true;}//check third row
		else if(board[0][0]==board[1][0] && board[0][0]==board[2][0]){return true;}//check first colum
		else if(board[0][1]==board[1][1] && board[0][1]==board[2][1]){return true;}//check second colum
		else if(board[0][2]==board[1][2] && board[0][2]==board[2][2]){return true;}//check third colum
		else if(board[0][0]==board[1][1] && board[0][0]==board[2][2]){return true;}//check diagonal
		else if(board[0][2]==board[1][1] && board[0][2]==board[2][0]){return true;}//check subdiagonal
		else return false;
	}
	boolean isChecked(char index) // this method check whether the index already contain 'X' or 'O' character
	{

			boolean Flag=false;
			switch(index)
		{
			case '1':
				if(board[0][0] == 'X' || board[0][0] == 'O') Flag=true;
			break;
			case '2':
				if(board[0][1] == 'X' || board[0][1] == 'O') Flag=true;
			break;
			case '3':
				if(board[0][2] == 'X' || board[0][2] == 'O') Flag=true;
			break;
			case '4':
				if(board[1][0] == 'X' || board[1][0] == 'O') Flag=true;
			break;
			case '5':
				if(board[1][1] == 'X' || board[1][1] == 'O') Flag=true;

			break;
			case '6':
				if(board[1][2] == 'X' || board[1][2] == 'O') Flag=true;
			break;
			case '7':
				if(board[2][0] == 'X' || board[2][0] == 'O') Flag=true;
			break;
			case '8':
				if(board[2][1] == 'X' || board[2][1] == 'O') Flag=true;
			break;
			case '9':
				if(board[2][2] == 'X' || board[2][2] == 'O') Flag=true;
			break;
			default:
			Flag=false;
		}
		return Flag;


	}

	public static void main(String[] args)
    	{   
    		Tic ttt = new Tic();
    		ttt.play();
		System.out.println("THANK FOR PLAYING BYE BYE");
    	}




}

جربت الكود و فعلا يشتغل تمام .. يعطيك العافية ... وااايد حبيت حركة الارقام يعني اللاعب بس يدخل الرقم واااااايد اسهل عن طريقتية اللي لازم يدخل فيها رقمين ^_^

1
#8

عندي سؤال .... لو حبيت اخلي اللاعب يلعب ضد الكمبيوتر ... كيف اقدر اغير الميثود ؟؟ يعني شو الميثود اللي تخلي الكمبيوتر يلعب

#9

تحتاجي للقراءة في الذكاء الصناعي

هناك عدة خوارزميات تتعلق باللعب مع الكمبيوتر منها A* على ما أعتقد

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

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