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

كود لعبة Tic Tae Toe

بدأه Ah.K.EL-Saman في 27 مارس 2011 · 5 رد · 1,541 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

هذا كود متواضع كتبته أمس استجابة لطلب دكتور البرمجة - طبعاً الكود ليس مطور لدرجة كبيرة نظراً لأنه مكتوب عشان ناخد الدرجات فى الكلية وشكراً :) - وحبيت أشاركه ونادراً ما أشارك فى قسم الجافا ان لم تكن اخر مرة :lol:

لأن بصراحة مش بكتب بيها كتير الا عشان خاطر الكلية ... معلش بقى متعود من زمان على C

ويارب يفيد أى حد عشان أحس انى مش زعلان انى بتكتب بجافا :lol:

رجالة الجافا مش عايزحدف طوب أو طماطم ... رجالة الجافا أجدع ناس :happy:

/**
 * @Program Name          : xo.java
 * @Author                : Ahmed Kamal Salah EL-Dein Fouad EL-Saman
 * @Last Modification Date: 26/03/2011
 * @version               : 1.0
 * Purpose                : tic tae toe game@
*/
import java.util.Scanner;
import java.util.InputMismatchException;
public class xo{

	// global variables used cross over program methods
	public static String player="One"; 						// change between the two players
	public static boolean acceptInput=false;					// acceptInput is true only if the user input between 1 and 3
	public static byte row=0; 							// contain user row number input
	public static byte column=0; 							// contain user column number input
	public static boolean endGame=false; 						// true only if the game end by winning one of the two players or game draw
	public static byte count=0; 							// count filled cells
	public static char [][] arr={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}}; 	// array contain the choice of players
	public static Scanner kb = new Scanner ( System.in ); 				// scanner object to take user input
	// ==========================================================================================================
	// main method ( entery of the program )
	public static void main(String[] args) {
		// welcome message
		System.out.println();
		System.out.println(" Welcome to SWE102 X-O");
		System.out.println(" ======================");	

		// method for drawing game table
		drawGameTable();
		// start game
		startGame();
	}
	// ==========================================================================================================
	// draw game table method
	public static void drawGameTable() {
		System.out.println(); // print line of space before the game table
		System.out.println("      1     2     3     ");
		System.out.println("   +-----+-----+-----+");
		System.out.println(" 1 |  " + arr[0][0] + "  |  " + arr[0][1] + "  |  " +  arr[0][2] + "  |   "); // draw first row
		System.out.println("   +-----+-----+-----+");
		System.out.println(" 2 |  " + arr[1][0] + "  |  " + arr[1][1] + "  |  " +  arr[1][2] + "  |   "); // draw seconed row
		System.out.println("   +-----+-----+-----+");
		System.out.println(" 3 |  " + arr[2][0] + "  |  " + arr[2][1] + "  |  " +  arr[2][2] + "  |   "); // draw third row
		System.out.println("   +-----+-----+-----+");
		System.out.println(); // print line of space after the game table
	}
	// ==========================================================================================================
	// start game method
	public static void startGame() {
		do {
			takeRow(); // take row number input from user between 1 and 3
			takeColumn(); // take column number input from user between 1 and 3
			setChoice(); // set player choice
		} while (endGame==false); // continue game if endGame variable is not set to true
	}
	// ==========================================================================================================
	// take row number input from user between 1 and 3
	public static void takeRow() {
		acceptInput=false; // reset the value to false for the next play
		do { // using do to loop at least one time
			System.out.println("Please Player " + player + " Enter Your Choice 1, 2 Or 3 - ");
			System.out.print("(row): ");
			try {  // try to catch exception if the user input it's type is not byte.
				row=kb.nextByte(); // scan user input of row number
			} catch (InputMismatchException exception) {
				kb.next();  // get the wrong value and discard it
				row=0; // reset row variable to it's original value to reset loop
			}
			if (row>=1 && row<=3 ) // check if user input row number is between 1 and 3
				acceptInput=true; // accept the row number of the user
		} while (acceptInput==false); // do until user enter a right value
	}
	// ==========================================================================================================
	// take column number input from user between 1 and 3
	public static void takeColumn() {
		acceptInput=false; // reset the value to false for the next play
		do { // using do to loop at least one time
			System.out.print("(Column): ");
			try { // try to catch exception if the user input it's type is not byte.
				column=kb.nextByte(); // scan user input of column number
			} catch (InputMismatchException exception) {
				kb.next(); // get the wrong value and discard it
				column=0; // reset column variable to it's original value to reset loop
			}
			if (column>=1 && column<=3 ) // check if user input column number is between 1 and 3
				acceptInput=true; // accept the column number of the user
		} while (acceptInput==false); // do until user enter a right value
	}
	// ==========================================================================================================
	// set player choice
	public static void setChoice() {
		// Put ' X ' For Player One and ' O ' For Player Two (((( if choosen cell is empty ))))
		if ( arr[row-1][column-1]==' ') {
			if (player=="One") { // check if the choice for player one ?
				arr[row-1][column-1]='X'; // arraye index = user input - 1
				// check if the last choice is the win for a player one or end of free cells before change to player two 		
				checkEndOfGame(); 
				player="Two"; // set the next choice for player two
			} else { // if the choice isn't for player one then for player two
				arr[row-1][column-1]='O'; // arraye index = user input - 1
				// check if the last choice is the win for a player two or end of free cells before change to player one			
				checkEndOfGame();
				player="One"; // set the next choice for player one
			}
		} else { // (((( if choosen cell not empty ))))
			// ask user to choose empty choice
			System.out.println();
			System.out.println(" ******************************** ");
			System.out.println(" *** Please Choose Empty cell *** ");
			System.out.println(" ******************************** ");
		}
		// draw game table after set player choice
		drawGameTable();
	}
	// ==========================================================================================================
	// check if the last choice is the win for a player or end of free cells
	public static void checkEndOfGame() {
		count++; // add cell to filled cells
		if (arr[0][0] == arr[0][1] && arr[0][0]==arr[0][2] && (arr[0][0]!=' ')) endGame=true; // check if a player connect row number 1 
		if (arr[1][0] == arr[1][1] && arr[1][0]==arr[1][2] && (arr[1][0]!=' ')) endGame=true; // check if a player connect row number 2
		if (arr[2][0] == arr[2][1] && arr[2][0]==arr[2][2] && (arr[2][0]!=' ')) endGame=true; // check if a player connect row number 3
		if (arr[0][0] == arr[1][0] && arr[0][0]==arr[2][0] && (arr[0][0]!=' ')) endGame=true; // check if a player connect column number 1
		if (arr[0][1] == arr[1][1] && arr[0][1]==arr[2][1] && (arr[0][1]!=' ')) endGame=true; // check if a player connect column number 2
		if (arr[0][2] == arr[1][2] && arr[0][2]==arr[2][2] && (arr[0][2]!=' ')) endGame=true; // check if a player connect column number 3
		// check if a player connect diagonal ( top left to bottom right )
		if (arr[0][0] == arr[1][1] && arr[1][1]==arr[2][2] && (arr[1][1]!=' ')) endGame=true;
		// check if a player connect diagonal ( top right to bottom left )
		if (arr[0][2] == arr[1][1] && arr[1][1]==arr[2][0] && (arr[1][1]!=' ')) endGame=true;

		if (endGame==true) { // end program if a player won
			System.out.println();
			System.out.println(" *********************************************** ");
			System.out.println(" *** Congratulation player " + player + " is the winner *** ");
			System.out.println(" *********************************************** ");		
		} else if(count==9) { // end program if there's no more free cell
			endGame=true;
			System.out.println();
			System.out.println(" ********************************************* ");
			System.out.println(" *** You are good players ... game is draw *** ");
			System.out.println(" ********************************************* ");
		}
	}
}

