You are on page 1of 4

package net.etfbl.

pokeworld;
import net.etfbl.pokeworld.pokebase.Pokedex;
import net.etfbl.pokeworld.pokemons.*;
import java.net.*;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
import java.util.Random;
public class PokeArenaClient extends Thread {
protected static int SERVER_PORT=9999;
protected Random rnd=new Random();
protected String upit;
protected String parametar;
protected BufferedReader in;
protected PrintWriter out;
protected Socket sock;
protected String prevod="";
protected Object obj;
protected boolean ready=false;
protected String odg="";
protected boolean sw=true;
protected Console cons;
protected Object mat[][];
protected List<Pokemon> pokeballs=new LinkedList<Pokemon>();
public PokeArenaClient(){
try{
generatePokemons();
cons=System.console();
InetAddress adr=InetAddress.getByName("127.0.0.1");
sock=new Socket(adr,SERVER_PORT);
in=new BufferedReader(
new InputStreamReader(
sock.getInputStream()));
out=new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
sock.getOutputStream())),true);
upit="HELLO";
out.println(upit);
odg=in.readLine();
System.out.println(odg+pokeballs.size());
while(pokeballs.size()>1){
Pokemon tmpA[]=getTwo();
Pokemon tmpP=send2(tmpA[0],tmpA[1]);
pokeballs.add(tmpP);
}
System.out.println("Pobjednik je : "+pokeballs.get(0));
}catch(Exception e){
e.printStackTrace();
}
}
public Pokemon [] getTwo(){
Pokemon tmpA[]=new Pokemon[2];
tmpA[0]=pokeballs.remove(rnd.nextInt(pokeballs.size()));
tmpA[1]=pokeballs.remove(rnd.nextInt(pokeballs.size()));
return tmpA;
}
public void generatePokemons() throws Exception{
String tmpS="net.etfbl.pokeworld.pokemons.";
for(int i=0;i<3;i++){
pokeballs.add(new IcePokemon());
pokeballs.add(new WaterPokemon());
pokeballs.add(new FirePokemon());
pokeballs.add(new ElectricPokemon());
pokeballs.add(new GroundPokemon());
pokeballs.add(new NaturalPokemon());
}
for(Pokemon p:pokeballs){
System.out.println(p);
}
}
public void run(){
try{
}catch(Exception e){
}
}
public Pokemon send2(Pokemon p1,Pokemon p2) throws Exception {
Pokemon pokemon=null;
out.println("SENDING2");
odg=in.readLine();
System.out.println("Odgovor : "+odg);
OutputStream os=sock.getOutputStream();
ObjectOutput oos=new ObjectOutputStream(os);
if(odg.equals("SEND1")){
System.out.println(p1);
oos.writeObject(p1);
odg=in.readLine();
System.out.println("Odgovor : "+odg);
if(odg.equals("SEND2")){
System.out.println(p2);
oos.writeObject(p2);
odg=in.readLine();
System.out.println("Odgovor : "+odg);
boolean sw=true;
//Printer pr=new Printer();
//pr.start();
odg=in.readLine();
out.println("SEND");
System.out.println("Odgovor : "+odg);
if(odg.equals("DONE")){
//pr.setSw(false);
System.out.println("A");
}
InputStream is=sock.getInputStream();
ObjectInputStream ois=new ObjectInputStream(is);
pokemon=(Pokemon) ois.readObject();
System.out.println(" Pobjedio je : "+pokemon);
}
}
return pokemon;
}
/*public void serijalizuj(Object obj)throws Exception{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutput oo=new ObjectOutputStream(baos);
oo.writeObject(obj);
byte niz[]=baos.toByteArray();
out.println("SERIJAL#"+niz.length);
odg=in.readLine();
//System.out.println(odg);
if(odg.equalsIgnoreCase("PREP")){
OutputStream os=sock.getOutputStream();
int duzina=niz.length;
int pomDuzina=0;
int fleg=0;
byte bafer[]=new byte[2*1024*1024];
InputStream is=new ByteArrayInputStream(niz);
out.println("OK");
odg=in.readLine();
//System.out.println(odg);
if(odg.equalsIgnoreCase("SEND")){
while((pomDuzina=is.read(bafer))>0){
os.write(bafer,0,pomDuzina);
fleg+=pomDuzina;
if(fleg==duzina) break;
}
os.flush();
out.println("DONE");
//System.out.print("DONE");
}
}
sw=false;
//in.readLine();
//System.out.println(odg);
//if(odg.equalsIgnoreCase("DONE"))
//out.println("END");
out.close();
in.close();
}
public Object deSerijalizuj() throws Exception {
upit="GETSER#";
out.println(upit);
odg=in.readLine();
//System.out.println(odg);
if(odg.contains("PREP#")){
File tmpFajl=new File("net"+File.separator+"etfbl"+File.
separator+"projektni13"+File.separator+"helper"+File.separator+"tmp.ser");
tmpFajl.createNewFile();
String niz[]=odg.split("#");
int duzina=Integer.parseInt(niz[1]);
byte tmp[]=new byte[duzina];
byte bafer[]=new byte[2*1024*1024];
InputStream is=sock.getInputStream();
OutputStream os=new FileOutputStream(tmpFajl);
int pomDuzina=0;
int fleg=0;
out.println("SEND");
while((pomDuzina=is.read(bafer))>0){
os.write(bafer,0,pomDuzina);
fleg+=pomDuzina;
if(fleg==duzina) break;
}
os.flush();
tmpFajl.deleteOnExit();
odg=in.readLine();
//System.out.println(odg);
upit="END";
out.println(upit);
in.readLine();
in.close();
out.close();
os.close();
is.close();
ObjectInputStream ois=new ObjectInputStream(new FileInpu
tStream(tmpFajl));
Object izlaz=ois.readObject();
ois.close();
return izlaz;
}
return null;
}
public void dodajRijec(String engleski, String klingonski) throws Except
ion{
upit="ADD#"+engleski+"#"+klingonski;
out.println(upit);
odg=in.readLine();
//System.out.println(odg);
out.println("END");
odg=in.readLine();
//System.out.println(odg);
out.close();
in.close();
}*/
public static void main(String [] args){
try{
new PokeArenaClient();
}catch(Exception e){
e.printStackTrace();
}
}
}

You might also like