当前位置: 首页 > 知识库问答 >
问题:

在骰子游戏中确定多轮获胜者的函数问题

韦昊焜
2023-03-14

我们有一项任务来决定骰子游戏的最终赢家。以下是完整的规则:

在这个游戏中,你将与另外两个电脑玩家对决。有三轮。每轮开始时,一个玩家掷两个骰子。你那一轮掷骰子的总数加在你的点数上。你的积分总是从零开始。每连续一轮,积分加到总数上。第三轮结束时,谁的积分最多,谁就赢得比赛。

def welcome
    puts ""
    puts "Let's roll some die! You will be competing against 2 computers in 3 rounds, each player having 2 die."
    puts "Please enter your name and press 'Enter'"
    puts ""
    @userName = gets.chomp
    puts ("Welcome, " + @userName + ". Let's roll those die!")
    puts ""
end

def round(round)
    result_1 = Random.new.rand(1..6)
    result_2 = Random.new.rand(1..6)
    total_1 = result_1 + result_2

    result_3 = Random.new.rand(1..6)
    result_4 = Random.new.rand(1..6)
    total_2 = result_3 + result_4

    result_5 = Random.new.rand(1..6)
    result_6 = Random.new.rand(1..6)
    total_3 = result_5 + result_6

    winner = [total_1, total_2, total_3].max
    players = {total_1 => @userName, total_2 => "Computer 1", total_3 => "Computer 2"}.max

    puts "Round #{round}"
    puts "#{@userName}, your die are a #{result_1} and a #{result_2}, totaling #{total_1}"
    puts "Computer 1's die are a #{result_3} and a #{result_4}, totaling #{total_2}"
    puts "Computer 2's die are a #{result_5} and a #{result_6}, totaling #{total_3}"
    puts "Round #{round} highest score is: #{winner}"
        if total_1> total_2 && total_3
            puts "#{@userName} is the winner!"
        elsif total_2 > total_1 && total_3
            puts "Computer 1 is the winner!"
        elsif total_3 > total_1 && total_2
            puts "Computer 2 is the winner!"
        else
            puts "It was a tie, there are no winners."
        end
    puts ""  
end

def gameWinner(rounds)
    if total_1> total_2 && total_3
        roundWinner1 = 1
    elsif total_2 > total_1 && total_3
        roundWinner2 = 1
    else total_3 > total_1 && total_2
        roundWinner3 = 1
    end

    gameSummary1 = roundWinner1 + roundWinner1 + roundWinner1
    gameSummary2 = roundWinner2 + roundWinner2 + roundWinner2
    gameSummary3 = roundWinner3 + roundWinner3 + roundWinner3
    winnerOA = [gameSummary1, gameSummary2, gameSummary3].max

    puts "The winner of the game is #{winnerOA}"
end

def playAgain
    puts "Would you like to start over? (Y/N)\n"
    answer = gets.chomp.upcase
    if answer == "Y"
        play
    elsif answer == "N"
        puts "Thank you for playing.\n"
    else
        playAgain
    end    
  end

def play
    welcome
    round(1)
    round_1 = round(1)   
    round(2)
    round(3)
    gameWinner    
    playAgain
end

play

回溯(最近的调用):2:from rollem 2.rb:83:in'1:from rollem 2.rb:79:in play'rollem 2.rb:44:in'gamewinner':参数数错误(给定0,预期为1)(ArgumentError)

共有1个答案

柯栋
2023-03-14

调用方法GameWinner时没有参数,但看起来方法中没有使用所需的参数。所以试着把它从定义中移除:

def gameWinner
  #... your method code
end
 类似资料:
  • 谁能告诉我代码出了什么问题吗?

  • 代码的目的是让两个玩家掷一对骰子。第一个掷出20分的玩家赢得比赛。我很难弄清楚如何正确地跟踪滚动的总和;它只给我当前回合的总和,然后当每个玩家滚动10次时游戏就结束了。 我如何正确地计算每个玩家游戏的总和,然后当其中一个玩家的总和等于20时停止循环?

  • 我试图为一个游戏的掷骰子程序,其中用户输入一个下注金额和2个六面骰子滚动,如果7或11是滚动,他们赢了。如果掷2,3或12就输了,如果掷其他任何一个数字,它就显示该数字为一个点。它会继续掷骰子,直到掷7或该点为止,如果掷7就输了,否则就赢了。出于某种原因,在掷骰子时,他会掷骰子,然后再掷一次,看看他们是否赢了或输了。我不知道如何解决这个问题,任何帮助都不会附带

  • 提示:“写一个程序对着电脑玩猪游戏。在每个回合,当前玩家将掷出一对骰子并累积点数。目标是在你的对手之前达到100分或更多。(为了测试的目的,使用30点而不是100点)如果在任何回合中,玩家掷出一个1,则该回合积累的所有点数都将被没收,骰子的控制权将转移到另一个玩家身上。如果玩家在一个回合中滚动两个1,则该玩家失去迄今为止积累的所有点数,控制权转移到另一个玩家身上。玩家可以在每次掷骰后自愿交出骰子的

  • 有人能在这里给我指个正确的方向吗?我的游戏工作完美,但我想添加一些实际的互动/目标。谢谢

  • 我对C#和一般编码都是新手。为了提高我的技能,我试图创建一个基本的游戏,两个玩家掷骰子,并记录他们的得分。玩家达到20分即获胜。每个玩家轮流掷一个骰子,把他们的第一个骰子加到他们的第二个骰子上,以此类推,直到其中一个达到20。如果玩家掷出一个六,他们可以再次掷骰子。 任何帮助都将不胜感激。