Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hangman implemented as GUI #20

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ target/
.idea/
*.iml
.DS_Store
.classpath
.project
.settings
//.classpath
//.project
//.settings
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,79 @@ Then, run:
$ java -classpath target/classes hangman.Main
```



## 17/12/06 Code fixed by [GradeScholarship](https://github.com/GradeScholarship/hangman)

### Contents

1. implement GUI by using javax.swing

Start panel:

<img src="https://github.com/GradeScholarship/hangman/blob/master/images/capture1.PNG" width="250px" height="200px"/>

Game panel:

<img src="https://github.com/GradeScholarship/hangman/blob/master/images/capture2.PNG" width="250px" height="200px"/>

If user makes mistake:

<img src="https://github.com/GradeScholarship/hangman/blob/master/images/capture3.PNG" width="250px" height="200px"/>

If user makes a hit:

<img src="https://github.com/GradeScholarship/hangman/blob/master/images/capture4.PNG" width="250px" height="200px"/>

Continue menu:

<img src="https://github.com/GradeScholarship/hangman/blob/master/images/capture5.PNG" width="250px" height="200px"/>

How To Play Panel:

<img src="https://github.com/GradeScholarship/hangman/blob/master/images/capture6.PNG" width="250px" height="200px"/>


2. now, the picture changes if the user enters wrong letter

3. exec( ) problem fixed ( by implementing thread)

4. add more words

5. change words

6. now, the program imports word from Words.txt ( FIO )

### execution environment

Windows = available

OSX = available

Linux = didn't tried

etc.. = didn't tried

executing program with this method is right :

First, build it:

```
$ mvn clean package
```

Then, run:

```
$ java -classpath target/classes hangman.Main
```

but using [Eclipse oxygen](https://projects.eclipse.org/releases/oxygen) is easier for beginner





## It is not object-oriented

This Java code is very procedural, imperative and not elegant at all.
Expand Down
Binary file added images/capture1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/capture2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/capture3.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/capture4.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/capture5.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/capture6.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
202 changes: 202 additions & 0 deletions src/GamePanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package gui;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

@SuppressWarnings("serial")
public class GamePanel extends JPanel implements Runnable {

private int max;
String word = new String();
private static ArrayList<String> Words;
boolean[] visible;
boolean hit = false;
int mistakes = 0;

private PrimaryPanel primary;
// component ����
private JLabel wordBox;
private JLabel info;
private JTextField answer;
private JLabel message;
private JLabel lblResult,lblImage;
private ImageIcon winImage, loseImage,gamingImage[];

public GamePanel(PrimaryPanel p) {

setPreferredSize(new Dimension(450, 400));
setBackground(Color.white);
setLayout(null);

primary = p;
// game status set
max = 10;
try {
Words = readWords();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
word = Words.get(new Random().nextInt(Words.size()));
visible = new boolean[word.length()]; // �ܾ ������������ ����
gamingImage = new ImageIcon[10];
gamingImage[0]= new ImageIcon("./images/hangman1.png");
gamingImage[1]= new ImageIcon("./images/hangman2.png");
gamingImage[2]= new ImageIcon("./images/hangman3.png");
gamingImage[3]= new ImageIcon("./images/hangman4.png");
gamingImage[4]= new ImageIcon("./images/hangman5.png");
gamingImage[5]= new ImageIcon("./images/hangman6.png");
gamingImage[6]= new ImageIcon("./images/hangman7.png");
gamingImage[7]= new ImageIcon("./images/hangman8.png");
gamingImage[8]= new ImageIcon("./images/hangman9.png");
gamingImage[9]= new ImageIcon("./images/hangman10.png");

winImage =new ImageIcon("Images/hangmanLose.jpg");
loseImage = new ImageIcon("Images/hangman.png");
lblImage = new JLabel(gamingImage[0]);
lblImage.setVisible(true);
lblImage.setBounds(260, 50, 150, 150);
add(lblImage);

// component
message = new JLabel("input...");
message.setFont(new Font("Serif", Font.PLAIN, 15));
wordBox = new JLabel();
wordBox.setFont(new Font("Serif", Font.PLAIN, 15));
info = new JLabel("Guess a letter: ");
answer = new JTextField();
answer.setDocument(new JTextFieldLimit(1));
answer.setHorizontalAlignment(SwingConstants.CENTER);
answer.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 25));

answer.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
for (int i = 0; i < word.length(); ++i) {
if (word.charAt(i) == answer.getText().charAt(0) && !visible[i]) {
visible[i] = true;
hit = true;
}
}
if (hit) {
message.setText("Hit!\n");
hit = !hit;
}
else {
++mistakes;
lblImage.setIcon(gamingImage[mistakes]);
message.setText("\"Missed, mistake "+ mistakes + " out of "+ max);
}
answer.setText("");
}
}
});
answer.setBounds(140, 50, 30, 30);
info.setBounds(50, 50, 100, 30);
wordBox.setBounds(50, 90, 200, 30);
message.setBounds(50, 140, 150, 30);

add(message);
add(info);
add(wordBox);
add(answer);
}

@Override
public void run() {


StringBuffer str = new StringBuffer();
for(int i=0; i<word.length(); i++) {
str.append("_ ");
}
wordBox.setText(str.toString());
boolean done = true;
while (mistakes < this.max) { // �Ǽ��� �ִ밪�� ���� �ʴ� ����
done = true;
for (int i = 0; i < word.length(); ++i) { // ����� �� �ܾ� ���� �� ��ŭ ����
if (!visible[i]) { // ������ ���� ���� ���ڰ� �ִٸ�
done = false;
}
}
if (done) { // ���ڵ��� ��� ������ �ƴٸ� ����
break;
}

str = new StringBuffer("The word: ");
for (int i = 0; i < word.length(); ++i) {
if (visible[i]) {
str.append(word.charAt(i));
}
else {
str.append("_ ");
}
}
wordBox.setText(str.toString());
}
if (done) {
message.setText("You won!");
str = new StringBuffer("The word: ");
for (int i = 0; i < word.length(); ++i) {
if (visible[i]) {
str.append(word.charAt(i));
}
else {
str.append("_ ");
}
}
wordBox.setText(str.toString());
// ���� ���� ȭ��
answer.setEnabled(false);
lblImage.setIcon(winImage);
continueGame();

}
else {
message.setText("You lost.");
// ���� ���� ȭ��
answer.setEnabled(false);
lblImage.setIcon(loseImage);
continueGame();
}
}
public void continueGame() {
int result;

result = JOptionPane.showConfirmDialog(this, "CONTINUE?","Confirm",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE);

if(result == JOptionPane.YES_OPTION) {


}else if(result == JOptionPane.NO_OPTION) {
System.exit(0);
}
}

public static ArrayList<String> readWords() throws IOException {
String path = GamePanel.class.getResource("").getPath();
BufferedReader br = new BufferedReader(new FileReader(path + "Words.txt"));
ArrayList<String> L = new ArrayList<String>();
while(true) {
String line = br.readLine();
if (line==null) break;
L.add(line);
}
br.close();

return L;
}
}
71 changes: 71 additions & 0 deletions src/HowToPlayPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class HowToPlayPanel extends JPanel {

private JLabel lblTitle,lblFirst,lblSecond,lblThird,lblForth;
private JButton btnBack;
private Font fnt;
private PrimaryPanel primary;
private ButtonListener btnL;

public HowToPlayPanel(PrimaryPanel p) {

setPreferredSize(new Dimension(400,400));
setBackground(Color.white);
setLayout(null);

primary = p;

fnt = new Font("Consolas",Font.BOLD,15);

btnL = new ButtonListener();

lblTitle = new JLabel("GAME RULES");
lblTitle.setFont(new Font("consolas",Font.BOLD,40));
lblTitle.setBounds(75,30,250,40);
add(lblTitle);

lblFirst = new JLabel("1. Guess and Write a letter");
lblFirst.setFont(fnt);
lblFirst.setBounds(20,100,370,40);
add(lblFirst);

lblSecond = new JLabel("<html>2. If the letter is in a word, <br>you can know the position of the letter<br></html>");
lblSecond.setFont(fnt);
lblSecond.setBounds(20,160,370,40);
add(lblSecond);

lblThird = new JLabel("<html>3. If you do not finish within a limited<br>number, you will be defeated.<br></html>");
lblThird.setFont(fnt);
lblThird.setBounds(20,220,370,40);
add(lblThird);

lblForth = new JLabel("4. You win if you match the word");
lblForth.setFont(fnt);
lblForth.setBounds(20,280,370,40);
add(lblForth);

btnBack = new JButton("<<BACK");
btnBack.setBounds(250,320,90,30);
btnBack.setBackground(Color.white);
btnBack.setFont(new Font("consolas",Font.BOLD,13));
btnBack.addActionListener(btnL);

add(btnBack);
}

private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event) {

Object obj = event.getSource();

if (obj ==btnBack) {
primary.setVisibleStarPanel();
}

}
}
}
32 changes: 32 additions & 0 deletions src/JTextFieldLimit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gui;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class JTextFieldLimit extends PlainDocument {

private int limit;
private boolean toUppercase = false;
public JTextFieldLimit(int limit) {
super();
this.limit = limit;
}

public JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
this.toUppercase = upper;
}

public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if(str == null)
return;
if((getLength() + str.length()) <= limit) {
if(toUppercase) {
str = str.toUpperCase();
}
super.insertString(offset, str, attr);
}
}

}
Loading