Share This: Git and github may come across as complex at first but after a while it becomes natural. Years back I used local git repositories which required constant usage of CLI and you had to know what you are doing. These days github makes it much easier to understand and use. In about 10minutes […]
Continue reading…
Posts tagged with 'programming'
Java Hangman game with source code – my first game :-)
Share This: Written back at university my first Java game. Maybe not last ;] https://github.com/cann0nf0dder/java/tree/master/Hangman Please include me in your references if you copy any part of the code.User Guide: Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
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 } |
Java Network Programming Buyer/Seller Telnet Application
Share This: SellConnection class: package buyersellerprogram.server; import java.net.Socket; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; import java.io.OutputStream; import java.util.StringTokenizer; import buyersellerprogram.bargainer.Seller; public class SellConnection extends Thread { Socket connection; Seller seller; public SellConnection(Socket s, Seller sellerIn) { //** Set the value of the Socket to s connection = s; //** Set the value of […]
Continue reading…
Flex emulating sh terminal
Share This: 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 […]
Continue reading…
Java IpAddressLookup simple class
Share This: 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 ” + […]
Continue reading…