xo.java

تم تعديل هذه المشاركة بواسطة Ah.K.EL-Saman في 28 مارس 2011 في 22:44

4

سَبِّحِ اسْمَ رَبِّكَ الْأَعْلَى

#2

الكود مكتوب بعناية

مشكوريا بطل

إذا نظــرتَ نيـوب اللـّيـثِ بـارزةً         فـلا تظــنَنَ أَنَّ اللـّيـــثَ يبْتسـِـــمُ


 


 


رسم المخططات البيانية بواسطة الجافا


 


كتاب تحليل وتصميم نظم المعلومات


#3

أحييك على هذا الكود

597657.png
#4

سلام عليكم ....... الكود رائع جدا والطريقه جميله .........بس فى غلطات بسيطه

صلحتها فى الكود ده

package tictactoe;

import java.util.Scanner;

import java.util.InputMismatchException;

class MyXo {

// global variables used cross over program methods

public static String player ="One"; // change between the two players

public static boolean acceptInput =false;

public static int row ;

public static int column ;

public static boolean endGame =false;

public static char [][] arr ={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}}; // array contain the choice of players

public static Scanner kb = new Scanner ( System.in );

public static void drawGameTable() {

System.out.println();

System.out.println(" 1 2 3 ");

System.out.println(" +-----+-----+-----+");

System.out.println(" 1 | " + arr[0][0] + " | " + arr[0][1] + " | " + arr[0][2] + " | ");

System.out.println(" +-----+-----+-----+");

System.out.println(" 2 | " + arr[1][0] + " | " + arr[1][1] + " | " + arr[1][2] + " | ");

System.out.println(" +-----+-----+-----+");

System.out.println(" 3 | " + arr[2][0] + " | " + arr[2][1] + " | " + arr[2][2] + " | ");

System.out.println(" +-----+-----+-----+");

System.out.println();

}

