じゃんけんゲームは、対象向けを利用して
4426 ワード
:
1) , , , , , , 。
2) 。 。
:
Java
:
1) , : (Player) : (Computer)
2) : , , 。
3) , 。
package FingerGuess;
/**
* Description:
*
* @Author Fann
* @Data 2018/12/8
*/
public class Constant {
public static final int ROCK = 0;//A
public static final int SCISSORS = 1;//B
public static final int PAPER = 2;//C
}
package FingerGuess;
import java.util.Random;
import java.util.Scanner;
/**
* Description:
*
* @Author Fann
* @Data 2018/12/8
*/
public class GuessFingerGame {
public int score_Player; //
public int score_Computer;//
Scanner in = new Scanner( System.in ); //
Random random = new Random( ); //
// , .
public void gameStart() throws Exception {
System.out.println(" :-----------------");
for (int i = 0; i < 3; i++) {
System.out.println(" "+i+" ( 0 , 1 , 2 ):");
int player = in.nextInt();
int computer = random.nextInt(3); //0-2
if(computer == Constant.ROCK){
System.out.println(" :0( )");
}else if(computer == Constant.SCISSORS){
System.out.println(" :1( )");
}else{
System.out.println(" :2( )");
}
int result = compare( player,computer );
// , .
if(result == 1){ // ,
score_Player++;
}else if(result == -1){ // ,
score_Computer++;
}
}
// , .
gameEnd();
}
public void showScore(){
System.out.print("player :"+score_Player+" :"+score_Computer);
}
/**
*
* 1 0 -1
*/
public int compare(int player,int computer) throws Exception {
//
if(player < 0 || player > 2 || computer < 0 || computer > 2){
throw new Exception( " " );
}
if(player == Constant.ROCK){ //
if(computer == Constant.ROCK){
return 0; // .
}else if(computer == Constant.PAPER){
return -1; // ,
}else{
return 1;
}
}
if(player == Constant.PAPER){ //
if(computer == Constant.ROCK){
return 1; // ,
}else if(computer == Constant.PAPER){
return 0; // , .
}else{
return -1;
}
}
if(player == Constant.SCISSORS){ //
if(computer == Constant.SCISSORS){
return 0; // , .
}else if(computer == Constant.ROCK){
return -1; // , -1.
}else{
return 1;
}
}
return 2;
}
/**
* , .
*/
public void gameEnd(){
System.out.println(" :-------------");
if(score_Computer > score_Player){
System.out.println(" "+score_Computer+" , "+score_Player+" , !!");
}else if(score_Player == score_Computer){
System.out.println(" "+score_Computer+" , "+score_Player+" , !!");
}else{
System.out.println(" "+score_Computer+" , "+score_Player+" , !!");
}
}
}
package FingerGuess;
import java.util.Scanner;
/**
* Description:
*
* @Author Fann
* @Data 2018/12/8
*/
public class Main {
public static void main(String[] args) throws Exception {
//
Scanner in = new Scanner( System.in );
while (true){
GuessFingerGame game = new GuessFingerGame();
game.gameStart();
System.out.println(" y/n?");
if(in.nextLine().equalsIgnoreCase( "n" )){
break;
}
}
System.out.println(" ");
}
}