Two Plus Two Newer Archives  

Go Back   Two Plus Two Newer Archives > Other Topics > Science, Math, and Philosophy
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #21  
Old 05-16-2006, 08:37 PM
DougShrapnel DougShrapnel is offline
Senior Member
 
Join Date: Oct 2004
Posts: 2,155
Default Re: Computer science java script project

[ QUOTE ]
[ QUOTE ]
Btw- I think this line is wrong.

while(random1 == (random2 || random3 || random4 || random5))

If you still have a chance to drop this class, you probably should. I think you are in way above your head.

[/ QUOTE ]

why is it wrong? the while statement makes it so that the same card won't show up twice.

i have all the hands done. now i gotta add the betting.
here's what i have so far. http://dan.122blue.com/

[/ QUOTE ]It's wrong because try and shuffle 52 cards that way. It's lame. It's not encapsulated or polymorphic. It's also wrong because I just don't see what it does?

Doesn't this "random2 || random3 || random4 || random5)" evalute to true or false? So, while(random == (true Or false) is always false? Now even if I am wrong about the expression what if random2 and random5 are the same. It's a shoddy way to code.

Create a Card object. Create a Deck object with a shuffle and deal methods and a 52 card object array. Use deck.shuffle() and deck.deal() methods.
Reply With Quote
  #22  
Old 05-16-2006, 09:01 PM
madnak madnak is offline
Senior Member
 
Join Date: Aug 2005
Location: Brooklyn (Red Hook)
Posts: 5,271
Default Re: Computer science java script project

I think you guys are going overboard. He's just getting confused. Any solution he comes up with is going to be pretty dirty. He's not going to be willing or able to implement a Knuth shuffle, and he probably doesn't even know what an object is.
Reply With Quote
  #23  
Old 05-16-2006, 09:06 PM
TomCollins TomCollins is offline
Senior Member
 
Join Date: Jul 2003
Location: Approving of Iron\'s Moderation
Posts: 7,517
Default Re: Computer science java script project

[ QUOTE ]
I think you guys are going overboard. He's just getting confused. Any solution he comes up with is going to be pretty dirty. He's not going to be willing or able to implement a Knuth shuffle, and he probably doesn't even know what an object is.

[/ QUOTE ]

A Knuth shuffle is actually simpler than what hes doing.
Reply With Quote
  #24  
Old 05-16-2006, 09:13 PM
madnak madnak is offline
Senior Member
 
Join Date: Aug 2005
Location: Brooklyn (Red Hook)
Posts: 5,271
Default Re: Computer science java script project

Easier in terms of implementation, not in terms of theory. And it wouldn't be simpler given his setup. Remember he's using the array index to determine rank and suit, the array has to remain static.
Reply With Quote
  #25  
Old 05-16-2006, 09:20 PM
TomCollins TomCollins is offline
Senior Member
 
Join Date: Jul 2003
Location: Approving of Iron\'s Moderation
Posts: 7,517
Default Re: Computer science java script project

[ QUOTE ]
Easier in terms of implementation, not in terms of theory. And it wouldn't be simpler given his setup. Remember he's using the array index to determine rank and suit, the array has to remain static.

[/ QUOTE ]

He only needs to make an array of 52 integers, and swap 5 times. Seems pretty simple to me.

It's funny because we use this exact question to interview people.
Reply With Quote
  #26  
Old 05-16-2006, 09:22 PM
DougShrapnel DougShrapnel is offline
Senior Member
 
Join Date: Oct 2004
Posts: 2,155
Default Re: Computer science java script project

[ QUOTE ]
I think you guys are going overboard. He's just getting confused. Any solution he comes up with is going to be pretty dirty. He's not going to be willing or able to implement a Knuth shuffle, and he probably doesn't even know what an object is.

[/ QUOTE ]

fine, I'll be nice
All of this

