📄 bullsandcows.java.bak
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.lang.*;class gamePanel extends JPanel{ JPanel p= new JPanel(); JButton b1=new JButton("1"); JButton b2=new JButton("1"); JButton b3=new JButton("1"); JButton b4=new JButton("1"); JButton b5=new JButton("GO"); public gamePanel() { makePanel(); } void makePanel() { p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS)); p.setBackground(Color.RED); p.add(b1); p.add(b2); p.add(b3); p.add(b4); p.add(b5); b1.setBackground(new Color(255,255,255)); b2.setBackground(new Color(255,255,255)); b3.setBackground(new Color(255,255,255)); b4.setBackground(new Color(255,255,255)); b1.setFocusable(false); b2.setFocusable(false); b3.setFocusable(false); b4.setFocusable(false); p.setVisible(false); }}//The main classpublic class BullsAndCows implements ActionListener{ GameInterface frame = new GameInterface(); gamePanel gp[]=new gamePanel[10]; int turn = 0; String code= new String(""); int guess [][]= new int [4][2]; int secret[][] = new int [4][2]; int cows=0,bulls=0; public static void main(String args[]) { BullsAndCows obj= new BullsAndCows(); obj.drawGame(); obj.init(); } void init() { int i; for(i=0;i<10;i++) { gp[i].b1.addActionListener(this); gp[i].b2.addActionListener(this); gp[i].b3.addActionListener(this); gp[i].b4.addActionListener(this); gp[i].b5.addActionListener(this); } frame.NewGameButton.addActionListener(this); frame.Instructions.addActionListener(this); } void drawGame() { int i; frame.setTitle("Bulls And Cows --- By Ankit Kumar & Ankush Jhalani "); frame.setSize(500,500); frame.setResizable(false); for(i=0;i<10;i++) { gp[i]=new gamePanel(); gp[i].p.setBounds(30, 55 + (40*i),215,30); gp[i].b5.setBounds(190,0,55,30); frame.getContentPane().add(gp[i].p); } frame.show(); } public void actionPerformed(ActionEvent e) { String s=e.getActionCommand(); Object source = e.getSource(); if(s.charAt(0)>='1' && s.charAt(0)<'6') { int num=Integer.parseInt(s); if(source==gp[turn].b1) gp[turn].b1.setText(String.valueOf(num+1)); if(source==gp[turn].b2) gp[turn].b2.setText(String.valueOf(num+1)); if(source==gp[turn].b3) gp[turn].b3.setText(String.valueOf(num+1)); if(source==gp[turn].b4) gp[turn].b4.setText(String.valueOf(num+1)); } if(s.charAt(0)=='6') { if(source==gp[turn].b1) gp[turn].b1.setText(String.valueOf(1)); if(source==gp[turn].b2) gp[turn].b2.setText(String.valueOf(1)); if(source==gp[turn].b3) gp[turn].b3.setText(String.valueOf(1)); if(source==gp[turn].b4) gp[turn].b4.setText(String.valueOf(1)); } if(s.equals("GO")) { processNextTurn(); } if(s.equals("New Game")) { startNewGame(); } if(s.equals("Instructions")) { showInstructions(); } } void processNextTurn() { if(turn<=9) { turn++; gp[turn-1].b1.setEnabled(false); gp[turn-1].b2.setEnabled(false); gp[turn-1].b3.setEnabled(false); gp[turn-1].b4.setEnabled(false); gp[turn-1].b5.setEnabled(false); if(turn<10) { gp[turn].p.setVisible(true); } } guess[3][0]=Integer.parseInt(gp[turn-1].b1.getText()); guess[2][0]=Integer.parseInt(gp[turn-1].b2.getText()); guess[1][0]=Integer.parseInt(gp[turn-1].b3.getText()); guess[0][0]=Integer.parseInt(gp[turn-1].b4.getText()); getCowsAndBulls(); drawCowsAndBulls(); if (bulls==4) { JOptionPane.showMessageDialog(frame,"Congratulations!!!\nYou have cracked the code","Congratulations",JOptionPane.INFORMATION_MESSAGE); if(JOptionPane.showConfirmDialog(frame, "Do you want to start a new game?", "Wanna play again?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) startNewGame(); else gp[turn].p.setVisible(false); } if(turn==10 && bulls!=4) { JOptionPane.showMessageDialog(frame,"You could not the find the code in 10 turns. The code was "+ code,"You Lost!!!",JOptionPane.INFORMATION_MESSAGE); if(JOptionPane.showConfirmDialog(frame, "Do you want to start a new game?", "Wanna play again?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) startNewGame(); } System.out.println("Your guess was\n"); for(int j=0;j<4;j++) { System.out.println(guess[3-j][0]); secret[j][1]=0; guess[j][1]=0; guess[3-j][0]=1; } cows=0; bulls=0; } void showInstructions() { JOptionPane.showMessageDialog(frame," Bulls And Cows\n"+"-----------------------------------------------------------------\n"+"The objective of the game is to guess the secret code.\n"+"You have 10 chances to correctly guess the code.\n"+"You start with making a guess about the number\n"+"If the position of a digit in the guessed number\n"+"matches with that of the secret code you have hit a bull\n"+"If a digit of yours matches another digit in the secret code\n"+"but not in the same corresponding position,you hit a cow\n"+"Otherwise you don't hit anything. Complete the game by \nguessing "+"the secret code in the minimum number of tries \npossible. You can have a maximum of 10 tries per code.","Instructions",JOptionPane.PLAIN_MESSAGE,new ImageIcon("cow.gif")); } void startNewGame() { turn=0; for(int i=0;i<10;i++) { for(int j=0;j<4;j++) { frame.ResultLabel[i][j].setIcon(null); } } for(int i=0;i<10;i++) { gp[i].p.setVisible(false); gp[i].b1.setText("1"); gp[i].b2.setText("1"); gp[i].b3.setText("1"); gp[i].b4.setText("1"); gp[i].b1.setEnabled(true); gp[i].b2.setEnabled(true); gp[i].b3.setEnabled(true); gp[i].b4.setEnabled(true); gp[i].b5.setEnabled(true); } gp[turn].p.setVisible(true); secret[3][0]=Integer.parseInt(Long.toString(Math.round(Math.random() * 5 + 1))); secret[2][0]=Integer.parseInt(Long.toString(Math.round(Math.random() * 5 + 1))); secret[1][0]=Integer.parseInt(Long.toString(Math.round(Math.random() * 5 + 1))); secret[0][0]=Integer.parseInt(Long.toString(Math.round(Math.random() * 5 + 1))); code=String.valueOf(secret[3][0])+String.valueOf(secret[2][0])+String.valueOf(secret[1][0])+String.valueOf(secret[0][0]); proccode(); } void drawCowsAndBulls() { int i; for(i=0;i<bulls;i++) frame.ResultLabel[turn-1][i].setIcon(new ImageIcon("bull.gif")); for(int j=i;j<cows+bulls;j++) frame.ResultLabel[turn-1][j].setIcon(new ImageIcon("cow.gif")); } void getCowsAndBulls() { for(int i=0;i<4;i++) { if(guess[i][0]==secret[i][0]) { bulls++; secret[i][1]=1; guess[i][1]=1; } } for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(guess[i][1]==0) { if(secret[j][1]==0) { if(guess[i][0]==secret[j][0]) { cows++; guess[i][1]=1; secret[j][1]=1; } } } } } System.out.println("bulls are " + bulls +"\n cows are " + cows); } void proccode() { int i; System.out.println("The secret code is " + code); for(i=0;i<4;i++) { System.out.println(secret[3-i][0]); secret[3-i][1]=0; guess[i][1]=0; guess[i][0]=0; } } }class GameInterface extends JFrame{ JButton NewGameButton = new JButton("New Game"); JButton Instructions = new JButton("Instructions"); JLabel ResultLabel[][] = new JLabel[10][4]; public GameInterface() { makeFrame(); addGUI(); } void makeFrame() { getContentPane().setLayout(null); getContentPane().setBackground(new Color(102,102,255)); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitGame(evt); }} ); } void exitGame(WindowEvent e) { System.exit(0); }void addGUI(){ showResultLabel(); NewGameButton.setBounds(30,10,100,30); getContentPane().add(NewGameButton); Instructions.setBounds(150,10,120,30); getContentPane().add(Instructions); }void showResultLabel(){ for(int i=0;i<10;i++) { for(int j=0;j<4;j++) { ResultLabel[i][j]= new JLabel(); ResultLabel[i][j].setBounds(330+j*31, 49 + (40*i),50,40); ResultLabel[i][j].setBackground(new Color(255,255,255)); getContentPane().add(ResultLabel[i][j]); } }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -