LoginSignup
1
1

More than 5 years have passed since last update.

第1回オフラインリアルタイムどう書く(Java)

Last updated at Posted at 2014-04-18

Main.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
        private static int[] table = new int[(int)Math.pow(3, 9)];
    private static int convertBoardToInt(char[] inp) {
        int sum = 0;
        int[] board = new int[9];
        for (int i = 0; i < 9; i++) {
            if(hasWon(sum) != 0) return sum;
            int p = i % 2 + 1;
            int d = inp[i] - '0' - 1;
            if (board[d] != 0) return -p;
            else               board[d] = p;
            sum += p * (int)Math.pow(3, d);
        }
        return sum;
    }
    private static int[] hashToIntArray(int h) {
        int[] a = new int[9];
        for(int i = 0; i < 9; i++) {
            a[i] = h % 3;
            h /= 3;
        }
        return a;
    }
    private static void setResults() {
            int size = table.length;
        for(int h = 0; h < size; h++) {
            int[] a = hashToIntArray(h);
            if      (a[0] != 0    &&
                                 a[0] == a[1] && 
                                 a[0] == a[2]) table[h] = a[0];
            else if (a[3] != 0 &&
                 a[3] == a[4] &&
                                 a[3] == a[5]) table[h] = a[3];
            else if (a[6] != 0 &&
                 a[6] == a[7] &&
                                 a[6] == a[8]) table[h] = a[6];
            else if (a[0] != 0 &&
                 a[0] == a[3] &&
                                 a[0] == a[6]) table[h] = a[0];
            else if (a[1] != 0 &&
                 a[1] == a[4] &&
                                 a[1] == a[7]) table[h] = a[1];
            else if (a[2] != 0 &&
                 a[2] == a[5] &&
                                 a[2] == a[8]) table[h] = a[2];
            else if (a[0] != 0 &&
                 a[0] == a[4] &&
                                 a[0] == a[8]) table[h] = a[0];
            else if (a[2] != 0 &&
                 a[2] == a[4] &&
                                 a[2] == a[6]) table[h] = a[2];
        }  
        }
    private static int hasWon(int h) {
        return table[h];
        } 
    public static void main(String[] args) throws Exception {
        setResults();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while((line = br.readLine()) != null) {
            String[] ss = line.split("\\t");
            int v = convertBoardToInt(ss[1].toCharArray());
            String result = null;
            if(v < 0) {
                result = String.format("Foul : %s won.", (-v == 1)? "x": "o");
            } else {
                switch(hasWon(v)) {
                case 0: result = "Draw game."; break;
                case 1: result = "o won."; break;
                case 2: result = "x won."; break;
                }
            }
            if(!result.equals(ss[2])) {
                System.out.println("input:" + ss[1]);
                System.out.println("expected:" + ss[2]);
                System.out.println("actual:" + result);
            }
        }
    }

}
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