码迷,mamicode.com
首页 > 其他好文 > 详细

Epic - Tic Tac Toe

时间:2015-06-13 09:46:51      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If 3 consecutive samecolor found, that color will get 1 point. So if 4 red are vertically then pointis 2. Find the winner.

 

def tic_tac_toe(board,n)
  red, black =0, 0
  n.times do |i|
    n.times do |j|
      if board[i][j]
        red += 1 if i<m-2 and board[i+1][j] and board[i+2][j]
        red += 1 if j<n-2 and board[i][j+1] and board[i][j+2]
        red += 1 if i<m-2 and j<n-2 and board[i+1][j+1] and board[i+2][j+2]
        red += 1 if i>=2 and j>=2 and board[i-1][j-1] and board[i-2][j-2]
      else
        black += 1 if i<m-2 and !board[i+1][j] and !board[i+2][j]
        black += 1 if j<n-2 and !board[i][j+1] and !board[i][j+2]
        black += 1 if i<m-2 and j<n-2 and !board[i+1][j+1] and !board[i+2][j+2]
        black += 1 if i>=2 and j>=2 and !board[i-1][j-1] and !board[i-2][j-2]
      end
    end
  end
  red > black
end

 

Epic - Tic Tac Toe

标签:

原文地址:http://www.cnblogs.com/lilixu/p/4573002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!