jeudi 25 décembre 2014

For Statement With Randomiser

My application was working perfectly until I basically re-did a bunch of the code to add more features. The game is basically a randomizer telling you your job in the game, because you don't want others knowing your job. There is Murderer, Judge, Backup-Judge, and Innocent. I created a admin panel to make sure certain features are working. This version of the app supports 5-8 players. In the app there is an admin function so if you only have 5 players you can click a button and it will tell you whether or not the main jobs, Murderer, And Judge(or Backup Judge incase the main judge dies or the randomiser selects this before the judge) are in the first 5 strings. Because if it is not, you must re-run the application until you can play correctly. So i have a feature that checks it it will work for: 5 players, 6 players, 7 players, and 8 players.


UPDATE - Sorry: The main problem is that it is no longer setting the variable to: job1, job2, etc. When you run the code and it opens the window, and you click the button to see the first variable, it will display it, when you click it 2 times to get to the next one, its blank. When using the admin panel and clicking on 8 Players, it will only say their is 1 of the xx position. xx is whatever job is the first variable/job1. It only displays if its murderer, judge, co-judge/backup-judge.


Here is the code:



package main;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {

private static JFrame window = new JFrame();

private static JPanel one = new JPanel();
private static JPanel two = new JPanel();
private static JPanel three = new JPanel();

//1-4 Players
private static JLabel job1 = new JLabel();
private static JLabel job2 = new JLabel();
private static JLabel job3 = new JLabel();
private static JLabel job4 = new JLabel();
private static JLabel job5 = new JLabel();
private static JLabel job6 = new JLabel();
private static JLabel job7 = new JLabel();
private static JLabel job8 = new JLabel();
private static JLabel jobprint = new JLabel();
private static JLabel apcheck = new JLabel();

private static JButton next = new JButton("Click To Find Your Position");
private static JButton ap = new JButton("Admin Panel");

private static JButton five = new JButton("Five Players");
private static JButton six = new JButton("Six Players");
private static JButton seven = new JButton("Seven Players");
private static JButton eight = new JButton("Eight Players");
private static JButton apexit= new JButton("Exit Admin Panel");

private static int countertwo = 0;
private static int counter = 0;
private static int m5 = 0;
private static int j5 = 0;
private static int m6 = 0;
private static int j6 = 0;
private static int m7 = 0;
private static int j7 = 0;
private static int m8 = 0;
private static int j8 = 0;
private static int cj = 0;



public static void shuffleArray(String[] a) {
int n = a.length;
Random random = new Random();
random.nextInt();
for (int i = 0; i < n; i++) {
int change = i + random.nextInt(n - i);
swap(a, i, change);
}
}

private static void swap(String[] a, int i, int change) {
String helper = a[i];
a[i] = a[change];
a[change] = helper;
}

public static void main(String[] args) {
String[] a = new String[] {"Murderer", "Judge", "Backup Judge", "Innocent", "Innocent", "Innocent", "Innocent", "Innocent"};
shuffleArray(a);

next.setVisible(true);
job1.setVisible(false);
job2.setVisible(false);
job3.setVisible(false);
job4.setVisible(false);


for (String i : a) {
System.out.println(i);
counter++;
if(counter == 1){
job1.setText(i);
if(job1.getText() == "Murderer"){
m5++;
}
if(job1.getText() == "Judge"){
j5++;
}
if(job1.getText() == "Backup Judge"){
cj++;
}
}
if(counter == 2){
job1.setText(i);
if(job2.getText() == "Murderer"){
m5++;
}
if(job2.getText() == "Judge"){
j5++;
}
if(job2.getText() == "Backup Judge"){
cj++;
}
}
if(counter == 3){
job1.setText(i);
if(job3.getText() == "Murderer"){
m5++;
}
if(job3.getText() == "Judge"){
j5++;
}
if(job3.getText() == "Backup Judge"){
cj++;
}
}
if(counter == 4){
job1.setText(i);
if(job4.getText() == "Murderer"){
m5++;
}
if(job4.getText() == "Judge"){
j5++;
}
if(job4.getText() == "Backup Judge"){
cj++;
}
}
if(counter == 5){
job1.setText(i);
if(job5.getText() == "Murderer"){
m5++;
}
if(job5.getText() == "Judge"){
j5++;
}
if(job5.getText() == "Backup Judge"){
cj++;
}
}
if(counter == 6){
job1.setText(i);
if(job6.getText() == "Murderer"){
m6++;
}
if(job6.getText() == "Judge"){
j6++;
}
if(job6.getText() == "Backup Judge"){
cj++;
}
}
if(counter == 7){
job1.setText(i);
if(job7.getText() == "Murderer"){
m7++;
}
if(job7.getText() == "Judge"){
j7++;
}
if(job7.getText() == "Backup Judge"){
cj++;
}
}
if(counter == 8){
job1.setText(i);
if(job8.getText() == "Murderer"){
m8++;
}
if(job8.getText() == "Judge"){
j8++;
}
if(job8.getText() == "Backup Judge"){
cj++;
}
}
}
apexit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
five.setVisible(false);
six.setVisible(false);
seven.setVisible(false);
eight.setVisible(false);
apcheck.setVisible(false);
}
});

