2012年9月2日日曜日

開発環境

『初めての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 cols = 8;
        const int rows = 8;
        string[,] chessboard = new string[rows, cols];
        for (int i = 0; i < cols; i++)
        {
            if (i % 2 == 0)
            {
                for (int j = 0; j < rows; j++)
                {
                    chessboard[i, j] = j % 2 == 0 ? "黒" : "白";
                }
            }
            else
            {
                for (int j = 0; j < rows; j++)
                {
                    chessboard[i, j] = 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("座標を入力");
            string ms = Console.ReadLine();
            if (ms == "")
            {
                break;
            }
            string ns = Console.ReadLine();
            int m = Convert.ToInt16(ms);
            int n = Convert.ToInt16(ns);
            Console.WriteLine(chessboard[m - 1, n - 1]);
        }
    }
    static void Main()
    {
        Tester t = new Tester();
        t.Run();
    }
}

入出力結果(Console Window)

チェス盤
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
座標を入力
1
1
黒
座標を入力
8
8
黒
座標を入力
5
6
白
座標を入力
6
5
白
座標を入力
6
6
黒
座標を入力
7
6
白
座標を入力

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

0 コメント:

コメントを投稿