【问题标题】:Creating Dice game in java using Multithreading [closed]使用多线程在java中创建骰子游戏[关闭]
【发布时间】:2013-07-24 14:25:53
【问题描述】:

我想提供一个 4 人骰子游戏的实现,其中每个玩家都被实现为一个线程。每个玩家都有机会按照玩家编号的顺序掷骰子。每个投掷骰子只返回一个数字 1 到 6。每当任何玩家得分 25 分或更多打印获胜者玩家时,游戏就会停止。

我正在考虑创建类

骰子用于处理骰子

Board用于处理所有线程和玩家分数

TheGame 用于开始游戏

我的班级骰子

import java.util.Random;

public class Dice {
Random dice;
public Dice()
{
dice=new Random();
}

public int throwDice()
{
    int a= dice.nextInt(70);
    a=a/10;
    if (a==0)
        return 1;
    else return a;
}

}

我的玩家类

public class Player extends Thread {
Board board;
int num;
public Player(Board b,int n)
{
board=b;
num=n;
}
public void run()
{
System.out.println("Player "+num+" is started, playing game...");
while(board.flag)
{
board.play(num);    
}
}
}

游戏类

public class TheGame {


    public static void main(String[] args) {
        System.out.println("Initializing board...");
        Board b=new Board();  //Creating object of class board
        System.out.println("Initializing players...");
        Player p1=new Player(b,1); // Every player is Thread
        Player p2=new Player(b,2);
        Player p3=new Player(b,3);
        Player p4=new Player(b,4);
        p1.start(); //Starting Thread
        p3.start();
        p2.start();
        p4.start();
    }

}

我无法思考逻辑或决定从哪里开始上课。 请帮我提供板代码

这不是作业或家庭作业。 我想自己做,但对线程中的同步没有任何深入的了解

【问题讨论】:

  • 如果你的导师告诉你在基于文本的骰子游戏中使用多线程,我会找一个新导师。
  • @hexafraction 我只是想尝试一下。如果你能提供帮助,那就太好了
  • 在答案中放置完整的线程教程相对困难。我建议the Oracle tutorial 并在您有具体问题时询问。
  • 你需要同步逻辑。阅读本教程的更多内容或重新表述您的问题以更具体。
  • 我在我的问题中添加了代码和更多详细信息希望它会再次打开。

标签: java multithreading sync


【解决方案1】:

这只是为您指明正确的方向。你需要阅读比我在这里写的更多的东西。然后,您需要修复各种奇怪且无法重现的错误。但你确实问过……

我认为如果所有 4 名玩家同时投掷,它会简单得多,也更有趣。 (尽管您可能有不止一个获胜者。)要做到这一点:

创建全局监视器和字段(可能在 Game 类中):

public static final turnMonitor = new Object();
public static final controlMonitor = new Object();
public static volatile gameOn = true;

然后创建一个带有 run 方法的 Player 类。 (Player 可以扩展 Thread,也可以扩展 Runnable,你可以将它传递给一个新的 Thread。)像这样:

public void run()  {
    while (gameOn)  {
        synchronized (turnMonitor)  { turnMonitor.wait(); }
        ...roll dice here...
        ...Put sum of Player rolls in volatile instance field or synched field...
        ...Update volatile player turn counter...
        synchronized (controlMonitor)  {
            // Tell control monitor we're done.
            controlMonitor.notifyAll();
        }
    }
}

然后你会想要控制代码(在 Game 类中?):

while (true)  {
    // Roll dice
    synchronized (turnMonitor)  { turnMonitor.notifyAll(); }
    // Wait for dice to roll
    do  {
        synchronized (controlMonitor)  { controlMonitor.wait(); }
    }  while ( ...not all players have rolled this turn... );
    if ( there's a winner )  break;
}
gameOn = false;

这应该给你一个开始。学习同步、易失性、wait() 和 notifyAll()。如果您在任何地方都找不到好的示例,请查看here(了解基础知识)。

启动时,打印大量调试消息。当您不希望它们运行时,线程总是在运行。你会感到惊讶。

要让玩家一次滚动一个,我认为您需要多个回合监视器。您一次只能通知一个等待线程,但您无法控制哪个线程收到通知,因此尝试从一台监视器上获取正确的线程会很困难。

【讨论】:

  • 非常感谢,对我有很大帮助,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 2014-12-06
  • 2017-08-13
  • 2013-02-03
相关资源
最近更新 更多