行数が多いので見にくい。JavaScriptにはJavaのimportにあたるような機能はないのかな?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Putting Objects to Use | |
// make a Card constructor here. It should take two parameters | |
// representing the suit and number of the card to be created | |
function Card(suit, number){ | |
this.suit = suit; | |
this.number = number; | |
} | |
// 2. Get at it | |
// make a Card constructor here. It should take two parameters | |
// representing the suit and number of the card to be created | |
function Card(suit, number){ | |
this.suit = suit; | |
this.number = number; | |
this.getSuit = function() { | |
return this.suit; | |
} | |
this.getNumber = function() { | |
return this.number; | |
} | |
} | |
// 3. Aces up the sleeve | |
// change our card constructor to make suit and number private | |
function Card(s, n) { | |
var suit = s; | |
var number = n; | |
} | |
// let's say someone is initially dealt bad cards | |
var card1 = new Card(2, 7); | |
var card2 = new Card(3, 6); | |
// but they have figured out a way to cheat our system! | |
function cheat() { | |
if(card1.hasOwnProperty("number")) { | |
console.log("I first have a "+card1.number+" and a "+card2.number); | |
card1.number = 13; | |
card2.number = 1; | |
console.log("haha now I have blackjack!"); | |
} | |
} | |
cheat(); | |
// 4. Deal them out | |
// Make your card constructor again here, but make sure to use private | |
// variables! | |
function Card(suit, number){ | |
var suit = suit; | |
var number = number; | |
this.getSuit = function() { | |
return suit; | |
} | |
this.getNumber = function() { | |
return number; | |
} | |
} | |
// Make a deal function here. It should return a new card with a suit | |
// that is a random number from 1 to 4, and a number that is a random | |
// number between 1 and 13 | |
var deal = function() { | |
var suit = Math.floor(Math.random() * 4) + 1; | |
var number = Math.floor(Math.random() * 13) + 1; | |
return new Card(suit, number); | |
} | |
// examples of the deal function in action | |
var card1 = deal(); | |
var card2 = deal(); | |
// 5. Bring Scoring Back | |
// Make your card constructor again here, but make sure to use private | |
// variables! | |
function Card(suit, number){ | |
var suit = suit; | |
var number = number; | |
this.getSuit = function() { | |
return suit; | |
} | |
this.getNumber = function() { | |
return number; | |
} | |
this.getValue = function() { | |
if(11 <= number) { | |
return 10; | |
} | |
if(1 === number) { | |
return 11; | |
} | |
return number; | |
} | |
} | |
// Make a deal function here. It should return a new card with a suit | |
// that is a random number from 1 to 4, and a number that is a random | |
// number between 1 and 13 | |
var deal = function() { | |
var suit = Math.floor(Math.random * 4) + 1; | |
var number = Math.floor(Math.random * 13) + 1; | |
return new Card(suit, number); | |
} | |
// examples of the deal function in action | |
var card1 = deal(); | |
var card2 = deal(); | |
// 6. Give me a hand | |
// Make your card constructor again here, but make sure to use private | |
// variables! | |
function Card(suit, number){ | |
var suit = suit; | |
var number = number; | |
this.getSuit = function() { | |
return suit; | |
} | |
this.getNumber = function() { | |
return number; | |
} | |
this.getValue = function() { | |
if(11 <= number) { | |
return 10; | |
} | |
if(1 === number) { | |
return 11; | |
} | |
return number; | |
} | |
} | |
// Make a deal function here. It should return a new card with a suit | |
// that is a random number from 1 to 4, and a number that is a random | |
// number between 1 and 13 | |
var deal = function() { | |
var suit = Math.floor(Math.random() * 4) + 1; | |
var number = Math.floor(Math.random() * 13) + 1; | |
return new Card(suit, number); | |
} | |
function Hand() { | |
this.card1 = deal(); | |
this.card2 = deal(); | |
} | |
// 7. Finishing Touches | |
// Make your card constructor again here, but make sure to use private | |
// variables! | |
function Card(suit, number){ | |
var suit = suit; | |
var number = number; | |
this.getSuit = function() { | |
return suit; | |
} | |
this.getNumber = function() { | |
return number; | |
} | |
this.getValue = function() { | |
if(11 <= number) { | |
return 10; | |
} | |
if(1 === number) { | |
return 11; | |
} | |
return number; | |
} | |
} | |
// Make a deal function here. It should return a new card with a suit | |
// that is a random number from 1 to 4, and a number that is a random | |
// number between 1 and 13 | |
var deal = function() { | |
var suit = Math.floor(Math.random() * 4) + 1; | |
var number = Math.floor(Math.random() * 13) + 1; | |
return new Card(suit, number); | |
} | |
function Hand() { | |
this.card1 = deal(); | |
this.card2 = deal(); | |
this.score = function() { | |
return this.card1.getValue() + this.card2.getValue(); | |
} | |
} | |
var myHand = new Hand(); var yourHand = new Hand(); | |
console.log("I scored a "+myHand.score()+" and you scored a "+ yourHand.score()); | |
if(yourHand.score() > myHand.score()) console.log("you win!"); else if(yourHand.score() < myHand.score()) console.log("I win!"); else console.log("We tied!"); | |
// 8. Extra Credit and Future Thoughts | |
// no changes. |
0 件のコメント:
コメントを投稿