CTF日本語サイト

VolgaCTF 2016 Quals

2016年03月29日01:59

VolgaCTF 2016 Quals writeup Tic-Tac-Toe

カテゴリ
VolgaCTF 2016 Quals
  • ctf_daisukictf_daisuki
  • Comment(0)

Tic-Tac-Toe

An important step towards the strong AI is the ability of an artificial agent to solve a well-defined problem. A project by the name 'tic-tac-toe' was one of such test problems. It's still up...

nc tic-tac-toe.2016.volgactf.ru 45679

ncコマンドで接続すると、次のとおり三目並べが始まります。2000試合やる必要がありますので、プログラムを書いて自動で対戦させます。

Welcome to Noughts & Crosses World Championship!
Please, name yourself.
aaa
The match is played over 2000 rounds, the winner of each round gets 1.0 points, the looser gets 0.0 points, and in case of a draw each player gets 0.5 points.
To make your move choose the empty cell and send it's index followed by '\n', e.g. '4\n'.The indices map:
 0 | 1 | 2
---+---+---
 3 | 4 | 5
---+---+---
 6 | 7 | 8

Round number 1.
Server vs. aaa. Current score: 0.0 - 0.0
   |   |  
---+---+---
   | X |  
---+---+---
   |   |  

CPUはそれほど強くありませんので、次の優先順位で置く場所を決めていきます。

  1. 次にCPUに置かれるとこちらが負けてしまう場所
  2. 空いている場所の中で価値の高い順番(真ん中→四隅→各辺の中央)

この程度のアルゴリズムで十分勝ち越すことができます。このRubyプログラムを以下に記載します。

require "socket"

def seikei(ary)
  return ary.collect do |item|
    if item.strip == "" then 0
    elsif item.strip == "O" then 1
    elsif item.strip == "X" then -1
    end
  end
end

def check_winner(ary, who, pos)
  tmp = ary.dup
  if who != 0 then
    tmp[pos] = who
  end
  for i in 0..2 do
    #横方向
    if tmp[i*3+0]+tmp[i*3+1]+tmp[i*3+2]==3 then
      return 1
    elsif tmp[i*3+0]+tmp[i*3+1]+tmp[i*3+2]==-3 then
      return -1
    end
    #縦方向
    if tmp[0+i]+tmp[3+i]+tmp[6+i]==3 then
      return 1
    elsif tmp[0+i]+tmp[3+i]+tmp[6+i]==-3 then
      return -1
    end
  end
  #斜め
  if tmp[0]+tmp[4]+tmp[8]==3 then
    return 1
  elsif tmp[0]+tmp[4]+tmp[8]==-3 then
    return -1
  end
  if tmp[2]+tmp[4]+tmp[6]==3 then
    return 1
  elsif tmp[2]+tmp[4]+tmp[6]==-3 then
    return -1
  end
  return 0
end

def tictactoe(sock, ary)
  cell = [4, 0, 2, 6, 8, 1, 3, 5, 7]
  if ary.length != 9 then
    return
  end
  tmp = seikei(ary)
  if check_winner(tmp, 0, 0) != 0 then
    ary.clear
    return
  end
  sengo = tmp.inject {|sum, n| sum + n }
  #X(-1)の方が多い場合はCPUがX(-1)
  if sengo == -1 then cpu = -1
  else cpu = 1
  end
  #次に置かれると負けてしまう場所に打つ
  for i in 0..8 do
    if tmp[i] == 0 then
      if check_winner(tmp, cpu, i) == cpu then
        printf(i.to_s + "\n")
        sock.write(i.to_s + "\n")
        STDOUT.flush
        ary.clear
        return
      end
    end
  end
  #価値の高い場所に打つ
  cell.each do |i|
    if tmp[i] == 0 then
      printf(i.to_s + "\n")
      sock.write(i.to_s + "\n")
      STDOUT.flush
      ary.clear
      return
    end
  end
  ary.clear
end

begin
  sock = TCPSocket.open("95.213.237.91", 45679)
rescue
  puts "TCPSocket.open failed : #$!\n"
else
    ary = []
    while sock.gets
      buf = $_
      printf( buf )
      STDOUT.flush
      if buf.start_with?("Please, name yourself.") then
        printf("aaa\n")
        sock.write("aaa\n")
        STDOUT.flush
      elsif buf.start_with?("   |") then
        ary.concat(buf.chomp.split("|"))
        tictactoe(sock, ary)
      elsif buf.start_with?(" X |") then
        ary.concat(buf.chomp.split("|"))
        tictactoe(sock, ary)
      elsif buf.start_with?(" O |") then
        ary.concat(buf.chomp.split("|"))
        tictactoe(sock, ary)
      else
      end
    end
  sock.close