ap.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
five.setVisible(true);
six.setVisible(true);
seven.setVisible(true);
eight.setVisible(true);
apcheck.setVisible(true);
// hide.setVisible(true);
}
});
five.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(j5 + m5 == 2){
apcheck.setText("Murderer And Judge Assigned");
System.out.println("Murderer & Judge Positions - ASSIGNED - 5 Players");
}else if(j5 + m5 != 2){
apcheck.setText("Murderer And Judge *NOT* Assigned - *RE-RUN*");
System.out.println("Murderer & Judge Positions - NOT ASSIGNED - 5 Players");
}
}
});
six.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(j6 + m6 + j5 + m5 == 2){
apcheck.setText("Murderer And Judge Assigned");
System.out.println("Murderer & Judge Positions - ASSIGNED - 6 Players");
}else if(j6 + m6 != 2){
apcheck.setText("Murderer And Judge *NOT* Assigned - *RE-RUN*");
System.out.println("Murderer & Judge Positions - NOT ASSIGNED - 6 Players");
}
}
});
seven.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(j7 + m7 + j6 + m6 + j5 + m5 == 2){
apcheck.setText("Murderer And Judge Assigned");
System.out.println("Murderer & Judge Positions - ASSIGNED - 7 Players");
}else if(j7 + m7 + j6 + m6 + j5 + m5 != 2){
apcheck.setText("Murderer And Judge *NOT* Assigned - *RE-RUN*");
System.out.println("Murderer & Judge Positions - NOT ASSIGNED - 7 Players");
}
}
});
eight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(j8 + m8 + j7 + m7 + j6 + m6 +j5 + m5 + cj == 3){
apcheck.setText("Murderer And Judge Assigned");
System.out.println("Murderer & Judge Positions - ASSIGNED - 8 Players");
}else if(j8 + m8 + j7 + m7 + j6 + m6 +j5 + m5 + cj!= 3){
apcheck.setText("Murderer And Judge *NOT* Assigned - *RE-RUN*");
System.out.println("ERROR: FIX CODE!");
System.out.println("CJ: " + cj + " J: " + (j5 + j6 + j7 + j8) + " M: " + (m5 + m6 + m7 + m8) + "**");
}
}
});

next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(countertwo == 0){
jobprint.setVisible(true);
jobprint.setText(job1.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 1){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 2){
jobprint.setText(job2.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 3){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 4){
jobprint.setText(job3.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 5){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 6){
jobprint.setText(job4.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 7){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 8){
jobprint.setText(job5.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 9){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 10){
jobprint.setText(job6.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 11){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 12){
jobprint.setText(job7.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 13){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 14){
jobprint.setText(job8.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 15){
jobprint.setText("By Fletcher Henneman");
next.setVisible(false);
}
}
});


window.setLayout(new GridLayout(5, 1));
window.setSize(500,400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);

window.add(one);
window.add(two);
window.add(three);

one.setLayout(new GridLayout(3, 4));
two.setLayout(new GridLayout(3, 4));

one.add(jobprint);

two.add(next);
two.add(ap);

two.add(five);
two.add(six);
two.add(seven);
two.add(eight);
three.add(apcheck);
three.add(apexit);

five.setVisible(false);
six.setVisible(false);
seven.setVisible(false);
eight.setVisible(false);

window.setVisible(true);
}
}




Aucun commentaire:

Enregistrer un commentaire