-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay45s1.java
More file actions
138 lines (114 loc) · 4.8 KB
/
Day45s1.java
File metadata and controls
138 lines (114 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Day45s1 {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SayiTahminOyunu oyun = new SayiTahminOyunu();
oyun.baslat();
});
}
}
class SayiTahminOyunu extends JFrame {
private static final int PENCERE_GENISLIK = 400;
private static final int PENCERE_YUKSEKLIK = 500;
private static final int MIN_SAYI = 1;
private static final int MAX_SAYI = 100;
private int hedefSayi;
private int tahminSayisi;
private Random random;
private JTextField tahminField;
private JTextArea sonucArea;
private JButton tahminButton;
private JButton yeniOyunButton;
private JLabel ipucuLabel;
public SayiTahminOyunu() {
random = new Random();
tahminSayisi = 0;
setTitle("Sayı Tahmin Oyunu");
setSize(PENCERE_GENISLIK, PENCERE_YUKSEKLIK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
// Ana panel
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Üst panel
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
tahminField = new JTextField(10);
tahminField.setFont(new Font("Arial", Font.PLAIN, 20));
tahminField.addActionListener(e -> tahminYap());
tahminButton = new JButton("Tahmin Et");
tahminButton.setFont(new Font("Arial", Font.BOLD, 14));
tahminButton.addActionListener(e -> tahminYap());
yeniOyunButton = new JButton("Yeni Oyun");
yeniOyunButton.setFont(new Font("Arial", Font.BOLD, 14));
yeniOyunButton.addActionListener(e -> yeniOyun());
topPanel.add(new JLabel("Tahmininiz: "));
topPanel.add(tahminField);
topPanel.add(tahminButton);
topPanel.add(yeniOyunButton);
// Orta panel
sonucArea = new JTextArea(10, 30);
sonucArea.setEditable(false);
sonucArea.setFont(new Font("Arial", Font.PLAIN, 16));
sonucArea.setLineWrap(true);
sonucArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(sonucArea);
// Alt panel
ipucuLabel = new JLabel("1 ile 100 arasında bir sayı seçildi!");
ipucuLabel.setFont(new Font("Arial", Font.BOLD, 16));
ipucuLabel.setHorizontalAlignment(SwingConstants.CENTER);
// Panelleri ana panele ekle
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(ipucuLabel, BorderLayout.SOUTH);
add(mainPanel);
// Yeni oyun başlat
yeniOyun();
}
public void baslat() {
setVisible(true);
}
private void yeniOyun() {
hedefSayi = random.nextInt(MAX_SAYI - MIN_SAYI + 1) + MIN_SAYI;
tahminSayisi = 0;
tahminField.setText("");
sonucArea.setText("");
ipucuLabel.setText("1 ile 100 arasında bir sayı seçildi!");
tahminField.setEnabled(true);
tahminButton.setEnabled(true);
tahminField.requestFocus();
}
private void tahminYap() {
try {
int tahmin = Integer.parseInt(tahminField.getText());
if (tahmin < MIN_SAYI || tahmin > MAX_SAYI) {
sonucArea.append("Lütfen " + MIN_SAYI + " ile " + MAX_SAYI + " arasında bir sayı girin!\n");
return;
}
tahminSayisi++;
if (tahmin == hedefSayi) {
sonucArea.append("Tebrikler! " + tahminSayisi + " denemede doğru sayıyı buldunuz!\n");
ipucuLabel.setText("Oyun bitti! Yeni oyun başlatmak için 'Yeni Oyun' butonuna tıklayın.");
tahminField.setEnabled(false);
tahminButton.setEnabled(false);
} else if (tahmin < hedefSayi) {
sonucArea.append(tahmin + " sayısı hedef sayıdan KÜÇÜK!\n");
ipucuLabel.setText("Daha büyük bir sayı deneyin!");
} else {
sonucArea.append(tahmin + " sayısı hedef sayıdan BÜYÜK!\n");
ipucuLabel.setText("Daha küçük bir sayı deneyin!");
}
tahminField.setText("");
tahminField.requestFocus();
} catch (NumberFormatException e) {
sonucArea.append("Lütfen geçerli bir sayı girin!\n");
tahminField.setText("");
tahminField.requestFocus();
}
}
}