Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Giúp đỡ về game phiêu lưu

    Hi các bạn. Hiện giờ mình đang có bài tập về cái game này. Luât chơi của game rất đơn giản chỉ cần đi thu thập những món đồ như đèn pin(tỏuch), ví(wallet), hay điện thoại(phone) và túi xách(handbag)... và người chơi chỉ được phép lượm 4 món tối đa. Nhưng gio xách vẫn lượm được cho dù người chơi đã đầy thùng đồ. Một khi đã lượm được gỏi xách thì có thể lươm thêm được 2 món đồ nữa. Những đồ vật kia có thể ném ra ngoài ngoại trừ gio xách (handbag). Mình add code đậy mong pro nào hướng dẫn cho mình.

    /**
    * Inventory takes care of items that the player is carrying
    *
    * @author
    * @version
    */
    public class Inventory
    {
    /**
    * Whether the player has each item
    */
    private boolean hasItem[] = new boolean[Item.values().length];

    /**
    * max number of items the player can carry without the handbag
    */
    private static final int MAX_ITEMS = 4;
    /**
    * Keeps track of how many things the player is carrying
    */
    private int numItems = 0;

    /**
    * This method can be used to have the player pick up an item
    *
    * @param item the thing to pick up
    * @param location where the things is picked up from
    */
    public void pickUp(Item item, Location location)
    {
    if(youHave(item))
    {
    System.out.println("You already have the " + item + "!");
    }
    else if(!location.isHere(item))
    {
    System.out.println("There is no " + item + " here!");
    }
    else if(numItems < MAX_ITEMS)
    {
    location.removeItem(item);
    hasItem[item.ordinal()] = true;
    numItems++;
    System.out.println("ok");
    }
    else
    {
    System.out.println("You can't carry any more!");
    }
    }

    /**
    * This method can be used to have the player drop an item
    *
    * @param item the item to drop
    * @param location where to drop it
    */
    public void drop(Item item, Location location)
    {
    if(!hasItem[item.ordinal()])
    {
    System.out.println("You don't have the " + item + "!");
    }
    else
    {
    hasItem[item.ordinal()] = false;
    location.addItem(item);
    numItems--;
    System.out.println("ok");
    }
    }

    /**
    * This method checks whether the player currently has the item
    *
    * @param item which item
    *
    * @return true if he/she does
    */
    public boolean youHave(Item item)
    {
    return hasItem[item.ordinal()];
    }

    /**
    * This method prints a list of what the player is currently carrying
    */
    public void listWhatYouHave()
    {
    if (hasItem[6]) // item is a handbag
    {
    System.out.println("You are carrying these items in your handbag:");
    }
    else
    {
    System.out.println("You are carrying:");
    }
    for (int ii = 0; ii < 6; ii++)
    {
    if (hasItem[ii])
    {
    switch (ii)
    {
    case 0:
    System.out.println("Torch");
    break;
    case 1:
    System.out.println("Wallet");
    break;
    case 2:
    System.out.println("Keys");
    break;
    case 3:
    System.out.println("Ring");
    break;
    case 4:
    System.out.println("USB");
    break;
    case 5:
    System.out.println("Mobile");
    break;
    default:
    System.out.println("Invalid item!");
    break;
    }
    }
    }
    }

    /**
    * This method checks whether an item is currently in the room,
    * or being carried by the player
    *
    * @param the item to check
    * @location where to check for it
    *
    * @return true if the item is here
    */
    public boolean isHere(Item item, Location location)
    {
    return youHave(item) || location.isHere(item);
    }

    }



    Mình không biết chỉnh 2 cái method
    pickup() method và
    drop() method cho phù hơp với cái túi xách. Mong mọi người chỉ giáo. Cảm ơn mọi người rất nhiều!

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Có pro nào hướng dẫn giúp mình với. Thanks

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    http://adcvietnam.net/

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •