Sha256: 43b0e67f8fcd8c9f38486f20394530527a0be9bd751f55e22077b0f4f13ad757

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

#!/usr/bin/env ruby
require 'curses'
require File.expand_path('../../lib/tic_tac_toe', __FILE__)

STATUS_LINE = 10

def write(line, column, text)
  Curses.setpos(line, column)
  Curses.addstr(text);
end

def init_screen
  Curses.noecho # do not show typed keys
  Curses.init_screen
  Curses.stdscr.keypad(true) # enable arrow keys
  begin
    yield
  ensure
    Curses.close_screen
  end
end

def display(ttt)
  write 0,0,ttt.board
  if winner = ttt.winner
    write(STATUS_LINE, 0, "Player #{winner} has won!!!!")
  elsif ttt.draw?
    write(STATUS_LINE, 0, "It is a draw...")
  else
    write(STATUS_LINE, 0, "It is #{ttt.player}`s turn...")
  end
end

init_screen do
  write(STATUS_LINE+1, 0, "q=Quit r=Reset a=AI-move")

  ttt = TicTacToe.new

  loop do
    display ttt

    case Curses.getch
    when Curses::Key::UP then ttt.move(0,-1)
    when Curses::Key::DOWN then ttt.move(0,1)
    when Curses::Key::RIGHT then ttt.move(1,0)
    when Curses::Key::LEFT then ttt.move(-1,0)
    when ?a then ttt.ai_move
    when 10 then ttt.set # enter
    when ?q then break
    when ?r then ttt = TicTacToe.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tic_tac_toe-0.2.1 bin/tic_tac_toe