1
0

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.

「どう書く」をこう書いてみた

Last updated at Posted at 2012-07-07

コードにするのに30分ぐらいでした。が、昨夜のうちに問題をちらっと見てしまっていたので考えてた時間は結構長いです。

お題はこちらです。
http://nabetani.sakura.ne.jp/hena/1/

…。
ちょっとミスってたのでコソっと修正。

#include <iostream>
#include <string>

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

Result doukaku(const std::string& t)
{
    int board[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    for(int i = 0; i < 9; ++i)
    {
        if(board[t[i] - '0' - 1] != 0)
        {
            return static_cast<Result>(i % 2);
        }
        board[t[i] - '0' - 1] = i % 2 + 1;
        if( (board[0] & board[1] & board[2]) ||
            (board[3] & board[4] & board[5]) ||
            (board[6] & board[7] & board[8]) ||
            (board[0] & board[3] & board[6]) ||
            (board[1] & board[4] & board[7]) ||
            (board[2] & board[5] & board[8]) ||
            (board[0] & board[4] & board[8]) ||
            (board[2] & board[4] & board[6]))
        {
            return static_cast<Result>((i % 2) + 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
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?