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!");
 }
}

Firefox on iPhone is a Bad Idea

22 March, 2009

I was on Digg this morning when I came across this article (here’s the Digg link). Matt Asay, author of CNET’s “The Open Road” does have some pretty good points on why Firefox should be on the iPhone, and I completely agree with him on how throwing Firefox onto an N810 isn’t really going to bring about wondrous numbers of community supporters, but it is simply not a good idea to let Firefox become an iPhone application.

To set the scene, everyone knows about the iPhone. Everyone’s super-excited about the App Stores – users and developers alike – and all its possibilities. Apple’s competitors have taken a lot of ideas from the iPhone, and mobile operating systems are all the hype these days (that includes netbooks). As far as I’m aware, the N810 runs Android, for which applications are written in Java.

When I first saw the title of Matt’s article (“Why no iPhone support for Firefox mobile beta?”), I thought, Why is this question even being asked? First off, it’s obviously going to be a competitor for Mobile Safari (which Apple won’t even allow in the app store ), and Firefox’s not that fast of a browser, either, in my opinion, compared to Safari. Besides, all iPhone applications are written in Cocoa. Let’s see Mozilla trying to rewrite all of Firefox, its plug-in architecture, its support for themes, etc, all in Cocoa Touch. Furthormore, quoting directly from the article, one of the many other reasons that Firefox won’t be available for the iPhone any time in the near future is because “it [has] to do with restrictions on run non-SDK code[.]”

Not only that, but it would also provide two completely different SDKs for software developers to chose from: the iPhone SDK and whatever Firefox plug-in developers use. This is really dangerous for Apple. Apple wants in no way for there to be an alternative development SDK if not that of the iPhone – that’s why Flash isn’t on iPhone, and won’t be, either. If a developer prefers Mozilla’s way of doing things and wants to target iPhone owners as potential users, there would be a visible drop in terms of iPhone SDK developers and, hence, Mac users (and possibly the number of new Mac developers too).

All in all, Firefox on the iPhone is a really bad idea. Although, idealistically, it sounds nice, Firefox won’t be able to live up to its “I’m a fast and customizable browser” promise.

Microsoft’s Silverlight Gaffe

14 March, 2009
I’m not sure whether this could be classified as false advertising or what, but I don’t think Microsoft can count megabytes… Perhaps it’s that large for Windows, but I’m pretty sure Microsoft’s able to update information based on the detected operating system.
Click the image for a larger size.

Networking Ubuntu and OS X with Samba

11 February, 2009

Let’s assume you have both Macs and Ubuntu PCs in your network. How can you set them up so you’re able to access Ubuntu from a Mac and vice-versa? It’s pretty simple via Samba.

Setting up Ubuntu

  1. Right-click the folder you want to share and select Sharing Options.
  2. Unless you already have Samba installed, you may be asked to install it now.
  3. Set all the options you want – select the name of the share (this will appear in the Finder’s sidebar), if you want to allow other people to be able to write in the folder or not, and if you want guests to be able to access this folder.
  4. After saving the modifications, a link to the Ubuntu computer should appear automatically in the Finder, with the iconic BSOD icon

Setting up Mac OS X

  1. Open System Preferences > Sharing. Select the file sharing checkbox.
  2. Select the “Options…” button.
  3. In the pop-up menu, add the names of the users whose Public folders will be shared and select “Share Files using SMB.”
  4. In Ubuntu, select Places > Connect to Server… and select Windows Share.
  5. Type in your computer’s network address (usually smb://***.***. … where the asterisks represent your IP address). You can access your computer either from the Places menu, the desktop, or in network:/// in Nautilus (or your file manager of choice).



Alpine and Mailing Lists

4 January, 2009


Alpine is a rather well-known email client. It stands for “Alternatively-Licensed Pine.” The developers at the University of Washington (Seattle) wanted to maintain a free code base while keeping a close relationship to the trademarked Pine.

I like Alpine. It’s really nice, but I only use it for my text-only mailing lists. That’s because a lot of modern newsletters and personal email are filled with images. Sure, I know I could just download the images from within Alpine and open them with Feh, but then I’d need to try to piece together the images to see what part of the email the image belongs to… all in all, it would be some tedious work.

However, with text-only emails, Alpine has the speed and stability I need to quickly get to a message, read it, reply (if needed), or send out a new one in a way that is yet unmatched by GUI mail clients. Moreover, the clean interface makes using it all the more fun.

Why not try out Alpine? Most distros’ package managers should have it, and it’s easy to set it up for using your Gmail account and IMAP.


Read Mail Really Fast

3 January, 2009

This is simply amazing. Click the image to view it full size.




Read Mail Really Fast

3 January, 2009

This is simply amazing. Click the image to view it full size.