问题描述
- 骰子小游戏,网格编程和多线程的应用,报很多错求帮助,又空指针又链接出错的!!!!
-
package com.zhouqy.client;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;import com.zhqy.MsgObj.MsgObj;
public class Client {
private ObjectOutputStream oos ;
private ObjectInputStream ois ;
private static Socket s;
public static void main(String[] args) { // TODO Auto-generated method stub Client c=new Client(); c.startClient("127.0.0.1",8888); } public void startClient(String ip,int port){ try { s=new Socket(ip,port); ois=new ObjectInputStream(s.getInputStream()); oos=new ObjectOutputStream(s.getOutputStream()); Send a=new Send(); Thread t=new Thread(a); Receive b=new Receive(); Thread t1=new Thread(b); t.start(); t1.start(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void close(){ try { ois.close(); oos.close(); s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
}
class Send implements Runnable{private ObjectOutputStream oos;
private ObjectInputStream ois;;
private Socket s = null;
public void run(){
try {MsgObj msg1=new MsgObj("挑战者");
msg1.throwDice();
msg1.showDice();
msg1.judgeDice();
//ois=new ObjectInputStream(s.getInputStream());
//oos=new ObjectOutputStream(s.getOutputStream());
oos.writeObject(msg1);
oos.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
class Receive implements Runnable{private ObjectOutputStream oos; private ObjectInputStream ois; private Socket s = null; @Override public void run() { // TODO Auto-generated method stub MsgObj recieveMsg; try { recieveMsg = (MsgObj) ois.readObject(); System.out.println(recieveMsg.getName()+":"); recieveMsg.showDice(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
解决方案
package com.zhouqy.server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import com.zhqy.MsgObj.MsgObj;
public class Server {
private Socket s;
private ObjectInputStream ois;
private ObjectOutputStream oos;
public static void main(String[] args) {
Server s=new Server();
s.startServer(8888);
}
public void startServer(int port){
try {
ServerSocket sSocket=new ServerSocket(port);
s=sSocket.accept();
oos=new ObjectOutputStream(s.getOutputStream());
ois=new ObjectInputStream(s.getInputStream());
Receive b=new Receive();
Thread t1=new Thread(b);
t1.start();
Send a=new Send();
Thread t=new Thread(a);
t.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Send implements Runnable{
private ObjectOutputStream oos;
private Socket s;
public void run(){
try {
s = new Socket("127.0.0.1",8888);
MsgObj msg2=new MsgObj("擂主");
msg2.throwDice();
msg2.showDice();
msg2.judgeDice();
oos=new ObjectOutputStream(s.getOutputStream());
oos.writeObject(msg2);
oos.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Receive implements Runnable{
private ObjectInputStream ois;
@Override
public void run() {
// TODO Auto-generated method stub
MsgObj recieveMsg;
try {
recieveMsg = (MsgObj)ois.readObject();
System.out.println(recieveMsg.getName()+":");
recieveMsg.showDice();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解决方案二:
package com.zhqy.MsgObj;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import com.zhouqy.dice.Dice;
import com.zhouqy.dice.DiceComparator;
public class MsgObj implements Serializable {
private String name;
private ArrayList dices;
private int max;
private int first;
private int secend;
private int third;
private int fourth;
public String getName() {
return name;
}
public int getMax() {
return max;
}
public int getFirst() {
return first;
}
public int getSecend() {
return secend;
}
public int getThird() {
return third;
}
public int getFourth() {
return fourth;
}
public MsgObj(String name) {
super();
// TODO Auto-generated constructor stub
this.name=name;
//创建一个叫dices的ArrayList
dices=new ArrayList<Dice>();
//创建一个size为5的dices ArrayList
for(int i=0;i<5;i++){
dices.add(new Dice());
}
}
//扔骰子
public void throwDice(){
for(int i=0;i<dices.size();i++){
Dice currdices=dices.get(i);
currdices.throwPoint();
}
//对ArrayList Dices排序
Comparator<Dice> ascComparator=new DiceComparator();
Collections.sort(dices, ascComparator);
}
//输出骰子点数
public void showDice(){
System.out.print(name+"扔出:");
for(int i=0;i<dices.size();i++){
Dice currdices=dices.get(i);
System.out.print(currdices.getPoint()+" ");
}
System.out.println("");
}
//判断骰子的牌型
public void judgeDice(){
int samenum=0;
//从ArrayList中提取到一个数组中
Dice[] currdices=new Dice[dices.size()];
for(int i=0;i<dices.size();i++){
currdices[i]=dices.get(i);
}
//samedicenum:相同的骰子数 lastdice:上一个骰子的点数 maxsame:最大相同数
int samedicenum=1,lastdice=0,maxsame=0;
for(int i=0;i<dices.size();i++){
if(lastdice!=currdices[i].getPoint()){
if(samedicenum>1){
if(maxsame<samedicenum){
maxsame=samedicenum;
}
samenum++;
}
samedicenum=1;
lastdice=currdices[i].getPoint();
}
else{
samedicenum++;
if(i==4){
if(maxsame<samedicenum){
maxsame=samedicenum;
}
samenum++;
}
}
}
switch(samenum){
case 0:
if(currdices[0].getPoint()==6 && currdices[4].getPoint()==1){
max=0;
first=currdices[1].getPoint();
System.out.println(name+":"+"散牌");
}
else{
first=currdices[0].getPoint();
max=4;
System.out.println(name+":"+"顺子");
}
break;
case 1:
if(maxsame==5){
max=7;
first=currdices[0].getPoint();
System.out.println(name+":"+"豹子");
}
if(maxsame==4){
max=6;
first=currdices[2].getPoint();
secend=currdices[0].getPoint();
third=currdices[4].getPoint();
System.out.println(name+":"+"铁枝");
}
if(maxsame==3){
max=3;
first=currdices[2].getPoint();
secend=currdices[3].getPoint();
third=currdices[4].getPoint();
System.out.println(name+":"+"三条");
}
if(maxsame==2){
max=1;
if(currdices[0].getPoint()==currdices[1].getPoint()){
first=currdices[0].getPoint();
secend=currdices[2].getPoint();
third=currdices[3].getPoint();
fourth=currdices[4].getPoint();
}
if(currdices[1].getPoint()==currdices[2].getPoint()){
first=currdices[1].getPoint();
secend=currdices[0].getPoint();
third=currdices[3].getPoint();
fourth=currdices[4].getPoint();
}
if(currdices[2].getPoint()==currdices[3].getPoint()){
first=currdices[3].getPoint();
secend=currdices[0].getPoint();
third=currdices[1].getPoint();
fourth=currdices[4].getPoint();
}
if(currdices[3].getPoint()==currdices[4].getPoint()){
first=currdices[3].getPoint();
secend=currdices[0].getPoint();
third=currdices[1].getPoint();
fourth=currdices[2].getPoint();
}
System.out.println(name+":"+"一对");
}
break;
case 2:
if(maxsame==3){
max=5;
first=currdices[2].getPoint();
secend=currdices[3].getPoint();
System.out.println(name+":"+"葫芦");
}
if(maxsame==2){
max=2;
first=currdices[1].getPoint();
secend=currdices[2].getPoint();
third=currdices[3].getPoint();
System.out.println(name+":"+"两对");
}
break;
}
}
}
解决方案三:
package com.zhouqy.dice;
import java.util.Random;
public class Dice implements Comparable {
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
/**
* @param args
*/
private int point;
public int point(){
return point;
}
public void throwPoint(){
point=new Random().nextInt(6)+1;
}
@Override
public int compareTo(Dice o) {
// TODO Auto-generated method stub
return 0;
}
}
解决方案四:
Exception in thread "Thread-0" java.lang.NullPointerException
at com.zhouqy.server.Receive.run(Server.java:76)
at java.lang.Thread.run(Thread.java:619)
擂主扔出:6 6 3 2 1
擂主:一对
java.io.NotSerializableException: com.zhouqy.dice.Dice
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at java.util.ArrayList.writeObject(ArrayList.java:570)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at com.zhouqy.server.Send.run(Server.java:56)
at java.lang.Thread.run(Thread.java:619)
解决方案五:
Exception in thread "Thread-1" java.lang.NullPointerException
at com.zhouqy.client.Receive.run(Client.java:102)
at java.lang.Thread.run(Thread.java:619)
挑战者扔出:5 5 2 Exception in thread "Thread-0" java.lang.NullPointerException
2 1
挑战者:两对
at com.zhouqy.client.Send.run(Client.java:80)
at java.lang.Thread.run(Thread.java:619)