I have been assigned the task of coding the RPS (Rock/Paper/Scissors) game. As Cam stated, it ties some of the most important elements, logics and concepts of programming together.
I am brand new at the semantics and pragmatics of Java, JSP (Java Server Page) and OOP (Object Oriented Programming), but my past experience is turning out to be a real shining star. Many of the the concepts are the same and the if and for syntax statements are exactly the same. I already understand variable declaration, nesting, loops and passing data.
Trying to type out as much code as possible without blasting out to Google and cutting and pasting existing code. I am finding the whole bracketing concept quite easy to logically segregate different functions, BUT THEY ARE EVERYWHERE in the code!!! My most prevalent compiling error so far has been parsing errors.
So, as of right now, with the help of Cam supplying concepts and syntax where needed, the first copy of a standalone version of RPS is completed, along with a loop of 3 times and a counter. I am about to tear up the code and try and loop for a greater number of runs, randomize the user input (player 1 = server, player 2 = user) to allow for non-user input and begin keeping track of the number of wins, the number of losses and the number of ties.
1 comments:
My finalized RPS project:
import java.util.*;
public class AdvancedRPSRandom {
public static void main(String args[]) {
// Variable declarations
String player1 = null;
String player2 = null;
int p1win = 0;
int p1lose = 0;
int p2win = 0;
int p2lose = 0;
int tie = 0;
String result = null;
// Loop one hundred times
for (int i=0;i<100;i++) {
// Create player 1 absolute random 0 - 2
Random r1 = new Random();
int randomInt1 = Math.abs(r1.nextInt() % 3);
// Assign player 1 variable
if(randomInt1 == 0) {
player1="rock";
}
if(randomInt1 == 1) {
player1="paper";
}
if(randomInt1 == 2) {
player1="scissors";
}
// Create player 2 absolute random 0 - 2
Random r2 = new Random();
int randomInt2 = Math.abs(r2.nextInt() % 3);
// Assign player 2 variable
if(randomInt2 == 0) {
player2="rock";
}
if(randomInt2 == 1) {
player2="paper";
}
if(randomInt2 == 2) {
player2="scissors";
}
// Tie Condition
if (player1.equals(player2)) {
result="Tie";
tie = tie +1;
}
// Win/Lose Conditions
if (player1.equals("rock")) {
if (player2.equals("paper")) {
result="Player 2 Wins";
p2win = p2win +1;
p1lose = p1lose +1;
}
if (player2.equals("scissors")) {
result="Player 2 Loses";
p2lose = p2lose +1;
p1win = p1win +1;
}
}
if (player1.equals("paper")) {
if (player2.equals("rock")) {
result="Player 2 Loses";
p2lose = p2lose +1;
p1win = p1win +1;
}
if (player2.equals("scissors")) {
result="Player 2 Wins";
p2win = p2win +1;
p1lose = p1lose +1;
}
}
if (player1.equals("scissors")) {
if (player2.equals("rock")) {
result="Player 2 Wins";
p2win = p2win +1;
p1lose = p1lose +1;
}
if (player2.equals("paper")) {
result="Player 2 Loses";
p2lose = p2lose +1;
p1win = p1win +1;
}
}
// Print to standard output per game result, print game counter
System.out.println(result);
System.out.println("Counter: " + (i+1));
System.out.println(" ");
}
// Print total ties, wins and loses
System.out.println("\n \t Number of ties: " + tie);
System.out.println("\n \t Player 1 Wins: " + p1win);
System.out.println("\n \t Player 1 Loses: " + p1lose);
System.out.println("\n \t Player 2 Wins: " + p2win);
System.out.println("\n \t Player 2 Loses: " +p2lose);
Post a Comment