Showing posts with label Source Code. Show all posts
Showing posts with label Source Code. Show all posts

Sunday, June 30, 2013

Java: Rock, Paper, Scissor Game Source Code

This is the source code for how to implement a rock, paper, scissor game in JAVA. It has a swing GUI interface (using JOptionPane) that prompt the user for input.

 
// A game that plays rock paper scissor with user

import javax.swing.JOptionPane;

public class GameRockPaperScissor {
 public static void main(String[] args) {
  // Prompt the user to input the option they choose
  String humanInput = JOptionPane.showInputDialog(
    "Enter rock, paper, or scissor:");
  // Convert to lower case
  humanInput = humanInput.toLowerCase();
  
  // Generate a random number
  // Scissor (0),  rock (1), paper (2)
  int random = (int)(Math.random() * 3);
  String computerInput; // Computer's choice
  if (random == 0) 
   computerInput = "rock";
  else if (random == 1)
   computerInput = "paper";
  else
   computerInput = "scissor";
  
  String message = "The computer is " + computerInput + ". You are "
    + humanInput;
  
  // Determine who win
  boolean isTie = computerInput.equals(humanInput);
  boolean isWin = 
   ((computerInput.equals("rock") && humanInput.equals("scissor")) ||
   (computerInput.equals("paper") && humanInput.equals("rock")) ||
   (computerInput.equals("scissor") && humanInput.equals("paper")));
  
  // Prepare the message
  if (isWin)
   message += ". Computer Won.";
  else if (isTie)
   message += " too. It is Tie";
  // There are 3 options, computer win, tie, or you win
  // So we don't have to test here, if the code reach here, human win.
  else
   message += ". You Won";
   
  
  // Display the result on the screen
  JOptionPane.showMessageDialog(null, message);
 }
}

Tuesday, May 22, 2012

Python: How to make a clock in tkinter using time.strftime

This is a tutorial on how to create a clock / Timer using tkinter in Python, note you might have to change Tkinter to tkinter depending on your version of the Python you have. I am using Python 3.2 at the moment. The code itself is quite simple, the only part you need to know is how to get the current time, using time.strftime. You can also get the day of the week, the current month, etc, by changing the parameter you are supplying the function. (See the link for the full table of possible options). But yes, almost anything you can think of any use of, it is there already. :) So this can actual be developed to a calendar like GUI.

Related Tutorial with source code
Stop watch GUI (for counting time): http://ygchan.blogspot.com/2012/05/python-stop-watch-timer-source-code.html


import Tkinter as tk
import time

def update_timeText():
    # Get the current time, note you can change the format as you wish
    current = time.strftime("%H:%M:%S")
    # Update the timeText Label box with the current time
    timeText.configure(text=current)
    # Call the update_timeText() function after 1 second
    root.after(1000, update_timeText)

root = tk.Tk()
root.wm_title("Simple Clock Example")

# Create a timeText Label (a text box)
timeText = tk.Label(root, text="", font=("Helvetica", 150))
timeText.pack()
update_timeText()
root.mainloop()
Reference: http://docs.python.org/library/time.html#time.strftime
Reference: http://docs.python.org/library/string.html#formatstrings