服务器数字猜测游戏(socket编程)

0ejtzxu1  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(254)

我想写一个客户端与服务器通信的代码例子,用猜数游戏。我的java不是很好。代码必须这样做:客户端将连接到服务器。服务器将生成一个随机数。客户会猜出来的。我可以输入1次。然后就冻僵了。

这是我的猜谜游戏代码

{
  private int goal;
  private boolean correct;

  public GuessingGame()
  {
    goal = (int) (Math.random() * 9 + 1);
    correct = false;
   }

  public int getGoal()
  {
    return goal;
  }

  public boolean getCorrect()
  {
    return correct;
  }

public String guess(int guess)
{
if(guess == goal)
  {
  correct = true;
  return "Well done, your answer is correct";
  }
else if(guess > goal)
{
  return "Too high, can you try it with another number?";
}

else return "Too low, you can try to pick up more bigger number please";
    }
}

这是我的服务器代码

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSide
{
public static void main(String[] args) throws IOException
{
GuessingGame game = new GuessingGame();
ServerSocket socket = null;
        Socket client = null;
        String result;
boolean correct = false;
int attempt;
try
{
socket = new ServerSocket(1234);
}
catch(IOException ioe)
{
System.err.println(ioe);
return;
}
System.out.println("Our server is still running...");
client = socket.accept();
System.out.println("Yes, our Client had connected");
DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
while(!correct)
        {
attempt = in.readInt();
result = game.guess(attempt);
correct = game.getCorrect();
out.writeUTF(result);
out.writeBoolean(correct);
out.flush();
if(correct == false)
            {
client = socket.accept();
attempt = in.readInt();
result = game.guess(attempt);
correct = game.getCorrect();
out.writeUTF(result);
out.writeBoolean(correct);
out.flush();
            }
else
            {
client.close();
socket.close();
            }

        }
    }
}

这是我的客户代码

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class ClientSide
{
 public static void main(String[] args) throws IOException
 {
   System.out.println("This is Number Guessing Game. \nChoose any number between 1 to 10 : ");
   Scanner keyboard = new Scanner(System.in);
   int attempt = 0;
   try
   {
     attempt = keyboard.nextInt();
     if(attempt < 1 || attempt > 9)
     {
       System.out.println("Your number is too large/small, please make a guess between 1 to 10");
       attempt = keyboard.nextInt();
      }
    }
catch(NumberFormatException nfe)
{
  System.out.println("Just choose numbers! Try again");
  attempt = keyboard.nextInt();
}

try
        {
            Socket server = new Socket("localhost", 1234);
            System.out.println("Connecting...");

DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));

out.writeInt(attempt);
out.flush();
System.out.println("Our server is still running...");
            String result = in.readUTF();
boolean correct = in.readBoolean();
System.out.println(result);
while (!correct)
{
attempt = keyboard.nextInt();
out.writeInt(attempt);
out.flush();
System.out.println("Our server is still running...");
result = in.readUTF();
System.out.println(result);
correct = in.readBoolean();
}

server.close();
keyboard.close();
System.out.println("Finish. Thank you");
System.exit(0);

}
catch(IOException ioe)
{
System.err.println(ioe);
}
    }
}

样本输出

This is Number Guessing Game. 
Choose any number between 1 to 10 : 
23
Your number is too large/small, please make a guess between 1 to 10
12
Connecting...
Our server is still running...
Too high, can you try it with another number?
15
Our server is still running...

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题