Ads

New Domain

Blog has been moved to new domain: www.it-googled.com Enjoy!
Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Tuesday, 2 August 2011

Java Hangman game with source code

Please include me in your references if you copy any part of the code.

User Guide:


Source Code:

package hangMAN;

import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
* @author Malkor13
* ispired by System of a Down mix albums playlist
*/
public class hangman2 {


//list of hidden words.

private List wordList = new ArrayList();

// list of labels

private List labelList = new ArrayList();

// The secret word.

private String hiddenWord;

// fiel for input.

private JTextField inputLetterField;

// Frame

private JFrame frame;

// panel to display the game

private JPanel gamePanel;

// panel to dispay the label

private JPanel labelPanel;

// panel to display the lives

private JPanel livesPanel;

// Panel for cathegory

private JPanel cathegoryPanel;
//Label for cathegory

private JLabel catLabel;


// index from list for keeping track of secret hidden word.


private int previousIndex;

// lives.

private int lives;

// Label for lives.

private JLabel livesLabel;

//Main method

public static void main(String[] args) {
hangman2 hangman = new hangman2();
hangman.addWords(); // add secret words to list
hangman.getWord(); // make secret word
hangman.display(); // display game
}


// List of the premiership teams to make it easier to guess

public void addWords() {
wordList.add("manchesterunited");
wordList.add("liverpool");
wordList.add("chelsea");
wordList.add("arsenal");
wordList.add("astonvilla");
wordList.add("everton");
wordList.add("fulham");
wordList.add("westham");
wordList.add("manchestercity");
wordList.add("tottenham");
wordList.add("wigan");
wordList.add("stoke");
wordList.add("bolton");
wordList.add("portsmouth");
wordList.add("blackburn");
wordList.add("hull");
wordList.add("newcastle");
wordList.add("middlesbourgh");
wordList.add("westbrom");


}
//method for random word
public void getWord() {
Random random = new Random();
int index = random.nextInt(wordList.size());
while (index == previousIndex) {
index = random.nextInt(wordList.size());
}
hiddenWord = wordList.get(index);
previousIndex = index;
}


// gui

public void display() {
frame = new JFrame("HaNgMaN");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(new GameBar());
gamePanel = new JPanel(new BorderLayout());
livesPanel = new JPanel();
lives = 6;
livesLabel = new JLabel(lives + " lives remaining");
livesPanel.add(livesLabel);
gamePanel.add(livesPanel, BorderLayout.WEST);
cathegoryPanel = new JPanel();
catLabel = new JLabel("Cathegory: Premiership Football Teams 2008/09");
cathegoryPanel.add(catLabel);
gamePanel.add(cathegoryPanel, BorderLayout.NORTH);


JPanel fieldPanel = new JPanel();
inputLetterField = new JTextField(1);
inputLetterField.addKeyListener(new LetterChecker());
fieldPanel.add(inputLetterField);
gamePanel.add(fieldPanel, BorderLayout.CENTER);

labelPanel = new JPanel();
setupLabels();
frame.add(gamePanel, BorderLayout.CENTER);

frame.setSize(300,150);
frame.setResizable(false);
frame.setVisible(true);
}

//lebel to display dashes
private void setupLabels() {
for (int i=0; i < hiddenWord.length(); i++) {
JLabel label = new JLabel("-");
labelList.add(label);
labelPanel.add(label);
}
gamePanel.add(labelPanel, BorderLayout.SOUTH);
}

//newgame method ,sets lives to 7 and refresh panels
private void newGame() {

lives = 6; // reset lives
livesLabel.setText(lives + " lives remaining");
livesPanel.validate(); // refresh lives panel
// remove all labels
for (JLabel label : labelList) {
labelPanel.remove(label);
}
labelPanel.validate(); // refresh label panel
gamePanel.remove(labelPanel); // remove label panel
gamePanel.validate(); // refresh game panel
labelList.clear();
getWord();
setupLabels();
frame.validate(); // refresh frame
}

//inner class for menu bar
private class GameBar extends JMenuBar {
private GameBar() {
super();
generateGameMenu();
}

//gui for menu
private void generateGameMenu() {
JMenu gameMenu = new JMenu("Game");
gameMenu.setMnemonic(KeyEvent.VK_G);
JMenuItem newGameItem = new JMenuItem(new GameAction());
newGameItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.ALT_DOWN_MASK)); //shortcut
gameMenu.add(newGameItem);
gameMenu.addSeparator();
JMenuItem exitItem = new JMenuItem(new ExitAction());
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,
InputEvent.ALT_DOWN_MASK)); // shortcut ;)
gameMenu.add(exitItem);
add(gameMenu);
}

} // end inner class

//inner class for action
private class GameAction extends AbstractAction {
private GameAction() {
putValue(AbstractAction.NAME, "New");
putValue(AbstractAction.SHORT_DESCRIPTION, "New game");
putValue(AbstractAction.MNEMONIC_KEY, KeyEvent.VK_N);
}
//new game
@Override
public void actionPerformed(ActionEvent e) {
newGame();
}

} // end inner class
//inner class for another action
private class ExitAction extends AbstractAction {
private ExitAction() {
putValue(AbstractAction.NAME, "Exit");
putValue(AbstractAction.SHORT_DESCRIPTION, "Exit game");
putValue(AbstractAction.MNEMONIC_KEY, KeyEvent.VK_X);
}
//for exit
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}

} // end inner class


//checks user input

