In which the author learns JavaScript

1 August, 2014

After going on a several-year stint of no programming (I got distracted by linguistics, history of ethnic separatism, and urban planning) aside from writing a short Python loop to process Facebook data for a marketing job, I decided to try my hand at learning some JavaScript. I spent about three and a half hours two mornings ago typing up just about 100 lines of code to write a simple rock-paper-scissors program, admittedly far less advanced than what I was doing back in 2009. You can have a try at the simple game here (on a free webhost, pardon any ads that pop up), and I’ve posted the JavaScript code below. For the HTML page, opening up the source code in the link above should suffice.

Cheers!

/* STEP 0: Set up the number of rounds to play */


function startGameExternally() {
 window.open("rps.html", "_self");
}

function setValue(idName, valueToSet){
 document.getElementById(idName).innerHTML = valueToSet;
}

var roundsToPlay = 3; //sets a default value in case page is loaded directly
console.log(roundsToPlay);

function setNumberOfRounds() {
 var didUserInputNumber = false;
 
 while (!didUserInputNumber){
 newNumber = prompt("How many rounds of Rock/Paper/Scissors do you want to play?");
 
 //make sure user put in a number
 
 // if user presses cancel or leaves box blank
 if (newNumber == "" || newNumber == null) { 
 didUserInputNumber = true;
 //returns to the game and does nothing
 }
 else {
 if (isNaN(newNumber) || ( newNumber < 0)){
 alert("Please input a positive number!");
 } else {
 clearScores();
 
 roundsToPlay = newNumber;
 didUserInputNumber = true;
 setValue("numRoundsToPlay", roundsToPlay);
 
 //prevent user from adding rounds in the middle of the game
 
 }
 }
 
 } 
 return roundsToPlay
}

function clearScores(){
 playerScore = 0;
 compScore = 0;
 
 roundNumber = 1
 setValue("currentRound", roundNumber);
 roundsToPlay = 3;
 setValue("numRoundsToPlay", roundsToPlay);
 
 setValue("whatUserPlayed", "Nothing yet.");
 setValue("whatComputerPlayed", "Nothing yet.");
 setValue("whoWon", "Nobody yet.");
 
 setValue("displayPlayerScore", 0);
 setValue("displayComputerScore", 0);
 
 setValue("finalResult", "None yet.");
}


/* STEP 1: FIGURE OUT WHAT THE USER CHOSE */
var roundNumber = 1;

function isRock(){
 playAgainstComputer("rock");
}

function isPaper(){
 playAgainstComputer("paper");
}

function isScissors(){
 playAgainstComputer("scissors");
}


/* STEP 2: PLAY IT AGAINST THE COMPUTER */

var playerScore = 0;
var compScore = 0;

function playAgainstComputer(playerChoice) {
 
 var compChoice = computerPlay();
 setValue("whatUserPlayed", playerChoice);
 setValue("whatComputerPlayed", compChoice);
 
 console.log(roundNumber, "of", roundsToPlay);
 console.log(roundNumber==roundsToPlay); 
 
 
 if(compChoice == playerChoice){
 setValue("whoWon", "This round is a tie!");
 } else {
 var whetherPlayerWon = didPlayerWin(playerChoice, compChoice);
 console.log("Did player win?", whetherPlayerWon);
 
 if(whetherPlayerWon){
 playerScore++;
 updateScores(playerScore, compScore);
 setValue("whoWon", "User wins this round.");
 } else {
 compScore++;
 updateScores(playerScore, compScore);
 setValue("whoWon", "Computer wins this round.");
 } 
 
 } 
 roundNumber++;
 setValue("currentRound", roundNumber);

 if (roundNumber > roundsToPlay){
 var whetherPlayerWon = didPlayerWin(playerChoice, compChoice);
 setValue("currentRound", "Done!");
 setValue("buttonSection", "Game over!");
 
 updateScores(playerScore, compScore);
 whoWonGame(playerScore, compScore);
 }
}

function computerPlay(){
 var compChoice = Math.random();
 var play;
 
 if (compChoice <= (1/3)){
 play = "rock";
 } else if (compChoice <= (2/3)){
 play = "paper";
 } else {
 play = "scissors";
 }

 return play;
}

/* STEP 3: FIGURE OUT WHO WON */

function updateScores(playerScore, compScore){
 setValue("displayPlayerScore", playerScore);
 setValue("displayComputerScore", compScore);
}

function didPlayerWin(playerChoice, compChoice) {
 if ((playerChoice == "rock" && compChoice == "scissors") ||
 (playerChoice == "paper" && compChoice == "rock") ||
 (playerChoice == "scissors" && compChoice == "paper")
 ){
 return true;
 } else {
 return false;
 }
} 

function whoWonGame(playerScore, compScore){ 
 updateScores(playerScore, compScore);
 
 if (playerScore == compScore){
 setValue("finalResult","Both players tied.");
 } else if (playerScore < compScore) {
 setValue("finalResult", "The computer wins!");
 } else {
 setValue("finalResult","The user wins!");
 }
}

Find Standard Deviation with C++

2 January, 2010

I wrote a small C++ program to calculate the mean and standard deviation of a given data set. Here it is:

