import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.util.Random;
//Maximum length of 1 char only
class JTextFieldLimit extends PlainDocument {
private int limit;
JTextFieldLimit(int limit) {
super();
this.limit = limit;
}
JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (str == null)
return;
if ((getLength() + str.length()) <= limit) {
super.insertString(offset, str, attr);
}
}
}
public class TwoFourSeven extends javax.swing.JApplet {
/** Initializes the applet TwoFourSeven */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jOptionPane1 = new javax.swing.JOptionPane();
jPanel1 = new javax.swing.JPanel();
box1 = new javax.swing.JTextField();
box2 = new javax.swing.JTextField();
box3 = new javax.swing.JTextField();
field1 = new javax.swing.JTextField();
field1.setDocument(new JTextFieldLimit(1));
takeButton = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
status = new javax.swing.JTextArea();
radio1 = new javax.swing.JRadioButton();
radio2 = new javax.swing.JRadioButton();
radio3 = new javax.swing.JRadioButton();
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setStub(null);
box1.setEditable(false);
box1.setFont(new java.awt.Font("Tahoma", 1, 48));
box1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
box1.setText("2");
box2.setEditable(false);
box2.setFont(new java.awt.Font("Tahoma", 1, 48));
box2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
box2.setText("4");
box3.setEditable(false);
box3.setFont(new java.awt.Font("Tahoma", 1, 48));
box3.setHorizontalAlignment(javax.swing.JTextField.CENTER);
box3.setText("7");
field1.setFont(new java.awt.Font("Tahoma", 1, 48));
field1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
field1.setScrollOffset(1);
takeButton.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
takeButton.setText("Get!");
takeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
takeButtonActionPerformed(evt);
}
});
status.setColumns(20);
status.setFont(new java.awt.Font("Tahoma", 0, 13));
status.setLineWrap(true);
status.setRows(5);
status.setText("Please subtract a number from one box.");
status.setWrapStyleWord(true);
jScrollPane2.setViewportView(status);
buttonGroup1.add(radio1);
radio1.setSelected(true);
radio1.setText("Box 1");
buttonGroup1.add(radio2);
radio2.setText("Box 2");
buttonGroup1.add(radio3);
radio3.setText("Box 3");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(box1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(box2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(box3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(radio3)
.addComponent(radio2)
.addComponent(radio1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(field1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(takeButton)
.addGap(58, 58, 58)))
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(box1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(box2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(box3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(radio1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(radio2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(radio3))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(11, 11, 11)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(takeButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(field1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(18, 18, 18)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
}// </editor-fold>
Random rand = new Random();
boolean set1Empty = false, set2Empty = false, set3Empty = false,
set1Selected = false, set2Selected = false, set3Selected = false;
int set1 = 2, set2 = 4, set3 = 7, whichSetRand, howManyRand,
update1, update2, update3, aiUpdate1, aiUpdate2, aiUpdate3;
int[] take = {0, 0, 0};
private void takeButtonActionPerformed(java.awt.event.ActionEvent evt) {
resetTake();
int selected = checkWhichSelected();
if(checkIfZero()) {
status.setText("You can\'t subtract from a number which is already zero");
return;
}
if(checkIfEmpty()) {
return;
} else {
loadTakeVars(selected, take);
}
if (checkIfOver())
return;
updateSets(take);
updateBoxText();
resetFieldText();
updateSetEmpty();
if(checkIfWon()) {
status.setText("You have won! Play again!");
return;
}
AI();
if(checkIfWon()) {
status.setText("From your move of " + update1 + "-" + update2 +
"-" + update3 + ", the computer turned it into " + aiUpdate1 +
"-" + aiUpdate2 + "-" + aiUpdate3 +". The computer has won! Play again!");
return;
}
}
public void AI() {
AIUpdate();
AISelect();
AITake();
updateSetEmpty();
AIResetSelected();
updateBoxText();
status.setText("From your move of " + update1 + "-" + update2 +
"-" + update3 + ", the computer turned it into " + set1 +
"-" + set2 + "-" + set3);
}
public void AIUpdate() {
update1 = set1;
update2 = set2;
update3 = set3;
}
public void AISelect() {
if(!set1Empty && !set2Empty && !set3Empty) {
whichSetRand = rand.nextInt(3) + 1;
switch(whichSetRand) {
case 1: set1Selected = true; break;
case 2: set2Selected = true; break;
case 3: set3Selected = true; break;
}
} else if(!set1Empty && !set2Empty && set3Empty) {
whichSetRand = rand.nextInt(2) + 1;
if(whichSetRand == 1) {
set1Selected = true;
} else {
set2Selected = true;
}
} else if(!set1Empty && set2Empty && !set3Empty) {
whichSetRand = rand.nextInt(2) + 1;
if(whichSetRand == 1) {
set1Selected = true;
} else {
set3Selected = true;
}
} else if(set1Empty && !set2Empty && !set3Empty) {
whichSetRand = rand.nextInt(2) + 1;
if(whichSetRand == 1) {
set2Selected = true;
} else {
set3Selected = true;
}
} else if(!set1Empty && set2Empty && set3Empty) {
set1Selected = true;
} else if(set1Empty && !set2Empty && set3Empty) {
set2Selected = true;
} else if(set1Empty && set2Empty && !set3Empty) {
set3Selected = true;
}
}
public void AITake() {
if(set1Selected) {
if(set2Empty && set3Empty) {
howManyRand = set1 - 1;
} else {
howManyRand = rand.nextInt(set1) + 1;
}
set1 -= howManyRand;
} else if(set2Selected) {
if(set1Empty && set3Empty) {
howManyRand = set2 - 1;
} else {
howManyRand = rand.nextInt(set2) + 1;
}
set2 -= howManyRand;
} else if(set3Selected) {
if(set1Empty && set2Empty) {
howManyRand = set3 - 1;
} else {
howManyRand = rand.nextInt(set3) + 1;
}
set3 -= howManyRand;
}
}
public void AIResetSelected() {
set1Selected = false;
set2Selected = false;
set3Selected = false;
}
public void resetTake() {
take[0] = 0;
take[1] = 0;
take[2] = 0;
}
public int checkWhichSelected() {
if(radio1.isSelected()) {
return 1;
} else if(radio2.isSelected()) {
return 2;
} else if(radio3.isSelected()) {
return 3;
}
return 1;
}
public boolean checkIfZero() {
if(radio1.isSelected()) {
if(set1Empty) {
return true;
}
} else if(radio2.isSelected()) {
if(set2Empty) {
return true;
}
} else if(radio3.isSelected()) {
if(set3Empty) {
return true;
}
}
return false;
}
public boolean checkIfEmpty() {
if(field1.getText().equals("")) {
status.setText("Please subtract atleast 1 from a number");
return true;
}
return false;
}
public boolean checkIfOver() {
if((set2 == 0 && set3 == 0) && (set1-take[0] < 1) ||
(set1 == 0 && set3 == 0) && (set2-take[1] < 1) ||
(set1 == 0 && set2 == 0) && (set3-take[2] < 1)) {
status.setText("You must leave atleast 1 in order to win");
return true;
}
return false;
}
public void loadTakeVars(int i, int[] take) {
switch(i) {
case 1:
take[0] = Integer.parseInt(field1.getText());
break;
case 2:
take[1] = Integer.parseInt(field1.getText());
break;
case 3:
take[2] = Integer.parseInt(field1.getText());
break;
}
}
public void updateSets(int[] take) {
set1 -= take[0];
set2 -= take[1];
set3 -= take[2];
if(set1 < 0)
set1 = 0;
if(set2 < 0)
set2 = 0;
if(set3 < 0)
set3 = 0;
}
public void updateBoxText() {
box1.setText(Integer.toString(set1));
box2.setText(Integer.toString(set2));
box3.setText(Integer.toString(set3));
}
public void resetFieldText() {
field1.setText("");
}
public void updateSetEmpty() {
if(set1 == 0)
set1Empty = true;
if(set2 == 0)
set2Empty = true;
if(set3 == 0)
set3Empty = true;
}
public boolean checkIfWon() {
if((set1 == 1 && set2 == 0 && set3 == 0) ||
(set1 == 0 && set2 == 1 && set3 == 0) ||
(set1 == 0 && set2 == 0 && set3 == 1)) {
aiUpdate1 = set1;
aiUpdate2 = set2;
aiUpdate3 = set3;
set1 = 2;
set2 = 4;
set3 = 7;
set1Empty = false;
set2Empty = false;
set3Empty = false;
box1.setText(Integer.toString(set1));
box2.setText(Integer.toString(set2));
box3.setText(Integer.toString(set3));
return true;
}
return false;
}
// Variables declaration - do not modify
private javax.swing.JTextField box1;
private javax.swing.JTextField box2;
private javax.swing.JTextField box3;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JTextField field1;
private javax.swing.JOptionPane jOptionPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JRadioButton radio1;
private javax.swing.JRadioButton radio2;
private javax.swing.JRadioButton radio3;
private javax.swing.JTextArea status;
private javax.swing.JButton takeButton;
// End of variables declaration
}