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.

第一回オフラインリアルタイムどう書く

Posted at

第一回 オフラインリアルタイムどう書く(http://atnd.org/events/30285)
の問題をJavaで解いてみました。
問題のURL(http://nabetani.sakura.ne.jp/hena/1/)
所要時間は、問題を見てから1週間以上、
書き始めてから1時間ほどです。

TickTackToe.java
public class TickTackToe {
    private static final String DRAW_MESSAGE = "Draw game.";
    private enum Player {
        FIRST( "o won.", "Foul : x won.") {
            @Override Player next() { return SECOND; }
        },
        SECOND( "x won.", "Foul : o won." ) {
            @Override Player next() { return FIRST; }
        },
        ;
        Player( String won, String foul ) {
            WON_MESSAGE = won;
            FOUL_MESSAGE = foul;
        }
        private final String WON_MESSAGE;
        private final String FOUL_MESSAGE;
        abstract Player next();
        String won() { return WON_MESSAGE; };
        String foul() { return FOUL_MESSAGE; };
    }

    private static final int BOXES_MAX = 9;
    private static final int[][] LINES = {
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        {0, 3, 6},
        {1, 4, 7},
        {2, 5, 8},
        {0, 4, 8},
        {2, 4, 6},
    };

    public String match( String process ) {
        Player[] boxes = new Player[BOXES_MAX];
        Player move = Player.FIRST;
        int indexSize = process.length() < BOXES_MAX
            ? process.length()
            : BOXES_MAX;
        for ( int i = 0; i < indexSize; i++ ) {
            String result = put( process.charAt(i), move, boxes );
            if ( result != null ) {
                return result;
            }
            move = move.next();
        }
        return DRAW_MESSAGE;
    }

    private String put( char place, Player move, Player[] boxes ) {
        int boxesIndex = place - '1';
        if ( boxes[boxesIndex] != null ) {
            return move.foul();
        }
        boxes[boxesIndex] = move;
        for ( int[] line : LINES ) {
            if ( isFullLine( move, line, boxes ) ) {
                return move.won();
            }
        }
        return null;
    }

    private boolean isFullLine( Player move, int[] line, Player[] boxes ) {
        for ( int boxIndex : line ) {
            if ( boxes[boxIndex] != move ) {
                return false;
            }
        }
        return true;
    }
    
    public static void main( String[] args ) {
	if ( args.length == 0 ) {
	    System.out.println( "argument error." );
	    return;
	}
	TickTackToe ttt = new TickTackToe();
	for ( String process : args ) {
	    System.out.println( ttt.match(process) );
	}
    }
}
1
1
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
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?