end

以下が実行結果です。

Welcome to Noughts & Crosses World Championship!
Please, name yourself.
aaa
The match is played over 2000 rounds, the winner of each round gets 1.0 points, the looser gets 0.0 points, and in case of a draw each player gets 0.5 points.
To make your move choose the empty cell and send it's index followed by '\n', e.g. '4\n'.The indices map:
 0 | 1 | 2
---+---+---
 3 | 4 | 5
---+---+---
 6 | 7 | 8

Round number 1.
Server vs. aaa. Current score: 0.0 - 0.0
   |   |  
---+---+---
   | X |  
---+---+---
   |   |  
0

 O |   | X
---+---+---
   | X |  
---+---+---
   |   |  
6

 O |   | X
---+---+---
   | X |  
---+---+---
 O |   | X
5

 O | X | X
---+---+---
   | X | O
---+---+---
 O |   | X
7

 O | X | X
---+---+---
 X | X | O
---+---+---
 O | O | X

Round number 2.
Server vs. aaa. Current score: 0.5 - 0.5

(略)

Round number 2000.
Server vs. aaa. Current score: 565.5 - 1433.5
   |   |  
---+---+---
   |   |  
---+---+---
   |   |  
4

   |   |  
---+---+---
   | X |  
---+---+---
   |   | O
0

 X |   | O
---+---+---
   | X |  
---+---+---
   |   | O
5

 X |   | O
---+---+---
 O | X | X
---+---+---
   |   | O
6

 X | O | O
---+---+---
 O | X | X
---+---+---
 X |   | O
7

Server vs. aaa. Final score: 566.0 - 1434.0
Congratulations! Your flag is: VolgaCTF{tic_t@c_t0e_is_the_e@rly_step_t0wards_AI}

2000試合対戦して勝ち越しましたので、フラグが表示されました。2000試合は多すぎると思います。

フラグは、

VolgaCTF{tic_t@c_t0e_is_the_e@rly_step_t0wards_AI}

です。



HTML5+CoffeeScriptで作る最強の三目並べプログラム: MIN-MAX法からαβ法へ
HTML5+CoffeeScriptで作る最強の三目並べプログラム: MIN-MAX法からαβ法へ [Kindle版]
happyclam
2016-03-15



このエントリーをはてなブックマークに追加
記事検索
ギャラリー
  • TetCTF 2023 NewYearBot
  • UUT CTF writeup Find The Password
  • UUT CTF writeup The Puzzle
  • Hack Zone Tunisia 2019 writeup Microscope
  • Hack Zone Tunisia 2019 writeup Welcome
  • SwampCTF 2019 writeup Brokerboard
  • SwampCTF 2019 writeup Leap of Faith
  • SwampCTF 2019 writeup Last Transmission
  • CBM CTF 2019 writeup Long road
タグクラウド
  • 101
  • Baby
  • Baby'sFirst
  • BINARY
  • Binary
  • Bonus
  • Code
  • Crypto
  • crypto
  • CRYPTO
  • Cryptography
  • Digital
  • DriveByInc
  • Engineering
  • Foren
  • FORENSIC
  • Forensics
  • General
  • GrabBag
  • Information
  • Joy
  • Linux
  • Malware
  • MicroServices
  • Misc
  • MISC
  • misc
  • Miscellaneous
  • Mobile
  • n00b
  • Network
  • Pentest
  • PPC
  • Pwn
  • pwn
  • RE
  • ReadingRainbow
  • RECON
  • Rev
  • rev
  • Reverse
  • reverse
  • Reversing
  • Sanity
  • Security
  • Story
  • The
  • Trivia
  • warm-up
  • warmup
  • Warmup
  • WarmUp
  • Web
  • web
  • Welcome
