Sha256: 22363e60a8d667b348101b66e841cb71f9bf3f582e6609711800476177c4760a

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

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

STATUS = 10

def write(x,y,text)
  Curses.setpos(y,x)
  Curses.addstr(text);
end

def init_screen
  Curses.noecho
  Curses.init_screen
  Curses.stdscr.keypad(true)
  begin
    yield
  ensure
    Curses.close_screen
  end
end

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

init_screen do
  write(0, STATUS+1, "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.0 bin/tic_tac_toe