paizaC#最終章突入。
// 標準入力から文字のドットデータを読み込む
using System;
public class Lesson05
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
string[][] table = new string[n][];
for (int i = 0; i < n; i++)
{
// ここに、標準入力から2次元配列に代入するコードを書く
table [i] = Console.ReadLine().Split(' ');
}
// 2次元配列から文字を出力
for (int i = 0; i < table.Length; i++)
{
for (int j = 0; j < table[i].Length; j++)
{
if (table[i][j] == "1")
{
Console.Write("#");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
}
}
// RPGの冒険者にMPを設定しよう
using System;
public class Practice
{
public static void Main()
{
var adventurer = new Adventurer("冒険者", 120);
var wizard = new Adventurer("ウィザード", 549);
var crusader = new Adventurer("クルセイダー", 50);
var priest = new Adventurer("プリースト", 480);
Adventurer[] party = {adventurer, wizard, crusader, priest};
foreach (Adventurer player in party)
{
player.Attack();
Console.WriteLine("残りMP" + player.GetMP());
}
}
}
class Adventurer
{
private string job;
private int mp;
public Adventurer(string job, int mp)
{
this.job = job;
this.mp = mp;
}
public void Attack()
{
Console.WriteLine(job + "は魔王を攻撃した");
mp -= 5;
}
// 現在のMPの値を返すGetMP()メソッドを実装する
public int GetMP()
{
return mp = mp;
}
}
// メソッドのオーバーライド
using System;
class Lesson08
{
public static void Main()
{
var box = new Box("鋼鉄の剣");
box.Open();
var magicBox = new MagicBox("モノマネモンスター");
magicBox.Look();
magicBox.Open();
}
}
class Box
{
public string Item { get; private set; }
public Box(string item)
{
Item = item;
}
public virtual void Open()
{
Console.WriteLine("宝箱を開いた。" + Item + "を手に入れた。");
}
}
class MagicBox : Box
{
public MagicBox(string item) : base(item)
{
}
public void Look()
{
Console.WriteLine("宝箱はキラキラと輝いている。");
}
public override void Open()
{
Console.WriteLine("宝箱を開いた。" + Item + "が襲ってきた!");
}
}
// アクセッサをプロパティに置き換えよう
フィールドcount=0を使わずに、public static int Count{get; private set;}を使う流れ
using System;
public class Practice
{
public static void Main()
{
var adventurer = new Adventurer("冒険者", 120);
var wizard = new Adventurer("ウィザード", 549);
var crusader = new Adventurer("クルセイダー", 50);
var priest = new Adventurer("プリースト", 480);
Adventurer[] party = {adventurer, wizard, crusader, priest};
foreach (Adventurer player in party)
{
player.Attack();
Console.WriteLine("残りMP" + player.GetMP());
}
Console.WriteLine("冒険者の総人数は" + Adventurer.Count + "人");
}
}
class Adventurer
{
private string job;
private int mp;
public static int Count{ get; private set;}
public Adventurer(string job, int mp)
{
this.job = job;
this.mp = mp;
Count += 1;
}
public void Attack()
{
Console.WriteLine(job + "は魔王を攻撃した");
mp -= 5;
}
public int GetMP()
{
return mp;
}
// public static int GetCount()
// {
// return count;
// }
// Countプロパティに置き換える
}
using System;
public class Hello{
public static void Main(){
var data = Console.ReadLine();
string [] team = data.Split(',');
data = Console.ReadLine();
string [] enemy = data.Split(',');
foreach(var str in enemy){
Console.WriteLine(str +"が現れた");
}
var num = enemy.Length;
Console.WriteLine("敵は" + num + "匹");
var random = new Random();
var rand = 0;
var attack = "";
foreach(var str in team){
rand = random.Next(num);
attack = enemy[rand];
Console.WriteLine(str + "の攻撃!");
Console.WriteLine(attack + "に会心の一撃!");
}
}
}
——————
勇者,戦士,忍者,俺
スライム,モンスター,魔王,かば
// クラスを継承しよう
using System;
class Lesson08
{
public static void Main()
{
var box = new Box("鋼鉄の剣");
box.Open();
var jewelryBox = new JewelryBox("魔法の指輪");
jewelryBox.Look();
jewelryBox.Open();
}
}
class Box
{
public string Item { get; private set; }
public Box(string item)
{
Item = item;
}
public void Open()
{
Console.WriteLine("宝箱を開いた。" + Item + "を手に入れた。");
}
}
class JewelryBox : Box
{
public JewelryBox(string item) : base(item)
{
}
public void Look()
{
Console.WriteLine("宝箱はキラキラと輝いている。");
}
}
// RPGの冒険者クラスを作ろう
using System;
public class Practice
{
public static void Main()
{
var adventurer = new Adventurer("冒険者");
var wizard = new Adventurer("ウィザード");
var crusader = new Adventurer("クルセイダー");
var priest = new Adventurer("プリースト");
Adventurer[] party = {adventurer, wizard, crusader, priest};
foreach (Adventurer player in party)
{
player.Attack();
}
}
}
// Adventurerクラスを定義する
public class Adventurer
{
private string job;
public Adventurer(string job){
this.job = job;
}
public void Attack()
{
Console.WriteLine(job + "は魔王を攻撃した");
}
}
Humanクラスの定義にpublicをつけるかどうか。と思いきや”う”が抜けていただけ。
// クラスとメソッドを定義しよう
using System;
public class Practice
{
public static void Main()
{
var human = new Human();
human.Greet();
}
}
// Human クラスを定義する
class Human
{
public void Greet()
{
Console.WriteLine("さようなら世界");
}
}
誤答
using System;
public class Practice
{
public static void Main()
{
var human = new Human();
human.Greet();
}
}
// Human クラスを定義する
public class Human
{
public void Greet()
{
Console.WriteLine("さよなら世界");
}
}
// クラスとメソッドを定義しよう
using System;
public class Practice
{
public static void Main()
{
// Humanクラスのオブジェクトを生成してGreet()メソッドを呼び出す
var human1 = new Human();
human1.Greet();
}
}
class Human
{
public void Greet()
{
Console.WriteLine("こんにちは世界");
}
}
// RPGのプレイヤーを継承で書こう - その1
using System;
class Lesson08
{
public static void Main()
{
Console.WriteLine("=== パーティーでドラゴンと戦う ===");
var hero = new Player("勇者");
var warrior = new Player("戦士");
// party配列にプレイヤークラスのオブジェクトを代入する
Player [] party = {hero,warrior};
// party配列から順番に呼び出しスライムを攻撃する
foreach(var player in party)
{
player.Attack("スライム");
}
}
}
class Player
{
public string Name { get; private set; }
public Player(string name)
{
Name = name;
}
public void Attack(string enemy)
{
Console.WriteLine(Name + "は" + enemy + "を攻撃した");
}
}
オブジェクトがないときにメソッドを作成したいときにstaticを使用
だから起動時の、メインはstaticが記載されている
// 生成されたオブジェクトの数を数えられるようにしよう
using System;
public class Practice
{
public static void Main()
{
var adventurer = new Adventurer("冒険者", 120);
var wizard = new Adventurer("ウィザード", 549);
var crusader = new Adventurer("クルセイダー", 50);
var priest = new Adventurer("プリースト", 480);
Adventurer[] party = {adventurer, wizard, crusader, priest};
foreach (Adventurer player in party)
{
player.Attack();
Console.WriteLine("残りMP" + player.GetMP());
}
Console.WriteLine("冒険者の総人数は" + Adventurer.GetCount() + "人");
}
}
class Adventurer
{
private string job;
private int mp;
private static int count = 0;
// 外部からアクセスできないスタティックな変数を定義する
public Adventurer(string job, int mp)
{
this.job = job;
this.mp = mp;
count++;
}
public void Attack()
{
Console.WriteLine(job + "は魔王を攻撃した");
mp -= 5;
}
public int GetMP()
{
return mp;
}
// 冒険者の総人数を返すstaticなメソッドを追加する
public static int GetCount()
{
return count;
}
}
using System;
public class Hello{
public static void Main(){
string[]enemy = Console.ReadLine().Split(',');
foreach(var s in enemy){
Console.WriteLine(s);
}
var num = enemy.Length;
Console.WriteLine(num);
var random = new Random();
var rand = random.Next(num);
var attack = enemy[rand];
Console.WriteLine(attack + "に会心の一撃!" + attack + "を倒した!");
}
}
// オブジェクト生成時にフィールドを初期化しよう2
using System;
public class Practice
{
public static void Main()
{
var kyoko = new Human("霧島京子");
kyoko.Greet();
}
}
class Human
{
private string name;
// コンストラクタの引数で文字列を受け取り、nameフィールドに代入する
public Human(string name)
{
this.name = name;
}
public void Greet()
{
Console.WriteLine("みなさんこんにちは" + name + "です");
}
}
// 可変長引数
using System;
public class Program
{
public static void Main()
{
Introduce("スライム", "ドラゴン", "魔王");
}
public static void Introduce(params string[]enemies)
{
foreach (var enemy in enemies)
{
Console.WriteLine("戦士は" + enemy + "と戦った");
}
}
}
// オブジェクト生成時にフィールドを初期化しよう1
using System;
public class Practice
{
public static void Main()
{
// オブジェクト生成時にコンストラクタに値を渡し、nameフィールドを初期化する
var player1 = new Human("霧島京子");
player1.Greet();
}
}
class Human
{
private string name;
public Human(string name)
{
this.name = name;
}
public void Greet()
{
Console.WriteLine("みなさんこんにちは" + name + "です");
}
}
// 標準ライブラリを使ってみよう
using System;
using System.Collections.Generic;
class Lesson08
{
public static void Main()
{
var team = new List<string>();
team.Add("勇者");
team.Add("戦士");
team.Add("魔法使い");
foreach (var member in team)
{
Console.WriteLine(member);
}
Console.WriteLine();
team.Insert(2,"忍者");
foreach (var member in team)
{
Console.WriteLine(member);
}
Console.WriteLine();
team.Remove("戦士");
foreach (var member in team)
{
Console.WriteLine(member);
}
}
}
// 2次元配列で画像を表示する
using System;
public class Lesson05
{
public static void Main()
{
//画像URL用の配列A
string[] playerImages = {
"https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Empty.png",
"https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Dragon.png",
"https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Crystal.png",
"https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Hero.png",
"https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Heroine.png"
};
//キャラクター配置用の配列
int[][] characters = {
new int[] {1,1,1,1,1},
new int[] {2,3,3,3,2},
new int[] {2,4,4,4,2}
};
Console.WriteLine("<table>");
foreach (int[] line in characters)
{
Console.WriteLine("<tr>");
foreach (int imageId in line)
{
Console.Write("<td><img src='" + playerImages[imageId] + "'></td>");
}
Console.WriteLine("</tr>");
}
Console.WriteLine("</table>");
}
}
// foreachで2次元配列を出力してみよう
using System;
public class Lesson05
{
public static void Main()
{
string[][] array = {
new string[] {"勇者", "忍者"},
new string[] {"武士", "戦士"},
new string[] {"僧侶", "魔法使い"}
};
// foreachで、arrayを出力してみよう
foreach(string [] arra in array){
foreach(string player in arra){
Console.WriteLine(player);
}
}
}
}
// ドットで文字を出力しよう
using System;
public class Lesson05
{
public static void Main()
{
int[][] letterA = {
new int[] {0, 0, 1, 1, 0, 0},
new int[] {0, 1, 0, 0, 1, 0},
new int[] {1, 0, 0, 0, 0, 1},
new int[] {1, 1, 1, 1, 1, 1},
new int[] {1, 0, 0, 0, 0, 1},
new int[] {1, 0, 0, 0, 0, 1}
};
// ここに、ドットを表示するコードを記述する
foreach(int []letter in letterA){
foreach(int dot in letter){
if(dot == 1){
Console.Write("@");
}else{
Console.Write(" ");
}
}
Console.WriteLine();
}
}
}
using System;
public class Hello{
public static void Main(){
string[]enemy = Console.ReadLine().Split(',');
foreach(var s in enemy){
Console.WriteLine(s);
}
}
}
// 文字列をカンマで分割する
using System;
using System.Collections.Generic;
public class Program{
public static void Main(){
var team = "勇者,戦士,忍者,魔法使い";
// ここに文字列を分割するコードを記述する
string[] array = team.Split(',');
foreach (var str in array) {
Console.WriteLine(str);
}
}
}
// 英文の単語数を数える
using System;
using System.Collections.Generic;
public class Program{
public static void Main(){
string str = "One cold rainy day when my father was a little boy he met an old alley cat on his street";
// ここに文字列を分割するコードを記述する
int count = str.Split(' ').Length;
Console.WriteLine(count);
}
}
——————————————
// 英文の単語数を数える
using System;
using System.Collections.Generic;
public class Program{
public static void Main(){
string str = "One cold rainy day when my father was a little boy he met an old alley cat on his street";
// ここに文字列を分割するコードを記述する
string[]array = str.Split(' ');
int count = array.Length;
Console.WriteLine(count);
}
}
// 複数行のカンマ区切りデータを出力する
using System;
using System.Collections.Generic;
public class Hello{
public static void Main(){
string line;
while ((line=Console.ReadLine()) != null) {
// ここに、文字列を分割して、出力するコードを書く
string[] enemy = line.Split(',');
Console.WriteLine(enemy[0] + "が" + enemy[1] + "匹現れた");
}
}
}
using System;
using System.Collections.Generic;
public class Program{
public static void Main(){
// Here your code !
var team = new List<string>();
team.Add("勇者");
// Console.WriteLine(team.Count);
team.Add("戦士");
team.Add("魔法使い");
team.Insert(1,"忍者");
team.Remove("戦士");
foreach(string str in team){
Console.WriteLine(str);
}
}
}
Attackの戻り値がsumできちんと蓄積されない。
新変数numではなく、enemyHpをそのまま使えばよかっただけ
//RPGの攻撃シーンを作ろう
using System;
public class Program
{
public static void Main()
{
string[] players = { "勇者", "戦士", "魔法使い" };
var rand = new Random();
int enemyHp = int.Parse(Console.ReadLine());
foreach (var player in players)
{
var hit = rand.Next(1, 4) * 10;
Console.WriteLine(player + "はスライムを攻撃した");
// ここでAttackメソッドを呼び出して、返り値をenemyHpに代入する
enemyHp = Attack(enemyHp, hit);
Console.WriteLine("敵のHPは残り" + enemyHp + "です");
}
}
public static int Attack(int enemyHp, int hit)
{
enemyHp -= hit;
return enemyHp;
}
}
誤答
//RPGの攻撃シーンを作ろう
using System;
public class Program
{
public static void Main()
{
string[] players = { "勇者", "戦士", "魔法使い" };
var rand = new Random();
int enemyHp = int.Parse(Console.ReadLine());
foreach (var player in players)
{
var hit = rand.Next(1, 4) * 10;
Console.WriteLine(player + "はスライムを攻撃した");
// 下記にコードを追加する
var num = Attack(enemyHp,hit);
Console.WriteLine("敵のHPは残り" + num + "です");
}
}
public static int Attack(int enemyHp, int hit)
{
enemyHp = num - hit;
return enemyHp;
}
}
// 2次元配列をnewで作成しよう
using System;
public class Lesson05
{
public static void Main()
{
int[][] array = new int[2][];
for (int i = 0; i < array.Length; i++)
{
// この下で、配列を作成しよう
array[i] = new int [3];
}
foreach (int[] item in array)
{
foreach (int num in item)
{
Console.Write(num);
}
Console.WriteLine();
}
}
}