Uproszczenie krótkiego programu

0

Witam bardzo serdecznie! Proszę o pomoc w wyjaśnieniu lub uproszczeniu gry Kamień Papier Nożyce. Przyznaje że mało wiem z Javy starałem się kod modyfikować samemu.

import java.io.*;

 class PapierNozyceKamien {
	 BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
	 public static void main(String[] args) {
		 PapierNozyceKamien kpn = new PapierNozyceKamien();
		 System.out.println("Siemanko, zagraj sobie!");
		 System.out.print("Ile rund chcesz grac : ");
		 int Runda = kpn.rundy();
		 int ja = 0;
		 int komp = 0;
		 for (int i = 0; i < Runda; i++) {
			 if (kpn.wybrany(i + 1))
				 ja++;
			 else
				 komp++;
		 }
		 kpn.wynik(ja, komp);
	 }
	
	 void wynik(int ja, int komp) {
		 System.out.println("wynik:"); 
		 System.out.println("Twoich wygranych : " + ja); 
		 System.out.println("Komputer wygrał : " + komp); 
		 if (ja > komp)
			 System.out.println("Wygrałeś z komputerem! Gratulacje!!!\n");
		 else
			 System.out.println("Komputer Cię pokonał. Przykro mi masz pecha...\n");
	 }
	
	 int rundy() {
		 try {
			 int temp = Integer.parseInt(BR.readLine());
			 if (temp % 2 == 0)
				 throw new Exception();
			 return temp;
		 }
		 catch (Exception err) {
			 System.out.print("Wybrałeś złą liczbę! Podaj ją ponownie. : ");
			 return rundy();
		 }
	 }
	
	 boolean wybrany(int val) {
		 System.out.print("[Runda " + val + "] kamień, papier czy nożyce? : ");
		 String input = "";
		 try {
			 input = BR.readLine();
		 }
		 catch(Exception err) {
		 }
		 if (input.equals("kamień"))
			 return process(1, val);
		 else if (input.equals("papier"))
			 return process(2, val);
		 else if (input.equals("nożyce"))
			 return process(3, val);
		 else {
			 System.out.println("Odpowiedź \"" + input + "\" została źle wpisana, wpisz ją dokładniej. \n");
			 return wybrany(val);
		 }
	 }
	
	 boolean process(int ja, int val) {
		 String[] hand = {"", "kamień", "papier", "nożyce"};
		 int komp = (int)(Math.random() * 3) + 1;
		 if (ja == komp) {
			 System.out.println("Komputer wybrał " + hand[ja] + ". Remis\n");
			 return wybrany(val);
		 }
		 else if (ja == 1 && komp == 3) {
			 System.out.println("Komputer wybrał " + hand[komp] + ". Wygrana! :)\n");
			 return true;
		 }
		 else if (ja == 2 && komp == 3) {
			 System.out.println("Komputer wybrał " + hand[komp] + ". Przegrałeś! :(\n");
			 return false;
		 }
		 else if (ja == 1 && komp == 2) {
			 System.out.println("Komputer wybrał " + hand[komp] + ". Przegrałeś! :(\n");
			 return false;
		 }
		 else if (ja == 3 && komp == 2) {
			 System.out.println("Komputer wybrał " + hand[komp] + ". Wygrana! :)\n");
			 return true;
		 }
		 else if (ja == 2 && komp == 1) {
			 System.out.println("Komputer wybrał " + hand[komp] + ". Wygrana! :)\n");
			 return true;
		 }
		 else {
			 System.out.println("Komputer wybrał " + hand[komp] + ". Przegrałeś! :(\n");
			 return false;
		 }
	 }
 }  

Bardzo was proszę

0

Ja bym zrobił tablicę z wynikami:
Papier Kamień Nożyce:

char wynik[][]={{0,1,-1},
{-1,0,1},
{1,-1,0}};
char P=0;
char K=1;
char N=2;

I potem sprawdzasz wynik:

w=wynik[P][N];

Jeśli w==0, to remis, w==-1 to gracz z papierem przegrywa, a jeśli w==1, to wygrywa.

Same sprawdzanie Stringa możesz w Javie 7 do Switcha skrócić. Ewentualnie użyć ENUMa:

enum T{
Papier, Kamien, Nozyce
};
String s="Papier";
//T wybor=T.valueOf(s);
int wybor=T.valueOf(s).ordinal(); //zwróci numerek
0

jest jeszcze prostszy sposób:

zakładamy: papier=1 kamień=2 nożyczki=3

int komputer;
int gracz;

if(komputer-gracz==0) remis
else if(komputer-gracz==1 || komputer-gracz==-2) gracz
else komputer 

1 użytkowników online, w tym zalogowanych: 0, gości: 1