Friday, August 16, 2013

Javascript Game pt. 2 - Rolling the Dice

 Since this game revolves around rolling dice, that is the first thing that I needed to do. Rolling a die is in essence, a random number generation function. So I ended up with the following function:


function rolldice ()

{die1=Math.floor(Math.random ()*6+1);

dieroll1.innerHTML="<p>"+die1"<p>";

die2=Math.floor(Math.random ()*6+1);

dieroll2.innerHTML="<p>"+die2+"<p>";

die3=Math.floor(Math.random ()*6+1);

dieroll3.innerHTML="<p>"+die3+"<p>";}


Math.floor () is the Javascript round down command. Therefore Math.floor (12.6) would result in 12. Nested inside the round down command I placed the actual random generator. Math.random by itself will generate a random decimal greater than zero, but less than one, and up to 10 digits long. Therefore to turn this random generation into a method that resulted in an integer between one and six, I had to multiply the result by six, add one, and round it down – as shown above. In totality the rolldice () function showed above will obtain a random die result for three dice, and then show them in the HTML of the page.

Monday, August 12, 2013

Javascript Game pt. 1 - Defining the Variables

I have learned that the best way to learn something is to do it. Therefore in my quest to learn Javascript I set myself the task of programming a game. I based this game off of an ancient Asian dice game used to gamble, known here in America as Chinchirorin. The basic rules for this game can be found here. This really taught me a lot, especially in terms of planning out how to program something. Previous to this the program effects I was trying to accomplish through Javascript were relatively simplistic.

It turns out that the keys to successfully defining a larger Javascript project is to successfully map the functions that you'll need as well as the variables. In this particular blogpost I'm going to only review the variables that I ended up defining for this game, a list whose length surprised me. All these variables I defined in a list at the beginning of my embedded script.

var housemoney=500; (This variable is what I used to track the money held by the bank that the player could gamble against and potentially win. Though you could create the game without this variable, the game would be endless without it. I used it in the programming to track when the player has 'broke the bank' and provide a win condition to the game.)

var h0us3m0n3y=document.getElementById("housemoney"); (This variable was created to interact with the HTML of the page so that the current amount of house money could be displayed and updated.)

var yourmoney=50; (This is the variable that I used to set how much money the player had available to bet. If a player runs out of this they could lose.)

var y0urm0n3y=document.getElementById("yourmoney"); (This variable was created to interact with the HTML of the page so that the current amount of player money could be displayed and updated.)

var housescore=0; (The internal variable to define what the house scored on a given turn barring a auto-win/loss.)

var h0us3sc0r3=document.getElementById("housescore"); (This variable was created to interact with the HTML of the page so that what the house scored could be displayed and updated.)

var yourscore=0; (The internal variable to define what the player scored on a given turn barring a auto-win/loss.)

var y0ursc0r3=document.getElementById("yourscore"); (This variable was created to interact with the HTML of the page so that what the player scored could be displayed and updated.)

var score=0; (The internal variable designed to keep track of a score on a given roll. This was then reassigned to the yourscore or housescore variable as appropriate. Resets every roll.)

var yourbet=0; (The internal variable designed to keep track of what the player chose to bet on a given turn.)

var y0urb3t=document.getElementById("yourbet"); (This variable was created to interact with the HTML of the page so that what the player chose to bet could be displayed and updated.)

var housebet=0; (The internal variable designed to track the house's bet on a given turn.)

var h0us3b3t=document.getElementById("housebet"); (This variable was created to interact with the HTML of the page so that the player knew that the house always matched their bet, and the the house's bet could be displayed and updated.)

var die1=0; (The internal variable designed to keep track of the result of die rolls on the first die.)

var dieroll1=document.getElementById("die1"); (A variable designed to interact with the HTML of the page so that the results of the die rolls of the first die could be displayed and updated.)

var die2=0; (The internal variable designed to keep track of the result of die rolls on the second die.)

var dieroll2=document.getElementById("die2"); (A variable designed to interact with the HTML of the page so that the results of the die rolls of the second die could be displayed and updated.)

var die3=0; (The internal variable designed to keep track of the result of die rolls on the third die.)

var dieroll3=document.getElementById("die3"); (A variable designed to interact with the HTML of the page so that the results of the die rolls of the third die could be displayed and updated.)

var winlose="undecided"; (An internal variable designed to keep track of whether a given roll matched a automatic win or loss condition.)

var triples="undecided"; (An internal variable designed to keep track of whether the roll result was a triple.)

var doubles="undecided"; (An internal variable designed to keep track of whether the roll result was a double.)

var stage=0; (An internal variable designed to keep track of what stage the game is currently at so that the programming can implement the appropriate next functions, or wait for player input.)

var zelda=document.getElementById("zelda"); (This variable was arbitrarily named after my cat. It was created to interact with the bottom text bar of the page, and display announcements about game play as they were triggered.)


Saturday, August 10, 2013

Announcing Urban Hermit Games

Since writing last I have been focusing my efforts on a website that I have worked to help launch. As one of the founding members I will have many more interesting experiences to share with this process that I have been going through in founding this website with my partner: the Hermit. Please go and check out Urban Hermit Games! I will need to do further posts on finding and configuring a VPS, choosing your VPS OS and configuring it, learning SSH, choosing your web content management system, and then making it work. However setting up this website has been an interesting experience to say the least.

We are currently trying to set up some social media interaction and see if we can find a way to get the free SSL certificate we obtained to not slow down our site by 200%. I have also managed to program a somewhat complex if terribly ugly game. At least it functions the way its supposed to. So there is much to come on this blog! There may even be reviews for the free library-boosting utilities that I originally intended to focus this blog on soon. But please, go take a look at Urban Hermit Games!