Arrow Sorrow Game using multiple java concepts

oops concepts in java pdf
oops concepts in java with realtime examples
oop principles java
object oriented programming examples
all oops concepts in one program in java
oops concepts tutorialspoint
oops full form
object-oriented programming examples java

This is my code for a simple arrow sequencing application.

It generates a random sequence of size 4 which comprise of UP, DOWN, LEFT and/or RIGHT arrow keys and displays them one at a time. If they user repeats the sequence correctly, it displays another sequence of 5 keys. The size of the sequence keeps incrementing as long as the user enters the correct sequence but decrements if an invalid sequence has been entered.

The problem I'm encountering is, the first execution is flawless, it even displays the sequence the second time, but it doesn't accept my keyboard inputs during the second iteration.

For a better understanding of the problem I have broken it down into the main action performing blocks and the full code as well at the end.

Sequence Generation

for(int flag=1;flag<size;flag++)
    {
    random = randomGenerator.nextInt(4);
    generated[flag]=random;
    setVisible(true);
    switch(random)
        {
            case 0:
            left();
            break;
            case 1:
            up();
            break;
            case 2:
            right();
            break;
            case 3:
            down();
            break;
        }
        delaybig();     
    }

User Input Sequence

public void keyPressed(KeyEvent e)
{
    if(cflag<=size) 
    {
        if(e.getKeyCode()==37)
            {
                entered[cflag]=0;
                cflag++;
                Keys.setText("LEFT");
                left();
            }
        else if(e.getKeyCode()==38)
            {
                entered[cflag]=1;
                cflag++;
                Keys.setText("UP");
                up();
            }
        else if(e.getKeyCode()==39)
            {
                entered[cflag]=2;
                cflag++;
                Keys.setText("RIGHT");
                right();
            }
        else if(e.getKeyCode()==40)
            {
                entered[cflag]=3;
                cflag++;
                Keys.setText("DOWN");
                down();
            }
    else
        {
            Keys.setText("INVALID");
        }
    }

Sequence Comparision using Arrays.equals

if(cflag==size)
    {
        boolean check = Arrays.equals(generated, entered);
        if(check)
            {
                delaysmall();
                Keys.setText("PERFECT");
                size++;
                cflag=1;
                delaysmall();
                Keys.setText("GO AGAIN");
                delaybig();
                restart();  
            }
        else
            {
                delaysmall();
                Keys.setText("FAILED");
                if(size>5)
                    {
                        delaybig();
                        restart();
                        size--;
                        cflag=1;
                    }
            }
    }

Looping Thread

public void restart() 
{ 
    Thread goagain =  new Thread() 
    {
        @Override
        public void run()
             {
                launchframe();
             }
        };
        goagain.start();
}

I've spent quite some time on this to no avail so here is the full code just incase you suspect the error likes elsewhere.

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Arrays;

class ArrowSorrow extends Frame implements KeyListener{

int flag;
Image img;
int random;
int cflag=1,size=5;
boolean ShowImage=true;
int entered[]=new int[50];
int generated[]=new int[50];
TextField Keys=new TextField(8);
Random randomGenerator = new Random();
MediaTracker mt = new MediaTracker(this);

public ArrowSorrow(String title)
    {
        super(title);
    }

public void restart() 
    { 
        // Create a new thread
    Thread goagain =  new Thread() 
    {
        // Override run() to provide the running behavior of this thread.
        @Override
        public void run()
             {
                launchframe();
             }
        };
        goagain.start();
}
public void launchframe()
{
    for(int flag=1;flag<1;flag++)
        generated[flag]=0;
    for(int flag=1;flag<1;flag++)
        entered[flag]=0;
            splash();
    add(Keys);
            delaybig();
    setSize(400,400);
    Keys.setEditable(false);
    Keys.setText("MEMORIZE");
    Keys.addKeyListener(this);      
    setBackground(Color.black);
    setLayout(new FlowLayout());
for(int flag=1;flag<size;flag++)
        {
        random = randomGenerator.nextInt(4);
        generated[flag]=random;
        setVisible(true);
        switch(random)
            {
                case 0:
                left();
                break;
                case 1:
                up();
                break;
                case 2:
                right();
                break;
                case 3:
                down();
                break;
            }
            delaybig();     
        }
        String sequence=new String("");
        for(flag=1;flag<size;flag++)
            {
                sequence=sequence+(Integer.toString(generated[flag]));
            }
            Keys.setText(sequence);
            delaysmall();
            Keys.setText("REPEAT");
            delaysmall();
            setVisible(true);

            addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent we)
                        {
                            dispose();
                        }
                });
    }
