|
|
| Programming Discussions about computer programming |
07-19-2012, 03:22 PM
|
#16
|
|
Pooh-Bah
Join Date: Jan 2005
Posts: 5,652
|
Re: Learning Javascript Thread
If anyone here is at a proficient level it would be great if they'd volunteer to maybe post a project or task for some of to try and tackle as a learning experience.
|
|
|
07-19-2012, 07:07 PM
|
#17
|
|
newbie
Join Date: Nov 2009
Posts: 39
|
Re: Learning Javascript Thread
Quote:
Originally Posted by Zygote
If anyone here is at a proficient level it would be great if they'd volunteer to maybe post a project or task for some of to try and tackle as a learning experience.
|
i think a blackjack game would be great for learning.
|
|
|
07-20-2012, 11:04 AM
|
#18
|
|
veteran
Join Date: Mar 2007
Location: Shoving AK
Posts: 2,839
|
Re: Learning Javascript Thread
Quote:
Originally Posted by Loc
i think a blackjack game would be great for learning.
|
I think a blackjack game might be a bit complex. Granted there is no AI, but its still quite a big project.
|
|
|
07-20-2012, 12:32 PM
|
#19
|
|
Pooh-Bah
Join Date: Jan 2005
Posts: 5,652
|
Re: Learning Javascript Thread
Quote:
Originally Posted by Loc
i think a blackjack game would be great for learning.
|
I'll give it a shot. I've covered quite a few topics in Javascript already that should have the tools to build it, though I have very little experience using what i've learned and am still in need of heavy review. As a project this may help. It may be too advanced for me though so if no one is here to help if i get stuck it may be an utter failure.
In all likelihood it will be a very limited version of blackback, but i'll take a stab nonetheless, and post progress in case anyone wants to join in.
|
|
|
07-20-2012, 01:20 PM
|
#20
|
|
newbie
Join Date: Nov 2009
Posts: 39
|
Re: Learning Javascript Thread
imo a text-based version can be done on a single page of code. the completeness of the app depends on how far you want to go with it, which is why i think it's an excellent project suggestion.
for example, a poor man's deck:
Code:
var deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10].sort(function(a, b) { return 0.5 - Math.random(); });
draw 4 from this for dealer and player1. check blackjacks (sum == 11). implement hit/stand. implement house rules for dealer drawing. evaluate hands.
if you want to go beyond one page, you could improve on this with better shuffling, distinct cards, betting, splitting, multiple players, graphics, etc.
Last edited by Loc; 07-20-2012 at 01:50 PM.
|
|
|
07-20-2012, 01:55 PM
|
#21
|
|
newbie
Join Date: Nov 2009
Posts: 39
|
Re: Learning Javascript Thread
oops, change the 1's to 11's - i meant sum == 21 obv.
|
|
|
07-20-2012, 05:41 PM
|
#22
|
|
grinder
Join Date: Aug 2004
Posts: 403
|
Re: Learning Javascript Thread
I think a blackjack game is a great idea. I wrote a very limited version in Monkey (a proprietary language that target javascript) a couple of months ago as a demo. I had the card graphics laying around already and it only has hit/stand options, but then, it only took a week, so I think it's a good size for a practice project. It was about 350 lines in Monkey. I don't have it up anywhere at the moment, but I've been meaning to put it up.
I'm be tempted to rewrite it in Coffeescript to see how much faster I could get it done.
|
|
|
07-20-2012, 06:00 PM
|
#23
|
|
old hand
Join Date: Feb 2010
Location: ( ͡° ᴥ͡°)
Posts: 1,825
|
Re: Learning Javascript Thread
If you're not going to be programming games is there any reason to get into javascript beyond playing around with jquery for various design purposes? The whole anonymous function thing and language in general seemed very odd to me coming from python, php and a bit of C.
|
|
|
07-20-2012, 07:26 PM
|
#24
|
|
newbie
Join Date: Nov 2009
Posts: 39
|
Re: Learning Javascript Thread
Quote:
Originally Posted by e i pi
If you're not going to be programming games is there any reason to get into javascript beyond playing around with jquery for various design purposes? The whole anonymous function thing and language in general seemed very odd to me coming from python, php and a bit of C.
|
i suppose learning javascript isn't necessary if you mostly work on apps without a lot of dynamic content, but it is a very forward-thinking endeavor if you plan on developing for the web for some time. javascript isn't just for game interfaces/physics - there are plenty of non-game uses of javascript on the server (node.js/vert.x), like chat apps or any kind of server io e.g. polled from an ajax-like call. even in "traditional" web apps, once you start developing ui's with serious production value the code is going to be so much easier to manage with javascript-powered libraries that enable a structured design paradigm/pattern (e.g. backbone.js, knockout.js, etc.).
phones aren't getting any less powerful and esp with firefox os coming, learning javascript as any kind of web developer seems like a no-brainer to me.
|
|
|
07-21-2012, 06:19 PM
|
#25
|
|
_Pooh_Bah_
Join Date: Feb 2005
Location: UK
Posts: 9,144
|
Re: Learning Javascript Thread
[CODE] tags for code [/CODE]
[QUOTE] tags for quotes [/QUOTE]
Last edited by _dave_; 07-21-2012 at 06:22 PM.
Reason: and now it's deleted!
|
|
|
07-21-2012, 06:28 PM
|
#26
|
|
Pooh-Bah
Join Date: Jan 2005
Posts: 5,652
|
Re: Learning Javascript Thread
Code:
var deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10].sort(function(a, b) { return 0.5 - Math.random(); });
function deal(){
var rankDeal = deck[Math.floor(Math.random()*52+1)];
return rankDeal;
}
function dealHand(){
var cardHolderDealer = [];
var cardHolderPlayer = [];
cardHolderDealer[0] = deal();
cardHolderDealer[1] = deal();
cardHolderPlayer[0] = deal();
cardHolderPlayer[1] = deal();
var playerIndex = 1;
var dealerIndex = 1;
var playerSum = cardHolderPlayer[0] + cardHolderPlayer[1];
var dealerSum = cardHolderDealer[0] + cardHolderDealer[1];
alert("dealer shows one card: " + cardHolderDealer[0]);
console.log("you have been dealt a total of "+ playerSum + " " + "[" + cardHolderPlayer + "]");
var total = 0;
for (var i in cardHolderPlayer)
total += cardHolderPlayer[i];
function nextMove(){
var hitOrStay = prompt('Hit or Stay');
function Hit(){
playerIndex++;
cardHolderPlayer[playerIndex] = deal();
hitTotal = 0;
for(var i in cardHolderPlayer){
hitTotal += cardHolderPlayer[i];
}
if (hitTotal === 21){
return console.log(hitTotal + ' you got blackjack');
}
else if (hitTotal > 21){
return console.log(hitTotal + ' you busted');
}
else{
console.log("After hitting, your new hand total is " + hitTotal + " " + "[" + cardHolderPlayer + "]");
return nextMove();
}
}
function dealerHit(){
dealerIndex++;
cardHolderDealer[dealerIndex] = deal();
dealerHitTotal = 0;
for(var i in cardHolderDealer){
dealerHitTotal += cardHolderDealer[i];
}
if (dealerHitTotal === 21){
return console.log(dealerHitTotal + " " + "[" + cardHolderDealer + "]" + ' dealer has blackjack. You lose');
}
else if (dealerHitTotal > 21){
return console.log(dealerHitTotal + " " + "[" + cardHolderDealer + "]" + ' dealer busted. You Win');
}
else if (dealerHitTotal > 16){
if(dealerHitTotal > hitTotal){
return console.log('dealer has ' + dealerHitTotal + ' You lose');
}
else if(dealerHitTotal === hitTotal){
return console.log('you push');
}
else{
return console.log('dealer has ' + dealerHitTotal + ' You Win');
}
}
else{
console.log("dealer hits and his new hand is " + dealerHitTotal + " " + "[" + cardHolderDealer + "]");
return dealerHit();
}
}
if (total > 21){
console.log("you busted");
}
else if (hitOrStay === "Hit" || hitOrStay === "hit"){
Hit();
}
else{
console.log("you have chosen to stay");
console.log("dealer shows "+ dealerSum + " " + "[" + cardHolderDealer + "]");
return dealerHit();
}
}
if (playerSum === 21){
return console.log(playerSum + "[" + cardHolderPlayer + "]" + ' you got blackjack');
}
else {
return nextMove();
}
}
dealHand();
above is my code. it took a bit over three hours. Lots is missing from real blackjack and its only heads up. Aces are always ones, for one, and because of that you nor the dealer can get blackjack with two cards though i've conditioned for this already for when i add the 11 for aces. otherwise the game works pretty well in terms of functionality. Not sure if my code is the most efficient.
Last edited by Zygote; 07-21-2012 at 06:44 PM.
|
|
|
07-21-2012, 06:49 PM
|
#27
|
|
( •_•) (•_• )
Join Date: Mar 2007
Posts: 17,698
|
Re: Learning Javascript Thread
Code:
#IWasUnAwareOfThisFeature
#include <coolbeans>
|
|
|
07-21-2012, 07:12 PM
|
#28
|
|
veteran
Join Date: Sep 2004
Posts: 2,814
|
Re: Learning Javascript Thread
Quote:
Originally Posted by e i pi
If you're not going to be programming games is there any reason to get into javascript beyond playing around with jquery for various design purposes? The whole anonymous function thing and language in general seemed very odd to me coming from python, php and a bit of C.
|
You can use javascript to replace php because javascript can run on the server. So the use case of JS is anything really.
|
|
|
07-21-2012, 07:22 PM
|
#29
|
|
newbie
Join Date: Nov 2009
Posts: 39
|
Re: Learning Javascript Thread
Quote:
Originally Posted by Zygote
above is my code. it took a bit over three hours. Lots is missing from real blackjack and its only heads up. Aces are always ones, for one, and because of that you nor the dealer can get blackjack with two cards though i've conditioned for this already for when i add the 11 for aces. otherwise the game works pretty well in terms of functionality. Not sure if my code is the most efficient.
|
see, not too tough of a project if you go bare bones  .
just a quick glance over, you're dealing infinity-deck blackjack. if you want to go single deck, you can do card = deck.pop(); or you can do card = deck[deckPosition]; and increment deckPosition. maybe i'll post poor man's blackjack later.
|
|
|
07-21-2012, 07:33 PM
|
#30
|
|
Pooh-Bah
Join Date: Jan 2005
Posts: 5,652
|
Re: Learning Javascript Thread
Quote:
Originally Posted by Loc
see, not too tough of a project if you go bare bones  .
just a quick glance over, you're dealing infinity-deck blackjack. if you want to go single deck, you can do card = deck.pop(); or you can do card = deck[deckPosition]; and increment deckPosition. maybe i'll post poor man's blackjack later.
|
try count that deck MIT bj team.
the project wasn't easy for me and im quite amazed i actually made it through tbh. Is my code in need of a lot of improvement or is that a pretty succinct effort at the task?
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -4. The time now is 05:12 PM.
|