[ QUOTE ]
{
random1=Math.floor(Math.random()*52);
random2=Math.floor(Math.random()*52);
random3=Math.floor(Math.random()*52);
random4=Math.floor(Math.random()*52);
random5=Math.floor(Math.random()*52);
message = "";

while(random1 == (random2 || random3 || random4 || random5))
{
random1 = Math.floor(Math.random()*52);
}
while(random2 == (random1 || random3 || random4 || random5))
{
random2 = Math.floor(Math.random()*52);
}
while(random3 == (random1 || random2 || random4 || random5))
{
random3 = Math.floor(Math.random()*52);
}
while(random4 == (random1 || random2 || random3 || random5))
{
random4 = Math.floor(Math.random()*52);
}
while(random5 == (random1 || random2 || random3 || random4))
{
random5 = Math.floor(Math.random()*52);
}



[/ QUOTE ] Becomes this. You should just use the shuffle they are talking about. But if you insist on doing it your way.
random1=Math.floor(Math.random()*52);

do(
random2=Math.floor(Math.random()*52)
while(random2 == random1)

do(
random3=Math.floor(Math.random()*52)
while((random1 == random3) Or (random2 == random3))

do(
random4=Math.floor(Math.random()*52)
while(
(random4 == random3) OR (random4 == random2) OR (random4 == random1) Or (random4 == random1))

random5=Math.floor(Math.random()*52);
I'll let him type out #5
message = "";

edit and change random1, random2... to card1, card2.. for readability reasons.
Reply With Quote
  #27  
Old 05-16-2006, 09:32 PM
madnak madnak is offline
Senior Member
 
Join Date: Aug 2005
Location: Brooklyn (Red Hook)
Posts: 5,271
Default Re: Computer science java script project

Right now he just needs to alter his conditional statements and he's all set. The fact it's an interview question is a perfect example of why it's too much; this is something a software engineer is supposed to know, not a guy who took an intro course to grab a credit.
Reply With Quote
  #28  
Old 05-16-2006, 10:01 PM
hurricaneace67 hurricaneace67 is offline
Member
 
Join Date: May 2006
Posts: 45
Default Re: Computer science java script project

well this is a high school level computer tools I and II class. i havn't learned about a lot of what you guys are saying, and i think my teacher would find it odd if my code ha d all this stuff we had never learned. the while loop serves it's purpose for me though. it makes it so that 5 different cards come up. i realize that it might not be the best shuffling method, but again this is just a high school project.

my code might be ugly, but it is working perfectly right now. now i am working on adding betting and so that you can check a box to hold cards.

<html>

<head>
<title>Dan's Cards</title>

<script>
var random1;
var ramdom2;
var random3;
var random4;
var random5;
var cards = new Array();
cards[0]="ah.jpg";
cards[1]="2h.jpg";
cards[2]="3h.jpg";
cards[3]="4h.jpg";
cards[4]="5h.jpg";
cards[5]="6h.jpg";
cards[6]="7h.jpg";
cards[7]="8h.jpg";
cards[8]="9h.jpg";
cards[9]="10h.jpg";
cards[10]="jh.jpg";
cards[11]="qh.jpg";
cards[12]="kh.jpg";
cards[13]="ad.jpg";
cards[14]="2d.jpg";
cards[15]="3d.jpg";
cards[16]="4d.jpg";
cards[17]="5d.jpg";
cards[18]="6d.jpg";
cards[19]="7d.jpg";
cards[20]="8d.jpg";
cards[21]="9d.jpg";
cards[22]="10d.jpg";
cards[23]="jd.jpg";
cards[24]="qd.jpg";
cards[25]="kd.jpg";
cards[26]="as.jpg";
cards[27]="2s.jpg";
cards[28]="3s.jpg";
cards[29]="4s.jpg";
cards[30]="5s.jpg";
cards[31]="6s.jpg";
cards[32]="7s.jpg";
cards[33]="8s.jpg";
cards[34]="9s.jpg";
cards[35]="10s.jpg";
cards[36]="js.jpg";
cards[37]="qs.jpg";
cards[38]="ks.jpg";
cards[39]="ac.jpg";
cards[40]="2c.jpg";
cards[41]="3c.jpg";
cards[42]="4c.jpg";
cards[43]="5c.jpg";
cards[44]="6c.jpg";
cards[45]="7c.jpg";
cards[46]="8c.jpg";
cards[47]="9c.jpg";
cards[48]="10c.jpg";
cards[49]="jc.jpg";
cards[50]="qc.jpg";
cards[51]="kc.jpg";

function deal()
{




random1=Math.floor(Math.random()*52);
random2=Math.floor(Math.random()*52);
random3=Math.floor(Math.random()*52);
random4=Math.floor(Math.random()*52);
random5=Math.floor(Math.random()*52);
/*random1 = 1;
random2 = 2;
random3 = 3;
random4 = 4;
random5 = 5;*/


while(random1 == (random2 || random3 || random4 || random5))
{
random1 = Math.floor(Math.random()*52);
}
while(random2 == (random1 || random3 || random4 || random5))
{
random2 = Math.floor(Math.random()*52);
}
while(random3 == (random1 || random2 || random4 || random5))
{
random3 = Math.floor(Math.random()*52);
}
while(random4 == (random1 || random2 || random3 || random5))
{
random4 = Math.floor(Math.random()*52);
}
while(random5 == (random1 || random2 || random3 || random4))
{
random5 = Math.floor(Math.random()*52);
}

document.card1.src= './cards/' + cards[random1];
document.card2.src= './cards/' + cards[random2];
document.card3.src= './cards/' + cards[random3];
document.card4.src= './cards/' + cards[random4];
document.card5.src= './cards/' + cards[random5];

//Four of a kind
if(random1%13==random2%13 && random2%13==random3%13 && random3%13==random4%13 ||

random1%13==random2%13 && random2%13==random3%13 && random3%13==random5%13 ||

random1%13==random2%13 && random2%13==random4%13 && random4%13==random5%13 ||

random1%13==random3%13 && random3%13==random4%13 && random4%13==random5%13 ||

random2%13==random3%13 && random3%13==random4%13 && random4%13==random5%13)
{
alert("You got Four of a kind!");
}
else if(
(random1 >= 0 && random1 <= 12) && (random2 >= 0 && random2 <= 12) && (random3 >= 0 && random3 <= 12) && (random4 >= 0 && random4 <= 12) && (random5 >= 0 && random5 <= 12)
(random1 >= 13 && random1 <= 25) && (random2 >= 13 && random2 <= 25) && (random3 >= 13 && random3 <= 25) && (random4 >= 13 && random4 <= 25) && (random5 >= 13 && random5 <= 25)
(random1 >= 26 && random1 <= 38) && (random2 >= 26 && random2 <= 38) && (random3 >= 26 && random3 <= 38) && (random4 >= 26 && random4 <= 38) && (random5 >= 26 && random5 <= 38)
(random1 >= 39 && random1 <= 51) && (random2 >= 39 && random2 <= 51) && (random3 >= 39 && random3 <= 51) && (random4 >= 39 && random4 <= 51) && (random5 >= 39 && random5 <= 51))
{
alert("You got a Flush!");
}
//Three of a kind
else if(random1%13==random2%13 && random2%13==random3%13 ||
random1%13==random2%13 && random2%13==random4%13 ||
random1%13==random2%13 && random2%13==random5%13 ||
random1%13==random3%13 && random3%13==random4%13 ||
random1%13==random3%13 && random3%13==random5%13 ||
random1%13==random4%13 && random4%13==random5%13 ||
random2%13==random3%13 && random3%13==random4%13 ||
random2%13==random3%13 && random3%13==random5%13 ||
random2%13==random4%13 && random4%13==random5%13 ||
random3%13==random4%13 && random4%13==random5%13)
{
alert("You got Three of a kind!");
}
else if(random1 % 13 == random2 % 13 && random3 % 13 == random4 % 13 ||
random1 % 13 == random2 % 13 && random3 % 13 == random5 % 13 ||
random1 % 13 == random2 % 13 && random4 % 13 == random5 % 13 ||
random1 % 13 == random3 % 13 && random4 % 13 == random5 % 13 ||
random1 % 13 == random3 % 13 && random2 % 13 == random4 % 13 ||
random1 % 13 == random3 % 13 && random2 % 13 == random5 % 13 ||
random1 % 13 == random4 % 13 && random2 % 13 == random3 % 13 ||
random1 % 13 == random4 % 13 && random2 % 13 == random5 % 13 ||
random1 % 13 == random4 % 13 && random3 % 13 == random5 % 13 ||
random1 % 13 == random5 % 13 && random2 % 13 == random3 % 13 ||
random1 % 13 == random5 % 13 && random2 % 13 == random4 % 13 ||
random1 % 13 == random5 % 13 && random3 % 13 == random4 % 13)
{
alert("You got Two Pair!");
}

else if(random1 %13 == random2 %13 || random1 %13 == random3 %13 ||
random1 %13 == random4 %13 || random1 %13 == random5 %13 ||
random2 %13 == random3 %13 || random2 %13 == random4 %13 ||
random2 %13 == random5 %13 || random3 %13 == random4 %13 ||
random3 %13 == random5 %13 || random4 %13 == random5 %13)
{
alert("You got a pair!");
}
else
{
alert("Sorry, you didn't win");
}


}

function reset_cards()
{
document.card1.src= './cards/card.jpg';
document.card2.src= './cards/card.jpg';
document.card3.src= './cards/card.jpg';
document.card4.src= './cards/card.jpg';
document.card5.src= './cards/card.jpg';
}
</script>

</head>

<body>
<form name="myForm">
<table align='center' border='1'>
<tr align='center'><td colspan='5'>Cards</td></tr>
<tr><td><img id='card1' src='./cards/card.jpg'></td><td><img id='card2' src='./cards/card.jpg'></td><td><img id='card3' src='./cards/card.jpg'></td><td><img id='card4' src='./cards/card.jpg'></td><td><img id='card5' src='./cards/card.jpg'></td></tr>
<tr align='center'><td id='the_message' colspan='5'>Click the 'deal cards' button to deal your hand.</td></tr>
<tr align='center'><td colspan='5'><input type='button' value='Deal Cards' onclick='deal()'> - <input type='button' value='Reset Cards' onclick='reset_cards();' /></td></tr>
<tr><th colspan='5'>Betting Center</th></tr>
<tr><td colspan='5'>How much do you want to bet? $<input type='text' name='betBox'></td></tr>
<tr><td colspan='5'>In the bank: $<input type='text' value='500' name='bank'></td></tr>
</table>
</form>

</body>

</html>
Reply With Quote
  #29  
Old 05-16-2006, 10:06 PM
madnak madnak is offline
Senior Member
 
Join Date: Aug 2005
Location: Brooklyn (Red Hook)
Posts: 5,271
Default Re: Computer science java script project

It's not working perfectly, you need to change the (random2 || random3 || random4 || random5) statement or you'll be in danger of having duplicate cards.
Reply With Quote
  #30  
Old 05-16-2006, 10:37 PM
hurricaneace67 hurricaneace67 is offline
Member
 
Join Date: May 2006
Posts: 45
Default Re: Computer science java script project

GRRRRRR. you are right. it doesnt happen a lot, but i just got two 6 diamonds..
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:37 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.