客户端 - 服务器迷宫解算器无法正常工作

时间:2014-04-10 15:11:30

标签: java client-server

该项目正在制作一个程序,可以生成100×100的迷宫和另一个解决迷宫的程序。当我运行服务器然后客户端我什么都没得到。它只是保持运行而没有任何输出。到目前为止,我已经完全写出了应该通过整个事情发生的事情,但仍然感到困惑。

这是迷宫(服务器端)

import java.io.*;
import java.net.*;
import java.awt.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FinalMaze {    
static char[][] mazeArray = new char[][] {
        {'o','o','o','o','o','o'},
        {'o','w','w','r','w','o'},
        {'o','w','p','p','w','o'},
        {'o','w','p','w','w','o'},
        {'o','w','p','p','w','o'},
        {'o','w','w','p','w','o'},
        {'o','w','w','p','w','o'},
        {'o','o','o','o','o','o'},
};


static JFrame frame = new JFrame("Maze");
static char[][] ratArray = new char[3][3];
static int xPos;
static int yPos;
static String ratLocation = null;


public static void main(String[] args) throws Exception{

    new FinalMaze();
    run();

}

public FinalMaze() throws Exception{
    frame.setSize(1000,1000);
    frame.setLayout(new GridLayout(0,6));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    drawMaze();



}



private void drawMaze() {
    for(int i=0; i < mazeArray.length; i++){
        for(int j=0; j<mazeArray[i].length; j++){
            switch(mazeArray[i][j]){
                case 'p' : 
                    frame.add(new JLabel(""));
                    break;
                case 'w' : 
                    frame.add(new JButton(""));
                    break;
                case 'r' :
                    frame.add(new JLabel("RAT"));
                    xPos = j;
                    yPos = i;
                    break;
                case 'o' :
                    JButton temp = new JButton("");
                    temp.setBackground(Color.BLACK);
                    frame.add(temp);
            }
        }
    }
}

public static void run() throws Exception{
    ServerSocket srvsock= new ServerSocket(13000);
    Socket sock = srvsock.accept();
    PrintStream PS = new PrintStream(sock.getOutputStream());

    InputStreamReader IR = new InputStreamReader(sock.getInputStream());
    BufferedReader BR = new BufferedReader(IR);

        String currentLocation = null;
        String tempLocation = null;
        findLocation();
        PS.println(ratLocation);

        while(currentLocation!= "ooooooooo"){

            currentLocation=BR.readLine();
            if(currentLocation != tempLocation){


            setLocation(currentLocation);
            ratLocation = null;
            findLocation();
            PS.println(ratLocation);

            if(mazeArray[6][3] == 'r'){

                System.out.println("You win");
                srvsock.close();
                PS.println("ooooooooo");
                currentLocation = "ooooooooo";
            }
            tempLocation = currentLocation;

        }
        }
        srvsock.close();


}

private static void setLocation(String currentLocation) {
    mazeArray[yPos-1][xPos-1]=currentLocation.charAt(0);
    mazeArray[yPos-1][xPos]=currentLocation.charAt(1);
    mazeArray[yPos-1][xPos+1]=currentLocation.charAt(2);
    mazeArray[yPos][xPos-1]=currentLocation.charAt(3);
    mazeArray[yPos][xPos]=currentLocation.charAt(4);
    mazeArray[yPos][xPos+1]=currentLocation.charAt(5);
    mazeArray[yPos+1][xPos-1]=currentLocation.charAt(6);
    mazeArray[yPos+1][xPos]=currentLocation.charAt(7);
    mazeArray[yPos+1][xPos+1]=currentLocation.charAt(8);

    int tempPos = currentLocation.indexOf('r');
    switch(tempPos){
        case 1:

            yPos=yPos-1;
            break;
        case 3:
            xPos=xPos-1;
            break;
        case 5:
            xPos=xPos+1;
            break;
        case 7:
            yPos=yPos+1;
            break;
    }
}

private static void findLocation() {
    ratArray[1][1] = mazeArray[yPos][xPos];
    ratArray[0][0] = mazeArray[yPos-1][xPos-1];
    ratArray[0][1] = mazeArray[yPos-1][xPos];
    ratArray[0][2] = mazeArray[yPos-1][xPos+1];
    ratArray[1][0] = mazeArray[yPos][xPos-1];
    ratArray[1][2] = mazeArray[yPos][xPos+1];
    ratArray[2][0] = mazeArray[yPos+1][xPos-1];
    ratArray[2][1] = mazeArray[yPos+1][xPos];
    ratArray[2][2] = mazeArray[yPos+1][xPos+1];

     for (int i=0; i<9; i++){
            int X = 0;
            int j =0;
            if (i<=2){
                X = 0;
                j=i;
            }else{
                if (i<=5){
                    X=1;
                    j=i-3;
                }else{
                    if(i<=8){
                        X=2;
                        j=i-6;
                    }
                }
            }

            ratLocation = ratLocation + ratArray[X][j];
        }

}
}

