/**
* @time 2011-9-22 上午9:45:36
* @author Rookie
* @version 猜拳游戏 v1.0
* @function 游戏玩家接口
*
*/
package com.finger_guess_game;
public interface Player
{
public String name = "匿名";
public int score = 0;
//出拳
public String giveFist();
}
/**
* @time 2011-9-22 上午9:47:45
* @author Rookie
* @version 猜拳游戏 v1.0
* @function 电脑玩家类,实现了玩家接口
*
*/
package com.finger_guess_game;
import java.util.Random;
import java.util.Scanner;
public class ComputerPlayer implements Player
{
private String name = "匿名";
private int score = 0;
public ComputerPlayer()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getScore()
{
return score;
}
public void setScore(int score)
{
this.score = score;
}
@Override
// 出拳并返回出拳结果
public String giveFist()
{
Random random1 = new Random();
int i = random1.nextInt(3) + 1;
String s = "";
if (i == 1)
{
System.out.println(getName() + "出拳:剪刀");
s = "剪刀";
return s;
}
if (i == 2)
{
System.out.println(getName() + "出拳:石头");
s = "石头";
return s;
}
if (i == 3)
{
System.out.println(getName() + "出拳:布");
s = "布";
return s;
}
return s;
}
}
/**
* @time 2011-9-22 上午9:51:18
* @author Rookie
* @version 猜拳游戏 v1.0
* @function 用户玩家类,实现了玩家接口
*
*/
package com.finger_guess_game;
import java.util.Scanner;
public class UserPlayer implements Player
{
private String name = "匿名";
private int score = 0;
//用户结束标志
private static boolean finishFlag = false;
public UserPlayer()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getScore()
{
return score;
}
public void setScore(int score)
{
this.score = score;
}
public static boolean isFinishFlag()
{
return finishFlag;
}
public static void setFinishFlag(boolean finishFlag)
{
UserPlayer.finishFlag = finishFlag;
}
@Override
// 出拳并返回出拳结果
public String giveFist()
{
System.out.println();
System.out.println("请出拳,1、剪刀 2、石头 3、布 : ,输入1,2,3以外的数字表示结束游戏");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
String s = "";
if (i == 1)
{
System.out.println(getName() + "出拳:剪刀");
s = "剪刀";
return s;
}
if (i == 2)
{
System.out.println(getName() + "出拳:石头");
s = "石头";
return s;
}
if (i == 3)
{
System.out.println(getName() + "出拳:布");
s = "布";
return s;
}
// 如果输入除1,2, 3 之外的数字表示结束游戏
else
{
finishFlag = true;
return s;
}
}
}
/**
* @time 2011-9-22 上午9:45:36
* @author Rookie
* @version 1.0
* @function
*
*/
package com.finger_guess_game;
import java.util.Scanner;
public class FingerGuessGame
{
private static UserPlayer userPlayer;
private static ComputerPlayer computerPlayer;
private static int drawTimes = 0;
private static int userScore = 0;
private static int computerScore = 0;
// 游戏次数
private static int gameTimes = 0;
// 用户每次出拳结果
static String eachUserGiveFistResult = "";
// 计算机每次出拳结果
static String computerEachFistResult = "";
// 构造方法
public FingerGuessGame()
{
System.out
.println("---------------------欢 迎 进 入 游 戏 世 界---------------------");
System.out.println();
System.out
.println(" ***************** ");
System.out
.println(" *** 猜拳,开始 *** ");
System.out
.println(" ***************** ");
System.out.println();
System.out.println();
System.out.println("出拳规则: 1、剪刀 2、石头 3、布");
System.out.println("请选择角色(1、孙权 2、刘备 3、曹操):");
System.out.println();
userPlayer = new UserPlayer();
computerPlayer = new ComputerPlayer();
}
// 游戏选择角色
public void chooseRole()
{
int user_role_flag = 0;
Scanner input = new Scanner(System.in);
user_role_flag = input.nextInt();
if (user_role_flag == 1)
{
userPlayer.setName("孙权");
System.out.println("用户角色为:孙权");
}
if (user_role_flag == 2)
{
userPlayer.setName("刘备");
System.out.println("用户角色为:刘备");
}
if (user_role_flag == 3)
{
userPlayer.setName("曹操");
System.out.println("用户角色为:曹操");
}
System.out.println();
}
// 一次对战
public static void aGame()
{
boolean isOver = false;
// 用户出拳并返回用户每一次出拳的结果
while (isOver == false)
{
eachUserGiveFistResult = userPlayer.giveFist();
// 如果你输入的是1,2, 3 以后的数字,表示要结束游戏则: 显示结果 ,初始化,然后退出
if (UserPlayer.isFinishFlag())
{
isOver = true;
showResult();
initialize();
System.exit(0);
} else
{
// 计算机出拳
computerEachFistResult = computerPlayer.giveFist();
gameTimes++;
System.out.println("第" + gameTimes + "次游戏结果为: ");
getEachGameResult();
// System.out.println("gameTimes:" +gameTimes);
}
}
}
// 初始化
public static void initialize()
{
// 设置游戏次数为0,积分为0
userPlayer = null;
computerPlayer = null;
}
// 游戏开始
public static void start()
{
// 创建游戏
FingerGuessGame newGame = new FingerGuessGame();
// 选择用户角色
newGame.chooseRole();
// 对战一次
aGame();
showResult();
}
// 得到每一个游戏的结果,返回结果
public static String getEachGameResult()
{
// 判断输赢
// 如果出拳相同,则平手,各加一分
if (eachUserGiveFistResult.equals(computerEachFistResult))
{
userScore = userScore + 0;
computerScore = computerScore + 0;
drawTimes++;
userPlayer.setScore(userScore);
computerPlayer.setScore(computerScore);
System.out.println("平手,各加0分");
System.out.println("用户成绩为:" + userPlayer.getScore());
System.out.println("电脑成绩为:" + computerPlayer.getScore());
}
// 如果出拳不相同
if (!eachUserGiveFistResult.equals(computerEachFistResult))
{
// 如果用户出拳为剪刀,分两种情况
if (eachUserGiveFistResult.equals("剪刀"))
{
// 如果计算机出拳为石头,则计算机赢,计算机score得一分
if (computerEachFistResult.equals("石头"))
{
computerScore += 1;
computerPlayer.setScore(computerScore);
System.out.println(computerPlayer.getName() + "赢");
System.out.println("用户成绩为:" + userPlayer.getScore());
System.out.println("电脑成绩为:" + computerPlayer.getScore());
}
// 如果计算机出拳为布,则用户赢,用户score得一分
else
{
userScore += 1;
userPlayer.setScore(userScore);
System.out.println(userPlayer.getName() + "赢");
System.out.println("用户成绩为:" + userPlayer.getScore());
System.out.println("电脑成绩为:" + computerPlayer.getScore());
}
}
// 如果用户出拳为石头,分两种情况
if (eachUserGiveFistResult.equals("石头"))
{
// 如果计算机出拳为布,则计算机赢,计算机score得一分
if (computerEachFistResult.equals("布"))
{
computerScore += 1;
computerPlayer.setScore(computerScore);
System.out.println(computerPlayer.getName() + "赢");
System.out.println("用户成绩为:" + userPlayer.getScore());
System.out.println("电脑成绩为:" + computerPlayer.getScore());
}
// 如果计算机出拳为剪刀,则用户赢,用户score得一分
else
{
userScore += 1;
userPlayer.setScore(userScore);
System.out.println(userPlayer.getName() + "赢");
System.out.println("用户成绩为:" + userPlayer.getScore());
System.out.println("电脑成绩为:" + computerPlayer.getScore());
}
}
// 如果用户出拳为布,分两种情况
if (eachUserGiveFistResult.equals("布"))
{
// 如果计算机出拳为剪刀,则计算机赢,计算机score得一分
if (computerEachFistResult.equals("剪刀"))
{
computerScore += 1;
computerPlayer.setScore(computerScore);
System.out.println(computerPlayer.getName() + "赢");
System.out.println("用户成绩为:" + userPlayer.getScore());
System.out.println("电脑成绩为:" + computerPlayer.getScore());
}
// 如果计算机出拳为石头,则用户赢,用户score得一分
else
{
userScore += 1;
userPlayer.setScore(userScore);
System.out.println(userPlayer.getName() + "赢");
System.out.println("用户成绩为:" + userPlayer.getScore());
System.out.println("电脑成绩为:" + computerPlayer.getScore());
}
}
System.out.println();
}
String result = userPlayer.getName() + " : " + userPlayer.getScore()
+ " vs " + computerPlayer.getName() + " : "
+ computerPlayer.getScore();
return result;
}
// 显示结果
public static void showResult()
{
System.out.print("游戏结果: ");
if (userPlayer.getScore() == computerPlayer.getScore())
{
System.out.println("平手");
}
if (userPlayer.getScore() > computerPlayer.getScore())
{
System.out.println("用户赢:");
}
if (userPlayer.getScore() > computerPlayer.getScore())
{
System.out.println("计算机赢");
}
System.out.println();
System.out.println("详细信息如下: ");
System.out.println("对战次数: " + gameTimes);
System.out.println("用户:" + userPlayer.getName() + " 得分为: "
+ userPlayer.getScore());
System.out.println("计算机:" + computerPlayer.getName() + "得分为 : "
+ computerPlayer.getScore());
System.out.println("用户:" + userPlayer.getName() + "赢"
+ userPlayer.getScore() + "次");
System.out.println("用户:" + userPlayer.getName() + "输"
+ (gameTimes - drawTimes - userPlayer.getScore()) + "次");
System.out.println("平手次数为: " + drawTimes);
}
}
/**
* @time 2011-9-22 上午11:03:32
* @author Rookie
* @version 猜拳游戏 v1.0
* @function 测试类 游戏入口类
*
*/
package com.finger_guess_game;
public class GameTest
{
public static void main(String[] args)
{
FingerGuessGame.start();
}
}
类实现的有点乱,因为中间出现了问题,后来时间仓促,很多地方又重新改了一些,违背了很多原来的初衷,所以代码看起来有点乱。