2012年10月18日木曜日

開発環境

『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第10章(配列)10.9(練習問題)練習10-4を解いてみる。

その他参考書籍

練習10-4.

コード

using System;


class Tester
{
    public void Run()
    {
        const int rows = 8;
        const int cols = 8;
        string[,] chessboard = new string[rows, cols];
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                chessboard[i, j] = i % 2 == 0 ?
                    (j % 2 == 0 ? "黒" : "白") :
                    (j % 2 == 0 ? "白" : "黒");
            }
        }
        Console.WriteLine("チェス盤");
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                Console.Write(chessboard[i, j]);
            }
            Console.WriteLine();
        }
        while (true)
        {
            Console.WriteLine("座標(8×8)を入力");
            string strM = Console.ReadLine();
            if (strM == "") break;
            int m = Convert.ToInt16(strM);
            int n = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("{0}×{1}: {2}",
                m, n, chessboard[m - 1, n - 1]);
        }
    }
    static void Main()
    {
        Tester t = new Tester();
        t.Run();
    }
}

入出力結果(Console Window)

チェス盤
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
座標(8×8)を入力
1
1
1×1: 黒
座標(8×8)を入力
1
2
1×2: 白
座標(8×8)を入力
8
8
8×8: 黒
座標(8×8)を入力
8
7
8×7: 白
座標(8×8)を入力

続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