Uploaded by William Smith

AungSoeKhine OOP Assignment

advertisement
Higher National Unit Specification: General Information
Statement and Confirmation of Own Work
Unit Title:
Software Development: Object Oriented Programming
Unit code:
H171 35
Superclass:
CB
Publication Date:
February 2018
Source:
Scottish Qualifications Authority
Version:
01
Student declaration
I have read and understood SQA HND’s Policy on Academic Dishonesty and Plagiarism.
I can confirm the following details:
Student ID/Registration number :
Name
: Aung Soe Khine
Centre Name
: KMD Computer Centre (Yangon)
Assessor’s Name
: SA THAR HTUT NYAN
Student Signature
:
Submitted Date
: 8 March 2017
Contents
Introduction .................................................................................................................................................. 3
Analysis ..................................................................................................................................................... 3
Assumption ............................................................................................................................................... 3
Outcomes 1 Implementation ........................................................................................................................ 3
Design Documentation ............................................................................................................................. 3
Use Case ................................................................................................................................................ 4
Use Case Description ............................................................................................................................ 4
Detail Class Diagram ............................................................................................................................. 6
How to program operates......................................................................................................................... 8
Encapsulation ........................................................................................................................................ 8
Coupling and Cohesion ....................................................................................................................... 11
Outcomes 2 Working Solution .................................................................................................................... 24
User Interfaces ........................................................................................................................................ 24
Classes Involved ...................................................................................................................................... 26
Pre-Defined Classes............................................................................................................................. 26
User-Defined Classes........................................................................................................................... 26
Outcomes 3 Testing .................................................................................................................................... 27
Test Plan .................................................................................................................................................. 27
Test Scripts .............................................................................................................................................. 27
Reference .................................................................................................................................................... 28
Introduction
Analysis
Outcome 1 ka br khine lal
Usecase diagram swal khine
Outcome 2 ka br khine lal
Assumption
Bl lol logic nae bl lol swal ya lal
Bl lol yay kae lal
Outcome 1,2,3 ka khine kae tr ko bl lol yay kae lal
Outcomes 1 Implementation
Design Documentation
Use Case Diagram
Sokoban Game
Add Level
Create Map
Push Crate
Control Actor
Player
Sokoban
Record Moves
Display Result
Use Case Description
1. Add Level
Usecase: Add Level
Actor
: System of the game
Def
: Edit New Level
Add Level is the formal process of building options and editing new level to the game if
the admin wishes to add more levels to make it more fun.
2. Create Map
Usecase: Create Map
Actor
: System of the game
Def
: Build the map of game
Create Map is a discipline of game development involving creation of locales, stages or
missions. This is commonly done using a game development software.
3. Push Crate
Usecase: Push Crate
Actor
: Player
Def
: move the crate to the diamond
Push Crate is the process of moving the crates from the start point to the diamond in order
to win the level.
4. Control Actor
Usecase: Move Actor
Actor
: Player
Def
: control the actor by using arrow key
Control Actor is the process of moving the actor in order to push the crate to the diamond.
5. Record Moves
Usecase: Record Moves
Actor
: System of the game
Def
: record the actions of the game
Record moves is the formal process of recording the actions of characters and stages by
the system of the game.
6. Display Result
Usecase: View Result
Actor
: System of the game
Def
: show the player’s result
Display Result is the process of showing the consequences of player’s resolution. This is
commonly done by the system of the game.
Detail Class Diagram
How to program operates
Encapsulation
package sokoban_aungsoekhine;
import java.awt.*;
/**
*
* @author ice-pc
*/
public class Movement
{
private int space=30;
private int x;
private int y;
private Image image;
public int getSpace() {
return space;
}
public void setSpace(int space) {
this.space = space;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public Movement(int x, int y)
{
this.x=x;
this.y=y;
}
public boolean isleft(Movement actor)
{
if((this.getX()-space==actor.getX())&&(this.getY()==actor.getY()))
{
return true;
}
else
{
return false;
}
}
public boolean isright(Movement actor)
{
if((this.getX()+space==actor.getX())&&(this.getY()==actor.getY()))
{
return true;
}
else
{
return false;
}
}
public boolean istop(Movement actor)
{
if((this.getY()-space==actor.getY())&&(this.getX()==actor.getX()))
{
return true;
}
else
{
return false;
}
}
public boolean isbottom(Movement actor)
{
if((this.getY()+space==actor.getY())&&(this.getX()==actor.getX()))
{
return true;
}
else
{
return false;
}
}
}
Coupling and Cohesion
package sokoban_aungsoekhine;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
/**
*
* @author ice-pc
*/
public class Main extends JFrame
{
JLabel lblTitle,lblLevel,lblRule,lblCount;
JLabel lblActor,lblWall,lblCrate,lblDiamond;
JLabel lblBoradImage;
private ArrayList alWall=new ArrayList();
private ArrayList alArea=new ArrayList();
private ArrayList alCrate=new ArrayList();
private Actor ASK_Actor;
private boolean completestatus=false;
String CompleteLevel="";
private final int OFFSET=180;
private final int SPACE=30;
Wall objWall;
Crate objCrate;
Diamond objDiamond;
Map m=new Map();
private int width=0;
private int height=0;
private int movecount=0;
private final int LEFT_COLLISION=1;
private final int RIGHT_COLLISION=2;
private final int TOP_COLLISION=3;
private final int BOTTOM_COLLISION=4;
JPanel pnlBoard;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main m=new Main();
m.nextlevel();
}
public Main()
{
this.setBounds(100, 50, 700, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.getContentPane().setBackground(Color.LIGHT_GRAY);
pnlBoard=new JPanel();
pnlBoard.setLayout(null);
pnlBoard.setBounds(0,100,700,500);
pnlBoard.setBackground(Color.CYAN);
this.add(pnlBoard);
lblTitle=new JLabel("SOKOBAN_PUZZEL_GAME");
lblTitle.setBounds(20,40,200,20);
this.add(lblTitle);
CompleteLevel=m.Level_1;
lblLevel=new JLabel("Level 1");
RunGameBoard(CompleteLevel);
lblLevel.setBounds(240,40,100,20);
this.add(lblLevel);
lblCount=new JLabel("Count");
lblCount.setBounds(360,40,100,20);
this.add(lblCount);
lblRule=new JLabel("Use Arrow Key");
lblRule.setBounds(480,40,200,20);
this.add(lblRule);
ASK_Actor=new Actor(30,30);
lblActor=new JLabel(new ImageIcon(ASK_Actor.getImage()));
lblActor.setSize(30,30);
Wall w=new Wall(30,30);
lblWall=new JLabel(new ImageIcon(w.getImage()));
lblWall.setSize(30,30);
Diamond d=new Diamond(30,30);
lblDiamond=new JLabel(new ImageIcon(d.getImage()));
lblDiamond.setSize(30,30);
Crate c=new Crate(30,30);
lblCrate=new JLabel(new ImageIcon(c.getImage()));
lblCrate.setSize(30,30);
lblBoradImage=new JLabel();
pnlBoard.add(lblBoradImage);
pnlBoard.addKeyListener(new MovementKeyAdapter());
pnlBoard.setFocusable(true);
this.setVisible(true);
}
@Override
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D) g;
super.paint(g2d);
buildLevel_Map(g2d);
}
private void buildLevel_Map(Graphics2D g2d)
{
ImageIcon imgicon=new ImageIcon("wallpaper.jpg");
Image img=imgicon.getImage();
g2d.drawImage(img, 130,20, null);
g2d.fillRect(0, 0, lblBoradImage.getWidth(),lblBoradImage.getHeight());
ArrayList alLevelMap=new ArrayList();
alLevelMap.addAll(alWall);
alLevelMap.addAll(alArea);
alLevelMap.addAll(alCrate);
alLevelMap.add(ASK_Actor);
for(int i=0; i<alLevelMap.size(); i++)
{
Movement item=(Movement) alLevelMap.get(i);
if((item instanceof Actor) || (item instanceof Crate))
{
g2d.drawImage(item.getImage(), item.getX()+2, item.getY()+2, lblBoradImage);
}
else
{
g2d.drawImage(item.getImage(), item.getX(), item.getY(), lblBoradImage);
}
if(completestatus)
{
g2d.setColor(new Color(0, 0, 0));
g2d.drawString("Completed",25,20);
if(CompleteLevel.equals("Level1"))
{
CompleteLevel="Level2";
RunGameBoard(CompleteLevel);
}
else if(CompleteLevel.equals("Level2"))
{
CompleteLevel="Level3";
RunGameBoard(CompleteLevel);
}
else if(CompleteLevel.equals("Level3"))
{
CompleteLevel="Level4";
RunGameBoard(CompleteLevel);
}
else
{
CompleteLevel="Level5";
RunGameBoard(CompleteLevel);
}
}
}
}
private void RunGameBoard(String CompleteLevel)
{
int x=OFFSET;
int y=130;
for(int i=0;i<CompleteLevel.length();i++)
{
char item=CompleteLevel.charAt(i);
if(item=='e')
{
y+=SPACE;
if(this.width<x)
{
this.width=x;
}
x=OFFSET;
}
else if(item=='#')
{
objWall=new Wall(x,y);
alWall.add(objWall);
x+=SPACE;
}
else if(item=='*')
{
objCrate=new Crate(x,y);
alCrate.add(objCrate);
x+=SPACE;
}
else if(item=='$')
{
objDiamond=new Diamond(x,y);
alArea.add(objDiamond);
x+=SPACE;
}
else if(item=='@')
{
ASK_Actor=new Actor(x, y);
x+=SPACE;
}
else if(item==' ')
{
x+=SPACE;
}
height=y;
}
}
public void isLevelComplete()
{
int num=alCrate.size();
int complete=0;
for(int i=0; i<num; i++)
{
Crate crate=(Crate) alCrate.get(i);
for(int j=0; j<num; j++)
{
Diamond area=(Diamond) alArea.get(j);
if(crate.getX()==area.getX() && crate.getY()==area.getY())
{
complete +=1;
}
}
}
if(complete==num)
{
completestatus=false;
if(CompleteLevel.equals(m.Level_1))
{
CompleteLevel=m.Level_2;
lblLevel.setText("Level 2");
RunGameBoard(CompleteLevel);
}
else if (CompleteLevel.equals(m.Level_2))
{
CompleteLevel=m.Level_3;
lblLevel.setText("Level 3");
RunGameBoard(CompleteLevel);
}
else if(CompleteLevel.equals(m.Level_3))
{
CompleteLevel=m.Level_4;
lblLevel.setText("Level 4");
RunGameBoard(CompleteLevel);
}
else
{
CompleteLevel=m.Level_5;
lblLevel.setText("Level 5");
RunGameBoard(CompleteLevel);
}
JOptionPane.showMessageDialog(null, "Level Complete");
nextlevel();
repaint();
}
}
public void nextlevel()
{
alArea.clear();
alCrate.clear();
alWall.clear();
RunGameBoard(CompleteLevel);
movecount=0;
if(completestatus)
{
completestatus=false;
}
}
private boolean checkWallCollision(Movement actor, int type)
{
if (type==LEFT_COLLISION)
{
for(int i=0; i<alWall.size(); i++)
{
Wall wall=(Wall) alWall.get(i);
if(actor.isleft(wall))
{
return true;
}
}
return false;
}
else if (type==RIGHT_COLLISION)
{
for(int i=0; i<alWall.size(); i++)
{
Wall wall=(Wall) alWall.get(i);
if(actor.isright(wall))
{
return true;
}
}
return false;
}
else if (type==TOP_COLLISION)
{
for(int i=0; i<alWall.size(); i++)
{
Wall wall=(Wall) alWall.get(i);
if(actor.istop(wall))
{
return true;
}
}
return false;
}
if (type==BOTTOM_COLLISION)
{
for(int i=0; i<alWall.size(); i++)
{
Wall wall=(Wall) alWall.get(i);
if(actor.isbottom(wall))
{
return true;
}
}
return false;
}
return false;
}
private boolean checkCrateCollision(int collision_type)
{
if (collision_type==LEFT_COLLISION)
{
for (int i=0;i <alCrate.size(); i++)
{
Crate crate=(Crate) alCrate.get(i);
if(ASK_Actor.isleft(crate))
{
for(int j=0; j<alCrate.size(); j++)
{
Crate item=(Crate) alCrate.get(j);
if(!crate.equals(item))
{
if(crate.isleft(item))
{
return true;
}
}
if(checkWallCollision(crate, LEFT_COLLISION))
{
return true;
}
}
crate.move(-SPACE, 0);
isLevelComplete();
}
}
return false;
}
else if (collision_type==RIGHT_COLLISION)
{
for (int i=0;i <alCrate.size(); i++)
{
Crate crate=(Crate) alCrate.get(i);
if(ASK_Actor.isright(crate))
{
for(int j=0; j<alCrate.size(); j++)
{
Crate item=(Crate) alCrate.get(j);
if(!crate.equals(item))
{
if(crate.isright(item))
{
return true;
}
}
if(checkWallCollision(crate, RIGHT_COLLISION))
{
return true;
}
}
crate.move(SPACE, 0);
isLevelComplete();
}
}
return false;
}
else if (collision_type==TOP_COLLISION)
{
for (int i=0;i <alCrate.size(); i++)
{
Crate crate=(Crate) alCrate.get(i);
if(ASK_Actor.istop(crate))
{
for(int j=0; j<alCrate.size(); j++)
{
Crate item=(Crate) alCrate.get(j);
if(!crate.equals(item))
{
if(crate.istop(item))
{
return true;
}
}
if(checkWallCollision(crate, TOP_COLLISION))
{
return true;
}
}
crate.move(0, -SPACE);
isLevelComplete();
}
}
return false;
}
else if (collision_type==BOTTOM_COLLISION)
{
for (int i=0;i <alCrate.size(); i++)
{
Crate crate=(Crate) alCrate.get(i);
if(ASK_Actor.isbottom(crate))
{
for(int j=0; j<alCrate.size(); j++)
{
Crate item=(Crate) alCrate.get(j);
if(!crate.equals(item))
{
if(crate.istop(item))
{
return true;
}
}
if(checkWallCollision(crate, BOTTOM_COLLISION))
{
return true;
}
}
crate.move(0,SPACE);
isLevelComplete();
}
}
return false;
}
return false;
}
class MovementKeyAdapter extends KeyAdapter
{
@Override
public void keyPressed(KeyEvent e)
{
if(completestatus)
{
return;
}
int key=e.getKeyCode();
if(key==KeyEvent.VK_LEFT)
{
if(checkWallCollision(ASK_Actor,LEFT_COLLISION))
{
JOptionPane.showMessageDialog(null,"Left Wall");
return;
}
if(checkCrateCollision(LEFT_COLLISION))
{
JOptionPane.showMessageDialog(null, "Left Wall");
return;
}
movecount+=1;
ASK_Actor.move(-SPACE, 0);
}
if(key==KeyEvent.VK_RIGHT)
{
if(checkWallCollision(ASK_Actor,RIGHT_COLLISION))
{
JOptionPane.showMessageDialog(null,"Right Wall");
return;
}
if(checkCrateCollision(RIGHT_COLLISION))
{
JOptionPane.showMessageDialog(null, "Right Wall");
return;
}
movecount+=1;
ASK_Actor.move(SPACE, 0);
}
if(key==KeyEvent.VK_UP)
{
if(checkWallCollision(ASK_Actor,TOP_COLLISION))
{
JOptionPane.showMessageDialog(null,"TOP Wall");
return;
}
if(checkCrateCollision(TOP_COLLISION))
{
JOptionPane.showMessageDialog(null, "TOP Wall");
return;
}
movecount+=1;
ASK_Actor.move(0, -SPACE);
}
if(key==KeyEvent.VK_DOWN)
{
if(checkWallCollision(ASK_Actor,BOTTOM_COLLISION))
{
JOptionPane.showMessageDialog(null,"BOTTOM Wall");
return;
}
if(checkCrateCollision(BOTTOM_COLLISION))
{
JOptionPane.showMessageDialog(null, "BOTTOM Wall");
return;
}
movecount+=1;
ASK_Actor.move(0, SPACE);
}
else if(key==KeyEvent.VK_R)
{
nextlevel();
}
lblCount.setText(String.valueOf(movecount));
repaint();
}
}
}
Outcomes 2 Working Solution
User Interfaces
Level 1
Fig (1.1)
Level 2
Fig (1.2)
Level 3
Fig (1.3)
Level 4
Fig (1.4)
Level 5
Fig (1.5)
Classes Involved
Pre-Defined Classes
JFrame.Class, Key Adapter, Key Press, Key Event
JFrame must be used in Sokoban Game to appear a standard framed window with border and title area
or icon. JFrame has an associated pane which shows its visual contents. User can request that the lookand-feel provide the decorations for a frame. The user can also specify that the frame have no window
decorations at all, a feature that can be used on its own, or to provide his/her own decorations, or with
full-screen exclusive mode.
User-Defined Classes
Wall
Image Icon br kyount tone lal so tr yay
Actor
X nae Y nae ko catch chin loz, Current place ko lol chin lox, movement method lay yay htr tr pl,
Map
Design twy level 1 ku nae 1 ku pyoung phoe a twat
Main
Set bound twy cha
Gameboard label twy kyay nyar move
Wall twy ko tite tae a khr paw lrr tat harr twy yay, Message box twy lal htr tr dl so tr lal yay
Outcomes 3 Testing
Test Plan
No
1
Type of Testing
Unit Testing
Date
21/ 3/ 2018
Test Action
Test Objective
Press left key on Move actor to left
keyboard
Press right key on Move
keyboard
right
actor
to
Press top key on Move actor to top
keyboard
Press bottom key Move actor
on keyboard
bottom
Test Scripts
Test Case No
Test Objective
Test Action
Expected Result
Actual Result
Before Testing
Fig 1.1
After testing
Fig 1.2
1
Move actor to left
Press left key on keyboard
The actor is moved to left
Fig 1.2
to
Reference
(Anon., 1995)
Download