Skip to content

Instantly share code, notes, and snippets.

@mrlin0518
Last active July 5, 2018 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrlin0518/944ed0c3eff7e29f2186a45cd635e825 to your computer and use it in GitHub Desktop.
Save mrlin0518/944ed0c3eff7e29f2186a45cd635e825 to your computer and use it in GitHub Desktop.
Game-使用DAO概念設計兩種猜拳
package Roshambo;
public interface RoshamboDAO {
void COMPUTERGUESS();
void PLAYERSSGUESS();
RoshamboEume getCpu();
RoshamboEume getPly();
}
package Roshambo;
public class RoshamboDAOFactory {
public RoshamboDAO creatRoshamboDAO(){
return new RoshamboDAOImp2();
}
}
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;
}
}
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;
}
}
package Roshambo;
public enum RoshamboEume {
隨機, 剪刀, 石頭, 布;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment