Old 24-03-2007, 19:15   #1 (permalink)
Intrinzik.v2
NeverFuckWithoutaRubber
 
Intrinzik.v2's Avatar
 
Join Date: Nov 2006
Location: Monticello In
Posts: 259
Send a message via AIM to Intrinzik.v2 Send a message via MSN to Intrinzik.v2 Send a message via Yahoo to Intrinzik.v2 Send a message via Skype™ to Intrinzik.v2
Jacascript Help

So I have an assignment and it's to create a "War" based card game. For those unfimiliar with it, basically you start out with a deck of cards and each person draws one card, the highest card wins and the winner keeps both cards until the game is over and then the cards are counted up and you see who wins. Ive been sifting through my notes/homework from the class aswell as the internet but I couldnt come up with anything.

If anyone could point me in the right direction I would greatly appreciate it.
  Reply With Quote
Old 24-03-2007, 19:34   #2 (permalink)
ludesign
ExCoder
 
ludesign's Avatar
 
Join Date: Mar 2006
Location: In The Darkness
Posts: 33
Send a message via ICQ to ludesign Send a message via MSN to ludesign Send a message via Yahoo to ludesign Send a message via Skype™ to ludesign
Heya,

So what you are looking for? Do you need the algorythm or just ideas how you can implement the game rules with javascript?

I know this game very well, I've played it alot when I was a child.
  Reply With Quote
Old 24-03-2007, 19:38   #3 (permalink)
Intrinzik.v2
NeverFuckWithoutaRubber
 
Intrinzik.v2's Avatar
 
Join Date: Nov 2006
Location: Monticello In
Posts: 259
Send a message via AIM to Intrinzik.v2 Send a message via MSN to Intrinzik.v2 Send a message via Yahoo to Intrinzik.v2 Send a message via Skype™ to Intrinzik.v2
Anything would be appreciated, ive lost all interest in this class the past couple of weeks so im lost.
  Reply With Quote
Old 24-03-2007, 19:56   #4 (permalink)
ludesign
ExCoder
 
ludesign's Avatar
 
Join Date: Mar 2006
Location: In The Darkness
Posts: 33
Send a message via ICQ to ludesign Send a message via MSN to ludesign Send a message via Yahoo to ludesign Send a message via Skype™ to ludesign
Quote:
Originally Posted by Intrinzik.v2
Anything would be appreciated, ive lost all interest in this class the past couple of weeks so im lost.



Okey.

First you should have all cards stored into an array. Some example code:
Code:
cards = ['2_heart', '2_spade', '2_diamond', '2_club', .... ];

Now you should mess up the order in the array before each new game. You can sort it by diff. criteria.

Then you should split the array on half (one part for each player).
Some example code:
Code:
player1 = ['...']; player2 = ['...'];

And you can start playing the game. You can compare the cards like this:
Code:
player1[x] > player2[x]

If the statement is true player1 wins, otherwise player2 wins and you should run one extra check:
Code:
player1[x] == player2[x]

In this case you have War.

Basically this is how I would do it. Good luck.
  Reply With Quote
Old 24-03-2007, 20:09   #5 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
Quote:
Originally Posted by ludesign
Code:
cards = ['2_heart', '2_spade', '2_diamond', '2_club', .... ];

you could probably make it a bit easier on yourself by disreguarding suit considering suit does not matter in this game.

Code:
cards = [2, 2, 2, 2, 3, 3, 3, 3, .... ];
then you don't have to split the sting to compare numbers.
  Reply With Quote
Old 24-03-2007, 20:14   #6 (permalink)
ludesign
ExCoder
 
ludesign's Avatar
 
Join Date: Mar 2006
Location: In The Darkness
Posts: 33
Send a message via ICQ to ludesign Send a message via MSN to ludesign Send a message via Yahoo to ludesign Send a message via Skype™ to ludesign
Hi,

@d3mcfadden Yes, of course you should use only number in your array to avoid any unnecessary extra code to your script. My code was just an example how the array should be structured, so I decide to put the suits to make it more clear/easy to understood.
  Reply With Quote
Old 24-03-2007, 20:16   #7 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
Quote:
Originally Posted by ludesign
Hi,

@d3mcfadden Yes, of course you should use only number in your array to avoid any unnecessary extra code to your script. My code was just an example how the array should be structured, so I decide to put the suits to make it more clear/easy to understood.
gotcha.
  Reply With Quote
Old 24-03-2007, 20:17   #8 (permalink)
Intrinzik.v2
NeverFuckWithoutaRubber
 
Intrinzik.v2's Avatar
 
Join Date: Nov 2006
Location: Monticello In
Posts: 259
Send a message via AIM to Intrinzik.v2 Send a message via MSN to Intrinzik.v2 Send a message via Yahoo to Intrinzik.v2 Send a message via Skype™ to Intrinzik.v2
ok So I got the array setup;

Code:
<html> <head> <script type="text/javascript"> var cards = 52 cards[0] = "2 of Hearts" cards[1] = "3 of Hearts" cards[2] = "4 of Hearts" cards[3] = "5 of Hearts" cards[4] = "6 of Hearts" cards[5] = "7 of Hearts" cards[6] = "8 of Hearts" cards[7] = "9 of Hearts" cards[8] = "10 of Hearts" cards[9] = "Jack of Hearts" cards[10] = "Queen of Hearts" cards[11] = "King of Hearts" cards[12] = "Ace of Hearts" cards[13] = "2 of Diamonds" cards[14] = "3 of Diamonds" cards[15] = "4 of Diamonds" cards[16] = "5 of Diamonds" cards[17] = "6 of Diamonds" cards[18] = "7 of Diamonds" cards[19] = "8 of Diamonds" cards[20] = "9 of Diamonds" cards[21] = "10 of Diamonds" cards[22] = "Jack of Diamonds" cards[23] = "Queen of Diamonds" cards[24] = "King of Diamonds" cards[25] = "Ace of Diamonds" cards[26] = "2 of Spades" cards[27] = "3 of Spades" cards[28] = "4 of Spades" cards[29] = "5 of Spades" cards[30] = "6 of Spades" cards[31] = "7 of Spades" cards[32] = "8 of Spades" cards[33] = "9 of Spades" cards[34] = "10 of Spades" cards[35] = "Jack of Spades" cards[36] = "Queen of Spades" cards[37] = "King of Spades" cards[38] = "Ace of Spades" cards[39] = "2 of Clubs" cards[40] = "3 of Clubs" cards[41] = "4 of Clubs" cards[42] = "5 of Clubs" cards[43] = "6 of Clubs" cards[44] = "7 of Clubs" cards[45] = "8 of Clubs" cards[46] = "9 of Clubs" cards[47] = "10 of Clubs" cards[48] = "Jack of Clubs" cards[49] = "Queen of Clubs" cards[50] = "King of Clubs" cards[51] = "Ace of Clubs"

And I was looking for a Randomizing function which I thnk I found but didnt implement it right;

Original:
Code:
function fisherYates ( myArray ) { var i = myArray.length; if ( i == 0 ) return false; while ( --i ) { var j = Math.floor( Math.random() * ( i + 1 ) ); var tempi = myArray[i]; var tempj = myArray[j]; myArray[i] = tempj; myArray[j] = tempi; } }

Mine:
Code:
function randomize ( cards ) { var i = cards.51; if ( i == 0 ) return false; while ( --i ) { var j = Math.floor( Math.random() * ( i + 1 ) ); var tempi = cards[i]; var tempj = cards[j]; cards[i] = tempj; cards[j] = tempi; } }

Am I doing it right or atleast coming close?
  Reply With Quote
Old 24-03-2007, 20:28   #9 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
your array is fucked. use numbers.
  Reply With Quote
Old 24-03-2007, 20:29   #10 (permalink)
Intrinzik.v2
NeverFuckWithoutaRubber
 
Intrinzik.v2's Avatar
 
Join Date: Nov 2006
Location: Monticello In
Posts: 259
Send a message via AIM to Intrinzik.v2 Send a message via MSN to Intrinzik.v2 Send a message via Yahoo to Intrinzik.v2 Send a message via Skype™ to Intrinzik.v2
Quote:
Originally Posted by d3mcfadden
your array is fucked. use numbers.

Why? Im not worried about file size or the extra time it would take to type it out; I want to keep this as simple as possible for me.

Or is there a specific reason why this wouldnt work?
  Reply With Quote
Old 24-03-2007, 20:34   #11 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
how are you going to compare this "Jack of Diamonds" to this "3 of Hearts"...?

has shit to do with filesize
  Reply With Quote
Old 24-03-2007, 20:38   #12 (permalink)
Intrinzik.v2
NeverFuckWithoutaRubber
 
Intrinzik.v2's Avatar
 
Join Date: Nov 2006
Location: Monticello In
Posts: 259
Send a message via AIM to Intrinzik.v2 Send a message via MSN to Intrinzik.v2 Send a message via Yahoo to Intrinzik.v2 Send a message via Skype™ to Intrinzik.v2
ohhh I see
  Reply With Quote
Old 24-03-2007, 20:46   #13 (permalink)
Intrinzik.v2
NeverFuckWithoutaRubber
 
Intrinzik.v2's Avatar
 
