HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<h1>オブジェクト指向に則ったじゃんけんプログラム in JS</h1>
<div class="field">
<div class="con">
<h3>プレイヤー紹介</h3>
<table class="player-intro">
<thead>
<tr><th>名前</th><th>戦略</th></tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="con janken-times">
<h3>じゃんけん回数</h3>
<p><span class="janken-times-span"></span>回</div></p>
<div class="con match-history">
<h3>戦歴</h3>
</div>
<div class="con final-result">
<h3>最終結果</h3>
</div>
</div>
$base-color1: #eeeeee;
$base-color2: #393e46;
$base-color3: #222831;
$accent-color1: #00adb5;
body{
margin: 0;
font-family: sans-serif;
color: $base-color3;
background-color: $base-color2;
}
h1{
margin: 0;
padding: .5em 1em;
font-size: 1.5em;
color: $base-color1;
font-weight: normal;
text-align: center;
background-color: $accent-color1;
}
.field{
margin: 2em auto;
width: 80%;
.con{
margin: 1em auto;
background-color: $base-color1;
h3{
margin: 0;
padding: 1em;
color: $base-color1;
font-weight: normal;
text-align: center;
background-color: $base-color3;
}
}
.player-intro{
width: 100%;
margin: 1em auto;
padding: 1em;
th{
padding: 1em;
text-align: left;
border-bottom: 2px dotted $base-color3;
}
td{
padding: 1em;
}
}
.janken-times, .final-result{
p{
margin: 0;
padding: 1em;
text-align: center;
}
}
.match-history{
.history{
width: 80%;
margin: 2em auto;
padding: 2em 1em;
background-color: #ddd;
border-radius: 10px;
.player1, .player2, .result{
display: inline-block;
width: 30%;
}
.player-name, .result{
margin: 0 1em;
padding: 1em 2em;
color: $base-color1;
text-align: center;
background-color: $base-color2;
border-radius: 10px;
}
.player-hand{
width: 2em;
height: 2em;
margin: .5em auto;
padding: 1em;
font-size: 1.5em;
text-align: center;
background-color: $accent-color1;
border-radius: 50%;
}
.result{
width: 20%;
height: 100%;
background-color: $base-color3;
}
}
}
}
// じゃんけんの手を定義
class JankenHand {
constructor(char) {
this.char = char;
}
toString() {
return this.char;
}
// じゃんけんの手の文字とプログラム中の数値とをリンク
static fromInt(index) {
switch(index) {
case 0: // 0=グー
return JankenHandEnum.Rock;
break;
case 1: // 1=チョキ
return JankenHandEnum.Scissors;
break;
case 2: // 2=パー
return JankenHandEnum.Paper;
break;
default:
throw new Error("indexが不正です。" + index);
}
}
// 勝ったかどうかの判定
winTo(hisHand) {
switch(this) {
case JankenHandEnum.Rock:
return hisHand === JankenHandEnum.Scissors;
break;
case JankenHandEnum.Scissors:
return hisHand === JankenHandEnum.Paper;
break;
case JankenHandEnum.Paper:
return hisHand === JankenHandEnum.Rock;
break;
default:
throw new Error("hisHandが不正です。" + hisHand);
}
}
// 負けたかどうかの判定
loseTo(hisHand) {
return this !== hisHand && !this.winTo(hisHand);
}
}
const JankenHandEnum = {};
JankenHandEnum.Rock = new JankenHand("✊");
JankenHandEnum.Scissors = new JankenHand("✌️");
JankenHandEnum.Paper = new JankenHand("✋");
// -----------------------------------
// じゃんけん戦略-テンプレート
class JankenStrategy {
toString() {
}
nextHand() {
// なにもしない
}
prevHands() {
// なにもしない
}
}
// じゃんけん戦略-ランダムに手を出す
class RandomStrategy extends JankenStrategy {
toString(){
return "ランダム戦略";
}
nextHand() {
// 3パターンなので[0−2]の範囲でランダムな値を返すようにする
const index = Math.round(Math.random() * 2);
return JankenHand.fromInt(index);
}
}
// じゃんけん戦略-同じ手を出す
class FixedHandStrategy extends JankenStrategy {
toString(){
return "固定戦略";
}
constructor(hand) {
super();
this.hand = hand;
}
nextHand() {
// いつも決まった手を返すようにする
return this.hand;
}
}
// じゃんけん戦略-前回の試合時の手を記憶
class ChottoKashikoiStrategy extends JankenStrategy {
toString(){
return "試合記憶戦略";
}
prevHands(myHand, hisHand) {
this.myHand = myHand;
this.hisHand = hisHand;
}
nextHand() {
// 初回は前回の手がないためランダムとする
if(!this.myHand || !this.hisHand) {
return new RandomStrategy().nextHand();
}
if(this.myHand.winTo(this.hisHand)) {
// 自分が勝った場合は、同じ手を使う
return this.myHand;
} else if(this.myHand.loseTo(this.hisHand)) {
// あいてが勝った場合は、あいての手を使う
return this.hisHand;
} else {
return new RandomStrategy().nextHand();
}
}
}
// -----------------------------------
// プレイヤーの雛形、デフォルトでランダム戦略をとる
class Player {
constructor(name, strategy = new RandomStrategy(), win=0) {
this.name = name;
this.strategy = strategy;
}
getName() {
return this.name;
}
getStrategy() {
return this.strategy.toString();
}
getWin() {
return this.win;
}
nextHand() {
return this.strategy.nextHand();
}
prevHands(myHand, hisHand) {
this.strategy.prevHands(myHand, hisHand);
}
}
// -----------------------------------
const PlayerList = {};
PlayerList.p1 = new Player("Taro");
PlayerList.p2 = new Player("Hanako", new ChottoKashikoiStrategy());
let player1Win = 0;
let player2Win = 0;
for(p in PlayerList){
$(".player-intro tbody").append("<tr><td>"+ PlayerList[p].getName() +"</td><td>"+ PlayerList[p].getStrategy() +"</td></tr>");
}
const jankenTimes = 10;
$(".janken-times-span").append(jankenTimes);
// 10回じゃんけんをする
for(let i = 1; i <= jankenTimes; i++) {
$(".match-history").append('<div id="history'+i+'" class="history"></div');
const hand1 = PlayerList.p1.nextHand();
const hand2 = PlayerList.p2.nextHand();
$("#history"+i).append('<div class="player1"><div class="player-name">'
+ PlayerList.p1.getName()
+'</div><div class="player-hand">'
+ hand1
+'</div></div><div class="player2"><div class="player-name">'
+ PlayerList.p2.getName()
+'</div><div class="player-hand">'
+ hand2
+'</div></div>');
const result = hand1.winTo(hand2) ? "勝ち" : hand1.loseTo(hand2) ? "負け" : "あいこ";
if(hand1.winTo(hand2)) {
player1Win++;
$("#history"+i).append('<div class="result">'
+ PlayerList.p1.getName()
+'の勝ち!</div>');
}
else if(hand1.loseTo(hand2)) {
player2Win++;
$("#history"+i).append('<div class="result">'
+ PlayerList.p2.getName()
+'の勝ち!</div>');
}else{
$("#history"+i).append('<div class="result">'
+'あいこです!</div>');
}
// 前の手を覚えておく
PlayerList.p1.prevHands(hand1, hand2);
PlayerList.p2.prevHands(hand2, hand1);
// ここで結果を出力する
}
const finalResult = player1Win > player2Win ? PlayerList.p1.getName() :
player1Win < player2Win ? PlayerList.p2.getName() :
"even";
// ここで最終結果を出力する
if(finalResult == "even"){
$(".final-result").append("<p>引き分けです!</p>");
}else{
$(".final-result").append("<p>"+ finalResult +"の勝ちです!</p>");
}
Also see: Tab Triggers