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

لعبة حلوة جدا(Puzzle)

مغلق
بدأه سند في 1 يوليو 2004 · 7 رد · 1,084 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

شباب هذه لعبة Puzzle معمولة بالجافا

جربوها

import java.awt.*;
import java.awt.event.*;

class Puzzle extends Frame implements ActionListener
{

	Button[] btn;
	String[] cpt ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
	MenuBar mbar;
	Menu mGame;
	MenuItem mnuNewGame = new MenuItem("New Game");
	MenuItem mnuArraing = new MenuItem("Arraing");
	MenuItem mnuExit = new MenuItem("Exit");
	Label lblMov = new Label("");

	int movements;


	public Puzzle(String t,boolean n)
	{
  super(t);
  setResizable(false);
  btn= new Button[16];
  setLayout(new GridLayout(5,4));
  for(int i=0;i<16;i++)
  {
 	 btn=new Button("");
 	 add(btn);
 	 btn.setForeground(Color.white);
 	 btn.addActionListener(this);
  }

  newGame(n);

  mbar= new MenuBar();
  mGame = new Menu("Game");
  setMenuBar(mbar);
  mbar.add(mGame);
  mGame.add(mnuNewGame);
  mGame.add(mnuArraing);
  mGame.addSeparator();
  mGame.add(mnuExit);

  mnuNewGame.addActionListener(this);
  mnuArraing.addActionListener(this);
  mnuExit.addActionListener(this);
  add(lblMov);

	}

	public static void main(String args[])
	{

  Puzzle pzl = new Puzzle("Frame - Mohammed H. Ismail",true);
  pzl.setSize(250,250);
  pzl.show();

	}

	public void actionPerformed(ActionEvent ae)
	{
  int bcIndex = -1;
  int cdIndex=-1;
  String cdLabel = ae.getActionCommand();

  for(int i=0;i<16;i++)
  {
 	 if(btn.getLabel().equals(" "))
    bcIndex = i;
 	 
 	 if(btn.getLabel().equals(cdLabel))
    cdIndex = i;
  }

  if(cdIndex==bcIndex-1 && cdIndex!=3 && cdIndex!=7 && cdIndex!=11 ||
     cdIndex==bcIndex+1 && cdIndex!=4 && cdIndex!=8 && cdIndex!=12 || 
     cdIndex==bcIndex-4 || 
     cdIndex==bcIndex+4
    )
 	 switchBtns(cdIndex,bcIndex);	
  

  if(cdLabel=="New Game")
  {
 	 newGame(true);
  }
  else if(cdLabel=="Arraing")
  {
 	 newGame(false);
  }
  else if(cdLabel=="Exit")
  {
 	 System.exit(0);
  }

	}

	public void switchBtns(int srcIndex,int distIndex)
	{
  btn[distIndex].setLabel(btn[srcIndex].getLabel());
  btn[srcIndex].setLabel(" ");
  btn[distIndex].setBackground(new Color(150, 157, 201));
  btn[srcIndex].setBackground(Color.white);
  
  movements++;
  lblMov.setText("" + movements);
	}

	public void newGame(boolean rand)
	{
  lblMov.setText("");
  movements=0;
  if(rand)
  {
 	 for(int i=0;i<15;i++)
 	 {
    int x=(int)(15 * Math.random())+1;
    btn.setLabel("" + x);
    while(isExistPrev(i))
    {
   	 x = (int)(15 * Math.random())+1;
   	 btn.setLabel("" + x);
    }
    btn.setBackground(new Color(150, 157, 201)); 	 }
  }
  else
  {
 	 for(int i=0;i<15;i++)
 	 {
    btn.setLabel(cpt);
    btn.setBackground(new Color(150, 157, 201));
 	 }
  }

  btn[15].setLabel(" ");
  btn[15].setBackground(Color.white);
	}

	public boolean isExistPrev(int btnIndex)
	{
  boolean exist = false;
  for(int i=0;i<btnIndex;i++)
  {
 	 if(btn.getLabel().equals(btn[btnIndex].getLabel()))
 	 {
    exist = true;
    break;
 	 }
  }
  return exist;
	}

}
D:\My%20Pictures\صور\my%20images\digital.jpeg
#2

شكرا لك.... سوف يتم نقله للارشيف

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why

#3

thanks Sanad..

but my compiler give me three errors ..?

they are at:

اقتباس
cannot resolve symbol: constructor Label (java.lang.String)in class Label at line 14 (14:16)

cannot resolve symbol: constructor Label (java.lang.String)in class Label at line 14 (14:16)

Label lblMov = new Label("");

,

اقتباس
cannot resolve symbol: constructor Frame (java.lang.String)in class Frame at line 21 (21:2)

JFrame(java.lang.String) in javax.swing.JFrame is not defined in a public class or interface; cannot be accessed from outside package at line 21 (21:2)

super(t);

and

اقتباس
cannot resolve symbol: method setText (java.lang.String)in class Label at line 106 (106:9)

lblMov.setText("" + movements);

lblMov.setText("");

so what do think the problem ?

#4

صراحة اخي "المبرمج1" انا مستغرب على الاخطاء اللي ظهرت عندك ولاحظ ان الخطا الثاني

يتكلم عن JFrame من Javax.swing بالرغم من ان كل مكونات البرنامج من Java.awt

البرنامج اشتغل عندي تمام وارجو انك تسئل شخص اكثر خبرة عن هذه الاخطاء

على العموم حاول تغير بدل super اعمل setTitle(); وامسح الوسيط في الكونستركتر

D:\My%20Pictures\صور\my%20images\digital.jpeg
#5

لعبة حلوة ... احسن من كودي بكثيييير ..

كودي بالمرفقات سويتها قبل يمكن 4 شهور ... وانا احاول اطور الكود بحيث ان البرنامج يقدر يحل اللعبة باقصر الطرق اذا عجز المستخدم عن ذلك ..

arabteam.zip

اسف سوف اقطعك من كتابة الكود، ادخل هنا لدقيقتين فقط

http://www.shbab1.com/2minutes.htm

#6

OK ...

thanks سند

#7

لو تكرم احد الاخوه مشكور بشرح طريقه العب وان امكن مع الدعاء له ان يشرح لنا الكود

شكرا على المجهود

زورو موقعنا

www.algabal.com

#8

تصفيق حار لكل من سند و مبتدئ جدًا

هذا الموضوع مغلق.

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