السلام عليكم ورحمه الله وبركاته
كان فيه لعبه عرفتها كدا اثناء التدريب
لعبه للتفكير على اساس ان المتدربين اول مره يتعاملوا مع الجافا
بس طبعا من عادتى اننى لم احضر التدريب بل اشرح :lol:
لكن حضرت وسالت عن اللعبه وتفاصيلها وبدات العمل بها
والحمدلله انتهيت منها فى غضون 10 دقائق
لكن لعبه بالـ console
ليس فى اطار و هكذا
اللعبة سهله جميله ممتعه
هى ان الحاسوب يقوم بتخمين رقم من 1 الى 100
وعلى المستخدم ان يكون لديه 6 محاولات لتخمين هذا الرقم
وهناك تسهيلات من البرنامج
يقود المستخدم الى ان يبلغه هل الرقم الذى ادخله مسبقا هل هو اكبر من المخمن ام اقل منه
واذا تم تخمين الرقم الصحيح يخرج ويقول له انك كسبت
واذا انهى كل المحاولات الستة يقول له انك خسرت
هذه فكره سهله
لكن احببت ان اشارك بها كل مبتدأ ولا مانع من ان يقوم محترف بالتعليق عليها و هل هناك اخطاء ام لا
هذا هو الكود الخاص بالـ console مع استخدام JOPtionPane
Game.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hilogame;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
* @author Mahmoud Mahrous
*/
public class Game {
private int randomNumber;
private int gussedNumberFromUser;
private boolean winOrNot = false;
private int i = 0;
public void start() {
descripRules();
generateSecretNumber();
playGame();
getNextGuss();
}
private void descripRules() {
JOptionPane.showMessageDialog(null, "* Enter number bettwen 1 and 100." +
"\n* Program will generat random number." +
"\n* You should guss this number and you have six trys to win." +
"\n* The program will but you to be closer to the gernerated number.");
}
private void generateSecretNumber() {
Random random = new Random();
randomNumber = random.nextInt(100) + 1;
getNextGuss();
}
private void getNextGuss() {
try {
gussedNumberFromUser = Integer.parseInt(JOptionPane.showInputDialog("Enter your gussed number ( " + (i + 1) + " ) : "));
if (gussedNumberFromUser >= 1 && gussedNumberFromUser <= 100) {
} else {
getNextGuss();
}
} catch (NumberFormatException e) {
getNextGuss();
}
}
private void playGame() {
for (i = 1; i < 6; i++) {
if (randomNumber == gussedNumberFromUser) {
winOrNot = true;
break;
} else {
winOrNot = false;
checkWhereGussedNumber();
getNextGuss();
}
}
checkWhereGussedNumber();
gameStauts();
}
private void checkWhereGussedNumber() {
if (winOrNot == false) {
if (gussedNumberFromUser >= randomNumber) {
JOptionPane.showMessageDialog(null, "Random number is lower than this.");
} else {
JOptionPane.showMessageDialog(null, "Random number is larger than this.");
}
}
}
private void gameStauts() {
if (winOrNot == false) {
JOptionPane.showMessageDialog(null, "Lose.");
} else {
JOptionPane.showMessageDialog(null, "Win.");
}
prompt();
}
private void prompt() {
String choise = (JOptionPane.showInputDialog("Do you want to paly again ? , write yes or no .")).trim().toLowerCase();
if (choise.equals("yes")) {
new Game();
} else if (choise.equals("no")) {
JOptionPane.showMessageDialog(null, "Thank you , hope you have good time with us.");
System.exit(0);
} else if (!choise.equals("yes") || !choise.equals("no")) {
JOptionPane.showMessageDialog(null, "Wrong choise.");
prompt();
}
}
}Main.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hilogame;
/**
*
* @author Mahmoud Mahrous
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}وهذا هو الكود الخاص بالـ GUI Game فى اطار واحد يعرض لك كل التفاصيل مع تصميم جيد ان شاء الله
GameFrame.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* GameFrame.java
*
* Created on Aug 9, 2010, 6:56:25 AM
*/
package hilogame;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
* @author Mahmoud Mahrous
*/
public class GameFrame extends javax.swing.JFrame {
private int randomNumber;
private boolean winOrNot = false;
int counter = 0;
private int gussedNumberFromUser;
/** Creates new form GameFrame */
public GameFrame() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(GameFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(GameFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(GameFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(GameFrame.class.getName()).log(Level.SEVERE, null, ex);
}
descripRules();
initComponents();
setLocationRelativeTo(null);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
mainPanel = new javax.swing.JPanel();
gussedLabel = new javax.swing.JLabel();
inputTextField = new javax.swing.JTextField();
inputStausLabel = new javax.swing.JLabel();
winOrLoseLabel = new javax.swing.JLabel();
checkButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("HiLo Game");
setResizable(false);
mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "HiLo Game", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 10))); // NOI18N
gussedLabel.setText("Enter your gussed number");
inputTextField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
inputTextFieldKeyPressed(evt);
}
});
inputStausLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
winOrLoseLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
winOrLoseLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
winOrLoseLabel.setText("Try number 1/6");
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(inputStausLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(mainPanelLayout.createSequentialGroup()
.addComponent(gussedLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(inputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
.addContainerGap(72, Short.MAX_VALUE)
.addComponent(winOrLoseLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(gussedLabel)
.addComponent(inputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(inputStausLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 18, Short.MAX_VALUE)
.addGap(11, 11, 11)
.addComponent(winOrLoseLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
checkButton.setText("Check");
checkButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
checkButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(217, Short.MAX_VALUE)
.addComponent(checkButton)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(checkButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (checkInput() == false) {
} else {
if (counter != 5) {
try {
gussedNumberFromUser = Integer.parseInt(inputTextField.getText().trim());
if (gussedNumberFromUser >= 1 && gussedNumberFromUser <= 100) {
counter++;
winOrLoseLabel.setText("Try number " + (counter + 1) + "/6");
if (gussedNumberFromUser == randomNumber) {
winOrNot = true;
checkGame();
} else {
winOrNot = false;
checkWhereGussedNumber();
}
} else {
inputStausLabel.setText("Error Range , 1 <= number <= 100");
inputStausLabel.setForeground(Color.red);
}
} catch (NumberFormatException es) {
inputStausLabel.setText("Invalid input number");
inputStausLabel.setForeground(Color.red);
}
} else if (counter == 5) {
checkGame();
}
inputTextField.setText("");
}
}
private void inputTextFieldKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
checkButtonActionPerformed(null);
}
}
private boolean checkInput() {
if (inputTextField.getText().trim().equals("")) {
inputStausLabel.setText("Enter number first");
inputStausLabel.setForeground(Color.blue);
return false;
} else {
return true;
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GameFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton checkButton;
private javax.swing.JLabel gussedLabel;
private javax.swing.JLabel inputStausLabel;
private javax.swing.JTextField inputTextField;
private javax.swing.JPanel mainPanel;
private javax.swing.JLabel winOrLoseLabel;
// End of variables declaration
private void descripRules() {
JOptionPane.showMessageDialog(null, "* Enter number bettwen 1 and 100" +
"\n* Program will generat random number" +
"\n* You should guss this number and you have six trys to win" +
"\n* The program will lead you to be closer to the gernerated number", "Game Rules", JOptionPane.INFORMATION_MESSAGE);
generateSecretNumber();
}
private void generateSecretNumber() {
Random random = new Random();
randomNumber = random.nextInt(100) + 1;
}
private void checkWhereGussedNumber() {
if (Integer.parseInt(inputTextField.getText().trim()) >= randomNumber) {
inputStausLabel.setText("Random number is lower than this");
inputStausLabel.setForeground(Color.GREEN);
} else {
inputStausLabel.setText("Random number is larger than this");
inputStausLabel.setForeground(Color.DARK_GRAY);
}
}
private void checkGame() {
if (winOrNot == false) {
winOrLoseLabel.setText("Lose");
winOrLoseLabel.setForeground(Color.red);
setGameEnable(false);
} else {
winOrLoseLabel.setText("Win");
winOrLoseLabel.setForeground(Color.BLUE);
}
palyAgain();
}
private void palyAgain() {
int choise = JOptionPane.showConfirmDialog(null, "You " + winOrLoseLabel.getText() + "\nDo you want to paly again ?");
if (choise == JOptionPane.OK_OPTION) {
setGameEnable(true);
counter = 0;
inputStausLabel.setText("");
winOrLoseLabel.setText("Try number 1/6");
} else if (choise == JOptionPane.NO_OPTION) {
System.exit(0);
} else {
inputTextField.setText("NAN");
}
}
private void setGameEnable(boolean stats) {
inputTextField.setEditable(stats);
checkButton.setEnabled(stats);
}
}Main.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hilogame;
/**
*
* @author Mahmoud Mahrous
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GameFrame gameFrame = new GameFrame();
gameFrame.setVisible(true);
}
}للتحميل
هناك لعبة اخرى اسمها
Connect 4
لكن للاسف اتمسحت مع الغاء الويندوز
هاحاول اعملها تانى ان شاء الله
وكمان احولها لـ GUI
