هذا كود متواضع كتبته أمس استجابة لطلب دكتور البرمجة - طبعاً الكود ليس مطور لدرجة كبيرة نظراً لأنه مكتوب عشان ناخد الدرجات فى الكلية وشكراً :) - وحبيت أشاركه ونادراً ما أشارك فى قسم الجافا ان لم تكن اخر مرة :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(" ********************************************* ");
}
}
}