超级老朊,看最后一个knock out,主播都说了:I have no idea

<embed src="http://player.youku.com/player.php/sid/XNjU3MTI1Ng==/v.swf" quality="high" width="450" height="372" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于Java NIO技术的Knock Knock小游戏的客户端/服务器端设计代码。 首先,我们需要定义一些常量和变量: ```java public class KnockKnockServer { private static final int PORT = 8080; private Selector selector; private ByteBuffer buffer = ByteBuffer.allocate(256); private Map<SocketChannel, String> clientResponses = new HashMap<>(); private Map<SocketChannel, Integer> clientStates = new HashMap<>(); private Map<SocketChannel, Queue<String>> clientQueues = new HashMap<>(); private ServerSocketChannel serverChannel; private int state = 0; private String[] jokes = {"Knock, knock.", "Who’s there?", "Boo.", "Boo who?", "Don’t cry. It’s only a joke."}; } ``` 然后,我们创建一个初始化方法,用于创建服务器端的SocketChannel和Selector对象: ```java private void init() throws IOException { selector = Selector.open(); serverChannel = ServerSocketChannel.open(); serverChannel.configureBlocking(false); serverChannel.socket().bind(new InetSocketAddress(PORT)); serverChannel.register(selector, SelectionKey.OP_ACCEPT); } ``` 接下来,我们需要实现一个方法,用于处理客户端连接请求: ```java private void accept(SelectionKey key) throws IOException { SocketChannel client = serverChannel.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_READ); clientQueues.put(client, new LinkedList<>()); clientStates.put(client, 0); System.out.println("Accepted new connection from " + client.getRemoteAddress()); } ``` 然后,我们需要实现一个方法,用于处理客户端的读取请求: ```java private void read(SelectionKey key) throws IOException { SocketChannel client = (SocketChannel) key.channel(); buffer.clear(); int read = client.read(buffer); if (read == -1) { client.close(); return; } buffer.flip(); String clientResponse = new String(buffer.array(), 0, read); System.out.println("Received: " + clientResponse + " from " + client.getRemoteAddress()); clientResponses.put(client, clientResponse); processClientResponse(client); key.interestOps(SelectionKey.OP_WRITE); } ``` 然后,我们需要实现一个方法,用于处理客户端的写入请求: ```java private void write(SelectionKey key) throws IOException { SocketChannel client = (SocketChannel) key.channel(); Queue<String> queue = clientQueues.get(client); if (queue.isEmpty()) { key.interestOps(SelectionKey.OP_READ); return; } String message = queue.poll(); ByteBuffer buffer = ByteBuffer.wrap(message.getBytes()); client.write(buffer); System.out.println("Sent: " + message + " to " + client.getRemoteAddress()); } ``` 接下来,我们需要实现一个方法,用于处理客户端的响应: ```java private void processClientResponse(SocketChannel client) throws IOException { String response = clientResponses.get(client); int currentState = clientStates.get(client); switch (currentState) { case 0: if (!response.equals("Knock, knock.")) { clientQueues.get(client).offer("You're supposed to say \"Knock, knock.\" Try again.\n"); } else { clientStates.put(client, 1); clientQueues.get(client).offer("Who's there?\n"); } break; case 1: if (!response.equals("Boo.")) { clientQueues.get(client).offer("You're supposed to say \"Boo.\" Try again.\n"); } else { clientStates.put(client, 2); clientQueues.get(client).offer("Boo who?\n"); } break; case 2: if (!response.equals("Don't cry. It's only a joke.")) { clientQueues.get(client).offer("You're supposed to say \"Don't cry. It's only a joke.\" Try again.\n"); } else { clientStates.put(client, 0); clientQueues.get(client).offer("Knock, knock.\n"); } break; } } ``` 最后,我们需要实现一个方法,用于运行服务器: ```java public void run() throws IOException { init(); while (true) { selector.select(); Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); while (keys.hasNext()) { SelectionKey key = keys.next(); keys.remove(); if (!key.isValid()) { continue; } if (key.isAcceptable()) { accept(key); } else if (key.isReadable()) { read(key); } else if (key.isWritable()) { write(key); } } } } ``` 以上就是基于Java NIO技术的Knock Knock小游戏的客户端/服务器端设计代码,希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值