Have some interesting things happening while looping through my RPS program to achieve a program run count of 100. Trying to figure it out without asking for help or looking online. I had also based an equation on total count but have rem'ed it out to concentrate on the loop.
Here's my code (excuse pragmatics, I am concentrating on concepts and debugging, not readability.)
import java.util.*;
public class AdvancedRPSLoopCounter {
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<101;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;
}
if (player2.equals("scissors")) {
result="Player 2 Loses";
p2lose = p2lose +1;
}
}
if (player1.equals("paper")) {
if (player2.equals("rock")) {
result="Player 2 Loses";
p2lose = p2lose +1;
}
if (player2.equals("scissors")) {
result="Player 2 Wins";
p2win = p2win +1;
}
}
if (player1.equals("scissors")) {
if (player2.equals("rock")) {
result="Player 2 Wins";
p2win = p2win +1;
}
if (player2.equals("paper")) {
result="Player 2 Loses";
p2lose = p2lose +1;
}
}
/* Print to standard output per game result, print game counter */
System.out.println(result);
System.out.println("Counter: " + i);
System.out.println(" ");
}
/* Print total ties, wins and loses */
/* p1win = i - (p2lose + tie); */
/* p1lose = i - (p2win + tie); */
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);
}
}
The output from my program is as follows:
Counter: 100
Number of ties: 39
Player 2 Wins: 34
Player 2 Losses: 28
Some interesting things to note. When I go to execution start, the first line count begins at 5. The program is only looping 96 times.
Will get back later when I figure out the solution or break and ask for help . . . .
7 comments:
I have created another small program to function as a loop and counter by itself to see how the loop functions while the counter counts real numbers. It is pretty self explanatory. Set the loop to less than 16, it starts at 0 and goes to 15, for a total of 16 cycles. Counter hits 16.
Going back to the main program to continue to decipher.
Here is the LoopCounter code:
public class LoopAndCounter {
public static void main(String args[]) {
int counter = 0;
for (int i=0;i<16;i++) {
counter = counter + 1;
System.out.println("\nCounter: \t" + counter);
System.out.println("Loop: \t \t" + i);
}
}
}
I took a break from working on RPS to mess around with the loops and have pretty much hacked up the original program (and assigned duties). Thankfully have a saved version before messing with the loops. Back to it! Cam has asked I provide a running copy of RPS that will keep track of wins, losses and ties.
Had a session with Bob Kelly who brought up a valid point and eventually the answer to the question of why the counter 'seemed' to begin to start counting at 5. It did not. We decreased the number of loops from 101 (i<101) to 61 (i<61), compiled and ran and scrolled to the top of the screen. The counter started at 0, ran to 60 for a total of 61 cycles.
Seems there is nothing wrong or fishy with the program at all, but an issue with the screen formatting of DOS with increased screen outputs. This is HUGE! Hours and hours of confusion were just saved thinking about things in a different light.
Thanks Bob!
So, I just ran the simple loop/counter program by itself to see what the screen output limitations of the command console are. My loop/counter program has three lines of output per cycle. It seems the 'top' of the program begins to get chopped off with a loop and counter of 100.
Therefore, I would deduce that the command console only supports approximately 300 lines of output before it starts deleting the first lines of output. The more the output, the more gets deleted at the top.
I just ran the program and here's what I got:
Number of ties: 37
Player 1 Wins: 0
Player 1 Loses: 0
Player 2 Wins: 39
Player 2 Loses: 25
So, I'm not sure if you updated the code in the original post, but it looks like that adds up to 101, which is the number of iterations you would expect from your loop.
It was the command console limitations that I was having difficulties with, thinking it was an issue with the loop. Very important thing to know . . . .
You're blaming the command console? I'm not buying that one!
Post a Comment