Java Program
Java Program
In our childhood, nearly all of us enjoyed playing classic snake games. Now
we will try to enhance it with the help of Java concepts. The concept
appears to be easy but it is not that effortless to implement.
The Snake will have the ability to move in all four directions.
The snake’s length grows as it eats food.
When the snake crosses itself or strikes the perimeter of the box, the
game is marked over.
Food is always given at different positions.
src/com/zetcode/Board.java
package com.zetcode;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public Board() {
initBoard();
}
addKeyListener(new TAdapter());
setBackground(Color.black);
setFocusable(true);
dots = 3;
locateApple();
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
if (inGame) {
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
}
dots++;
locateApple();
}
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void checkCollision() {
if (y[0] < 0) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
src/com/zetcode/Snake.java
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
public Snake() {
initUI();
}
add(new Board());
setResizable(false);
pack();
setTitle("Snake");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
EventQueue.invokeLater(() -> {
JFrame ex = new Snake();
ex.setVisible(true);
});
}
}
2.PASSWORD GENERATOR USING JAVA
Password Generator/src/Alphabet.java
if (uppercaseIncluded) pool.append(UPPERCASE_LETTERS);
if (lowercaseIncluded) pool.append(LOWERCASE_LETTERS);
if (numbersIncluded) pool.append(NUMBERS);
if (specialCharactersIncluded) pool.append(SYMBOLS);
Password Generator/src/Generator.java
import java.util.Objects;
import java.util.Scanner;
while (!userOption.equals("4")) {
userOption = keyboard.next();
switch (userOption) {
case "1" -> {
requestPassword();
printMenu();
}
case "2" -> {
checkPassword();
printMenu();
}
case "3" -> {
printUsefulInfo();
printMenu();
}
case "4" -> printQuitMessage();
default -> {
System.out.println();
System.out.println("Kindly select one of the available commands");
printMenu();
}
}
}
}
boolean correctParams;
System.out.println();
System.out.println("Hello, welcome to the Password Generator :) answer"
+ " the following questions by Yes or No \n");
do {
String input;
correctParams = false;
do {
System.out.println("Do you want Lowercase letters \"abcd...\" to be used? ");
input = keyboard.next();
PasswordRequestError(input);
} while (!input.equalsIgnoreCase("yes") && !input.equalsIgnoreCase("no"));
do {
System.out.println("Do you want Uppercase letters \"ABCD...\" to be used? ");
input = keyboard.next();
PasswordRequestError(input);
} while (!input.equalsIgnoreCase("yes") && !input.equalsIgnoreCase("no"));
do {
System.out.println("Do you want Numbers \"1234...\" to be used? ");
input = keyboard.next();
PasswordRequestError(input);
} while (!input.equalsIgnoreCase("yes") && !input.equalsIgnoreCase("no"));
do {
System.out.println("Do you want Symbols \"!@#$...\" to be used? ");
input = keyboard.next();
PasswordRequestError(input);
} while (!input.equalsIgnoreCase("yes") && !input.equalsIgnoreCase("no"));
} while (correctParams);
System.out.println(p.calculateScore());
}
import org.junit.jupiter.api.Test;
class GeneratorTest {
@Test
void test1() {
assertEquals("Secret", password.toString());
}
@Test
void test2() {
assertEquals(firstAlphabet.getAlphabet(), Alphabet.UPPERCASE_LETTERS);
}
@Test
void test3() {
assertEquals(secondAlphabet.getAlphabet(),
Alphabet.LOWERCASE_LETTERS + Alphabet.NUMBERS + Alphabet.SYMBOLS);
}
@Test
void test4() {
assertEquals(generator.alphabet.getAlphabet(),
Alphabet.UPPERCASE_LETTERS);
}
@Test
void test5() {
assertEquals(generator.alphabet.getAlphabet().length(), 26);
}
Password Generator/src/Main.java
import java.util.Scanner;
Password Generator/src/Password.java
public Password(String s) {
Value = s;
Length = s.length();
}
// Char is Digit
else if ((int) C >= 60 && (int) C <= 71) {
val = 3;
}
// Char is Symbol
else {
val = 4;
}
return val;
}
if (UsedUpper) Score += 1;
if (UsedLower) Score += 1;
if (UsedNum) Score += 1;
if (UsedSym) Score += 1;
return Score;
}
if (Score == 6) {
return "This is a very good password :D check the Useful Information section to
make sure it satisfies the guidelines";
} else if (Score >= 4) {
return "This is a good password :) but you can still do better";
} else if (Score >= 3) {
return "This is a medium password :/ try making it better";
} else {
return "This is a weak password :( definitely find a new one";
}
}
@Override
public String toString() {
return Value;
}
}