Join Date: Nov 2006
Location: Monticello In
Posts: 259
Send a message via AIM to Intrinzik.v2 Send a message via MSN to Intrinzik.v2 Send a message via Yahoo to Intrinzik.v2 Send a message via Skype™ to Intrinzik.v2
Code:
<html> <head> <script type="text/javascript"> var cards = new Array (52) cards[0] = "2" cards[1] = "3" cards[2] = "4" cards[3] = "5" cards[4] = "6" cards[5] = "7" cards[6] = "8" cards[7] = "9" cards[8] = "10" cards[9] = "11" cards[10] = "12" cards[11] = "13" cards[12] = "14" cards[13] = "2" cards[14] = "3" cards[15] = "4" cards[16] = "5" cards[17] = "6" cards[18] = "7" cards[19] = "8" cards[20] = "9" cards[21] = "10" cards[22] = "11" cards[23] = "12" cards[24] = "13" cards[25] = "14" cards[26] = "2" cards[27] = "3" cards[28] = "4" cards[29] = "5" cards[30] = "6" cards[31] = "7" cards[32] = "8" cards[33] = "9" cards[34] = "10" cards[35] = "11" cards[36] = "12" cards[37] = "13" cards[38] = "14" cards[39] = "2" cards[40] = "3" cards[41] = "4" cards[42] = "5" cards[43] = "6" cards[44] = "7" cards[45] = "8" cards[46] = "9" cards[47] = "10" cards[48] = "11" cards[49] = "12" cards[50] = "13" cards[51] = "14" function randomize ( cards ) { var i = cards.51; if ( i == 0 ) return false; while ( --i ) { var j = Math.floor( Math.random() * ( i + 1 ) ); var tempi = cards[i]; var tempj = cards[j]; cards[i] = tempj; cards[j] = tempi; } } </script> </head> <body> </body> </html>

Better?
  Reply With Quote
Old 24-03-2007, 20:51   #14 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
might need to check your syntax here too..
Quote:
var cards = 52

var i = cards.51;
  Reply With Quote
Old 24-03-2007, 20:52   #15 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
Quote:
Better?

yes. much. now drop your quotes around your integers, making them indeed integers and not strings and then fix your "cards.51" line...
  Reply With Quote
Old 24-03-2007, 20:57   #16 (permalink)
Intrinzik.v2
NeverFuckWithoutaRubber
 
Intrinzik.v2's Avatar
 
Join Date: Nov 2006
Location: Monticello In
Posts: 259
Send a message via AIM to Intrinzik.v2 Send a message via MSN to Intrinzik.v2 Send a message via Yahoo to Intrinzik.v2 Send a message via Skype™ to Intrinzik.v2
ok thanks;

So the randomizing function is correct?

If it is, would it make sense to have 2 randomizing functions? 1 for the player and 1 for the computer, then have them compared, and have points added accordinlgy or is there a better way?
  Reply With Quote
Old 24-03-2007, 20:59   #17 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
rendomize once. then spilt the random cards array into the player 1 / player 2 arrays
  Reply With Quote
Old 24-03-2007, 21:13   #18 (permalink)
ludesign
ExCoder
 
ludesign's Avatar
 
Join Date: Mar 2006
Location: In The Darkness
Posts: 33
Send a message via ICQ to ludesign Send a message via MSN to ludesign Send a message via Yahoo to ludesign Send a message via Skype™ to ludesign
Hi,

As I can see, you have made the most of the game so far. Here is a simple demo of such kind of game I just wrote. It's not fully functional.

Code:
/* create the array with all cards number inside for Jack Queen King and ect. non number cards use numbers too. For example use 11 for Jack, 12 for Queen, 13 for King and 14 for Ace */ cards = [ 2, 14, 2, 14, 3, 13, 3, 13, 4, 12, 4, 12, 5, 11, 5, 11, 6, 10, 6, 10, 7, 9, 7, 9, 8, 8, 8, 8, 9, 7, 9, 7, 10, 6, 10, 6, 11, 5, 11, 5, 12, 4, 12, 4, 13, 3, 13, 3, 14, 2, 14, 2 ]; // this is random number generator function sortRandom() { return ( Math.round( Math.random() ) - 0.7 ); } // now we are sorting the array by random cards.sort( sortRandom ); var player1 = new Array(); var player2 = new Array(); // once the array is sorted, it's time to split it for(i=0; i < cards.length; i++) { i < 26 ? player1[i] = cards[i] : player2[i-26] = cards[i] ; } // now we can start playing the game. var x = 0; var player1score = 0; var player2score = 0; while( --player1.length ) { // let's compare player1[x] > player2[x] ? player1score++ : player2score++ ; x++; } document.write( 'Score for player #1: ' + player1score + '<br />'); document.write( 'Score for player #2: ' + player2score );

Of course you can modify the code and make it better. In this version 'War!' is not detected and player 2 will win 95% of all games. You can improve the 'array split' code to split the array in better way.

Good Luck
  Reply With Quote
Old 24-03-2007, 21:14   #19 (permalink)
Intrinzik.v2
NeverFuckWithoutaRubber
 
Intrinzik.v2's Avatar
 
Join Date: Nov 2006
Location: Monticello In
Posts: 259
Send a message via AIM to Intrinzik.v2 Send a message via MSN to Intrinzik.v2 Send a message via Yahoo to Intrinzik.v2 Send a message via Skype™ to Intrinzik.v2
any help with that? Only thing I could come up with is this Javascript Tutorial - Split which seems would fit for what Im doing but I could be wrong.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8