public static void startGame() {

do {

takeRow();

takeColumn();

setChoice();

} while (endGame == false);

}

public static void takeRow() {

acceptInput = false; // reset the value to false for the next player

do {

System.out.println("Please Player " + player + " Enter Your Choice 1, 2 Or 3 - ");

System.out.print("(row): ");

try {

row = kb.nextInt();

} catch (InputMismatchException exception) {

System.out.println("wrong row number ");

kb.next(); // get the wrong value and discard it

row=0;

}

if (row >=1 && row <=3 )

acceptInput =true;

} while (acceptInput == false); // do until user enter a right value

}

public static void takeColumn() {

acceptInput = false;

do {

System.out.print("(Column): ");

try {

column = kb.nextInt();

} catch (InputMismatchException exception) {

System.out.println("wrong column number");

kb.next(); // get the wrong value and discard it

column = 0;

}

if (column >=1 && column <=3 )

acceptInput=true;

} while (acceptInput==false); // do until user enter a right value

}

public static void setChoice() {

// Put ' X ' For Player One and ' O ' For Player Two (((( if choosen cell is empty ))))

if ( arr[row-1][column-1]==' ') {

if ("One".equals(player)) { // check if the choice for player one ?

arr[row-1][column-1]='X';

// check if the last choice is the win for a player one or end of free cells before change to player two

checkEndOfGame();

player="Two"; // set the next choice for player two

} else {

arr[row-1][column-1]='O';

checkEndOfGame();

player="One"; // set the next choice for player one

}

} else {

System.out.println();

System.out.println(" ******************************** ");

System.out.println(" *** Please Choose Empty cell *** ");

System.out.println(" ******************************** ");

}

// draw game table after set player choice

drawGameTable();

}

// check if the last choice is the win for a player or end of free cells

public static void checkEndOfGame() {

// add cell to filled cells

if (arr[0][0] == arr[0][1] && arr[0][0]==arr[0][2] && (arr[0][0]!=' ')) endGame=true; // check if a player connect row number 1

if (arr[1][0] == arr[1][1] && arr[1][0]==arr[1][2] && (arr[1][0]!=' ')) endGame=true; // check if a player connect row number 2

if (arr[2][0] == arr[2][1] && arr[2][0]==arr[2][2] && (arr[2][0]!=' ')) endGame=true; // check if a player connect row number 3

if (arr[0][0] == arr[1][0] && arr[0][0]==arr[2][0] && (arr[0][0]!=' ')) endGame=true; // check if a player connect column number 1

if (arr[0][1] == arr[1][1] && arr[0][1]==arr[2][1] && (arr[0][1]!=' ')) endGame=true; // check if a player connect column number 2

if (arr[0][2] == arr[1][2] && arr[0][2]==arr[2][2] && (arr[0][2]!=' ')) endGame=true; // check if a player connect column number 3

// check if a player connect diagonal ( top left to right )

if (arr[0][0] == arr[1][1] && arr[1][1]==arr[2][2] && (arr[1][1]!=' ')) endGame=true;

// check if a player connect diagonal ( top right to left )

if (arr[0][2] == arr[1][1] && arr[1][1]==arr[2][0] && (arr[1][1]!=' ')) endGame=true;

if (endGame==true) { // end program if a player won

System.out.println();

System.out.println(" *********************************************** ");

System.out.println(" *** Congratulation player " + player + " is the winner *** ");

System.out.println(" *********************************************** ");

}

else { System.out.println();

System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

System.out.println(" ~~~~ hard luck no winner try again~~~~~~~~~");

System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

}

}

}

اقتباس

تم تعديل هذه المشاركة بواسطة Dory Doritos في 13 فبراير 2012 في 21:42

#5

ممكن اخي تحرر مشاركتك وتضع الكود في تاج كود

جزاكم الله خير

عندما تريد أن تتعلم جافا بالفيديو إليك قنوات الYouTube الخاصة بقسم الحاسب الآلي

أ. فاضل خداده

أ. عبدالعزيز الرومي

أ. وحيد الساير

(وَمَا أُوتِيتُمْ مِنْ الْعِلْمِ إِلاَّ قَلِيلاً) [سورة الإسراء: الآية 85]

#6

السلام عليكم

لعبة جميلة و كود أجمل, أهنئك أخي الكريم :)

العجيب في الأمر انني كنت أفكر قبل أيام في كتابة نفس اللعبة بالسي++ بحيث تكون اللعبة بين المستخدم و الجهاز نفسه و أظن أن الفرق يكمن هنا.

ما هي أفكاركم في هذه الحالة ؟

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