public class pssEngine { private final String[] strWeapons = {"Paper","Stone","Scissors"}; private int wins = 0, loses = 0, draws = 0, cpuWeapon = 0; public void pssEngine () { } public String play (int playerWeapon) { cpuWeapon = getRandomNumber(); String strDisplay = strWeapons[playerWeapon] + " "; if (playerWeapon == cpuWeapon) { strDisplay += "draws"; } else if (playerWeapon == 0) //Paper { if (cpuWeapon == 1) //Stone { strDisplay += "covers"; } else //Scissors { strDisplay += "is cut by"; } } else if (playerWeapon == 1) //Stone { if (cpuWeapon == 0) //paper { strDisplay += "is covered by"; } else //Scissors { strDisplay += "breaks"; } } else //Scissors { if (cpuWeapon == 0) //Paper { strDisplay += "cuts"; } else //Stone { strDisplay += "is broken by"; } } updateResults(playerWeapon); strDisplay += " " + strWeapons[cpuWeapon]; return strDisplay; } private int getRandomNumber () { return (1 + (int) (Math.random() * 3)) - 1; } private void updateResults (int playerWeapon) { int[] paperGrid = {0,1,-1}; int[] stoneGrid = {-1,0,1}; int[] scissorGrid = {1,-1,0}; int[][] pssGrid = {paperGrid, stoneGrid, scissorGrid}; int result = pssGrid[playerWeapon][cpuWeapon]; if (result == 0) { draws++; } else if (result == -1) { loses++; } else { wins++; } } public void reset () { wins = loses = draws = cpuWeapon = 0; } public String[] getWeapon () { return strWeapons; } public String getWeapon (int index) { return strWeapons[index]; } public String getStrCPUWeapon () { return strWeapons[cpuWeapon]; } public int getCPUWeapon () { return cpuWeapon; } public int getWins () { return wins; } public int getLoses () { return loses; } public int getDraws () { return draws; } }