1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

漢らしいC++によるどう書く、再び

Posted at

先に書いたコードに、鍋谷さんから「漢らしいC++」との評を頂いてしまいました。ありがとうございます。
もっと漢らしくしてみました(爆)。
なんだかもう呪文みたいになってしまってます。

#include <iostream>
#include <string>

enum Result { FAUL_X_WON, FAUL_O_WON, O_WON, X_WON, DRAW_GAME };

Result doukaku(const std::string& t)
{
    unsigned int b[9] = { 0xb6b6u, 0xeeeeu, 0x5e5eu, 0xf5f5u, 0x2d2du, 0xddddu, 0x7373u, 0xebebu, 0x9b9b };
    for(int i = 0; i < 9; ++i)
    {
        int n = t[i] - '0' - 1;
        int p = i % 2;
        if((b[n] ^ (b[n] >> 8)) & 0xffu)
        {
            return static_cast<Result>(p);
        }
        b[n] |= 0xffu << (8 * p);
        if(b[0] & b[1] & b[2] & b[3] & b[4] & b[5] & b[6] & b[7] & b[8])
        {
            return static_cast<Result>(p + 2);
        }
    }
    return DRAW_GAME;
}

int main(int, char* [])
{
    std::string s;
    while((std::cin >> s).good())
    {
        switch(doukaku(s))
        {
        case FAUL_X_WON: std::cout << "Faul: x won.\n"; break;
        case FAUL_O_WON: std::cout << "Faul: o won.\n"; break;
        case X_WON:      std::cout << "x won.\n";       break; 
        case O_WON:      std::cout << "o won.\n";       break;
        default:         std::cout << "Draw game.\n";   break;
        }
    }

    return 0;
}
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?