#include <iostream>
#include <cmath>
using namespace std;

int main (int argc, char* argv[])
{
	int number_of_items;
	cout << "How many pieces of data are in the list? ";
        cin >> number_of_items;
	float list[number_of_items];

	// get all the data items
	cout << "Please enter the data:" << endl;
	int i;
	for (i = 1; i <= number_of_items; i++)
	{
		cout << i << ". "; 		cin >> list[i];
	}

	// find the mean (average) of the data
	float xbar = 0;
	for (i = 0; i <= number_of_items; i++)
		xbar = xbar + list[i];

	xbar = xbar / number_of_items;
	cout << "The mean (average) is " << xbar << "." << endl;

	// find the standard deviation
	float numerator = 0;
	float denominator = number_of_items;

	for (i = 1; i <= number_of_items; i++)
		numerator = numerator + pow((list[i] - xbar), 2);

	float standard_deviation = sqrt (numerator/denominator);
	cout << "The standard deviation for the given data is "
		 << standard_deviation << "." << endl;

	return 0;
}

The Web Invaded the Desktop

30 September, 2009

I can officially say that the web has infiltrated itself into my desktop operating system. On my dock, eight out of the fourteen applications are directly web-related: Safari, my web browser; Mail, my mail client; Adium, my messaging client; Colloquy, my IRC client; Hulu Desktop, a desktop version of the online TV website; iTunes, especially in regards to the iTunes store; Songbird, for finding music online; and Times, for viewing news online.


Slowing Down

27 August, 2009

Due to other things that have come up, I haven’t been able to write too many blog posts recently. To any followers, I apologize. I will try to set some time apart to write something else soon.


Keynote Thumbnails in Quick Look

31 July, 2009

As can be seen from Apple’s Leopard presentations before release, as well as on page 22 of the Leopard manual, opening a Keynote presentation using Quick Look should allow you to see slide thumbnails on the side, as in 0:28 in this video. However, this is not set as the default behavior. Achieving this is very simple, though. In Keynote’s preferences, under General, simply check off “Include preview in document by default.” There you have it!

via Apple’s Support page


Snow Leopard is Darker

26 July, 2009

It seems that my predictions, which were one of the first on the internet regarding the subject, that Snow Leopard would be darker have come true! Here’s a quick timeline of how I came about the idea and all the steps that Apple took to make it glaringly obvious that Snow Leopard’s UI would be even the slightest bit darker than Leopard’s:

  1. Someone on IRC (nick JHKKHJK or similar, I can’t quite recall it)  in a WWDC discussion channel told me that Snow Leopard’s UI would be darker. That started originally what seemed like a wild goose chase, but ended up being a lot more meaningful than that.
  2. First up was MobileMe, which sported a dark toolbar. Knowing how Apple feels about design and a look of integration across all its products, I found it obvious that it was only a matter of time before other applications started going dark as well.
  3. Next, interfaces began to merge with iTunes 8, which  in Grid View has a dark “sub-toolbar” and even darker scrollbars.
  4. When QuickTime X’s new interface was announced I was sure that my ideas were no longer simply speculation and that Apple was giving way too many clus that they were going to give us a darker interfaces.
  5. Now, Dock context menus are dark. Having five interface items be dark is interesting enough; I wonder what else will be darker in future Snow Leopard releases. Besides, all the iLife apps have dark scrollbars.

Oh, and there are new wallpapers available for Snow Leopard; I’m already using some of them.


What Happened to Project Looking Glass?

9 July, 2009

Project Looking Glass is a cross-platform (compatible with many Unix-based operating systems) three-dimensional desktop environment based on Java technology made by Sun Microsystems. The technology started with the vision of recreating the way the desktop environment works, taking it from the plain 2D and 2.5D we are used to to something rather different.

The project originally went beyond a simple conception into a full-fledged stable desktop environment. Its last stable release (1.0.1), however, was in 2007. Its development seems to have virtually stopped, though. It’s plainly obvious, especially since its website hasn’t been updated in two years or so and we hardly hear anything about it. So what ever happened to Project Looking Glass? Why did the project stagnate?

After giving it some thought and research, here are a few reasons I came up with:

  • There is a lack of user base. Sure, it’s interesting, but not many people felt the compulsion to switch from their two-dimensional desktop environment, one to which they had gotten completely accustomed, to something completely new. Besides, how many common users besides “techies” have heard of Looking Glass at all?
  • There is a lack of interest. This builds up on the previous point. It seems Sun simply got tired of the project and promoting it. Besides, Sun has already accepted GNOME as the de facto standard desktop environment for its OS releases, such as OpenSolaris.
  • It’s another platform. Modern developers, despite being relatively welcoming to new technologies, seem to have already found their niche and divided themselves into what they find suitable. Further more, Looking Glass and its APIs, as far as it seems, has a lot unique features that are not easily or readily deployable to other platforms, so it’s not a WORA (write-once-run-anywhere) situation.

Given these, and other reasons, such as the easier availability of other 3D desktop environments, such as Compiz, it’s relatively easy to see why Looking Glass died out. The project’s latency is still a loss for the world of development.


Follow

Get every new post delivered to your Inbox.