typescriptの勉強を始めたので、とりあえずじゃんけんゲームを作成してみました。
全体コード
import * as readline from "readline";
let gamemasterhands: number = 0;
// インターフェースを作成
const rl = readline.createInterface({
input: process.stdin, // 標準入力(キーボードからの入力)
output: process.stdout // 標準出力(コンソールへの出力)
});
// ユーザーに質問
rl.question('ゲームを開始しますか? はい | いいえ :', (answer: string) => {
if(answer === "はい"){
explanation();
}
else{
console.log("また遊んでね!")
// 入力が完了したらインターフェースを閉じる
rl.close();
}
});
//ゲーム説明
function explanation(): void{
console.log("最初はグーの掛け声の後に 「[1]グー/[2]チョキ/[3]パー」 から数値で選んでください。");
console.log("じゃんけんに勝てばあなたの勝利です。");
rl.question("準備はできましたか? はい | いいえ :", (answer: string) => {
if(answer !== "はい"){
explanation();
}
else{
jyanken();
}
}
)};
//ゲーム本体
function jyanken(): void {
//あいこ用のカウンター
let count: number = 0;
// ゲームマスターの手をランダムに決定
gamemasterhands = Math.floor(Math.random() * 3) + 1;
rl.question("最初はグーじゃんけん:", (answer: string) =>{
let playerhands: number = parseInt(answer);
// 入力値が不正であれば再度入力を求める
//isNaN(playerhands)は数値に変換できる値かを判定
if (isNaN(playerhands) || playerhands < 1 || playerhands > 3) {
console.log("無効な入力です。1, 2, 3 のいずれかを入力してください。");
jyanken(); // 再度じゃんけんを開始
return; // ここで関数を抜ける
}
//勝敗判定
if(playerhands == gamemasterhands){
console.log(`あなたは${handname(playerhands)}を出しました`);
console.log(`相手はは${handname(gamemasterhands)}を出しました`);
console.log("あいこでした")
}
else if (
(playerhands === 1 && gamemasterhands === 2) ||
(playerhands === 2 && gamemasterhands === 3) ||
(playerhands === 3 && gamemasterhands === 1)
) {
// プレイヤーの勝ち
console.log(`あなたは${handname(playerhands)}を出しました。`);
console.log(`相手は${handname(gamemasterhands)}を出しました。`);
console.log("あなたの勝利です");
}
else {
// プレイヤーの負け
console.log(`あなたは${handname(playerhands)}を出しました。`);
console.log(`相手は${handname(gamemasterhands)}を出しました。`);
console.log("あなたの負けです");
}
rl.question('もう一度挑戦しますか? はい | いいえ :',(answer: String) => {
if(answer === 'はい'){
jyanken();
}
else{
rl.close();
}
});
});
}
function handname(handnumber: number):string{
switch(handnumber){
case 1:
return "グー";
case 2:
return "チョキ";
case 3:
return "パー";
default:
return "無効な手";
}
}
部分解説
import * as readline from "readline";
今回は、ユーザーがタームナルで入力した値が欲しいため、readlineが必要のためインポートしました。
// インターフェースを作成
const rl = readline.createInterface({
input: process.stdin, // 標準入力(キーボードからの入力)
output: process.stdout // 標準出力(コンソールへの出力)
});
ユーザーからの入力を受け付ける・取得するためにインターフェースを作成する必要がある。
rl.question("準備はできましたか? はい | いいえ :", (answer: string) => {
if(answer !== "はい"){
explanation();
}
rl.questionでユーザーからの入力を受け取ります。
文字型のみを受け取るため、数値を受け取りたい場合は数値変換してあげる必要があります。
gamemasterhands = Math.floor(Math.random() * 3) + 1;
Math.random() で0以上1未満のランダムな小数を生成(例: 0.23、0.87など)。
Math.random() * 3) これにより、0以上3未満の値(例:0~2.999...)が得られる。
Math.floor(...) 小数点以下を切り捨てて、整数に変換。これにより、0, 1, 2のいずれかになる。
+1 で 1, 2, 3のいずれかになるようにする。
if (isNaN(playerhands) || playerhands < 1 || playerhands > 3) {
console.log("無効な入力です。1, 2, 3 のいずれかを入力してください。");
jyanken(); // 再度じゃんけんを開始
return; // ここで関数を抜ける
}
isNaN(playerhands) で数値に変換できる値かを確認。
function handname(handnumber: number):string{
switch(handnumber){
case 1:
return "グー";
case 2:
return "チョキ";
case 3:
return "パー";
default:
return "無効な手";
}
}
引数に入った値で、それぞれ何を出したかを返す関数。
今後
- あいこであれば、じゃんけんを続けるような処理
- reactを使用した、画面で操作できるじゃんけんゲームの作成
を作成していく予定。