最新記事
Incognito 4.0 Meow
DiceCTF 2023 recursive-csp
DiceCTF 2023 Provably Secure
Insomni'hack teaser 2023 Welcome
Real World CTF 5th 0KPR00F
idekCTF 2022 Typop
ASIS CTF Finals 2022 Vindica
TetCTF 2023 Image Services Viewer
TetCTF 2023 shuffle128
TetCTF 2023 NewYearBot
アーカイブ
2023年02月
2023年01月
2019年04月
2019年03月
2019年02月
2019年01月
2018年12月
2018年05月
2018年04月
2018年03月
2018年02月
2018年01月
2017年12月
2017年11月
2017年10月
2017年09月
2017年08月
2017年05月
2017年04月
2017年03月
2017年02月
2016年12月
2016年11月
2016年10月
2016年09月
2016年07月
2016年06月
2016年05月
2016年04月
2016年03月
2016年02月
2016年01月
2015年12月
2015年11月
2015年07月
カテゴリー
ASIS CTF Quals 2014 (2)
WhiteHat Contest 10 (1)
HITCON CTF 2015 Quals (1)
Hackover CTF 2015 (2)
RCTF 2015 Quals (3)
Hack Dat Kiwi 2015 (3)
SECCON 2015 Online CTF (6)
9447 Security Society CTF 2015 (2)
Defcamp CTF Qualification 2015 (2)
32C3 CTF (1)
Trend Micro CTF Asia Pacific & Japan 2015 Online Qualifier (4)
CSAW CTF Qualification Round 2015 (3)
Insomni'hack teaser 2016 (2)
Break In 2016 (3)
HackIM 2016 (3)
Sharif University CTF 2016 (8)
Internetwache CTF 2016 (10)
SSCTF 2016 Quals (2)
Boston Key Party CTF 2016 (2)
0CTF 2016 Quals (1)
Sunshine CTF 2016 (2)
Codegate CTF 2016 Quals (2)
BCTF 2016 (3)
VolgaCTF 2016 Quals (1)
Pwn2Win CTF 2016 (4)
Securinets CTF Quals 2016 (2)
Nuit du Hack CTF Quals 2016 (4)
sCTF 2016 Q1 (8)
PlaidCTF 2016 (5)
Google Capture The Flag 2016 (6)
ASIS CTF Quals 2016 (2)
TU CTF 2016 (9)
DEF CON CTF Qualifier 2016 (2)
ALICTF 2016 (1)
BackdoorCTF 2016 (3)
IceCTF 2016 (1)
CSAW CTF Qualification Round 2016 (4)
HITCON CTF 2016 Quals (4)
EKOPARTY CTF 2016 (5)
Hack The Vote 2016 (5)
SharifCTF 7 (2)
BSides San Francisco CTF (9)
Securinets CTF Quals 2017 (2)
Teaser CONFidence CTF 2017 (1)
Insigne CTF (2)
PlaidCTF 2017 (4)
angstromCTF 2017 (2)
DEF CON CTF Qualifier 2017 (1)
CTFZone 2017 (2)
SHA2017 CTF (3)
HackIT CTF 2017 (1)
EKOPARTY CTF 2017 (1)
CSAW CTF Qualification Round 2017 (1)
Hack Dat Kiwi 2017 (1)
Pwn2Win CTF 2017 (3)
RC3 CTF 2017 (1)
WhiteHat Grand Prix 2017 (2)
InCTF (4)
34C3 CTF (2)
Break In CTF 2018 (2)
Harekaze CTF 2018 (1)
Pragyan CTF 2018 (4)
STEM CTF: Cyber Challenge 2018 (6)
*ctf 2018 (1)
NoNameCon CTF Quals 2018 (1)
ASIS CTF Quals 2018 (6)
PlaidCTF 2018 (1)
35C3 CTF (1)
FireShell CTF 2019 (4)
Evlz CTF (1)
Quals: Saudi and Oman National Cyber Security CTF (7)
STEM CTF: Cyber Challenge 2019 (3)
TAMUctf 19 (20)
BSidesSF 2019 CTF (2)
Aero CTF 2019 (4)
Pragyan CTF 2019 (4)
UTCTF (4)
Teaser CONFidence CTF 2019 (3)
0CTF/TCTF 2019 Quals (1)
Securinets CTF Quals 2019 (4)
b00t2root '19 (2)
VolgaCTF 2019 Qualifier (1)
RADARCTF (4)
Midnight Sun CTF 2019 Quals (2)
AceBear Security Contest 2019 (2)
SpamAndFlags Teaser 2019 (1)
SwampCTF 2019 (5)
CBM CTF 2019 (3)
Byte Bandits CTF 2019 (2)
PlaidCTF 2019 (1)
Hack Zone Tunisia 2019 (3)
YauzaCTF 2019 Quals (1)
TG:Hack (6)
ASIS CTF Quals 2019 (2)
ångstromCTF 2019 (9)
UUT CTF (8)
*CTF 2019 (1)
TetCTF 2023 (4)
ASIS CTF Finals 2022 (1)
Real World CTF 5th (1)
idekCTF 2022 (1)
Insomni'hack teaser 2023 (1)
DiceCTF 2023 (2)
Incognito 4.0 (1)

Powered by ライブドアブログ