这是Rat(客户端)

import java.io.*;
import java.net.Socket;

public class FinalRat {
static char[][] mazeArray = new char[3][3];
static int direction = 1;
static int moves = 0;

public static void main(String[] args) throws Exception {

    run();

}

public static void run() throws Exception {
    Socket sock = new Socket("localHost", 13000);
    PrintStream PS = new PrintStream(sock.getOutputStream());

    InputStreamReader IR = new InputStreamReader(sock.getInputStream());
    BufferedReader BR = new BufferedReader(IR);

    // finding location
    String LOCATION = null;
    String tempLocation = null;
    String newLOCATION = null;
    while (LOCATION != "ooooooooo") {
        moves = 0;
        LOCATION = BR.readLine();

        if (tempLocation != LOCATION) {
            newLOCATION = null;
            for (int i = 0; i < 9; i++) {
                int X = 0;
                int j = 0;
                if (i <= 2) {
                    X = 0;
                    j = i;
                } else {
                    if (i <= 5) {
                        X = 1;
                        j = i - 3;
                    } else {
                        if (i <= 8) {
                            X = 2;
                            j = i - 6;
                        }
                    }
                }

                mazeArray[X][j] = LOCATION.charAt(i);
            }

            // Solving

            switch (direction) {
            case 1:
                West();
                North();
                East();
                South();
                break;
            case 2:
                North();
                East();
                South();
                West();
                break;
            case 3:
                South();
                West();
                North();
                East();
                break;
            case 4:
                East();
                South();
                West();
                North();
                break;
            }

            moves = 0;

            // sending POS

            for (int i = 0; i < 9; i++) {
                int X = 0;
                int j = 0;
                if (i <= 2) {
                    X = 0;
                    j = i;
                } else {
                    if (i <= 5) {
                        X = 1;
                        j = i - 3;
                    } else {
                        if (i <= 8) {
                            X = 2;
                            j = i - 6;
                        }
                    }
                }

                newLOCATION = newLOCATION + mazeArray[X][j];

            }
            // PS.flush();
            PS.println(newLOCATION);

            tempLocation = LOCATION;

        }
    }
    System.out.println("you win!");
    sock.close();
}

// Decision

public static void North() {
    if (mazeArray[0][1] == 'p' && moves == 0) {
        mazeArray[0][1] = 'r';
        mazeArray[1][1] = 'p';

        direction = 1;
        moves++;
    }
}

public static void West() {
    if (mazeArray[1][0] == 'p' && moves == 0) {
        mazeArray[1][0] = 'r';
        mazeArray[1][1] = 'p';

        direction = 3;
        moves++;
    }
}

public static void East() {
    if (mazeArray[1][2] == 'p' && moves == 0) {
        mazeArray[1][2] = 'r';
        mazeArray[1][1] = 'p';

        direction = 2;
        moves++;
    }

}

public static void South() {
    if (mazeArray[2][1] == 'p' && moves == 0) {
        mazeArray[2][1] = 'r';
        mazeArray[1][1] = 'p';

        direction = 4;
        moves++;
    }

}

}

0 个答案:

没有答案