Java实现简单的程序设计,包含猜拳、猜数字和猜硬币,实现玩家和电脑之间的互动,电脑每次出的结果实现随机,玩家选择需要玩的游戏(猜拳、猜数字、猜硬币),选择需要进行的局数。
public class object {
public static void main(String[] args) throws IOException {
printOut printOut = new printOut();
adjust adjust = new adjust();
int x = printOut.chooseGame();
try {
adjust.adjustGame(x);
} catch (MyException e) {
System.out.println("警告!"+ e);
}
}
}
public class printOut implements InterfaceGuess {
private String enter; //玩家出的
private Integer num; //进行游戏对局的局数
private Integer Pd;
public printOut() {
}
public printOut(String enter) {
this.enter = enter;
}
public String getEnter() {
return enter;
}
public Integer getNum() {
return num;
}
public void setEnter(String enter,Integer Pd) throws IOException {
this.enter = enter;
this.Pd = Pd;
switch(Pd) {
case 0:
System.out.println("和局");
break;
case 1:
System.out.println("赢局");
break;
default:
System.out.println("输了");
break;
}
}
public void setNum(Integer num) {
this.num = num;
}
public void Info() {
System.out.println("第"+num+"局玩家出的是"+enter);
System.out.println("===========================");
}
@Override
public int chooseGame() throws IOException {
String chooseGame = "请选择游戏: 猜拳 | 猜数字 | 猜硬币";
System.out.println(chooseGame);
Scanner sc = new Scanner(System.in);
String enter = sc.next( );
//rederText.rederTest();
if(enter.equals("猜拳")){
return 1;
}else if(enter.equals("猜数字")){
return 2;
}else if (enter.equals("猜硬币")){
return 3;
}else if(enter.equals("查询")){
return 4;
}else{
return 0;
}
}
@Override
public int inputInt() {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
return x;
}
@Override
public String inputStr() {
Scanner sc = new Scanner(System.in);
String str = sc.next();
return str;
}
public void start() {
// TODO Auto-generated method stub
System.out.println("#####—— 游戏正式开始 ——#####");
}
@Override
public void end(int num) throws IOException {
System.out.println("###-游戏结束,您总共赢了" + num +"局!-###");
}
public void returnRule() throws IOException {
System.out.println("玩家选择(剪刀 石头 布):");
}
public void gameName() throws IOException {
System.out.println("猜猜电脑出什么:剪刀 石头 布");
}
public void gameNumber() throws IOException {
String s = "猜猜电脑出哪个数 0 1 2";
System.out.println(s);
//System.out.println("猜猜电脑出哪个数 0 1 2");
}
public void gameDoinb() throws IOException {
String s = "硬币抛出落下后,哪面朝上:1.正面 0.反面 ";
System.out.println(s);
//System.out.println("硬币抛出落下后,哪面朝上:1.正面 0.反面 ");
}
public static void printWarn() {
System.out.println("请配合游戏规则!!!");
}
public static void printTip() throws IOException {
String s = "请输入游戏局数";
System.out.println(s);
}
public static void aboutFile(String s) throws IOException {
//定义方法,将字符串写入文本在打印出
System.out.println(s);
}
}
public class adjust {
private String s;
public void adjustGame(int a) throws MyException, IOException{
printOut printOut = new printOut();
if(a==1){ //猜拳
FingerGuessing fg = new FingerGuessing();
printOut.printTip();
String enter = null;
int t = printOut.inputInt();
int i = fg.Judgement(enter, t, 0);
printOut.end(i);
}else if(a == 2){ //猜数字
NumberGuessing guessing = new NumberGuessing();
printOut.printTip();
String enter = null;
int t = printOut.inputInt();
int i = guessing.Judgement(enter, t, 0);
printOut.end(i);
}else if(a == 3){ //猜硬币
DoinbGuessing guessing = new DoinbGuessing();
printOut.printTip();
String enter = null;
int t = printOut.inputInt();
int i = guessing.Judgement(enter, t, 0);
printOut.end(i);
}else if(a == 4) {
}else {
//printOut.printWarn();
s = "请输入正确的游戏方式";
System.out.println(s);
}
}
public String adjustFinger(int a,String b) throws MyException, IOException{
int r = 0;
if(b.equals("剪刀")){
r = 0;
}else if(b.equals("石头")){
r = 1;
}else if(b.equals("布")){
r = 2;
}else {
s = "请按照游戏规则进行游戏";
System.out.println(s);
}
if(a > r){
s = "你输了";
System.out.println(s);
//return 0;
}else if(a < r){
s = "你赢了";
System.out.println(s);
//return 1;
}else if(a == r){
s = "平局";
System.out.println(s);
//return 0;
}else{
s = "系统报错";
System.out.println(s);
}
return s;
}
}
public abstract class AbstractGuess {
printOut out = new printOut();
public AbstractGuess(){
}
public void InputNotice(int rnd,String comp,String result) throws IOException{
System.out.println("第" + rnd + "局" + "电脑出的是" + comp);
System.out.println("这把"+result);
System.out.println("===========================================");
}
public abstract int Judgement(String str, int num ,int rnd);
}
public class DoinbGuessing extends AbstractGuess{
@Override
public int Judgement(String str, int num, int rnd) {
printOut print = new printOut();
try {
print.gameDoinb();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int win = 0;
for (rnd = 1;rnd <= num;rnd++){
int randomNumber = new Random().nextInt(2);
int i = print.inputInt();
String b;
if(randomNumber == 1){
b = "正面";
}else{
b = "反面";
}
if(randomNumber == i){
win++;
try {
InputNotice(rnd,b,"你赢了");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
InputNotice(rnd,b,"你输了");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return win;
}
}
public class FingerGuessing extends AbstractGuess{
@Override
public int Judgement(String str, int num, int rnd) {
//num代表执行的局数,rnd代表随机的数,str
printOut print = new printOut();
print.start();
try {
print.gameName();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int win = 0;
for(rnd = 1;rnd<=num;rnd++){
int randomNumber=new Random( ).nextInt(3);
String str1 = print.inputStr();
adjust adjust = new adjust();
String s = null;
try {
try {
s = adjust.adjustFinger(randomNumber,str1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MyException e) {
// TODO Auto-generated catch block
System.out.println("警告"+ e);
}
if(s != null) {
if(s.equals("你赢了")){
win++;
}
switch (randomNumber){ //0是剪刀,1是石头,2是布
case 0:
try {
InputNotice(rnd,"剪刀",s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 1:
try {
InputNotice(rnd,"石头",s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 2:
try {
InputNotice(rnd,"布",s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}else {
break;
}
}
return win;
}
}
public class NumberGuessing extends AbstractGuess{
@Override
public int Judgement(String str, int num, int rnd) {
printOut print = new printOut();
try {
print.gameNumber();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int win = 0;
for (rnd = 1;rnd <= num;rnd++){
int randomNumber = new Random().nextInt(3);
int i = print.inputInt();
String b= String.valueOf(randomNumber);
if(randomNumber == i){
win++;
try {
InputNotice(rnd,b,"你赢了");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
try {
InputNotice(rnd,b,"你输了");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return win;
}
}
public interface InterfaceGuess {
public abstract int chooseGame() throws IOException;
public abstract int inputInt();
public abstract String inputStr();
public abstract void start();
public abstract void end(int num) throws IOException;
}
public class MyException extends Exception{
MyException(String message) {
super(message);
}
}
Java程序设计真的太不容易了......