剪刀石头布的游戏
(1)定义一个计算机玩家类Computer,它有一个成员变量(name)和一个成员方法(show)。其中的成员变量表示它的名称,成员方法表示出拳的行为,出的拳值是由系统产生随机数表示。
(2)定义一个游戏玩家类Player,它有一个成员变量(name)和一个成员方法(show)。其中的成员变量表示它的名称,成员方法表示出拳的行为,出的拳值是由用户输入的整数表示。
(3)定义一个游戏类Game,它有两个成员变量(computer\player)和一个成员方法(start),其中的成员变量分别表示两个对手,一个是计算机玩家对象,一个是游戏玩家对象。成员方法表示一次游戏过程。
(4)编写一个测试类,完成一次剪刀石头布的游戏。
Top one:
import java.util.Scanner;
import java.util.Random;
public class example
{
public static void main(String []args)
{
Computer c =new Computer("computer");
Player p = new Player("player");
Game g = new Game(c,p);
g.start();
}
}
class Computer
{
String name;
public Computer(String name)
{
this.name = name;
}
int show()
{
int number=(int)(Math.random()*10) % 3;
return number;
}
}
class Player
{
String name;
int shu;
public Player(String name)
{
this.name = name;
}
int show()
{
int number;
number = 0;
System.out.println("0代表石头,1代表剪刀,2代表布");
System.out.println("请输入一个数:");
Scanner sc = new Scanner(System.in);
int shu = sc.nextInt();
System.out.println("玩家输入的是:"+shu);
System.out.println("计算机输入的为:"+number);
return shu;
}
}
class Game
{
Computer computer;
Player player;
public Game(Computer computer,Player player)
{
this.computer = computer;
this.player = player;
}
void start()
{
int c_show = computer.show();
int p_show = player.show();
if(c_show == 0&& p_show ==1||c_show == 1&&p_show ==2||c_show ==2&&p_show ==0)
{
System.out.println("计算机赢了!");
}
else if(c_show == p_show)
System.out.println("平局");
else
System.out.println("玩家赢了!");
}
}
运行结果如下:
Top two:
使用抽象类代码如下:
import java.util.Scanner;
import java.util.Random;
public class example
{
public static void main(String []args)
{
ComputerPlayer c =new ComputerPlayer("computer");
PersonPlayer p = new PersonPlayer("player");
Game g = new Game(c,p);
g.start();
}
}
//定义抽象类Player,有其特定的格式
abstract class Player
{
String name;
abstract int show();
}
class ComputerPlayer extends Player
{
String name;
public ComputerPlayer(String name)
{
this.name = name;
}
int show()
{
int number=(int)(Math.random()*10) % 3;
return number;
}
}
class PersonPlayer extends Player
{
String name;
int shu;
public PersonPlayer(String name)
{
this.name = name;
}
int show()
{
int number;
number = 0;
System.out.println("0代表石头,1代表剪刀,2代表布");
System.out.println("请输入一个数:");
Scanner sc = new Scanner(System.in);
int shu = sc.nextInt();
System.out.println("玩家输入的是:"+shu);
System.out.println("计算机输入的为:"+number);
return shu;
}
}
class Game
{
ComputerPlayer computer;
PersonPlayer player;
public Game(ComputerPlayer computer,PersonPlayer player)
{
this.computer = computer;
this.player = player;
}
void start()
{
int c_show = computer.show();
int p_show = player.show();
if(c_show == 0&& p_show ==1||c_show == 1&&p_show ==2||c_show ==2&&p_show ==0)
{
System.out.println("计算机赢了!");
}
else if(c_show == p_show)
System.out.println("平局");
else
System.out.println("玩家赢了!");
}
}
Top three:
使用接口进行操作代码如下:
import java.util.Scanner;
import java.util.Random;
public class example
{
public static void main(String []args)
{
Computer c =new Computer("computer");
Player p = new Player("player");
Game g = new Game(c,p);
g.start();
}
}
class Computer
{
String name;
public Computer(String name)
{
this.name = name;
}
int show()
{
int number=((int)(Math.random()*10))%3;
return number;
}
}
class Player
{
String name;
int shu;
public Player(String name)
{
this.name = name;
}
int show()
{
int number=((int)(Math.random()*10))%3;
System.out.println("0代表石头,1代表剪刀,2代表布");
System.out.println("请输入一个数:");
Scanner sc = new Scanner(System.in);
int shu = sc.nextInt();
System.out.println("玩家输入的是:"+shu);
System.out.println("计算机输入的是:"+number);
return shu;
}
}
class Game
{
Computer computer;
Player player;
public Game(Computer computer,Player player)
{
this.computer = computer;
this.player = player;
}
void start()
{
int c_show = computer.show();
int p_show = player.show();
if(c_show == 0&& p_show ==1||c_show == 1&&p_show ==2||c_show ==2&&p_show ==0)
{
System.out.println("计算机赢了!");
}
else if(c_show == p_show)
System.out.println("平局");
else
System.out.println("玩家赢了!");
}
}
By lijia 2017.4.10