ini adalah contoh game suit cina di program java :
package suitkomputer;
import java.io.*;
/**
*
* @author hidayat
*/
public class Main {
public static void main(String[] args) {
System.out.println("Suit Gunting Kertas Batu");
System.out.println("------------------------");
System.out.println("1. G untuk Gunting");
System.out.println("2. K untuk Kertas");
System.out.println("3. B untuk batu");
System.out.println("");
InputStreamReader istream = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (istream);
String input = null;
boolean inputvalid = false;
int computer = 0;
int player = 0;
while(!inputvalid){
System.out.print("Tolong Suit yang benar (G/K/B) = ");
try {
input =br.readLine();
} catch (IOException ioe){
System.out.println("Error pada IO....System Shut down");
System.exit(1);
}
if(input.length()>0);
switch (input.charAt(0)){
case 'g':
case 'G':
player = 0;
inputvalid = true;
break;
case 'k':
case 'K':
player = 1;
inputvalid = true;
break;
case 'B':
case 'b':
player = 2;
inputvalid = true;
break;
}
}
computer = (int)(Math.random()*3);
if (computer == 3){
computer = 2;
}
switch (computer){
case 0:
System.out.println("computer = Gunting");
switch (player){
case 0:
System.out.println("seri");
break;
case 1:
System.out.println("anda Kalah");
break;
case 2:
System.out.println("anda menang");
break;
}
break;
case 1:
System.out.println("Computer = Kertas");
switch (player){
case 0:
System.out.println("anda menang");
break;
case 1:
System.out.print("Seri");
break;
case 2:
System.out.println("anda kalah");
break;
}
break;
case 2:
System.out.println("komputer = Batu");
switch (player){
case 0:
System.out.println("anda kalah");
break;
case 1:
System.out.println("anda menang");
break;
case 2:
System.out.println("seri");
break;
}
break;
}
}
}
terima kasih
semoga bermanfaat
1 komentar:
Thank You Mas Bro....
Posting Komentar