Index of / Hasan Ünlü KILINÇ

Fantastic World and Software Fetishist

Science & Art & Philosophy

Contact Me

Name Date
Java – Random Sınıfı ve Zar Oyunu

Java – Random Sınıfı ve Zar Oyunu

zar_17287Merhaba arkadaşlar uzun bir süredir işlerin ve derslerin yoğunluğu sebebiyle yazamıyordum hala yoğunum ancak bulduğum ilk fırsatta birşeyler yazayım dedim. Çok yoğun olmasada girip takip edenler var biliyorum 🙂

Neyse   bu dersimizde Random Sınıfınagiriş yapmış olacağız aslında

Aşağıdaki tabloyu direk olarak
Oracle resmi sayfasından bulabilirsiniz.

protected  int next(int bits)
Generates the next pseudorandom number.
 boolean nextBoolean()
Returns the next pseudorandom, uniformly distributed boolean value from this random number generator’s sequence.
 void nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array.
 double nextDouble()
Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence.
 float nextFloat()
Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator’s sequence.
 double nextGaussian()
Returns the next pseudorandom, Gaussian (“normally”) distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator’s sequence.
 int nextInt()
Returns the next pseudorandom, uniformly distributed int value from this random number generator’s sequence.
 int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequence.
 long nextLong()
Returns the next pseudorandom, uniformly distributed long value from this random number generator’s sequence.
 void setSeed(long seed)
Sets the seed of this random number generator using a single long seed.

Tabloyu bilerek türkçeleştirmedim arkadaşlar birazda ingilizceyle haşır neşir olun istiyorum benim gibi.Zaten Java’da türkçe kaynak bulmak samanlıkta birilerini basmak kadar zor. Neyse  şimdi  zar atma oyunumuzun kodlarına gelelim

</pre>
package org.Hasanunlukilinc.com.Test;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

public class IkiZar extends JFrame
{
 private static final long serialVersionUID = 1L;

// Genel Değşkenler
 JPanel mcontentPane;
 static JTextField txtf_userDie;
 static JTextField txtf_cpuDie;
 static JTextField txtf_score;
 static JButton btn_rollDie;
 static Random random;

static int die1;

static int die2;

public static void main(String[] args)
 {
 EventQueue.invokeLater(new Runnable()
 {
 public void run()
 {
 try
 {
 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 IkiZar frame = new IkiZar();
 frame.setVisible(true);
 }
 catch (UnsupportedLookAndFeelException e)
 {
 // Hatayı ele al
 }
 catch (ClassNotFoundException e)
 {
 // Hatayı ele al
 }
 catch (InstantiationException e)
 {
 // Hatayı ele al
 }
 catch (IllegalAccessException e)
 {
 // Hatayı ele al
 }

catch (Exception e)
 {
 e.printStackTrace();
 }
 }
 });
 }

public IkiZar()
 {
 setTitle("İki Zar");
 setResizable(false);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setBounds(100, 100, 218, 159);
 mcontentPane = new JPanel();
 mcontentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 setContentPane(mcontentPane);
 mcontentPane.setLayout(null);

btn_rollDie = new JButton("Zar At");
 btn_rollDie.addActionListener(new ActionListener()
 {
 public void actionPerformed(ActionEvent arg0)
 {
 //Zar Atma

RollDie();
 }
 });
 btn_rollDie.setBounds(62, 100, 89, 23);
 mcontentPane.add(btn_rollDie);

txtf_userDie = new JTextField();
 txtf_userDie.setBounds(20, 41, 37, 20);
 mcontentPane.add(txtf_userDie);
 txtf_userDie.setColumns(10);

txtf_cpuDie = new JTextField();
 txtf_cpuDie.setColumns(10);
 txtf_cpuDie.setBounds(148, 41, 37, 20);
 mcontentPane.add(txtf_cpuDie);

txtf_score = new JTextField();
 txtf_score.setBounds(10, 69, 192, 20);
 mcontentPane.add(txtf_score);
 txtf_score.setColumns(10);

JLabel lblSizinZarnz = new JLabel("Sizin Zarınız");
 lblSizinZarnz.setBounds(10, 16, 75, 14);
 mcontentPane.add(lblSizinZarnz);

JLabel lblCpuZar = new JLabel("CPU Zarı");
 lblCpuZar.setBounds(138, 16, 75, 14);
 mcontentPane.add(lblCpuZar);

JLabel lblSonu = new JLabel("Sonuç");
 lblSonu.setBounds(85, 55, 46, 14);
 mcontentPane.add(lblSonu);
 }

public static void RollDie()
 {
 //Rastgele sistemi Oluşturuldu
 random = new Random();

//die1 ve die2 integer değişkenlerine değişkenleri atandı
 //+1 Sebebi 0 gelmesin diye
 die1 = random.nextInt(6) + 1;
 die2 = random.nextInt(6) + 1;

// die1 ve die2 değişkenleri String'e çevirilip textFieldlere atandı.
 txtf_userDie.setText(Integer.toString(die1));
 txtf_cpuDie.setText(Integer.toString(die2));

if (die1 == die2)
 txtf_score.setText("Zarlar Eşit - Tekrar Deneyin");
 else if (die1 > die2)
 txtf_score.setText("Büyük Attınız Tebikler, Kazandınız!");
 else
 txtf_score.setText("Küçük Attınız, Kaybettiniz'");
 }
}
<pre>

About Post Author


Tags: , , , , , , ,


One Comment

  1. Samet :

    Ben 2 zarın rastgele atılıp çift gelmesini sağlayan java kodlarını yazmanızı istiyorum

Leave a Reply