public void splash()
    {
    img = ToolKit.getDefaultToolkit().getImage("image address for splashscreen.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}

public void left()
{
    img = Toolkit.getDefaultToolkit().getImage("image address for left.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}

public void right()
{
    img = Toolkit.getDefaultToolkit().getImage("image address for right.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}

public void up()
{
    img = Toolkit.getDefaultToolkit().getImage("image address for up.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}

public void down()
{
    img = Toolkit.getDefaultToolkit().getImage("image address down.jpg");
    mt.addImage(img,0);
    repaint();
    setVisible(true);
}
//minor delay
public void delaysmall()
{
    try 
        {
            Thread.sleep(600);
        } 
    catch(InterruptedException ex) 
        {
            Thread.currentThread().interrupt();
        }   
}
//major delay
public void delaybig()
{
    try 
        {
            Thread.sleep(1200);
        } 
    catch(InterruptedException ex) 
        {
            Thread.currentThread().interrupt();
        }   
}

public void paint(Graphics g)
{
super.paint(g); 
if(img != null)
    g.drawImage(img,70,70, this);
else
    g.clearRect(0, 0, getSize().width, getSize().height);
}

public void keyPressed(KeyEvent e)
{
    if(cflag<size)  
    {
        if(e.getKeyCode()==37)
            {
                entered[cflag]=0;
                cflag++;
                Keys.setText("LEFT");
                left();
            }
        else if(e.getKeyCode()==38)
            {
                entered[cflag]=1;
                cflag++;
                Keys.setText("UP");
                up();
            }
        else if(e.getKeyCode()==39)
            {
                entered[cflag]=2;
                cflag++;
                Keys.setText("RIGHT");
                right();
            }
        else if(e.getKeyCode()==40)
            {
                entered[cflag]=3;
                cflag++;
                Keys.setText("DOWN");
                down();
            }
    else
        {
            Keys.setText("INVALID");
        }
    }
//comparing generated sequence and user input sequence      
if(cflag==size)
    {
        boolean check = Arrays.equals(generated, entered);
        if(check)
            {
                delaysmall();
                Keys.setText("PERFECT");
                size++;
                cflag=1;
                delaysmall();
                Keys.setText("GO AGAIN");
                delaybig();
                restart();  
            }
        else
            {
                delaysmall();
                Keys.setText("FAILED");
                if(size>5)
                    {
                        delaybig();
                        restart();
                        size--;
                        cflag=1;
                    }
            }
    }
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}

class ArrowSorrowLaunch{
public static void main(String args[])
    {
        ArrowSorrow instance=new ArrowSorrow("Arrow Sorrow");
        instance.launchframe();
    }
}

You call launchFrame to start each iteration of the game. However, you are doing a lot of work in launchFrame that should be done only once. You should move that initialization code out of launchFrame and do it only once. In particular you should not repeatedly call Keys.addKeyListener(this); or add multiple window listeners.

Castlevania: Dawn of Sorrow, The game is the sequel to Castlevania: Aria of Sorrow and incorporates many Players can trade souls wirelessly using two Dawn of Sorrow game cards.

  1. change Thread.sleep(int) to Swing Timer, otherwise you'll bothering with Concurency in Swing

  2. Thread.sleep(int) freeze Swing GUI until ended, during this sleep any Mouse or Key events aren't dispatched or consumed

  3. don't paint directly to the JFrame, put there JPanel or JComponent

  4. for JPanel or JComponent have to use paintComponent instead of paint()

  5. don't use KeyListener, use Keybindings instead, otherwise have to setFocusable()

6 OOP Concepts in Java with examples [2020] · Raygun Blog, In Java, polymorphism can take two forms: method overloading and method overriding. Method overloading happens when various methods with 

Solution by OP.

Fix, thanks to Ted Hopp.

Created this new method by moving them out of launchframe()

void oneTime()
{
    add(Keys);
    setSize(400,400);
    Keys.requestFocus();
    Keys.setEditable(false);
    Keys.setText("MEMORIZE");
    Keys.addKeyListener(this);      
    setBackground(Color.black);
    setLayout(new FlowLayout());
    launchframe();
}

Replaced

    instance.launchframe();//this
    instance.oneTime();//with this in main

Game Review – Castlevania Aria Of Sorrow : Mobile phone and , Welcome to Castlevania Aria Of Sorrow game review. After a bit of research on the original game we found that the JAVA version for mobile phones uses the Note that the two games are completely different in concept.

Object Oriented Programming (OOPs) Concept in Java , Mathematical Algorithms · Randomized Algorithms · Game Theory · Greedy Algorithms Object Oriented Programming (OOPs) Concept in Java This is done by Java with the help of the signature and declaration of these entities. For example: filter_none. edit close. play_arrow This sum takes two double parameters.

All 2048, Use your arrow keys or swipe to combine similar Doges and score points! Unlock all 11 by kpcourt. But anger is never without sorrow, since, as stated in Ethic. When two tiles with the same number touch, they merge into one!. Combine the That's a mystery that you can solve in this wacky and totally wild puzzle game.

[PDF] Object-Oriented Design Concepts via Playing Cards, students and then asking them to write programs that play games with cards. questions to test and review concepts ranging from Java syntax to problem- isPair that returns true if its two ICard parameters represent a pair and returns false.

Comments
  • Welcome to Stack Overflow. Remember, its not good to post huge blocks of code. Would you read if someone else posted it?
  • @AshishCH - Good to hear that it's working. Sorry about the extra "either" in my answer (now removed); I'm glad that you managed to understand what I was trying to say regardless. :)
  • Thanks a lot for the almost instantaneous help. I realized how much there is to learn by this simple error that I commited. I put all the one time declarations in a new method and called launchframe once in the new method and it works brilliantly. +rep.