private class LetterChecker extends KeyAdapter {
private boolean isComplete() {
int length = 0;
for (JLabel label : labelList) {
if (label.getText().charAt(0) != '-') {
length++;
}
}
if (hiddenWord.length() != length) {
return false;
}
else {
return true;
}
}
//validate user inpit
@Override
public void keyTyped(KeyEvent e) {
char ch = e.getKeyChar(); // get input
// check if input is numeric
if (e.isAltDown() || e.isShiftDown() || Character.isDigit(ch)) {}
else {
char[] ary = new char[hiddenWord.length()];
ary = hiddenWord.toCharArray();
boolean noMatch = true; // assume incorrect input
for (int i=0; i < ary.length; i++) {
if (ch == ary[i]) {
// get label index
JLabel charLabel = labelList.get(i);
// update label
charLabel.setText(Character.toString(ch));
noMatch = false;
if (isComplete()) { // check if word is completed
//shows dialog
int option = JOptionPane.showConfirmDialog(null,
"Congratulations ! New game??",
"Gooooooooooooooooood!",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (option == JOptionPane.OK_OPTION) {
newGame();
}
}
}
}
if (noMatch) { // incorrect input
lives--; // decrement lives
// update lives label
livesLabel.setText(lives + " lives remaining");
livesPanel.validate(); // refresh lives panel
if (lives == 0) {
int option = JOptionPane.showConfirmDialog(null,
"Game over! New game?",
"Buuuuuuuuuu!",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (option == JOptionPane.OK_OPTION) {
newGame(); // restart game
}
}
else {
JOptionPane.showMessageDialog(null, "No match :-P",
"Wrong", JOptionPane.PLAIN_MESSAGE);
gamePanel.validate(); // refresh game panel
frame.validate(); // refresh window
}
}
inputLetterField.setText(""); // display only one letter
}
}

} // end inner class
}

Monday, 1 August 2011

Flex emulating sh terminal

private function keyHandler(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.ENTER){
// variable to hold user input
var command:String = userInput.text;
// variable to read from output and keep history
var history:String = console.text;
// variables to create shell like enviroment
var root:String = "root@LB:~# ";
var rootchanged:String = "root@LB:";
var i:int = 0;
// cd command (for instance cd /home)
if (command.charAt(0) == "c" && command.charAt(1) == "d"){
var words:Array = command.split(" ");
var changedcurrent:String = words[1];
console.text = history + rootchanged + changedcurrent + "#" + "\n"}
else{
//arrays that holds possbile commands and output
known.push("pwd","ls","arp","help");
answer.push("/root" ,"filezilla3","192.168.25.254 either 00:50:56:f0:87:d2 eth1","pwd, ls, arp, help");
hint.push("help");
// loop through known commands
for (i=0; i if(userInput.text == known[i]){
//displaying output
console.text = history + root + userInput.text + "\n"+ answer[i] + "\n"
}}}}}

Primitive type is to be replaced by more object-oriented approach.

Thursday, 23 June 2011

Adobe Flex Animations +MS PowerPoint - Computer Based Training (CBT,WBT)

Computer/Web based training requires some sort of presentation component to display the main content and images to the users. Early days for development of first session were no much effective due to amount of time spent for use of adobe flash for simple animation. There was a time for analyses and new approach has been chosen.

The way new approach has been successful was to create a power point slides for each session and they have been broken down to 2,3 or 4 parts depending on the size of the session. Tool has been found to convert the created PowerPoint session into .avi video format using EM Free PowerPoint Video Converter.



After there was a need to convert that .avi format into flash .swf and Free Video to Flash Converter v 4.7.21 build 305 was found to do the job as shown below:



The output became a .swf flash video with all the PowerPoint build in animations and time frame that changes as it has been chosen using converters. Disadvantage of this method was to use Actionscript Timer class in order to trigger events.
.swf files can be easily added within flex applications.

Wednesday, 19 January 2011

Java IpAddressLookup simple class

import java.net.*;
import java.util.*;
public class IpAddressLookup {

public static void main(String[] args) {
try
{

System.out.print("Please enter the address : ");
Scanner Sc = new Scanner(System.in);
String host= Sc.next();
InetAddress PC = InetAddress.getByName(host);

String hostname = PC.getHostName();
byte[] signed = PC.getAddress();
System.out.println("Hostname is : " + hostname);
System.out.println("Signed address is " + signed);
int unsigned;
System.out.println();
System.out.print("IP Address is : ");

for(int i=0; i {
unsigned = signed[i] < 0 ? signed[i] + 256 : signed[i];

System.out.print(unsigned + ".");
}
System.out.println();
}
catch (UnknownHostException e){
System.out.println("Can't find the IP Address or the hostname");
}}}

Thursday, 18 February 2010

AutoIT Novell iPrint automation complete

iPrint information from wikipedia.
iPrint, a technology originally developed by Novell, allows users to install printer-drivers from a web browser and to submit print jobs over the Internet or a local network through the standard Internet Printing Protocol (IPP).

Although the system uses Novell infrastructure, desktop users require only an iPrint client.

iPrint servers utilise the combination of:
an IPP server, Print Manager ,Printer Agent ,iPrint Gateway ,driver store

Until now we have been using autoIT gui interface to install the client and printers in a very simple and efficient way:



Rescripted by me to allow installing multiple printers with only few mouse clicks (using checkboxes), new support for Windows Vista/7 32/64bit. Few printers added.



Conclusions:
It's sometimes much harder to modify existing code rather then creating things from scrach.

Monday, 21 December 2009

AutoIT scripting - GUI X-mas Application Window design


My first steps into AutoIt scripting ended with quite good looking Quick Launch Application Window. It doesn't take much time to come up with some simple scripts to automate your everyday tasks from setting up the correct screen resolution at the startup to more powerfull network related tasks. Latest windows 7 os with PowerShell 2 built-in may be perfect to link with your autoit backend.