第13章 指导学习:人机猜拳
Posted dabrk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第13章 指导学习:人机猜拳相关的知识,希望对你有一定的参考价值。
赵冬梅
2018-10-15public class Person { String name = "匿名"; // 名字 int score = 0; // 积分 /** * 出拳 * * @return 出拳结果:1.剪刀 2.石头 3.布 */ public int showFist() { // 接收用户的选择 Scanner input = new Scanner(System.in); System.out.print(" 请出拳:1.剪刀 2.石头 3.布 (输入相应数字) :"); int show = input.nextInt(); // 输出出拳结果,并返回 switch (show) { case 1: System.out.println("你出拳: 剪刀"); break; case 2: System.out.println("你出拳: 石头"); break; case 3: System.out.println("你出拳: 布"); break; } return show; } }
public class Computer { String name = "电脑"; // 名字 int score = 0;; // 积分 /** * 出拳 * @return 出拳结果:1.剪刀 2.石头 3.布 */ public int showFist(){ // 产生随机数 int show = (int)(Math.random()*10)%3 + 1; //产生随机数,表示电脑出拳 // 输出出拳结果并返回 switch(show){ case 1: System.out.println(name+"出拳: 剪刀"); break; case 2: System.out.println(name+"出拳: 石头"); break; case 3: System.out.println(name+"出拳: 布"); break; } return show; } }
public class Game1 { Person person; //甲方 Computer computer; //乙方 int count; //对战次数 /** * 初始化 */ public void initial(){ person = new Person(); computer = new Computer(); count = 0; } /** * 开始游戏 */ public void startGame() { initial(); System.out.println("----------------欢 迎 进 入 游 戏 世 界---------------- "); System.out.println(" ******************"); System.out.println (" ** 猜拳, 开始 **"); System.out.println (" ******************"); System.out.println(" 出拳规则:1.剪刀 2.石头 3.布"); /*选择对方角色*/ System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): "); Scanner input = new Scanner(System.in); int role = input.nextInt(); if(role == 1){ computer.name = "刘备"; }else if(role == 2){ computer.name = "孙权"; }else if(role == 3){ computer.name = "曹操"; } System.out.print("你选择了"+computer.name+"对战"); } }
public class Game2 { Person person; //甲方 Computer computer; //乙方 int count; //对战次数 /** * 初始化 */ public void initial(){ person = new Person(); computer = new Computer(); count = 0; } /** * 开始游戏 */ public void startGame() { initial(); // 初始化 System.out.println("----------------欢 迎 进 入 游 戏 世 界---------------- "); System.out.println(" ******************"); System.out.println (" ** 猜拳, 开始 **"); System.out.println (" ******************"); System.out.println(" 出拳规则:1.剪刀 2.石头 3.布"); /*选择对方角色*/ System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): "); Scanner input = new Scanner(System.in); int role = input.nextInt(); if(role == 1){ computer.name = "刘备"; }else if(role == 2){ computer.name = "孙权"; }else if(role == 3){ computer.name = "曹操"; } System.out.println("你选择了 "+computer.name+"对战"); /*开始游戏*/ System.out.print(" 要开始吗?(y/n) "); String con = input.next(); int perFist; //用户出的拳 int compFist; //计算机出的拳 if(con.equals("y")){ /*出拳*/ perFist = person.showFist(); compFist = computer.showFist(); /*裁决*/ if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){ System.out.println("结果:和局,真衰! "); //平局 }else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){ System.out.println("结果: 恭喜, 你赢了!"); //用户赢 }else{ System.out.println("结果说:^_^,你输了,真笨! "); //计算机赢 } } } }
public class Game3 { Person person; //甲方 Computer computer; //乙方 int count; //对战次数 /** * 初始化 */ public void initial(){ person = new Person(); computer = new Computer(); count = 0; } /** * 开始游戏 */ public void startGame() { initial(); // 初始化 System.out.println("----------------欢 迎 进 入 游 戏 世 界---------------- "); System.out.println(" ******************"); System.out.println (" ** 猜拳, 开始 **"); System.out.println (" ******************"); System.out.println(" 出拳规则:1.剪刀 2.石头 3.布"); /*选择对方角色*/ System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): "); Scanner input = new Scanner(System.in); int role = input.nextInt(); if(role == 1){ computer.name = "刘备"; }else if(role == 2){ computer.name = "孙权"; }else if(role == 3){ computer.name = "曹操"; } System.out.println("你选择了 "+computer.name+"对战"); /*开始游戏*/ System.out.print(" 要开始吗?(y/n) "); String con = input.next(); int perFist; //用户出的拳 int compFist; //计算机出的拳 while(con.equals("y")){ /*出拳*/ perFist = person.showFist(); compFist = computer.showFist(); /*裁决*/ if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){ System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 ! "); //平局 }else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){ System.out.println("结果: 恭喜, 你赢了!"); //用户赢 person.score++; }else{ System.out.println("结果说:^_^,你输了,真笨! "); //计算机赢 computer.score++; } count++; System.out.print(" 是否开始下一轮(y/n): "); con = input.next(); } } }
public class Game4 { Person person; //甲方 Computer computer; //乙方 int count; //对战次数 /** * 初始化 */ public void initial(){ person = new Person(); computer = new Computer(); count = 0; } /** * 开始游戏 */ public void startGame() { initial(); // 初始化 System.out.println("----------------欢 迎 进 入 游 戏 世 界---------------- "); System.out.println(" ******************"); System.out.println (" ** 猜拳, 开始 **"); System.out.println (" ******************"); System.out.println(" 出拳规则:1.剪刀 2.石头 3.布"); /*选择对方角色*/ System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): "); Scanner input = new Scanner(System.in); int role = input.nextInt(); if(role == 1){ computer.name = "刘备"; }else if(role == 2){ computer.name = "孙权"; }else if(role == 3){ computer.name = "曹操"; } System.out.println("你选择了 "+computer.name+"对战"); System.out.print(" 要开始吗?(y/n) "); String con = input.next(); int perFist; //用户出的拳 int compFist; //计算机出的拳 while(con.equals("y")){ /*出拳*/ perFist = person.showFist(); compFist = computer.showFist(); /*裁决*/ if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){ System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 ! "); //平局 }else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){ System.out.println("结果: 恭喜, 你赢了!"); //用户赢 person.score++; }else{ System.out.println("结果说:^_^,你输了,真笨! "); //计算机赢 computer.score++; } count++; System.out.print(" 是否开始下一轮(y/n): "); con = input.next(); } /*显示结果*/ showResult(); } /** * 显示比赛结果 */ public void showResult(){ /*显示最后结果*/ System.out.println("---------------------------------------------------"); System.out.println(computer.name + " VS " + person.name); System.out.println("对战次数:"+ count); int result = calcResult(); if(result == 1){ System.out.println("结果:打成平手,下次再和你一分高下!"); }else if(result == 2){ System.out.println("结果:恭喜恭喜!"); //用户获胜 }else{ System.out.println("结果:呵呵,笨笨,下次加油啊!"); //计算机获胜 } System.out.println("---------------------------------------------------"); } /** * 计算比赛结果 * @return 1:战平;2:用户赢;3:电脑赢 */ public int calcResult(){ if(person.score == computer.score){ return 1; // 战平 }else if(person.score > computer.score){ return 2; // 用户赢 }else{ return 3; // 电脑赢 } } }
public class Game { Person person; //甲方 Computer computer; //乙方 int count; //对战次数 /** * 初始化 */ public void initial(){ person = new Person(); computer = new Computer(); count = 0; } /** * 开始游戏 */ public void startGame() { System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------"); System.out.println(" ******************"); System.out.println (" ** 猜拳, 开始 **"); System.out.println (" ******************"); System.out.println(" 出拳规则:1.剪刀 2.石头 3.布"); Scanner input = new Scanner(System.in); String exit = "n"; // 退出系统 do{ initial(); // 初始化 /*选择对方角色*/ System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): "); int role = input.nextInt(); if(role == 1){ computer.name = "刘备"; }else if(role == 2){ computer.name = "孙权"; }else if(role == 3){ computer.name = "曹操"; } // 扩展功能1:输入用户姓名 /*输入用户姓名*/ System.out.print("请输入你的姓名:"); person.name = input.next(); System.out.println(person.name+" VS "+computer.name+" 对战 "); // 扩展功能1结束 System.out.print("要开始吗?(y/n) "); String start = input.next(); // 开始每一局游戏 int perFist; //用户出的拳 int compFist; //计算机出的拳 while(start.equals("y")){ /*出拳*/ perFist = person.showFist(); compFist = computer.showFist(); /*裁决*/ if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){ System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 ! "); //平局 }else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){ System.out.println("结果: 恭喜, 你赢了!"); //用户赢 person.score++; }else{ System.out.println("结果说:^_^,你输了,真笨! "); //计算机赢 computer.score++; } count++; System.out.print(" 是否开始下一轮(y/n): "); start = input.next(); } /*显示结果*/ showResult(); // 扩展功能3:循环游戏,直到退出系统 System.out.print(" 要开始下一局吗?(y/n):"); exit = input.next(); System.out.println(); // 扩展功能3结束 }while(!exit.equals("n")); System.out.println("系统退出!"); } /** * 显示比赛结果 */ public void showResult(){ /*显示对战次数*/ System.out.println("---------------------------------------------------"); System.out.println(computer.name + " VS " + person.name); System.out.println("对战次数:"+ count); // 扩展功能2:显示最终的得分 System.out.println(" 姓名 得分"); System.out.println(person.name+" "+person.score); System.out.println(computer.name+" "+computer.score+" "); // 扩展功能2结束 /*显示对战结果*/ int result = calcResult(); if(result == 1){ System.out.println("结果:打成平手,下次再和你一分高下!"); }else if(result == 2){ System.out.println("结果:恭喜恭喜!"); //用户获胜 }else{ System.out.println("结果:呵呵,笨笨,下次加油啊!"); //计算机获胜 } System.out.println("---------------------------------------------------"); } /** * 计算比赛结果 * @return 1:战平;2:用户赢;3:电脑赢 */ public int calcResult(){ if(person.score == computer.score){ return 1; // 战平 }else if(person.score > computer.score){ return 2; // 用户赢 }else{ return 3; // 电脑赢 } } }
以上是关于第13章 指导学习:人机猜拳的主要内容,如果未能解决你的问题,请参考以下文章