Permalink
Browse files

simple board logic

  • Loading branch information...
1 parent 9dd2ccf commit 6f62ec9a2084ecee59caf310aed75eb74c6e0221 @grosser committed Dec 31, 2010
Showing with 78 additions and 4 deletions.
  1. +78 −4 bin/tic_tac_toe
View
@@ -1,29 +1,103 @@
#!/usr/bin/env ruby
require 'curses'
-def write(x,y,text)
+BOARD = <<BOARD
+-------------
+| X | X | X |
+-------------
+| X | X | X |
+-------------
+| X | X | X |
+-------------
+BOARD
+
+class String
+ def indexes(needle)
+ found = []
+ current_index = -1
+ while current_index = index(needle, current_index+1)
+ found << current_index
+ end
+ found
+ end
+end
+"aa aaaa aa".indexes('a') # [0, 1, 3, 4, 5, 6, 11, 12]
+
+def goto(x,y)
Curses.setpos(y,x)
+end
+
+def write(x,y,text)
+ goto(x,y)
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 draw_board(fields)
+ index = -1
+ board = BOARD.gsub('X') do
+ index += 1
+ fields[index]
+ end
+ write(0,0, board)
+end
+
+def move_player(x,y)
+ # set new $position
+ current = possible_positions.index($position)
+ x = (current + x) % 3
+ y = ((current / 3) + y) % 3
+ $position = x + (y * 3)
+ write(0,0,$position.inspect)
+
+ # set the cursor
+ x,y = possible_positions[$position]
+ write x,y,'O'
+end
+
+def possible_positions
+ board_width = BOARD.split("\n").first.size + 1
+ BOARD.indexes('X').map do |index|
+ x = index % board_width
+ y = index / board_width
+ [x, y]
+ end
+end
+
+def all_indices_of(haystack, needle)
+ indexes = []
+ offset = 0
+ while index = BOARD.index('X', offset)
+ x = index % board_width
+ y = index / board_width
+ positions << [x, y]
+ offset = index
+ end
+end
+
init_screen do
- write(0,0,'TEST')
- write(0,10,'Y')
- write(10,0,'X')
+ $position = possible_positions.first
+ fields = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
+ draw_board fields
loop do
case Curses.getch
+ when Curses::Key::UP then move_player(0,1)
+ when Curses::Key::DOWN then move_player(0,-1)
+ when Curses::Key::RIGHT then move_player(1,0)
+ when Curses::Key::LEFT then move_player(-1,0)
when ?q then break
end
+ draw_board fields
end
end

0 comments on commit 6f62ec9

Please sign in to comment.