Talk:Main Page

From FANG Engine Sandbox

Jump to: navigation, search

package YourName.Nate;

import wiki.Wiki; import fang.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class Nate extends JPanel implements ActionListener { private static final int RED = 0; private static final int GREEN = 1; private static final int BLUE = 2; private static final int YELLOW = 3; private JComboBox c0, c1, c2, c3; private JLabel winlbl; private JButton b1; private JButton b2; private int answer[]; private int bulls; private int cows; private int cows2; private String s = "";

public Nate() { init(); bulls=0; cows=0; cows2=0; }

public void init() { answer = new int[4]; answer[0] = (int)(Math.random() * 4D); answer[1] = (int)(Math.random() * 4D); answer[2] = (int)(Math.random() * 4D); answer[3] = (int)(Math.random() * 4D);

for(int i = 0; i < 4; i++) System.out.print(answer[i] + " ");

System.out.println(); add(c0 = new JComboBox(new String[] {"blue","green","yellow","orange","red"})); add(c1 = new JComboBox(new String[] {"blue","green","yellow","orange","red"})); add(c2 = new JComboBox(new String[] {"blue","green","yellow","orange","red"})); add(c3 = new JComboBox(new String[] {"blue","green","yellow","orange","red"})); add(b1 = new JButton(" Continue ")); add(b2 = new JButton(" Exit ")); add(winlbl = new JLabel("")); b1.addActionListener(this); b2.addActionListener(this); }

public void actionPerformed(ActionEvent ae) { int guess[] = new int[4]; String[] temp = new String[4]; temp[0] = (String)c0.getSelectedItem(); temp[1] = (String)c1.getSelectedItem(); temp[2] = (String)c2.getSelectedItem(); temp[3] = (String)c3.getSelectedItem();



cows = 0; bulls = 0; cows2 = 0; for(int i=0; i < 4; i++) { if(temp[i].equals("red")) guess[i]=0; else if(temp[i].equals("green")) guess[i]=1; else if(temp[i].equals("blue")) guess[i]=2; else if(temp[i].equals("yellow")) guess[i]=3; }

System.out.println("temp[0] : "+temp[0]); System.out.println("temp[1] : "+temp[1]); System.out.println("temp[2] : "+temp[2]); System.out.println("temp[3] : "+temp[3]); System.out.println("answer[0] : "+answer[0]); System.out.println("answer[1] : "+answer[1]); System.out.println("answer[2] : "+answer[2]); System.out.println("answer[3] : "+answer[3]); System.out.println("guess[0] : "+guess[0]); System.out.println("guess[1] : "+guess[1]); System.out.println("guess[2] : "+guess[2]); System.out.println("guess[3] : "+guess[3]); System.out.println("\n+++++++++++++++++++++++++++++++++++++++");


for(int j=0; j<4; j++) { for(int k=0; k<4; k++){ if(answer[k]==guess[j]){ cows++; cows2++; break; } } } //s += temp[i] + " ";


if(guess[0]==answer[0]) { bulls++; cows2--; } if(guess[1]==answer[1]) { bulls++; cows2--; } if(guess[2]==answer[2]) { bulls++; cows2--; } if(guess[3]==answer[3]) { bulls++; cows2--; }

if(ae.getSource()==b1) { if(bulls != 4) { winlbl.setText(bulls + " bulls, " + cows + " cows" + " Number of matching colors " + cows2); } else{ winlbl.setText("YOU WIN!"); }


} if(ae.getSource()==b2){ System.exit(0); } }


public static void main(String [] args) { JFrame f = new JFrame("MasterMind"); f.getContentPane().add(new Nate()); f.setSize(500,200); f.setVisible(true); } }





Views
Personal tools