9/16 NSEG #92 に参加しました。参加者9人でした。
アベさんが「kotlin Null Safety」という発表をしました。
Kotlinでぬるぽを発生させる方法あれこれの紹介でした(?)。
http://rayflood.org/diary-temp/nseg92.html
それ以外はネタが無かったので、「横浜へなちょこプログラミング勉強会」の「オフラインリアルタイムどう書く」の過去問をパクってみんなでやりました。
「どう書く」はたしかRubyKaigi 2016の懇親会で聞いたと思うのですが、面白そうだから機会があればやりたいと思ってました。
Tick-Tack-Toe 〜 横へな 2012.7.6 を2時間くらいでもくもくと作り、その後各自書いたプログラムを披露するって感じで。
私は最初Rubyで書いて、その後Cで書き直しました。Cをかなり忘れててやばかったです。
@board = []
def next_player
@player == 'o' ? 'x' : 'o'
end
def judge
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7],
].each do |i, j, k|
if @board[i] && @board[i] == @board[j] && @board[j] == @board[k]
return "#{@board[i]} won."
end
end
nil
end
def ttt(input)
@board.clear
@player = 'o'
turn = 1
input.each_char do |c|
break if turn > 9
i = c.to_i
return "Foul : #{next_player} won." if @board[i]
@board[i] = @player
if result = judge
return result
end
@player = next_player
turn += 1
end
return 'Draw game.'
end
data = <<EOS
79538246 x won.
35497162193 x won.
61978543 x won.
254961323121 x won.
6134278187 x won.
4319581 Foul : x won.
9625663381 Foul : x won.
7975662 Foul : x won.
2368799597 Foul : x won.
18652368566 Foul : x won.
965715 o won.
38745796 o won.
371929 o won.
758698769 o won.
42683953 o won.
618843927 Foul : o won.
36535224 Foul : o won.
882973 Foul : o won.
653675681 Foul : o won.
9729934662 Foul : o won.
972651483927 Draw game.
5439126787 Draw game.
142583697 Draw game.
42198637563 Draw game.
657391482 Draw game.
EOS
data.each_line do |line|
input, output = line.chomp.split(/\t+/)
res = ttt(input)
unless res == output
puts "ttt(#{input}) returned #{res.inspect} but expected #{output.inspect}"
end
end
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char player = 'o';
char board[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char next_player()
{
return player == 'o' ? 'x' : 'o';
}
char judge()
{
int cond[][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{1, 4, 7},
{2, 5, 8},
{3, 6, 9},
{1, 5, 9},
{3, 5, 7},
};
for (int l = 0; l < sizeof(cond)/sizeof(cond[0]); l++) {
int i = cond[l][0];
int j = cond[l][1];
int k = cond[l][2];
if (board[i] && board[i] == board[j] && board[j] == board[k])
return board[i];
}
return 0;
}
int main(int argc, char *argv[])
{
int turn = 1;
for (int i = 0; i < strlen(argv[1]); i++) {
char c = argv[1][i];
if (turn > 9)
break;
int x = c - '0';
if (board[x]) {
printf("Foul : %c won.\n", next_player());
return 0;
}
board[x] = player;
char winner = judge();
if (winner) {
printf("%c won.\n", winner);
return 0;
}
player = next_player();
turn += 1;
}
puts("Draw game.");
return 0;
}