今回は受講生が作った問題です。
オブジェクト指向を使って(クラスをつくって)問題を解いてみましょう!
問1(S氏作) 難易度★
Humanクラスを作成し、HumanAppクラスから以下の処理を実現せよ。(アクセス性はすべてpublicでよい)
[実行結果1]
現在の体重は?:>70
目標体重は?:>50
目標に向けて今日は何キロ走りますか?1…10km、2…20km、3…30km、4…現在の情報、5…終了>1
1kg減りました!この調子で頑張りましょう!
目標に向けて今日は何キロ走りますか?1…10km、2…20km、3…30km、4…現在の情報、5…終了>2
5kg減りました!この調子で頑張りましょう!
目標に向けて今日は何キロ走りますか?1…10km、2…20km、3…30km、4…現在の情報、5…終了>3
10kg減りました!この調子で頑張りましょう!
目標に向けて今日は何キロ走りますか?1…10km、2…20km、3…30km、4…現在の情報、5…終了>4
16kg痩せてます!残り4kgです!頑張りましょう!
目標に向けて今日は何キロ走りますか?1…10km、2…20km、3…30km、4…現在の情報、5…終了>3
目標達成です!Congratulation!
[実行結果2]
目標に向けて今日は何キロ走りますか?1…10km、2…20km、3…30km、4…現在の情報、5…終了>5
アプリケーションを終了します。
問2(K氏作) 難易度 ★★
適切にクラスを作成し、以下の処理を実現せよ。尚親密度は初期値5で、0でバッドエンド、10でトゥルーエンドである。
[実行例]
ユーザーネーム:A(入力)
ヒロインネーム:B(入力)
Bちゃんとデートの待ち合わせをします。どこにしますか?(1…自宅、2…公園、3…駅、4…映画館): 1
自宅に隠し持っていた、A君の秘密がバレてしまった!(親密度-2)現在3
Bちゃんとデートの待ち合わせをします。どこにしますか?(1…自宅、2…公園、3…駅、4…映画館): 2
その公園は蚊が多く、Bちゃんが嫌がってしまった!(親密度-3)現在0
バッドエンド!!
続けますか?(1…yes、2…no):yes
Bちゃんとデートの待ち合わせをします。どこにしますか?(1…自宅、2…公園、3…駅、4…映画館): 3
思ったより早く着いたようだ、Bちゃんも嬉しそうだ!(好感度+2)現在7
Bちゃんとデートの待ち合わせをします。どこにしますか?(1…自宅、2…公園、3…駅、4…映画館): 4
丁度Bちゃんの好きな映画が上映されていた、とても満足のようだ!(親密度+3)現在10
Bちゃんが嬉しそうにA君を見つめている…
★トゥルーエンド★
問3(N氏作) ★★
1から13の数とジョーカーが2枚入ったトランプ(計54枚)を2組用意する
2組のトランプをランダムで引き二つとも同じ数を引いたときに勝ちとする以下の処理を実現せよ
「条件」
1 引けるのは5回まで
2 ジョーカーをどちらかが引いた場合引ける数が1増える
3 ジョーカーをそろえて引いた場合負けそこでゲーム終了 ただし引いたカードは減ることとする
例A 3をドローした
4をドローした
残念
例B 1をドローした
ジョーカーをドローした
引ける回数が一回増えました
例C 5をドローした
5をドローした
成功ですおめでとう
例D ジョーカーをドローした
ジョーカーをドローした
あなたの負けですゲーム終了
例E 5回で引けなかったら
あなたの負けですゲーム終了
問4(A氏作) 難易度 ★★
Diceクラスを作成し、SugorokuAppにて以下の処理を実現せよ。
[条件]
①現在地をフィールドに持つ。GOALまでのマスの数は10。GOALまでのマスの数を■で表示。
②diceを2つ生成。順にdiceを振る。diceの目は1~6のランダム。
③出た目の分だけ進む。先にぴったりゴールに到着した方が勝ち。ゴールを超えた場合は、超えた分だけマスを戻る。
④5ターン経過しても終了しない場合はDRAWとする。
[ex]
dice1:
ゴールまでの距離:■■■■■■■■■■
出た目:5
ゴールまでの距離:■■■■■
(キーの入力待ち)
dice2:
ゴールまでの距離:■■■■■■■■■■
出た目:6
ゴールまでの距離:■■■■
(キーの入力待ち)
dice1:
ゴールまでの距離:■■■■■
出た目:6
ゴールまでの距離:■
(キーの入力待ち)
dice2:
ゴールまでの距離:■■■■
出た目:4
ゴールまでの距離:ARRIVE!
dice2 WIN!
問5(Y氏作) 難易度 ★★★
ム◯キング(じゃんけんバトル)
・準備
小型甲虫(HP90,ATK50),中型甲虫(HP130,ATK40),大型甲虫(HP170,ATK30)のステータスを持つ三匹の中から一匹選んで貰い、それに名前と必殺技を出すためのじゃんけんの手(グー、チョキ、パーのどれか一つ)を決めて貰います。
出来上がった虫のステータスを表示し、同様の手順でCPUの虫も作成します。
・じゃんけんバトルのルール
CPUの手はランダムに決まります
お互いにじゃんけんをして勝った方の虫の攻撃力分負けた方の虫のHPを減らします。
あいこの場合はお互いのHPから相手の虫の攻撃力の半分の値を減らします。
必殺技を出すための手で勝った場合攻撃力の二倍の値を減らします。
じゃんけんを繰り返し、先にHPが0以下になったプレイヤーの負けです。
あいこによりお互いのHPが0になった場合はプレイヤー側の負けとします。
実行例
あなたが使う甲虫を選んでね
1:小型甲虫(HP90,ATK50),2:中型甲虫(HP130,ATK40),3:大型甲虫(HP170,ATK30)>1
名前をつけてね>かぶと丸
必殺技の手を選んでね(1:グー2:チョキ,3:パー)>3
かぶと丸(HP:90,ATK:50,必殺技:パー)が生まれました
CPUが使う甲虫を選んでね
1:小型甲虫(HP90,ATK50),2:中型甲虫(HP130,ATK40),3:大型甲虫(HP170,ATK30)>2
名前をつけてね>クワガタさん
必殺技の手を選んでね(1:グー2:チョキ,3:パー)>2
クワガタさん(HP:130,ATK:40,必殺技:チョキ)が生まれました
バトル開始!
かぶと丸 HP:90
クワガタさん HP:130
じゃんけんの手を選んでね(1:グー2:チョキ,3:パー)>2
あなた:チョキ
CPU:パー
あなたの勝ち
かぶと丸はクワガタさんに50のダメージを与えた
かぶと丸 HP:90
クワガタさん HP:80
じゃんけんの手を選んでね(1:グー2:チョキ,3:パー)>3
あなた:パー
CPU:パー
あいこ
かぶと丸はクワガタさんに25のダメージを与えた
クワガタさんはかぶと丸に20のダメージを与えた
かぶと丸 HP:70
クワガタさん HP:55
じゃんけんの手を選んでね(1:グー2:チョキ,3:パー)>3
あなた:パー
CPU:グー
あなたの勝ち
必殺技だー!
かぶと丸はクワガタさんに100のダメージを与えた
クワガタさんのHPが0になった
あなたの勝ち!
アプリケーションを終了します
問6(K氏作) 難易度 ★★★★
運というステータスを持った二人のキャラクターA,Bを作成し、
より少ない回数で目的のカードを引き当てた方が勝利するゲームを作成せよ
条件
1.ステータス【運】は1から20までの乱数によって決定し、AとBそれぞれで乱数を生成する。
2.目的のカードが排出される確率は100分の【運】とする。
3.【運】が相手より低かったのにも関わらず、先にカードを引き当て、勝利した場合【下剋上!】と表示される
4.両者が当たりを引いた場合、もしくははずれだった場合はゲームを継続させ勝敗がつくまで続けるものとする。
実行例
A 【運】10
B 【運】 3
Aのドロー ・・・・・・・・・はずれ
Bのドロー ・・・・・・・・・はずれ
or
Aのドロー ・・・・・・・・・当たり!
Bのドロー ・・・・・・・・・当たり!
or
Aのドロー ・・・・・・・・・当たり! 【WIN】
Bのドロー ・・・・・・・・・はずれ 【LOSE】
or
Aのドロー ・・・・・・・・・はずれ
Bのドロー ・・・・・・・・・当たり!! 【下剋上!】
解答例
問1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace games {
class Program {
static void Main(string[] args) {
Console.Write("現在の体重は?:>");
int start = int.Parse(Console.ReadLine());
Console.Write("目標体重は?:>");
int goal = int.Parse(Console.ReadLine());
Weight weight = new Weight(start, goal);
while (true) {
Console.Write("目標に向けて今日は何キロ走りますか?1…10km、2…20km、3…30km、4…現在の情報、5…終了>");
int input = int.Parse(Console.ReadLine());
switch (input) {
case 1:
if (weight.LostByRun(10)) {
return;
}
break;
case 2:
if (weight.LostByRun(20)) {
return;
}
break;
case 3:
if (weight.LostByRun(30)) {
return;
}
break;
case 4:
weight.ShowInfo();
break;
case 5:
Console.WriteLine("アプリケーションを終了します。");
break;
}
}
}
}
public class Weight {
public int Start { get; set; }
public int Now { get; set; }
public int Goal { get; set; }
public Weight(int start,int goal) {
Now= Start = start;
Goal = goal;
}
//走るメソッド 戻り値:達成したか?
public bool LostByRun(int km) {
int loseWeight=0;
switch (km) {
case 10:
loseWeight = 1;
break;
case 20:
loseWeight = 5;
break;
case 30:
loseWeight = 10;
break;
}
Now-= loseWeight;
if (Now <= Goal) {
Console.WriteLine("目標達成です!Congratulation!");
return true;
} else {
Console.WriteLine($"{loseWeight}kg減りました!この調子で頑張りましょう!");
return false;
}
}
public void ShowInfo() {
Console.WriteLine($"{Start-Now}kg痩せてます!残り{Now-Goal}kgです!頑張りましょう!");
}
}
}
問2
using System;
namespace games {
class MainClass {
const int DEFAULT_CLOSENESS = 5, TRUEEND = 10, BADEND = 0;
public static void Main(string[] args) {
Console.Write("ユーザーネーム:");
string heroName = Console.ReadLine();
Console.Write("ヒロインネーム:");
string heroineName = Console.ReadLine();
Hero hero = new Hero(heroName);
Heroine heroine = new Heroine(heroineName, DEFAULT_CLOSENESS);
while (true) {
Console.Write($"{heroineName}ちゃんとデートの待ち合わせをします。どこにしますか?(1…自宅、2…公園、3…駅、4...映画館):");
int input = int.Parse(Console.ReadLine());
switch (input) {
case 1:
hero.GoMyHouse(heroine);
break;
case 2:
hero.GoPark(heroine);
break;
case 3:
hero.GoStation(heroine);
break;
case 4:
hero.GoMovie(heroine);
break;
}
bool isEND = false;
if (heroine.Closeness >= TRUEEND) {
heroine.Stare(hero);
Console.WriteLine("★トゥルーエンド★");
isEND = true;
} else if (heroine.Closeness <= BADEND) {
Console.WriteLine("バッドエンド!!");
isEND = true;
}
if (isEND) {
Console.Write("続けますか?yes or no>");
string ans = Console.ReadLine();
if (ans == "yes") {
heroine.Closeness = DEFAULT_CLOSENESS;
} else {
break;
}
}
}
}
}
class Heroine {
public string Name { get; set; }
public int Closeness { get; set; }
public Heroine(string name, int closeness) {
Name = name;
Closeness = closeness;
}
public void Stare(Hero hero) {
Console.WriteLine($"{Name}ちゃんが嬉しそうに{hero.Name}君を見つめている…");
}
}
class Hero {
public string Name { get; set; }
public Hero(string name) {
Name = name;
}
public void GoMyHouse(Heroine heroine) {
heroine.Closeness -= 2;
Console.WriteLine($"自宅に隠し持っていた{Name}君の秘密がバレてしまった!(親密度-2)現在{heroine.Closeness}");
}
public void GoPark(Heroine heroine) {
heroine.Closeness -= 3;
Console.WriteLine($"その公園は蚊が多く{heroine.Name}ちゃんが嫌がってしまった!(親密度-3)現在{heroine.Closeness}");
}
public void GoStation(Heroine heroine) {
heroine.Closeness += 2;
Console.WriteLine($"思ったより早く着いたようだ、{heroine.Name}ちゃんも嬉しそうだ!(好感度+2)現在{heroine.Closeness}");
}
public void GoMovie(Heroine heroine) {
heroine.Closeness += 3;
Console.WriteLine($"丁度{heroine.Name}ちゃんの好きな映画が上映されていた、とても満足のようだ!(親密度+3)現在{heroine.Closeness}");
}
}
}
問3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace games {
class Program {
const int MAX_TURN = 5;
static void Main(string[] args) {
Deck d1 = new Deck();
Deck d2 = new Deck();
for(int i = 0; i < MAX_TURN; i++) {
string c1 = d1.Draw();
string c2 = d2.Draw();
if (c1 == "ジョーカー" && c2 == "ジョーカー") {
Console.WriteLine("あなたの負けですゲーム終了");
return;
} else if (c1 == "ジョーカー" || c2 == "ジョーカー") {
Console.WriteLine("引ける回数が一回増えました");
i--;
} else if (c1 == c2) {
Console.WriteLine("成功ですおめでとう");
return;
} else {
Console.WriteLine("残念");
}
Console.ReadLine();
}
Console.WriteLine("あなたの負けですゲーム終了");
}
}
public class Deck {
public List<string> Cards { get; set; }
public Deck() {
CreateDeck();
}
public void CreateDeck() {
var temp = new List<string>();
for (int i = 0; i < 54; i++) {
if (i < 52) {
temp.Add( i % 13 + 1 + "");
} else {
temp.Add( "ジョーカー");
}
}
Cards = temp.OrderBy(i => Guid.NewGuid()).ToList();
}
public string Draw() {
var card = Cards[0];
Console.WriteLine(card + "をドローした");
Cards.RemoveAt(0);
return card;
}
}
}
問4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace games {
class Program {
const int DICE_COUNT = 2;
static void Main(string[] args) {
Random rand = new Random();
var list = new List<Dice>();
for (int i = 0; i < DICE_COUNT; i++) {
list.Add(new Dice(i+1));
}
for(int i=0; ; i++) {
Dice currentDice = list[i % list.Count];
Console.WriteLine("dice" + currentDice.No);
currentDice.CheckPos();
int num = rand.Next(1, 7);
Console.WriteLine("出た目:" + num);
currentDice.Move(num);
if (currentDice.CheckPos()) {
Console.WriteLine($"\ndice{currentDice.No} WIN!");
break;
}
Console.ReadLine();
}
}
}
class Dice {
public int No { get; set; }
public int Distination { get; set; } = 10;
public int Pos { get; set; } = 0;
public Dice(int no) {
No = no;
}
public void Move(int n) {
Pos = Pos + n;
if (Pos > Distination) {
Pos = Distination - Pos % Distination;
}
}
public bool CheckPos() {
bool isGoal = false;
Console.Write("ゴールまでの距離:");
if (Distination ==Pos) {
Console.Write("Arrive!");
isGoal = true;
} else {
for (int i = 0; i < Distination - Pos; i++) {
Console.Write("■");
}
}
Console.WriteLine();
return isGoal;
}
}
}
問5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace games {
class Program {
static void Main(string[] args) {
Random rand = new Random();
var hands = new Dictionary<int, string> { { 1, "グー" }, { 2, "チョキ" }, { 3, "パー" } };
string[] players = { "あなた", "CPU" };
Beetle[] beetles = new Beetle[2];
for(int i = 0; i < players.Length; i++) {
Console.WriteLine($"{players[i]}が使う甲虫を選んでね");
Console.Write("1:小型甲虫(HP90,ATK50),2:中型甲虫(HP130,ATK40),3:大型甲虫(HP170,ATK30)>");
int beetleType = int.Parse(Console.ReadLine());
Console.Write("名前をつけてね>");
string name = Console.ReadLine();
Console.Write("必殺技の手を選んでね(1:グー2:チョキ,3:パー)>");
int deathNo = int.Parse(Console.ReadLine());
string deathblow = hands[deathNo];
switch (beetleType) {
case 1:
beetles[i] = new Beetle(90, 50, name, deathblow);
break;
case 2:
beetles[i] = new Beetle(130, 40, name, deathblow);
break;
case 3:
beetles[i] = new Beetle(170, 30, name, deathblow);
break;
}
Console.WriteLine(beetles[i].ShowStatus() + "が生まれました\n");
}
Console.WriteLine("バトル開始!");
while (true) {
foreach (var b in beetles) {
b.ShowHp();
}
Console.Write("じゃんけんの手を選んでね(1:グー,2:チョキ,3:パー)>");
int userHand = int.Parse(Console.ReadLine());
Console.WriteLine($"{players[0]}:{hands[userHand]}");
int cpuHand = rand.Next(1, 4);
Console.WriteLine($"{players[1]}:{hands[cpuHand]}");
int diff = cpuHand - userHand;
if(diff==0) {
Console.WriteLine("あいこ");
beetles[0].Attack(beetles[1], true, false);
beetles[1].Attack(beetles[0], true, false);
}else if ((diff + 3) % 3 == 1) {
Console.WriteLine("あなたの勝ち");
if (beetles[0].Deathblow == hands[userHand]) {
beetles[0].Attack(beetles[1], false, true);
} else {
beetles[0].Attack(beetles[1], false, false);
}
} else {
Console.WriteLine("CPUの勝ち");
if (beetles[1].Deathblow == hands[cpuHand]) {
beetles[1].Attack(beetles[0], false, true);
} else {
beetles[1].Attack(beetles[0], false, false);
}
}
if (beetles[0].Hp <= 0) {
Console.WriteLine($"{beetles[0].Name}のHPが0になった");
Console.WriteLine("あなたの負け...");
break;
}else if (beetles[1].Hp <= 0) {
Console.WriteLine($"{beetles[1].Name}のHPが0になった");
Console.WriteLine("あなたの勝ち!");
break;
}
}
Console.WriteLine("アプリケーションを終了します");
}
}
public class Beetle {
public int Hp { get; set; }
public int Atk { get; set; }
public string Name { get; set; }
public string Deathblow { get; set; }
public Beetle(int hp,int atk,string name,string deathblow) {
Hp = hp;
Atk = atk;
Name = name;
Deathblow = deathblow;
}
public void ShowHp() {
Console.WriteLine($"{Name} HP:{Hp}");
}
public string ShowStatus() {
return $"{Name}(HP:{Hp},ATK:{Atk},必殺技:{Deathblow})";
}
public void Attack(Beetle other,bool isDraw,bool isDeathblow) {
int attackPower;
if (isDraw) {
attackPower = Atk / 2;
}else if (isDeathblow) {
Console.WriteLine("必殺技だー!");
attackPower = Atk * 2;
} else {
attackPower = Atk;
}
Console.WriteLine($"{Name}は{other.Name}に{attackPower}のダメージを与えた");
other.Hp -= attackPower;
}
}
}
問6
using System;
using System.Collections.Generic;
namespace games {
class MainClass {
public static void Main(string[] args) {
const int LOW_LUCK = 1, MAX_LUCK = 20;
Random rand = new Random();
string[] playerNames = { "A", "B" };
var ps = new List<Player>();
for (int i = 0; i < playerNames.Length; i++) {
string name = playerNames[i];
int luck = rand.Next(LOW_LUCK, MAX_LUCK + 1);
Player player = new Player(name, luck);
player.ShowStatus();
ps.Add(new Player(name, luck));
}
while (true) {
if (Player.ContinueGame(ps,rand)) {
Console.ReadLine();
} else {
break;
}
}
}
}
class Player {
public string Name { get; set; }
public int Luck { get; set; }
public bool IsGekoku { get; set; }
public bool IsHit { get; set; }
public Player(string name, int luck) {
Name = name;
Luck = luck;
}
public void Draw(int n) {
IsHit = false;
//Console.WriteLine("num:" + n);
if (Luck >= n) {
IsHit = true;
}
}
public void ShowStatus() {
Console.WriteLine($"{Name} 【運】{Luck}");
}
public void ShowResult(bool isCont, bool isGekokuMode) {
string result = IsHit ? "当たり!" : "はずれ";
string suffix = IsGekoku ? "!" : " ";
string winOrLose = "";
if (!isCont) {
if (IsGekoku) {
winOrLose = " 【下剋上!】";
} else if (!isGekokuMode) {
winOrLose = IsHit ? "【WIN】" : " 【LOSE】";
}
}
Console.WriteLine($"{Name}のドロー ・・{result}{suffix}{winOrLose}");
}
public static bool ContinueGame(List<Player> ps,Random rand) {
bool isContinue = true;
ps.ForEach(p => p.Draw(rand.Next(1,101)));
if (ps[0].IsHit != ps[1].IsHit) {
isContinue = false;
}
if (!isContinue) {
if (ps[0].IsHit && ps[0].Luck < ps[1].Luck) {
ps[0].IsGekoku = true;
} else if (ps[1].IsHit && ps[1].Luck < ps[0].Luck) {
ps[1].IsGekoku = true;
}
}
bool isGekokuMode = ps[0].IsGekoku || ps[1].IsGekoku;
ps.ForEach(p => p.ShowResult(isContinue, isGekokuMode));
return isContinue;
}
}
}

コメント