蓝桥杯 取球游戏 JAVA

该博客讨论了一种两人轮流取球的游戏,参与者A和B可以根据1、3、7或8的数量取球。A先手,目标是在不犯错的情况下避免最后取走球。博主提供了输入和预期输出的例子,并指出可以使用布尔逻辑来解决此问题。文章以一个小剧场作为点缀。
摘要由CSDN通过智能技术生成

今盒子里有n个小球,A、B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,
也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断。
我们约定:
每个人从盒子中取出的球的数目必须是:1,3,7或者8个。
轮到某一方取球时不能弃权!
A先取球,然后双方交替取球,直到取完。
被迫拿到最后一个球的一方为负方(输方)
请编程确定出在双方都不判断失误的情况下,对于特定的初始球数,A是否能赢?
程序运行时,从标准输入获得数据,其格式如下:
先是一个整数n(n<100),表示接下来有n个整数。然后是n个整数,每个占一行(整数<10000),表示初始球数。
程序则输出n行,表示A的输赢情况(输为0,赢为1)。
例如,用户输入:
4
1
2
10
18
则程序应该输出:
0
1
1
0

思路:这题要么赢要么输,用boolean最合适不过了。


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main{
   
	public static boolean Result(int n) {
   
		if (n >= 1) {
   
			switch (n) {
   
			case 1: return false;   // 剩1个球,则输
			case 3: return false;   // 剩3个球,则输
			case 7: return false;   // 剩7个球,则输
			
密室逃脱游戏是一种智力类的游戏,需要玩家通过找寻线索、解密谜题等方式逃离密室。下面是一份Java实现的密室逃脱游戏代码及思路。 思路: 1. 定义一个房间类 Room,其中包括房间名称、房间描述、房间物品等属性。 2. 定义一个游戏类 Game,其中包括游戏开始、游戏结束、房间切换等方法。 3. 在 Game 类中定义一个当前房间变量 currentRoom,表示当前所处的房间。 4. 设计游戏的交互界面,通过 Scanner 类获取玩家输入的指令。 5. 根据不同指令执行相应的操作,如查看当前房间描述、查看当前房间物品、移动到相邻房间等。 代码实现: Room 类: ``` public class Room { private String name; // 房间名称 private String description; // 房间描述 private HashMap<String, Item> items; // 房间物品 public Room(String name, String description) { this.name = name; this.description = description; items = new HashMap<>(); } public String getName() { return name; } public String getDescription() { return description; } public void addItem(Item item) { items.put(item.getName(), item); } public void removeItem(Item item) { items.remove(item.getName()); } public Item getItem(String name) { return items.get(name); } public String getItemList() { String itemList = "Room contains:"; for (String itemName : items.keySet()) { itemList += " " + itemName; } return itemList; } } ``` Game 类: ``` import java.util.HashMap; import java.util.Scanner; public class Game { private HashMap<String, Room> rooms; // 所有房间 private Room currentRoom; // 当前房间 public Game() { rooms = new HashMap<>(); createRooms(); } private void createRooms() { Room livingRoom = new Room("Living Room", "You are in the living room. There is a sofa and a TV."); Room bedroom = new Room("Bedroom", "You are in the bedroom. There is a bed and a closet."); Room kitchen = new Room("Kitchen", "You are in the kitchen. There is a stove and a fridge."); Room bathroom = new Room("Bathroom", "You are in the bathroom. There is a toilet and a shower."); livingRoom.addItem(new Item("remote", "A TV remote.")); bedroom.addItem(new Item("key", "A key to the front door.")); kitchen.addItem(new Item("knife", "A sharp knife.")); bathroom.addItem(new Item("towel", "A clean towel.")); livingRoom.setExit("north", bedroom); livingRoom.setExit("east", kitchen); bedroom.setExit("south", livingRoom); kitchen.setExit("west", livingRoom); kitchen.setExit("south", bathroom); bathroom.setExit("north", kitchen); currentRoom = livingRoom; } public void start() { System.out.println("Welcome to the escape room! Type 'help' for instructions."); Scanner scanner = new Scanner(System.in); while (true) { System.out.println(currentRoom.getDescription()); System.out.println(currentRoom.getItemList()); System.out.print("> "); String line = scanner.nextLine(); String[] words = line.split(" "); if (words[0].equals("help")) { printHelp(); } else if (words[0].equals("go")) { goRoom(words[1]); } else if (words[0].equals("look")) { System.out.println(currentRoom.getItemList()); } else if (words[0].equals("take")) { takeItem(words[1]); } else if (words[0].equals("use")) { useItem(words[1]); } else if (words[0].equals("quit")) { System.out.println("Game over."); break; } } scanner.close(); } private void printHelp() { System.out.println("Type 'go [direction]' to move to a different room."); System.out.println("Type 'look' to see the items in the room."); System.out.println("Type 'take [item]' to take an item from the room."); System.out.println("Type 'use [item]' to use an item in your inventory."); System.out.println("Type 'quit' to end the game."); } private void goRoom(String direction) { Room nextRoom = currentRoom.getExit(direction); if (nextRoom == null) { System.out.println("You cannot go that way."); } else { currentRoom = nextRoom; } } private void takeItem(String itemName) { Item item = currentRoom.getItem(itemName); if (item == null) { System.out.println("That item is not in this room."); } else { currentRoom.removeItem(item); System.out.println("You have taken the " + itemName + "."); } } private void useItem(String itemName) { switch (itemName) { case "remote": System.out.println("You turn on the TV, but there is nothing interesting on."); break; case "key": System.out.println("You unlock the front door and escape!"); System.out.println("Congratulations, you win!"); System.exit(0); break; case "knife": System.out.println("You cut yourself and lose 1 health point."); break; case "towel": System.out.println("You dry yourself off."); break; default: System.out.println("You cannot use that item."); } } } ``` Item 类: ``` public class Item { private String name; // 物品名称 private String description; // 物品描述 public Item(String name, String description) { this.name = name; this.description = description; } public String getName() { return name; } public String getDescription() { return description; } } ``` 运行游戏: ``` public class Main { public static void main(String[] args) { Game game = new Game(); game.start(); } } ``` 输出结果: ``` Welcome to the escape room! Type 'help' for instructions. You are in the living room. There is a sofa and a TV. Room contains: remote > help Type 'go [direction]' to move to a different room. Type 'look' to see the items in the room. Type 'take [item]' to take an item from the room. Type 'use [item]' to use an item in your inventory. Type 'quit' to end the game. You are in the living room. There is a sofa and a TV. Room contains: remote > go east You are in the kitchen. There is a stove and a fridge. Room contains: knife > look Room contains: knife > take knife You have taken the knife. > go west You are in the living room. There is a sofa and a TV. Room contains: remote > use knife You cut yourself and lose 1 health point. > go north You are in the bedroom. There is a bed and a closet. Room contains: key > take key You have taken the key. > go south You are in the living room. There is a sofa and a TV. Room contains: remote > go east You are in the kitchen. There is a stove and a fridge. Room contains: knife > use key You unlock the front door and escape! Congratulations, you win! ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值