Tic-Tac-Toe:检查一名球员是否赢了

时间:2016-12-01 08:42:03

标签: ruby

我目前正在制作一款曾经如此受欢迎的Tic-Tac-Toe游戏的复制品,使用纯红宝石制作。测试了TDD风格。

我唯一的问题是,在我的生活中,我无法弄清楚如何检查玩家何时获胜。我知道这是一个相对简单的问题,但我只是在其中的一天 - 而且我真的只是作为最后的手段在这里发布。

非常感谢任何帮助 - 并且可以随意批评我当前的代码!我能应付。 :)

我的代码:

class TicTacToe

WINNING_COMBINATIONS = [[0, 1, 2], [3, 4, 5], [6, 7, 8], #Horizontal
                      [0, 3, 6], [1, 4, 7], [2, 5, 8], #Vertical
                      [0, 4, 8], [2, 4, 6]] #Diagonal    

def initialize
  @board = ["", "", "", "", "", "", "", "", ""]
  @players = ['player_one', 'player_two']
  @current_player = 'player_one'
end

def player_1
  @players.first
end

def player_2
  @players.last
end

def switch
  @current_player = opponent_of(@current_player)
end

def display_board
  puts " #{@board[0]} | #{@board[1]} | #{@board[2]} "
  puts "-----------"
  puts " #{@board[3]} | #{@board[4]} | #{@board[5]} "
  puts "-----------"
  puts " #{@board[6]} | #{@board[7]} | #{@board[8]} "
end

def move(position)
  fail "Please select a space within range." if position < 1 || position > 9
  fail "That space is already taken!" unless @board[position-1].empty?
if @current_player == player_1
  @board[position-1] = 'o'
else
  @board[position-1] = 'x'
end
  switch
end

def check_for_winner
  #Help!
end

private

def opponent_of(player)
  @players.select { |p| p != player }.first
  end
end

4 个答案:

答案 0 :(得分:2)

我会从这样的事情开始:

def check_for_winner
  WINNING_COMBINATIONS.each do |combination|
    case board.values_at(*combination)
    when %w(o o o)
      return 'Player 1 wins'
    when %w(x x x)
      return 'Player 2 wins'
    end
  end
end

答案 1 :(得分:1)

你非常接近。我不想为你编写代码,但我会给你一些伪代码,你应该从中识别出适用的正确算法:

def has_winner(board)
  WINNING_COMBINATIONS.each do |i, j, k|
    return true if board[i] == board[j] && board[i] == board[k]
  end
  false
end

根据需要进行整合,然后可能会试图通过一些更高阶的函数来获得所有的幻想。

顺便说一句,有一些非常奇特且更快的方法来计算基于位掩码和两个等的求和功能的获胜,但我猜测最可读的是什么。

答案 2 :(得分:1)

这是确定小阵列(获胜集)是否完全是较大阵列(板)的子集的问题。你可以用简单的数组减法来做到这一点。

def has_winner(board)
  WINNING_COMBINATIONS.each do |line|
    return true if (line - board) == []
  end
  return false
end

将@Stefan的优秀建议纳入另一个答案......

def has_winner(board)
   WINNING_COMBINATIONS.any? {|line| (line - board) == [] }
end

答案 3 :(得分:1)

Using if elsif else (:Tic Tac Toe)  *Game*" )
------------------------------------------------
a=['','','','','','','','','']

puts "(Tic Tac Toe)  *Game*"
puts ""
puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                            "
a.each_with_index do |n,index|
val=''
puts "Enter your Sign:"
val=gets.chomp
loc=''
puts "Enter location:"
loc=gets.to_i
if loc == 1
 a[0]=val
 puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "
end
# check posible condition to win loction (1)
if   (a[0] !="") && (a[0] == a[1]) && (a[0] == a[2])
  puts "You are win"
break
elsif  (a[0] !="") && (a[0] == a[3]) && (a[0] == a[6])
  puts "You are win"
break
elsif (a[0] !="") && (a[0] == a[4]) && (a[0] == a[8])
  puts "You are win"
break
else
end
if loc == 02
 a[1]=val
  puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "
end
# check posible condition to win loction (2)
if   (a[1] !="") && (a[0] == a[1]) && (a[0] == a[2])
  puts "You are win"
break
elsif (a[1] !="") && (a[1] == a[4]) && (a[1] == a[7])
  puts "You are win"
break
else
end
if loc == 3
a[2]=val
 puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "
end
# check posible condition to win loction (3)
if   (a[2] !="") && (a[2] == a[1]) && (a[2] == a[0])
  puts "you are win"
break
elsif (a[2] !="") && (a[2] == a[4]) && (a[2] == a[6])
  puts "You are win"
break
elsif (a[2] !="") && (a[2] == a[5]) && (a[2] == a[8])
  puts "You are win"
break
else
end
# End win posible conditions
if loc == 4
 a[3]=val
  puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "
end
# check posible condition to win loction (4)
if   (a[3] !="") && (a[3] == a[4]) && (a[3] == a[5])
  puts "you are win"
break
elsif  (a[3] !="") && (a[3] == a[0]) && (a[3] == a[6])
  puts "You are win"
break
else
end
# End win posible conditions
if loc == 5
 a[4]=val
  puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "
end
# check posible condition to win loction (5)
if   (a[4] !="") && (a[4] == a[1]) && (a[4] == a[7])
  puts "You are win"
break
elsif (a[4] !="") && (a[4] == a[3]) && (a[4] == a[5])
  puts "You are win"
break
elsif (a[4] !="") && (a[4] == a[2]) && (a[4] == a[6])
  puts "You are win"
break
else
end
if loc == 6
  a[5]=val
   puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "
end
# check posible condition to win loction (6)
if   (a[5] !="") && (a[5] == a[2]) && (a[5] == a[8])
  puts "You are win"
break
elsif  (a[5] !="") && (a[5] == a[3]) && (a[5] == a[4])
  puts "You are win"
break
else
end
if loc == 7
 a[6]=val
  puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "
end
# check posible condition to win loction (7)
if   (a[6] !="") && (a[6] == a[1]) && (a[6] == a[3])
  puts "You are win"
break
elsif (a[6] !="") && (a[6] == a[7]) && (a[6] == a[8])
  puts "You are win"
break
elsif (a[6] !="") && (a[6] == a[4]) && (a[6] == a[2])
   puts "You are win"
break
else
end
if loc == 8
a[7]=val
 puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "
end
# check posible condition to win loction (8)
if   (a[7] !="") && (a[7] == a[6]) && (a[7] == a[8])
   puts "You are win"
break
elsif  (a[7] !="") && (a[7] == a[4]) && (a[7] == a[1])
   puts "You are win"
break
else
end
if loc == 9
 a[8]=val
  puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "
end
# check posible condition to win loction (9)
if   (a[8] !="") && (a[8] == a[7]) && (a[8] == a[6])
   puts "You are win"
break
elsif (a[8] !="") && (a[8] == a[4]) && (a[8] == a[0])
  puts  puts "You are win"
break
elsif (a[8] !="") && (a[8] == a[5]) && (a[8] == a[2])
  puts "you are win"
break
else
  puts "Again play No winner"
end
end
puts "=============================="
puts "Game Preview"

puts " #{a[0]} | #{a[1]} | #{a[2]} "
puts "-----------"
puts " #{a[3]} | #{a[4]} | #{a[5]} "
puts "-----------"
puts " #{a[6]} | #{a[7]} | #{a[8]} "
puts "                             "