Last active
July 5, 2018 10:38
Game-使用DAO概念設計兩種猜拳
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Roshambo; | |
public interface RoshamboDAO { | |
void COMPUTERGUESS(); | |
void PLAYERSSGUESS(); | |
RoshamboEume getCpu(); | |
RoshamboEume getPly(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Roshambo; | |
public class RoshamboDAOFactory { | |
public RoshamboDAO creatRoshamboDAO(){ | |
return new RoshamboDAOImp2(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Roshambo; | |
import java.util.Scanner; | |
public class RoshamboDAOImp implements RoshamboDAO { | |
Scanner sc = new Scanner(System.in); | |
private RoshamboEume cpu ; | |
private RoshamboEume ply ; | |
@Override | |
public void COMPUTERGUESS() { | |
int a = (int) (Math.random() * 3 + 1);//亂數1~3 | |
switch (a) { | |
case 1: | |
System.out.println("電腦出剪刀"); | |
cpu = cpu.剪刀; | |
break; | |
case 2: | |
System.out.println("電腦出石頭"); | |
cpu = cpu.石頭; | |
break; | |
case 3: | |
System.out.println("電腦出布"); | |
cpu = cpu.布; | |
break; | |
} | |
} | |
@Override | |
public void PLAYERSSGUESS() { | |
System.out.println("請問你要出哪一個?\n0.隨機,1.剪刀,2.石頭,3.布"); | |
int a = sc.nextInt(); | |
if (a == 0) { | |
int b = (int) (Math.random() * 3 + 1); | |
a = b; | |
} | |
switch (a) { | |
case 1: | |
System.out.println("你出剪刀"); | |
ply = ply.剪刀; | |
break; | |
case 2: | |
System.out.println("你出石頭"); | |
ply = ply.石頭; | |
break; | |
case 3: | |
System.out.println("你出布"); | |
ply = ply.布; | |
break; | |
} | |
} | |
public RoshamboEume getCpu() { | |
return cpu; | |
} | |
public RoshamboEume getPly() { | |
return ply; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Roshambo; | |
import java.util.Scanner; | |
import javax.swing.JComboBox; | |
import javax.swing.JOptionPane; | |
public class RoshamboDAOImp2 implements RoshamboDAO { | |
private RoshamboEume cpu; | |
private RoshamboEume ply; | |
@Override | |
public void COMPUTERGUESS() { | |
int a = (int) (Math.random() * 3 + 1);//亂數1~3 | |
switch (a) { | |
case 1: | |
JOptionPane.showMessageDialog(null,"電腦出剪刀"); | |
cpu = cpu.剪刀; | |
break; | |
case 2: | |
JOptionPane.showMessageDialog(null,"電腦出石頭"); | |
cpu = cpu.石頭; | |
break; | |
case 3: | |
JOptionPane.showMessageDialog(null,"電腦出布"); | |
cpu = cpu.布; | |
break; | |
} | |
} | |
@Override | |
public void PLAYERSSGUESS() { | |
int youch=JOptionPane.PLAIN_MESSAGE; | |
RoshamboEume a =(RoshamboEume) JOptionPane.showInputDialog(null,"請選擇要出甚麼拳","選擇", | |
youch,null,RoshamboEume.values(),RoshamboEume.隨機); | |
int c = a.ordinal(); | |
if (a.equals(RoshamboEume.隨機) ) { | |
int b = (int) (Math.random() * 3 + 1); | |
c = b; | |
} | |
switch (c) { | |
case 1: | |
JOptionPane.showMessageDialog(null,"你出剪刀"); | |
ply = ply.剪刀; | |
break; | |
case 2: | |
JOptionPane.showMessageDialog(null,"你出石頭"); | |
ply = ply.石頭; | |
break; | |
case 3: | |
JOptionPane.showMessageDialog(null,"你出布"); | |
ply = ply.布; | |
break; | |
} | |
} | |
public RoshamboEume getCpu() { | |
return cpu; | |
} | |
public RoshamboEume getPly() { | |
return ply; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Roshambo; | |
public enum RoshamboEume { | |
隨機, 剪刀, 石頭, 布; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment