±Û¾´ÀÌ :¾çÁöÇö 1999³â 12¿ù 09ÀÏ 14:44:53
ÇÁ·Î±×·¥ÀÌ ÀüüÀûÀ¸·Î À߸øµÈ°Í °°Àºµ¥¿ä
Àß ¸ð¸£°Ú¾î¿ä Á» µµ¿ÍÁÖ¼¼¿ä ·¹Æ÷Æ®°Åµç¿ä
¼±¼ö°ú¸ñÀ» ¾Èµè°í µé¾î¼ ´õ Çì¸Þ°í ÀÖ½À´Ï´Ù ½Ã°£µÇ½Ã¸é °íÃļ Á¦°Ô À̸áÀ» Áֽøé
°¨»ç°¨»ç ÇÏ°ÚÁö¸¸....
¾ÕÀ¸·Î ÀÚ¹Ù°øºÎ¿½ÉÈ÷ÇÒ¶ó±¸¿ä µµ¿Í ÁÖ½Ã¸é °¨»çµå¸±²²¿ä
import java.io.*;
import java.util.*;
//main()ÇÔ¼ö¸¦ °®°í ÀÖÀ¸¸ç ±×¾È¿¡¼ Àüü°ÔÀÓÀ» ÁÖ°üÇϴ Ŭ·¡½º
//Çѹø°ÔÀÓÀ» ¼öÇàÇÒ¶§¸¶´Ù °ÔÀÓÀÇ ³¡¿¡°¡¼ °è¼Ó ¿©ºÎ¸¦ ¹°¾î »ç¿ëÀÚ°¡ ÁßÁö¸¦ ¿øÇÒ¶§±îÁö °è¼Ó°ÔÀÓ¼öÇà.
public class SRPPlay {
public static void main(String[] args) {
HumanPlayer hp = new HumanPlayer();
ComputerPlayer cp = new ComputerPlayer();
boolean keep = true;
static finalMax_Game = 20;//ÃÖ´ë °ÔÀÓ¼ö
int try_num = 0; //ÇöÀç±îÁöÀÇ °ÔÀÓ È½¼ö
int [Max_Game]choice = 0;//ÄÄÇ»ÅÍ¿¡ ¼±ÅÃÀ» ÀúÀå
string ChoiceToString(int choice);//¼±ÅûçÇ×À» ÁÖ¸é ½ºÆ®¸µÀ¸·Î ¹Ù²Ù¾î ¹Ýȯ
void print (string s, int i);//ÇöÀç ÄÄÇ»ÅÍ°¡ ³½ °á°ú¸¦ ȸ鿡 Ãâ·Â
int make_choice();
//°¡À§¹ÙÀ§º¸(¸Þ¼¼Áö Ãâ·Â °¡À§´Â1 ¹ÙÀ§´Â2 º¸´Â 3ÀÔ´Ï´Ù)Áß Çϳª¸¦ ¼±ÅÃÇÏ¿© ¹Ýȯ(return)
while(keep && try_num++ < Max_Game) {
System.out.println();
int hpNum = hp.makeChoice();
int cpNum = cp.makeChoice();
hp.print("Human", hpNum);
cp.print("Computer", cpNum);
switch(hpNum) {
case 1:
if(cpNum == 1) {
System.out.println("A drawn game.");
} else if(cpNum == 2) {
System.out.println("You win.");
} else if(cpNum == 3){
System.out.println("You lose.");
score += 5;
}
break;
case 2:
if(cpNum == 2) {
System.out.println("A drawn game.");
} else if(cpNum == 1) {
System.out.println("You win.");
} else if(cpNum == 3){
System.out.println("You lose.");
score += 5;
}
break;
case 3:
if(cpNum == 3) {
System.out.println("A drawn game.");
} else if(cpNum == 2) {
System.out.println("You win.");
} else if(cpNum == 1){
System.out.println("You lose.");
score += 5;
}
break;
default:
System.out.println("A drawn game.");
}
char ch;
System.out.print("continue(EXIT: n continue:¾Æ¹«Å°³ª) ? ");
StringBuffer str = new StringBuffer();
try {
while((ch = (char)System.in.read()) != '\n') {
str.append(ch);
}
} catch(IOException e) {
System.err.println("IOException : " + e);
}
if(str.charAt(0) == 'n'){
break;
}
}//end of while
System.out.println("´ç½ÅÀÇ Á¡¼ö´Â " + score + "Á¡ ÀÔ´Ï´Ù.");
}//end of main
}
public class AbstractPlayer{
static final Max_game = 20 // ÃÖ´ë°ÔÀÓ¼ö
int try_num = 0; //ÇöÀç±îÁöÀÇ °ÔÀÓȽ¼ö
int[Max_Game]choice; // »ç¶÷ÀÇ ¼±Åà ÀúÀå
String ChoiceToString(int choice); //¼±ÅûçÇ×À» ÁÖ¸é ½ºÆ®¸µÀ¸·Î ¹Ù²Ù¾î ¹Ýȯ
void print(String s,int i); //ÇöÀç »ç¶÷ÀÌ ³½ °á°ú¸¦ ȸ鿡 Ãâ·Â
int make_choice(); //°¡À§¹ÙÀ§º¸Áß ÇÏ´À¸¦ »ç¶÷¿¡¼ ¼±ÅÃÇÏ°Ô ÇÑÈÄ ±×°á°ú¸¦ ¹Ýȯ
}
public class HumanPlayer extends AbstractPlayer {
//°¡À§¹ÙÀ§º¸Áß Çϳª¸¦ »ç¶÷¿¡¼ ¼±ÅÃÇÏ°Ô ÇÑÈÄ ±×°á°ú¸¦ ¹Ýȯ(return)
public int makeChoice() {
BufferedReader stdin = new BufferedReader
(new InputStreamReader(System.in));
try {
System.out.print("1.°¡À§ 2.¹ÙÀ§ 3.º¸ ... ? ");
int choice = Integer.parseInt(stdin.readLine());
}//try
catch(IOException exception){
System.out.println("Á˼ÛÇÕ´Ï´Ù!! Error°¡ ¹ß»ýÇÏ¿´½À´Ï´Ù.");
} // catch
int choice = str.charAt(0) - 48;
if(try_num < Max_Game) {
choices[try_num] = choice;
}
try_num =+ 1;
return choice;
}
}
public class ComputerPlayer extends AbstractPlayer {
public int makeChoice() {
Random ran = new Random((int) System.currentTimeMillis());
int choice = Math.abs(ran.nextInt()) % 3 + 1;
if(try_num < Max_Game) {
choices[try_num] = choice;
}
try_num =+ 1;
return choice;
}
}