🔒 Closed Patulong po. Rock paper scissors

Status
Not open for further replies.

meta29

Enthusiast
Mga sir, hindi po ako Pro pero gusto ko mag improve. Patulong naman po kung may suggestion kayo sa dapat pang i improve sa simple game na code ko. Salamat po.

basic palang po ako sa java at walang proper schooling.
Code:
import java.util.Scanner;
class mechanics {
 
  String [] weapon = {"Rock","Paper","Scissors"};
  
  int [][] box  = {
  
   {1,2,0},
   {0,1,2},
   {2,0,1},
      
   };
 
  int uScore, pcScore = 0;
 
 
  void menu (){
    
    
    for( int i = 0; i < weapon.length; i++ ) {
  
      System.out.print( weapon[i] + ": " + i + " | " );
      
    
    }
  
      System.out.println();
 
  }
 
 
 
  void battle ( int user, int pc ){
    
    System.out.println(weapon[user] + " vs " + weapon[pc]);
    
    if ( box [user][user] > box [user][pc] ){
      
   System.out.println(weapon[user] + " beats " + weapon[pc]);
      System.out.println("User wins!");
      
      this.uScore++;
    }
    
    else if ( box [user][user] < box [user][pc] ) {
    
   System.out.println(weapon[pc] + " beats " + weapon[user]);
    System.out.println("Computer wins!");
  
    this.pcScore++;
    
   }
  
   else {
    
     System.out.println("Its a draw!");
    
   }
 
  }
 
 
  void score ( ){
 
    System.out.println();
    System.out.print("user: "+uScore);
    System.out.println(" | Opponent: "+pcScore);
    System.out.println();
    
  }
 
 
 
  boolean stopper ( int round){
  
   int x = this.uScore;
   int y = this.pcScore;
  
   boolean stop = true;
  
   int max = (round / 2 ) + 1;
  
  
   if ( x == max ^ y == max ){
    
    stop = false;
    
   }
  
   else if ((x > y && x + y == round) ||(y > x&& x + y == round)){
  
     stop = false;
    
    
   }
  
   else if (( x+1 == round && y == 0) || (y+1 == round && x == 0)){
    
    stop = false;
    
    
   }
   else if ( x == y && x + y == round){
    
     stop = false;
    
   }
  
   return stop;
 }


  int getUscore(){
    
    return this.uScore;
    
  }
 
  int getPcScore(){
    
   return this.pcScore;
  }
 



}

 

public class phcorner {

  public static void main(String[] args) {
  
 
  mechanics get = new mechanics();
   Scanner input = new Scanner (System.in);
 
   boolean loop = true;
  
   System.out.print("best of: ");
    int best = input.nextInt();
  
   get.menu();
   System.out.println("----------------------------------");
  
  while (loop){
 
 
   System.out.print("Choose your weapon!: ");
    int user = input.nextInt();   
 
   int pc = (int)(Math.random()*3);
   System.out.println("Opponent's weapon!: " + pc);
    
   System.out.println();
 
   get.battle(user,pc);
  
   get.score();
  
   loop =get.stopper(best);
  
   System.out.println("------------------------------");
 
  }
  
   int user = get.getUscore();
   int pc = get.getPcScore();
  
   if ( user > pc ){
    
     System.out.println("Champion! User");
   }
  else{
    System.out.println("Champion Computer! ");
  }
  
  
  
  
}
  }

ito po yung output

nshot_20200406_195804_com.duy.compiler.javanide.md.webp nshot_20200406_195825_com.duy.compiler.javanide.md.webp
 
Status
Not open for further replies.

About this Thread

  • 6
    Replies
  • 450
    Views
  • 3
    Participants
Last reply from:
meta29

Online now

Members online
1,024
Guests online
711
Total visitors
1,735

Forum statistics

Threads
2,276,969
Posts
28,973,413
Members
1,229,670
Latest member
Hyuga000
Back
Top