diff --git a/Cards.java b/Cards.java index 21107f4..6b120b8 100644 --- a/Cards.java +++ b/Cards.java @@ -10,7 +10,7 @@ public class Cards { ArrayList cards; public Cards() { this.cards=new ArrayList(); - for(int i=2;i<=10;i++) { + for(int i=2;i<=10;i++) { // 52 cards cards.add(new Card(i,"S")); cards.add(new Card(i,"H")); cards.add(new Card(i,"D")); @@ -48,13 +48,9 @@ Card pop() { } void show() { - Iterator iter=cards.iterator(); while(iter.hasNext()) { System.out.println(iter.next()); - } - - } } diff --git a/Game.java b/Game.java index 1d6213e..2e48973 100644 --- a/Game.java +++ b/Game.java @@ -4,12 +4,10 @@ import java.util.Iterator; import java.util.List; - - public class Game { Cards cards; - private ArrayList deck0,deck1,deck2; + ArrayList deck0,deck1,deck2; public Game() { cards=new Cards(); @@ -24,7 +22,6 @@ public Game() { } - ArrayList returnDeck0(){ return deck0; } diff --git a/LoginGUI.java b/LoginGUI.java new file mode 100644 index 0000000..39c6ef0 --- /dev/null +++ b/LoginGUI.java @@ -0,0 +1,188 @@ +import java.awt.BorderLayout; +import java.awt.Container; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Iterator; + +import javax.swing.*; + + +public class LoginGUI extends JFrame implements ActionListener{ +// JFrame frame; + JLabel label_id,label_pw; + JTextField tf_id; + JPasswordField tf_pw; + JButton logInBtn,signUpBtn; + String id; + String pw; + boolean flag; + ArrayList clientID; + ArrayList clientPassword; + ArrayList clientLogin; + + + LoginGUI(){ + flag=false; + clientID = new ArrayList(); + clientPassword = new ArrayList(); + clientLogin = new ArrayList(); + + label_id=new JLabel(); + label_pw=new JLabel(); + tf_id=new JTextField(); + tf_pw=new JPasswordField(); + logInBtn=new JButton(); + signUpBtn=new JButton(); + + label_id.setText("ID"); + label_id.setBounds(3,3,20,10); + label_pw.setText("Password"); + label_pw.setBounds(3,6,20,10); + logInBtn.setText("Login"); + signUpBtn.setText("Sign up"); + + this.setLayout(new BorderLayout()); + + JPanel panel1=new JPanel(); + panel1.setLayout(new GridLayout(2,2)); + panel1.add(label_id); + panel1.add(tf_id); + panel1.add(label_pw); + panel1.add(tf_pw); + + JPanel panel2=new JPanel(); + panel2.setLayout(new GridLayout(1,2)); + panel2.add(logInBtn); + panel2.add(signUpBtn); + + this.add(panel1,BorderLayout.CENTER); + this.add(panel2,BorderLayout.SOUTH); + + logInBtn.addActionListener(this); + signUpBtn.addActionListener(this); + + this.setSize(200,100); + this.setVisible(true); + this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + + } + public void actionPerformed(ActionEvent ae) { + try { + if(ae.getSource()==logInBtn) { + String tmp_id; + String tmp_pw; + + id=tf_id.getText(); + char[] tmp=tf_pw.getPassword(); + pw=""; + for(char c : tmp){ + Character.toString(c); + pw += (pw.equals("")) ? ""+c+"" : ""+c+""; + } + try { + File f = new File("account.txt"); + if(!f.exists()) { + FileWriter fw=null; + BufferedWriter bw=null; + File file=new File("account.txt"); + try { + fw=new FileWriter(file,true); + bw=new BufferedWriter(fw); + bw.write(""); + }catch(IOException io) { + io.printStackTrace(); + } + + JOptionPane.showMessageDialog(null, "계정이 없거나 비밀번호가 틀렸습니덜.", "로그인 실패", JOptionPane.ERROR_MESSAGE); + tf_id.setText(""); + tf_pw.setText(""); + } + if(f.length() == 0) { + JOptionPane.showMessageDialog(null, "계정이 없거나 비밀번호가 틀렸다.", "로그인 실패", JOptionPane.ERROR_MESSAGE); + tf_id.setText(""); + tf_pw.setText(""); + } + + BufferedReader br=new BufferedReader(new FileReader("account.txt")); + + String s; + while ((s = br.readLine()) != null) { + String[] split = s.split("\t"); + clientID.add(split[0]); + clientPassword.add(split[1]); + clientLogin.add(false); + } + + for(int i=0;i getClientList() throws RemoteException; + int getDeckLength(int player) throws RemoteException; + String getTopType() throws RemoteException; + String getTopValue() throws RemoteException; + boolean getDeckZeroStatus() throws RemoteException; + public void refreshDeck(int who) throws RemoteException; + public int whosWin() throws RemoteException; +// public int getDeckSize() throws RemoteException; + public void modifyBtnStatus(int whichBtn, String id) throws RemoteException; + public boolean checkBtnStatus(int whichBtn, String id) throws RemoteException; + public void modifyAnotherBtnStatus(int whichBtn, String id) throws RemoteException; + public String findAnotherId(String id) throws RemoteException; + +} \ No newline at end of file diff --git a/WarGameClient.java b/WarGameClient.java index 02a53f4..41253a3 100644 --- a/WarGameClient.java +++ b/WarGameClient.java @@ -1,99 +1,270 @@ import java.rmi.AccessException; import java.rmi.Naming; import java.rmi.RemoteException; -import java.rmi.server.UnicastRemoteObject; -import java.util.Scanner; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.net.InetAddress; import java.net.MalformedURLException; +import java.net.UnknownHostException; import java.rmi.NotBoundException; -import java.net.MalformedURLException; +import java.rmi.server.UnicastRemoteObject; +import java.util.ArrayList; +import java.util.Scanner; import java.rmi.NotBoundException; + + + public class WarGameClient implements Runnable {//handle connection and use rmi function here from client side -// private static final long serialVersionUID = 1L; - private String id; - boolean chkLog=false; - private WarGame server; - boolean boolean_ReadyBtnStatus; - boolean boolean_HitBtnStatus; - boolean boolean_DropBtnStatus; - - protected WarGameClient(WarGame wg,String id,String pw){ - boolean_ReadyBtnStatus=false; - boolean_HitBtnStatus=false; - boolean_DropBtnStatus=false; - this.server=wg; - this.id=id; - try { - chkLog=server.checkClientCredentials(wg, id, pw); - if(chkLog==false) { - System.out.println("wrong id or wrong passworld"); - System.exit(0); - } - - - }catch(RemoteException re) { - System.out.println("RemoteException: "+re); - - }catch(java.lang.ArithmeticException ae) { - System.out.println("java.lang.ArithmeticException: "+ae); - } - - } - - public void run() { - if(chkLog) { - System.out.println(id); - WarGameGUI gui=new WarGameGUI(id); - try { - while(server.checkAllReady()==false) { - boolean_ReadyBtnStatus=gui.returnReadyBtnStatus(); - server.setReadyStatus(id, boolean_ReadyBtnStatus); - } - - }catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯.. 예외처리 사이즈가 - e.printStackTrace(); - } - System.out.println("game start");//this will be deleted in the end - gui.setGameInfo("game start"); - gui.deactivateReadyBtn(); - -// try { -// do{//게임중... do while인 이유는 마지막 카드가 공격이어서 기회가 남아 있을수도 있다.. -// //////////////////// -// }while(server.checkStatus()); -// }catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯..예외처리 사이즈가 커 -// e.printStackTrace(); -// } - } - } - public static void main(String[] args) { - String mServer="127.0.0.1"; -// String mServer=args[0]; - String mServName="WarGame"; - - System.out.println("Remote Method Invocate to "+mServer+", service name: "+mServName); - - try { - - String id; - String pw; - Scanner sc=new Scanner(System.in);//this is for temporary - System.out.println("enter id: "); - id=sc.nextLine(); - System.out.println("enter pw: "); - pw=sc.nextLine(); - - WarGame wg=(WarGame)Naming.lookup("rmi://"+mServer+":1099/"+mServName); - new Thread(new WarGameClient(wg,id,pw)).start(); - - }catch(MalformedURLException mue) { - System.out.println("MalformedURLException: "+mue); - }catch(RemoteException re) { - System.out.println("RemoteException: "+re); - }catch(NotBoundException nbe) { - System.out.println("NotBoundException: "+nbe); - }catch(java.lang.ArithmeticException ae) { - System.out.println("java.lang.ArithmeticException: "+ae); - } - } - -} +// private static final long serialVersionUID = 1L; + private static final int PORT = 2020; + + private String id; + boolean chkLog=false; + private WarGame server; + boolean boolean_ReadyBtnStatus; + boolean boolean_HitBtnStatus; + boolean boolean_DropBtnStatus; + Card top; + ArrayList client; + WarGameGUI gui; + protected WarGameClient(WarGame wg,String id,String pw){ + boolean_ReadyBtnStatus=false; + boolean_HitBtnStatus=false; + boolean_DropBtnStatus=false; + this.server=wg; + this.id=id; + try { + chkLog=server.checkClientCredentials(wg, id, pw); + if(chkLog==false) { + System.out.println("wrong id or wrong password"); + System.exit(0); + } + + + }catch(RemoteException re) { + System.out.println("RemoteException: "+re); + + }catch(java.lang.ArithmeticException ae) { + System.out.println("java.lang.ArithmeticException: "+ae); + } + + } + void drop() { + boolean_DropBtnStatus=gui.returnDropBtnStatus(); + try { + server.doDrop(id);//id가 필요한지 모르겠음... + + }catch(Exception e) { + e.printStackTrace(); + } + } + void hit() { + boolean_HitBtnStatus=gui.returnHitBtnStatus(); + try { + server.doHit(id); + }catch(Exception e) { + e.printStackTrace(); + } + } + public void run() { + if(chkLog) { + gui=new WarGameGUI(id); + gui.deactivatedHitDropBtn(); + try { + while(server.checkAllReady()==false) { + boolean_ReadyBtnStatus=gui.returnReadyBtnStatus(); + server.setReadyStatus(id, boolean_ReadyBtnStatus); + } + + }catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯.. 예외처리 사이즈가 + e.printStackTrace(); + } + gui.activatedHitDropBtn(); + gui.setGameInfo("game start"); + gui.deactivateReadyBtn(); + try {//최종 참가자 리스트 추가 + client=server.getClientList(); + String tmp=client.get(0); + String tmp2=client.get(1); + gui.setPlayerList("Player1: "+tmp); + gui.setPlayerList("Player2: "+tmp2); + }catch(Exception e) { + e.printStackTrace(); + } + try { + while(server.checkEndingStatus()){//게임중... + if(!server.checkEndingStatus()) { + server.refreshDeck(server.whosWin()); + gui.deactivatedHitDropBtn(); + gui.setCardNumLabel(0, server.getDeckLength(0)); + gui.setCardNumLabel(1, server.getDeckLength(1)); + gui.updateCardImg(); + gui.getMessageBox(client.get(server.whosWin()-1)); + + } + + gui.setCardNumLabel(0, server.getDeckLength(0)); + gui.setCardNumLabel(1,server.getDeckLength(1)); + + gui.setGameInfo("turn : "+server.whosTurn()); + + if(server.checkBtnStatus(0, id)){ + gui.setMsgView("플레이어 "+ server.findAnotherId(id)+" 가 drop을 했습니다."); + server.modifyAnotherBtnStatus(0, id); + } + if(server.checkBtnStatus(1, id)) { + gui.setMsgView("플레이어 "+ server.findAnotherId(id)+" 가 hit을 했습니다."); + server.modifyAnotherBtnStatus(1, id); + } + + + while(id.equals(server.whosTurn())){//자신의 턴 동안... + gui.setGameInfo("turn : "+server.whosTurn()); + if(server.checkBtnStatus(0, id)){ + gui.setMsgView("플레이어 "+ server.findAnotherId(id)+" 가 drop을 했습니다."); + server.modifyAnotherBtnStatus(0, id); + } + if(server.checkBtnStatus(1, id)) { + gui.setMsgView("플레이어 "+ server.findAnotherId(id)+" 가 hit을 했습니다."); + server.modifyAnotherBtnStatus(1, id); + } + + int num = -1; + for(int i = 0 ; i < client.size(); i++) { + if(id.equals(client.get(i))) { + num = i; + break; + } + } + if(server.getDeckLength(num) == 0 || server.getDeckLength(num) == 52) { + break; + } + gui.setCardNumLabel(0, server.getDeckLength(0)); + gui.setCardNumLabel(1,server.getDeckLength(1)); + + + if(server.getDeckZeroStatus()) {//바닥에 카드가 있을때만 작동 + + String topType=server.getTopType(); + String topValue=server.getTopValue(); + if(!topType.isEmpty() && !topValue.isEmpty()) { + gui.updateCardImg(topType, topValue); + } + else { + gui.updateCardImg(); + } + + }else { + gui.updateCardImg(); + } + boolean_DropBtnStatus=gui.returnDropBtnStatus(); + boolean_HitBtnStatus=gui.returnHitBtnStatus(); + if(boolean_DropBtnStatus) { + drop(); + gui.setMsgView("플레이어 "+ id+" 가 drop을 했습니다"); + server.modifyBtnStatus(0, id); + gui.setDropStatusFalse();//버튼 눌리면 true로 변하기 때문에 변경 안하면 항상 true인 효과 한번 버튼 누르면 다시 false로 변환 + + }else if(boolean_HitBtnStatus) { + hit(); + gui.setMsgView("플레이어 "+ id+" 가 hit을 했습니다."); + server.modifyBtnStatus(1, id); + + gui.setHitStatusFalse(); + + }else { + + } + } + if(!id.equals(server.whosTurn())){//player가 잘못누르는 경우... + gui.setGameInfo("turn : "+server.whosTurn()); + gui.setCardNumLabel(0, server.getDeckLength(0)); + gui.setCardNumLabel(1, server.getDeckLength(1)); + + if(server.getDeckZeroStatus()) { + + String topType=server.getTopType(); + String topValue=server.getTopValue(); + gui.updateCardImg(topType, topValue); + + }else { + gui.updateCardImg();//아무것도 없는 경우 뒷장 보이게 함 + } + + + boolean_DropBtnStatus=gui.returnDropBtnStatus(); + boolean_HitBtnStatus=gui.returnHitBtnStatus(); + if(boolean_DropBtnStatus){ + + gui.setGameInfo("it's not your turn..please wait"); + gui.setDropStatusFalse(); + } + if(boolean_HitBtnStatus){ + hit(); + gui.setMsgView("플레이어 "+ id+" 가 hit을 했습니다."); + server.modifyBtnStatus(1, id); + gui.setHitStatusFalse(); + } + } + + } + + server.refreshDeck(server.whosWin()); + gui.setCardNumLabel(0, server.getDeckLength(0)); + gui.setCardNumLabel(1, server.getDeckLength(1)); + gui.deactivatedHitDropBtn(); + gui.updateCardImg(); + gui.getMessageBox(client.get(server.whosWin()-1)); + + }catch(Exception e) { + e.printStackTrace(); + } + } + } + public void sendMsgToAll(String str) {//행동 취하고 나면 반드시 해당함수를 써서 메시지를 보내야한다. 그리고 바로 receiveMSG도 작동해야한다. + try { + server.sendMsg(str); + + }catch(Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + String mServer="127.0.0.1"; + String mServName="WarGame"; + + System.out.println("Remote Method Invocate to "+mServer+", service name: "+mServName); + + try { + System.setProperty("javax.net.ssl.trustStore", "trustedcerts"); + System.setProperty("javax.net.ssl.trustStorePasssword", "123456"); + + String id=""; + String pw=""; + + LoginGUI login = new LoginGUI(); + + while(login.returnFlag()==false) { + System.out.print(""); + } + id=login.returnID(); + pw=login.returnPW(); + Registry registry = LocateRegistry.getRegistry( + InetAddress.getLocalHost().getHostName(), PORT, + new RMISSLClientSocketFactory()); + WarGame c=(WarGame)registry.lookup("WarGame"/*"rmi://"+mServer+":1099/"+mServName*/); + new Thread(new WarGameClient(c,id,pw)).start(); + + }catch(RemoteException re) { + System.out.println("RemoteException: "+re); + }catch(NotBoundException nbe) { + System.out.println("NotBoundException: "+nbe); + }catch(java.lang.ArithmeticException ae) { + System.out.println("java.lang.ArithmeticException: "+ae); + } catch (UnknownHostException e) { + System.out.println("UnknownHostException: "+e); + } + } + +} \ No newline at end of file diff --git a/WarGameGUI.java b/WarGameGUI.java index a695d46..4b832f6 100644 --- a/WarGameGUI.java +++ b/WarGameGUI.java @@ -23,7 +23,7 @@ class WarGameBoard extends JPanel{ ImageIcon icon; JButton dropBtn; JButton hitBtn; - + CardGUI []cardGUI; public WarGameBoard() { setLayout(null); @@ -34,37 +34,33 @@ public WarGameBoard() { frontSideCard=new ImageIcon[52]; String address="img/trumpCard/"; String tmp=null; + + cardGUI=new CardGUI[52]; for(int i=1;i<10;i++) { - tmp=address+(i+1)+"S"+".png"; - frontSideCard[13*0+i]=new ImageIcon(tmp); - tmp=address+(i+1)+"D"+".png"; - frontSideCard[13*1+i]=new ImageIcon(tmp); - tmp=address+(i+1)+"H"+".png"; - frontSideCard[13*2+i]=new ImageIcon(tmp); - tmp=address+(i+1)+"C"+".png"; - frontSideCard[13*3+i]=new ImageIcon(tmp); + cardGUI[13*0+i]=new CardGUI("S",i); + cardGUI[13*1+i]=new CardGUI("D",i); + cardGUI[13*2+i]=new CardGUI("H",i); + cardGUI[13*3+i]=new CardGUI("C",i); } - frontSideCard[13*0+0]=new ImageIcon("img/trumpCard/AS.png"); - frontSideCard[13*1+0]=new ImageIcon("img/trumpCard/AD.png"); - frontSideCard[13*2+0]=new ImageIcon("img/trumpCard/AH.png"); - frontSideCard[13*3+0]=new ImageIcon("img/trumpCard/AC.png"); - - frontSideCard[13*0+10]=new ImageIcon("img/trumpCard/JS.png"); - frontSideCard[13*1+10]=new ImageIcon("img/trumpCard/JD.png"); - frontSideCard[13*2+10]=new ImageIcon("img/trumpCard/JH.png"); - frontSideCard[13*3+10]=new ImageIcon("img/trumpCard/JC.png"); - - frontSideCard[13*0+11]=new ImageIcon("img/trumpCard/QS.png"); - frontSideCard[13*1+11]=new ImageIcon("img/trumpCard/QD.png"); - frontSideCard[13*2+11]=new ImageIcon("img/trumpCard/QH.png"); - frontSideCard[13*3+11]=new ImageIcon("img/trumpCard/QC.png"); - - frontSideCard[13*0+12]=new ImageIcon("img/trumpCard/KS.png"); - frontSideCard[13*1+12]=new ImageIcon("img/trumpCard/KD.png"); - frontSideCard[13*2+12]=new ImageIcon("img/trumpCard/KH.png"); - frontSideCard[13*3+12]=new ImageIcon("img/trumpCard/KC.png"); - - + cardGUI[13*0+0]=new CardGUI("S","A");//type,value + cardGUI[13*1+0]=new CardGUI("D","A");//type,value + cardGUI[13*2+0]=new CardGUI("H","A");//type,value + cardGUI[13*3+0]=new CardGUI("C","A");//type,value + + cardGUI[13*0+10]=new CardGUI("S","J");//type,value + cardGUI[13*1+10]=new CardGUI("D","J");//type,value + cardGUI[13*2+10]=new CardGUI("H","J");//type,value + cardGUI[13*3+10]=new CardGUI("C","J");//type,value + + cardGUI[13*0+11]=new CardGUI("S","Q"); + cardGUI[13*1+11]=new CardGUI("D","Q"); + cardGUI[13*2+11]=new CardGUI("H","Q"); + cardGUI[13*3+11]=new CardGUI("C","Q"); + + cardGUI[13*0+12]=new CardGUI("S","K"); + cardGUI[13*1+12]=new CardGUI("D","K"); + cardGUI[13*2+12]=new CardGUI("H","K"); + cardGUI[13*3+12]=new CardGUI("C","K"); backSideCard=new ImageIcon("img/trumpCard/red_back.png"); @@ -92,26 +88,47 @@ public WarGameBoard() { playerCard=backSideCard; + } + void updateCardImg(String type,String value) { + playerCard=findImg(type,value); + repaint(); + + } + void callRepaint() { + repaint(); + } + void updateCardImg() { + playerCard=new ImageIcon("img/trumpCard/red_back.png"); + repaint(); } protected void paintComponent(Graphics g) { super.paintComponent(g); playerCard.paintIcon(this,g,200,50); } + void setCardNumLabel(int p,int num) { + cardNumLabel[p].setText(""+num); + + } + ImageIcon findImg(String type,String value) { + for(int i=0;i"); JLabel la_playerInfo=new JLabel(""); -// boolean ready=false; + boolean boolean_ReadyBtn; boolean boolean_HitBtn; boolean boolean_DropBtn; @@ -128,12 +145,12 @@ public class WarGameGUI extends JFrame implements ActionListener{ public WarGameGUI(String id) { super("War Game"); -// WarGameClient client=new WarGameClient(); + this.id=id; boolean_ReadyBtn=false; boolean_HitBtn=false; boolean_DropBtn=false; - //it was in main() at first.but need to change to use rmi... + Container ct=getContentPane(); ct.setLayout(null); @@ -143,7 +160,7 @@ public WarGameGUI(String id) { hitBtn.setText("Hit"); hitBtn.setName("hitBtn"); - hitBtn.setBounds(120,420,157,82); + hitBtn.setBounds(120,560,157,82); ct.add(hitBtn); hitBtn.addActionListener(this); @@ -153,14 +170,14 @@ public WarGameGUI(String id) { dropBtn.setText("Drop"); dropBtn.setName("dropBtn"); - dropBtn.setBounds(300,420,157,82); + dropBtn.setBounds(300,560,157,82); ct.add(dropBtn); dropBtn.addActionListener(this); ta_msgView=new JTextArea(1,1); scPane=new JScrollPane(ta_msgView); -// EtchedBorder eborder = new EtchedBorder(EtchedBorder.RAISED); + ta_msgView.setEditable(false); la_GameInfo.setBounds(10,10,480,30); ct.add(la_GameInfo); @@ -171,17 +188,13 @@ public WarGameGUI(String id) { JPanel connectPanel=new JPanel(); connectPanel.setLayout(new GridLayout(1,1)); -// connectPanel.add(new Label("IP Address: ",2)); -// connectPanel.add(tf_IPAddress); -// connectPanel.add(new Label("name : ",2)); -// connectPanel.add(tf_Name); -// connectPanel.add(connectBtn) + readyBtn.setFont(new java.awt.Font("Lucida Grande", 0, 24));; connectPanel.add(readyBtn); -// readyBtn.setEnabled(false); + connectPanel.setBounds(620,50,250,70); ct.add(connectPanel); -// connectBtn.addActionListener(this); + readyBtn.addActionListener(this); JPanel participantsPanel=new JPanel(); @@ -194,16 +207,30 @@ public WarGameGUI(String id) { JPanel infoPanel=new JPanel(); infoPanel.setLayout(new BorderLayout()); infoPanel.add(scPane,"Center"); -// infoPanel.add(tf_Send,"South"); + infoPanel.setBounds(620,360,250,200); ct.add(infoPanel); -// tf_Send.addActionListener(this); + ta_msgView.append("press ready to play game\n"); - this.setSize(880,650); + this.setSize(880,690); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } + + public void callRepaint() { + board.callRepaint(); + } + //playerLabel[0],[1] + public void setCardNumLabel(int p,int num) { + board.setCardNumLabel(p, num); + } + public void updateCardImg(String type,String value) { + board.updateCardImg(type, value); + } + public void updateCardImg() { + board.updateCardImg(); + } public String returnID() { return id; } @@ -234,42 +261,34 @@ public void setPlayerList(String str) {//참가자 리스트를 보여줌 public void deactivateReadyBtn() { readyBtn.setEnabled(false); } + public void deactivatedHitDropBtn() { + hitBtn.setEnabled(false); + dropBtn.setEnabled(false); + } + public void activatedHitDropBtn() { + hitBtn.setEnabled(true); + dropBtn.setEnabled(true); + } + public void getMessageBox(String Gid) { + JOptionPane.showMessageDialog(null, Gid+"가 승리하였습니다."); + } public void actionPerformed(ActionEvent ae) { try { if(ae.getSource()==dropBtn) { -// System.out.println("drop"); boolean_DropBtn=true; - - } else if(ae.getSource()==hitBtn) { - System.out.println("hit"); + boolean_HitBtn=true; } -// else if(ae.getSource()==tf_Send) { -// String msg=tf_Send.getText(); -// if(msg.length()==0) -// return ; -// if(msg.length()>=30) { -// msg=msg.substring(0,30); -// } -//// writer.println("[MSG]: ",msg); -// System.out.println("[MSG]:"+ msg); -// tf_Send.setText(""); -// } -// else if(ae.getSource()==connectBtn) { -// System.out.println("[Connect]: button"); -// } + else if(ae.getSource()==readyBtn) { -// System.out.println("[Ready]: button"); if(boolean_ReadyBtn==false) { boolean_ReadyBtn=true; readyBtn.setText("not ready");//레디 박은 상 - System.out.println(boolean_ReadyBtn); } else { boolean_ReadyBtn=false; readyBtn.setText("ready");//레디 안했을 경우 - System.out.println(boolean_ReadyBtn); } } @@ -279,3 +298,34 @@ else if(ae.getSource()==readyBtn) { } } +class CardGUI{ + String type; + String value; + ImageIcon img; + String address="img/trumpCard/"; + String tmp=null; + CardGUI(String type,String value){ + this.type=type; + this.value=value; + tmp=address+value+type+".png"; + img=new ImageIcon(tmp); + + } + CardGUI(String type,int value){ + this.type=type; + value=value+1; + this.value=Integer.toString(value); + + tmp=address+value+type+".png"; + img=new ImageIcon(tmp); + } + String returnType() { + return type; + } + String returnValue() { + return value; + } + ImageIcon returnImg() { + return img; + } +} \ No newline at end of file diff --git a/WarGameImpl.java b/WarGameImpl.java index a3908fe..cdb77fd 100644 --- a/WarGameImpl.java +++ b/WarGameImpl.java @@ -1,3 +1,6 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.ArrayList; @@ -5,135 +8,518 @@ import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; + +import javax.swing.JOptionPane; + import java.util.Set; public class WarGameImpl extends UnicastRemoteObject implements WarGame { - private static final long serializedversion=1L; - private String clientID[]= {"aaa","bbb"}; - private String clientPassword[]= {"1234","1234"}; - private ArrayList clientList; - private Game game; - private boolean turn=true; - private ArrayList deck0,deck1,deck2; - private int clientCnt=0; - private HashMap readyStatus; - private boolean boolean_readyStatus[]; - protected WarGameImpl() throws RemoteException{ - super(); - //clientList=new ArrayList(); - game=new Game(); - deck1=game.returnDeck1(); - deck2=game.returnDeck2(); - deck0=null; - readyStatus=new HashMap(); - -// boolean_readyStatus=new boolean[2]; -// boolean_readyStatus[0]=false; -// boolean_readyStatus[1]=false; - //System.out.println(deck1);//test - } - - public synchronized boolean checkClientCredentials(WarGame wg,String id,String password) throws RemoteException{ - boolean checkLog=false; - for(int i=0;i> itr=readyStatus.entrySet().iterator(); - Set>set=readyStatus.entrySet(); - while(itr.hasNext()) { - Map.Entrye=(Map.Entry)itr.next(); - if(e.getValue()==1) - cnt++; - - } - if(cnt==2) - return true; - else - return false; - } - public synchronized void doDrop() throws RemoteException{ - Card c=null; - if(turn==true) {//player 1's drop - c=deck1.get(0); - if(c==null) { - System.out.println("Player2 win!!"); - } - deck0.add(c); - - }else {//player 2's drop - c=deck2.get(0); - if(c==null) { - System.out.println("Player1 win!!"); - } - deck0.add(c); - } - } - public synchronized void doHit() throws RemoteException{ - Card []c=new Card[2]; - c[0]=deck0.get(0); - c[1]=deck0.get(1); - if(c[0].returnType()==c[1].returnType()) { - System.out.println("Hit Success!!"); - } - else - System.out.println("Hit Fail..."); - - - } - public boolean changeTurn() { - return !turn; - } - public void pop() { - if(turn==true) { - - }else { - - } - } - public void push() { - if(turn==true) { - - }else { - - } - } - -} + private static final int PORT = 2020; + private static final long serializedversion=1L; + private ArrayList clientID; + private ArrayList clientPassword; + private ArrayList clientLogin; +// private ArrayList clientList; + private Game game; + private boolean turn=true; + private String msg; + private ArrayList deck0,deck1,deck2; + private int clientCnt=0; + //private HashMap clientStatus;//어떤 클라이언트의 순서인지 확인할 수 있다(이름,0)/(이름,1) -> 전자는 비활성화, 후자는 활성화 + private ArrayList client; + private HashMap readyStatus; + private boolean boolean_readyStatus[]; + private int attackCount=-1; + + private boolean boolean_dropStatus[]; + private boolean boolean_hitStatus[]; + + private boolean isAttack = false; + private int dropCount = 0; + private int whoIsWin=0; + + protected WarGameImpl() throws RemoteException{ + super(); + //clientList=new ArrayList(); + msg=""; + game=new Game(); + deck1=game.returnDeck1(); + deck2=game.returnDeck2(); + deck0=game.returnDeck0(); + clientID = new ArrayList(); + clientPassword = new ArrayList(); + clientLogin = new ArrayList(); + readyStatus=new HashMap(); + boolean_dropStatus = new boolean[2]; + boolean_hitStatus = new boolean[2]; + boolean_dropStatus[0] = false; + boolean_dropStatus[1] = false; + boolean_hitStatus[0] = false; + boolean_hitStatus[1] = false; + + client=new ArrayList(); + + } + + public String findAnotherId(String id) throws RemoteException{ + int num = -1; + String temp; + for(int i = 0; i < client.size(); i++) { + if(id.equals(client.get(i))) { + num = i; + break; + } + } + if(num == 0) { + temp = client.get(1); + } + else { + temp = client.get(0); + } + return temp; + } + + public boolean checkBtnStatus(int whichBtn, String id) throws RemoteException{ + int num = -1; + for(int i = 0; i < client.size(); i++) { + if(id.equals(client.get(i))) { + num = i; + break; + } + } + if(num == 0) { + num = 1; + } + else + num = 0; + + + if(whichBtn == 0) { //drop + return boolean_dropStatus[num]; + } + else { //hit + return boolean_hitStatus[num]; + } + } + + public void modifyBtnStatus(int whichBtn, String id) throws RemoteException{ + int num = -1; + for(int i = 0; i < client.size(); i++) { + if(id.equals(client.get(i))) { + num = i; + break; + } + } + if(whichBtn == 0) { //drop + boolean_dropStatus[num] = true; + } + else { //hit + boolean_hitStatus[num] = true; + } + } + + public void modifyAnotherBtnStatus(int whichBtn, String id) throws RemoteException{ + int num = -1; + for(int i = 0; i < client.size(); i++) { + if(id.equals(client.get(i))) { + num = i; + break; + } + } + if(num == 0) + num = 1; + else + num = 0; + + if(whichBtn == 0) //drop + boolean_dropStatus[num] = false; + else //hit + boolean_hitStatus[num] = false; + + } + + + public boolean getDeckZeroStatus() throws RemoteException{ + if(deck0.isEmpty()) + return false; + else + return true; + + } + public void refreshDeck(int who) throws RemoteException{ + if(who == 2) { + for(int i = 0; i < deck0.size(); i++) { + deck2.add(deck0.get(i)); + } + deck0 = new ArrayList(); + } + else if(who == 1) { + for(int i = 0; i < deck0.size(); i++) { + deck1.add(deck0.get(i)); + } + deck0 = new ArrayList(); + } + + } + public String getTopType() throws RemoteException{ + try { + if(deck0.size()>0) + return deck0.get(deck0.size()-1).returnType(); + else + return ""; + } + catch(NullPointerException e) { + System.out.println(e); + return ""; + } + } + public String getTopValue() throws RemoteException{ + try { + if(deck0.size()>0) + return deck0.get(deck0.size()-1).returnValue(); + else + return ""; + } + catch(NullPointerException e) { + System.out.println(e); + return ""; + } + } + public synchronized boolean checkClientCredentials(WarGame wg,String id,String password) throws RemoteException{ //id, pw 확인 + boolean checkLog=false; + try { + BufferedReader br=new BufferedReader(new FileReader("account.txt")); + String s; + while ((s = br.readLine()) != null) { + String[] split = s.split("\t"); + clientID.add(split[0]); + clientPassword.add(split[1]); + clientLogin.add(false); + } + }catch(IOException e) { + System.out.println(e); + } + + + for(int i=0;i getClientList() throws RemoteException{ + return client; + } + @Override + public synchronized void sendMsg(String msg) throws RemoteException{ + this.msg=msg; + } + @Override + public synchronized String receiveMsg() throws RemoteException{ + return msg; + } + @Override + public int checkGameStatus() throws RemoteException {//drop을 해야할지 hit를 해야할지, 공격중인 상황인지 평시 상황인지 판단한담에 거기에 맞춰 int리턴 + // TODO Auto-generated method st + int loc = deck0.size(); + String value=deck0.get(loc-1).returnValue(); + if(value=="K")//공격카드의 값만큼 리턴 + return 3; + else if(value.equals("Q")) + return 2; + else if(value.equals("J")) + return 1; + else if(value.equals("A")) + return 3; + else if(value.equals("2")) { + return 2; + } + else { + return -1;//평시 상태 + } + } + + public boolean checkEndingStatus() throws RemoteException{ //deck이 비면 끝남 + if(deck1.isEmpty()==true || deck2.isEmpty()==true) { + if(deck1.isEmpty()) { + whoIsWin = 2; + } + else if(deck2.isEmpty()) { + whoIsWin = 1; + } + return false;//game over + } + else + return true; + } + + public String whosTurn() throws RemoteException{ //누구의 턴인지 반환 + if(turn==true) + return client.get(0); + else + return client.get(1); + } + + public void changeTurn() throws RemoteException{ + turn=!turn; + } + + public synchronized void setReadyStatus(String id,boolean status) throws RemoteException{//플레이어 각각이 레디했는지 확인 + int tmp=0; + if(status==true) + tmp=1; + else + tmp=0; + if(!readyStatus.containsKey(id))//결국엔 같은건데 구분할까 말까? + readyStatus.put(id, tmp); + else { + readyStatus.put(id,tmp); + + } + + } + public synchronized boolean checkAllReady() throws RemoteException{//player 2명 전부 레디 했는지 확인 + int cnt=0; + Iterator> itr=readyStatus.entrySet().iterator(); + Set>set=readyStatus.entrySet(); + while(itr.hasNext()) { + Map.Entrye=(Map.Entry)itr.next(); + if(e.getValue()==1) { + cnt++; + } + + } + if(cnt==2) { + client.addAll(readyStatus.keySet()); + return true; + } + + else + return false; + } + public int whosWin() throws RemoteException { + return whoIsWin; + } + + public synchronized void doDrop(String id) throws RemoteException{ + + Card c=null; + if(turn==true) {//player 1's drop + if(isAttack == false) { + System.out.println("player 1 drop!"); + if(deck1.isEmpty()) { + System.out.println("Player2 win!!"); + return; + } + c=deck1.get(0); + pop(); //가져온 카드 1개 제거 + + deck0.add(c); + + attackCount = checkGameStatus(); + if(attackCount == -1) { + turn = !turn; + } + else { + isAttack = true; + turn = !turn; + } + } + else if(isAttack == true) { //공격 상태일 때 + System.out.println("player 1 drop! : Attack"); + if(deck1.isEmpty()) { + System.out.println("Player2 win!!"); + return; + } + c=deck1.get(0); + pop(); //가져온 카드 1개 제거 + + deck0.add(c); + if(deck1.isEmpty()) { + return; + } + if(checkGameStatus() == -1) { //공격카드가 아닐 경우 + dropCount++; + + if(dropCount == attackCount) { //낸 카드의 수가 조건을 넘어갔을 떄 + dropCount = 0; + isAttack = false; + for(int i = 0; i < deck0.size(); i++) { + deck2.add(deck0.get(i)); + } + deck0 = new ArrayList(); + turn = !turn; + } + } + else { + isAttack = true; + dropCount = 0; + if(deck1.isEmpty()) { + return; + } + attackCount = checkGameStatus(); + turn = !turn; + } + } + + }else {//player 2's drop + if(isAttack == false) { + System.out.println("player 2 drop!"); + if(deck2.isEmpty()) { + System.out.println("Player1 win!!"); + return; + } + c=deck2.get(0); + pop(); //가져온 카드 1개 제거 + + deck0.add(c); + attackCount = checkGameStatus(); + if(attackCount == -1) { + turn = !turn; + } + else { + isAttack = true; + turn = !turn; + } + + } + else if(isAttack == true) { //공격 상태일 때 + System.out.println("player 2 drop! : Attack"); + if(deck2.isEmpty()) { + System.out.println("Player1 win!!"); + return; + } + c=deck2.get(0); + pop(); + + deck0.add(c); + + if(checkGameStatus() == -1) { //공격카드가 아닐 경우 + dropCount++; + + if(dropCount == attackCount) { //낸 카드의 수가 조건을 넘어갔을 떄 + dropCount = 0; + isAttack = false; + for(int i = 0; i < deck0.size(); i++) { + deck1.add(deck0.get(i)); + } + deck0 = new ArrayList(); + turn = !turn; + } + } + else { + isAttack = true; + dropCount = 0; + if(deck2.isEmpty()) { + return; + } + attackCount = checkGameStatus(); + turn = !turn; + } + } + } + } + public synchronized void doHit(String id) throws RemoteException{ + int who = -1; + for(int i = 0; i < client.size(); i++) { + if(id.equals(client.get(i))) { + who = i+1; + break; + } + } + Card []c=new Card[2]; + if(deck0.size() < 2) { + System.out.println("Hit Fail"); + if(who == 1){ + for(int j = 0; j < deck0.size();j++) { + deck2.add(deck0.get(j)); + } + deck0 = new ArrayList(); + turn = false; + } + else if(who == 2){ + for(int j = 0; j < deck0.size();j++) { + deck1.add(deck0.get(j)); + } + deck0 = new ArrayList(); + turn = true; + } + + } + else { + c[0]=deck0.get(deck0.size()-2); + c[1]=deck0.get(deck0.size()-1); + if(c[0].returnValue().equals(c[1].returnValue())) { + System.out.println("Hit Success"); + if(who == 1) { + for(int j = 0; j < deck0.size();j++) { + deck1.add(deck0.get(j)); + } + deck0 = new ArrayList(); + turn = true; + } + else if(who == 2) { + for(int j = 0; j < deck0.size();j++) { + deck2.add(deck0.get(j)); + } + deck0 = new ArrayList(); + turn = false; + } + } + else { + System.out.println("Hit Fail"); + if(who == 1){ + for(int j = 0; j < deck0.size();j++) { + deck2.add(deck0.get(j)); + } + deck0 = new ArrayList(); + turn = false; + } + else if(who == 2){ + for(int j = 0; j < deck0.size();j++) { + deck1.add(deck0.get(j)); + } + deck0 = new ArrayList(); + turn = true; + } + } + } + dropCount = 0; + isAttack = false; + } + + public void pop() { + if(turn==true) { + deck1.remove(0); + }else { + deck2.remove(0); + } + } + public void push() { + if(turn==true) { + + }else { + + } + } + +} \ No newline at end of file diff --git a/WarGameServer.java b/WarGameServer.java index f41b75e..9aeca7e 100644 --- a/WarGameServer.java +++ b/WarGameServer.java @@ -4,137 +4,42 @@ import java.io.*; import java.rmi.Naming; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; -public class WarGameServer { +public class WarGameServer extends UnicastRemoteObject { public int clientCnt=0; -// private WarGameServerRunnable client[]=new WarGameServerRunnable[2]; - public WarGameServer(String server,String servName) { - try { - WarGame wg=new WarGameImpl(); - Naming.rebind("rmi://"+server+":1099/"+servName, wg); - }catch(Exception e) { - System.out.println("Trouble: "+e); - } + private static final int PORT = 2020; + + public WarGameServer(String server,String servName) throws Exception{ + super(PORT, + new RMISSLClientSocketFactory(), + new RMISSLServerSocketFactory()); + } public static void main(String[] args) throws UnknownHostException { -// String mServer=InetAddress.getLocalHost().getHostName(); -// String mServer=args[0]; String mServer="127.0.0.1"; String mServName="WarGame"; System.out.println("started at"+mServer+"and use default port(1099). service name: "+mServName); - new WarGameServer(mServer,mServName); + + try { + + //SSL-based registry 생성 + Registry registry = LocateRegistry.createRegistry(PORT, new RMISSLClientSocketFactory(), new RMISSLServerSocketFactory()); + + //Create new impl object + WarGameImpl warGameImpl = new WarGameImpl(); + + registry.bind("WarGame", warGameImpl); + + System.out.println("WarGame bound in registry"); + + } catch (Exception e) { + System.out.println("WarGame Error : " + e.getMessage()); + e.printStackTrace(); + } } } -// public int whoClient(int clientID) { -// for (int i = 0; i < clientCnt; i++) -// if (client[i].getClientID() == clientID) -// return i; -// return -1; -// } -// public void putClient(int clientID, String inputLine) { -// for (int i = 0; i < clientCnt; i++) -// if (client[i].getClientID() == clientID) { -// System.out.println("writer: "+clientID); -// } else { -// System.out.println("write: "+client[i].getClientID()); -// client[i].out.println(inputLine); -// } -// } - //this is about chatting. so.. -// public void addClient(ServerSocket serverSocket) { -// Socket clientSocket = null; -// -// if (clientCnt < client.length) { -// try { -// clientSocket = serverSocket.accept(); -// clientSocket.setSoTimeout(40000); // 1000/sec -// } catch (IOException i) { -// System.out.println ("Accept() fail: "+i); -// } -// client[clientCnt] = new WarGameServerRunnable(this, clientSocket); -// new Thread(client[clientCnt]).start(); -// clientCnt++; -// System.out.println ("Client connected: " + clientSocket.getPort() -// +", CurrentClient: " + clientCnt); -// } else { -// try { -// Socket dummySocket = serverSocket.accept(); -// WarGameServerRunnable dummyRunnable = new WarGameServerRunnable(this, dummySocket); -// new Thread(dummyRunnable); -// dummyRunnable.out.println(dummySocket.getPort() -// + " < Sorry maximum user connected now"); -// System.out.println("Client refused: maximum connection " -// + client.length + " reached."); -// dummyRunnable.close(); -// } catch (IOException i) { -// System.out.println(i); -// } -// } -// } -// public synchronized void delClient(int clientID) { -// int pos = whoClient(clientID); -// WarGameServerRunnable endClient = null; -// if (pos >= 0) { -// endClient = client[pos]; -// if (pos < clientCnt-1) -// for (int i = pos+1; i < clientCnt; i++) -// client[i-1] = client[i]; -// clientCnt--; -// System.out.println("Client removed: " + clientID -// + " at client[" + pos +"], CurrentClient: " + clientCnt); -// endClient.close(); -// } -// } -//} -//class WarGameServerRunnable implements Runnable{ -// protected WarGameServer warGameServer=null; -// protected Socket clientSocket=null; -// protected PrintWriter out=null; -// protected BufferedReader in=null; -// public int clientID=-1; -// -// Game game=new Game(); -// ArrayList deck0=null;//내려놓는 곳 -// ArrayList deck1;//p1 deck -// ArrayList deck2;//p2 deck -// int playerCardCnt[]=new int[2];//player의 카드 갯 -// int deck0Cnt;//내려놓는 곳 카드 수 -// int turn=0; -// public WarGameServerRunnable(WarGameServer server,Socket socket){ -// this.warGameServer=server; -// this.clientSocket=socket; -// clientID=clientSocket.getPort(); -// try{ -// out=new PrintWriter(clientSocket.getOutputStream(),true); -//// out=new BufferedWriter(clientSocket.getOutputStream()); -// in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); -// }catch(IOException ioe){} -// } -// public void run() { -// gameInit(); -// } -// public void close() { -// try { -// if(in !=null) in.close(); -// if(out !=null) out.close(); -// if(clientSocket!=null)clientSocket.close(); -// -// }catch(IOException i) { -// i.printStackTrace(); -// } -// } -// public int getClientID() { -// return clientID; -// } -// public void gameInit() { -// -// deck1=game.returnDeck1();//이미 셔플된 상태로 나온거라 따로 안해줘도 -// deck2=game.returnDeck2(); -// playerCardCnt[0]=deck1.size(); -// playerCardCnt[1]=deck2.size(); -// deck0Cnt=0; -// -// } - diff --git a/midWar.jpg b/midWar.jpg deleted file mode 100644 index fbb3822..0000000 Binary files a/midWar.jpg and /dev/null differ