2012年11月30日金曜日

開発環境

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

その他参考書籍

問題10-4.

コード

using System;
using System.Text.RegularExpressions;

class Dog
{
    private int weight;
    private string name;
    public Dog(int weight, string name)
    {
        this.weight = weight;
        this.name = name;
    }
    public int Weight
    {
        get { return weight; }
        set { weight = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}
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) ||
                    (i % 2 != 0 && j % 2 != 0) ?
                    "黒" : "白";
            }
        }
        Console.WriteLine("チェス盤(8k×8)");
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                Console.Write(chessboard[i, j]);
            }
            Console.WriteLine();
        }
        Console.WriteLine("座標から色を求める(空白で終了)");
        Regex reg = new Regex(@"^\s*$");
        while (true)
        {
            try
            {
                Console.Write("x座標: ");
                string sx = Console.ReadLine();
                if (reg.IsMatch(sx))
                {
                    break;
                }
                int x = Convert.ToInt16(sx) - 1;
                Console.Write("y座標: ");
                string sy = Console.ReadLine();
                if (reg.IsMatch(sy))
                {
                    break;
                }
                int y = Convert.ToInt16(sy) - 1;
                Console.WriteLine(chessboard[x, y]);
            }

            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
    static void Main()
    {
        Tester t = new Tester();
        t.Run();
    }
}

入出力結果(Console Window)

チェス盤(8k×8)
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
座標から色を求める(空白で終了)
x座標: 1
y座標: 1
黒
x座標: 5
y座標: 6
白
x座標: 5
y座標: 5
黒
x座標: 6
y座標: 5
白
x座標: 6
y座標: 6
黒
x座標: 10
y座標: 10
インデックスが配列の境界外です。
x座標: c
入力文字列の形式が正しくありません。
x座標: 10
y座標: c#
入力文字列の形式が正しくありません。
x座標:
続行するには何かキーを押してください . . .

ちなみにJavaScriptの場合。

コード(TextWrangler)

var result = "";
var chessboard = [];
var rows = cols = 8;
for(var i = 0; i < rows; i++){
  chessboard[i] = [];
  for(var j = 0; j < cols; j++){
    chessboard[i][j] = (i % 2 === 0 && j % 2 === 0) ||
      (i % 2 !== 0 && j % 2 !==0) ?
      "黒" : "白";
  }
}
result += "チェス盤(8×8)\n";
for(var i = 0; i < rows; i++){
  for(var j = 0; j < cols; j++){
    result += chessboard[i][j];
  }
  result += "\n";
}
try{
  var x = parseInt($('#t0').val()) - 1;
  var y = parseInt($('#t1').val()) - 1;
  if(!chessboard[x][y]) throw "エラー";
  result += chessboard[x][y] + "\n";
} catch (e){
  result += e + "\n" + 
    "入力座標を確認してください。";
}
$('#pre0').text(result);



pythonの場合。

sample.py

コード(TextWrangler)

#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-

rows = cols = 8
chessboard = []
for i in range(rows):
    chessboard.append([])
    for j in range(cols):
        if (i % 2 == 0 and j % 2 == 0) or (i % 2 != 0 and j % 2 != 0):
            chessboard[i].append("黒")
        else:
            chessboard[i].append("白")

for a in chessboard:
    for b in a:
        print(b, end="")
    print()

import re
pattern = re.compile(r"^\s*$")
while True:
    try:
        sx = input("x座標: ")
        if re.match(pattern, sx): break
        sy = input("y座標: ")
        x = int(sx)
        y = int(sy)
        print(chessboard[x][y])
    except ValueError as err:
        print(err)
    except IndexError as err:
        print(err)
    except Exception as err:
        print(err)

入出力結果(Terminal)

$ ./sample.py
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
黒白黒白黒白黒白
白黒白黒白黒白黒
x座標: 1
y座標: 1
黒
x座標: 5
y座標: 6
白
x座標: 5
y座標: 5
黒
x座標: 6
y座標: 5
白
x座標: 6
y座標: 6
黒
x座標: 10
y座標: 10
list index out of range
x座標: python
y座標: javascript
invalid literal for int() with base 10: 'python'
x座標: 10
y座標: python
invalid literal for int() with base 10: 'python'
x座標: python
y座標: 10
invalid literal for int() with base 10: 'python'
x座標: 
$

0 コメント:

コメントを投稿