Share the source code of the stone scissors game implemented by JavaScript, and share the source code of javascript

Source: Internet
Author: User

Share the source code of the stone scissors game implemented by JavaScript, and share the source code of javascript

This game is mainly designed:

The first is the victory or defeat calculation.

Because the stone scissors cloth is cyclical
Stone scissors
Cut and kill cloth
Killing stones
Stone scissors
...
Find out the rule based on the above features and write out the algorithm.

Random computers

This is relatively easy. I have previously written an article to introduce it. If you don't understand it, you can check it out.

Screen Flushing

In fact, this effect is not the key to the game, but in order to look more interactive and fun, I will add it. A modulo algorithm is used here to show the result cyclically Based on the remainder.

Interface

Last code

<! DOCTYPE html> 

2. The following program is used as the basis for programming the stone scissors game (the Code Section for you to complete is marked with // do it youself in the Program)

Import java. util. Random;
Import java. util. collections;

Class shitou2 {
// ID of the scissors Matrix
Private static int SHITOU = 1;
Private static int JIANZI = 2;
Private static int BU = 3;

// Shengping negative ID
Private static int SHENG = 1;
Private static int PING = 2;
Private static int FU = 3;

Private static int SHENG_FU_FLAG = 0;

Public static void main (String [] args ){
Int man; // your punch
Int computer; // a computer punch
String wantplay; // whether to continue playing (Y/N)

Running in = new Processing (System. in );
System. out. println ("----------- stone scissors game -------------- \ n ");
Do {
// You have a punch
While (true ){
System. out. println ("what do you mean? (1 -- stone 2 -- scissors 3 -- cloth ):");
Man = in. nextInt ();
If (man! = 3 & man! = 2 & man! = 1 ){
System. out. println ("You are not making a punch. Please try again! \ N ");
Continue;
}

// Display your punch
System. out. print ("your output is ");

// Do it youself displays your punch
// Switch is recommended.
Switch (man ){
Case 1:
System. out. println ("Stone ");
Break;
Case 2:
System. out. println ("Scissors ");
Break;
Case 3:
System. out. println ("cloth ");
Break;
}

Break;
}
// Blow a finger at the computer
// Do it youself is recommended to use random Functions
Random random = new Random ();
Computer = random. nextInt () % 3 + 1;

// Display the computer's punch
System. out. print ("computer out ");
// Do it youself displays the computer's punch
// Switch is recommended.
Switch (computer ){
Case 1:
Sy ...... remaining full text>

A source code that uses java to implement the game "Stone scissors"

Import java. awt. BorderLayout;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;

Import javax. swing. JApplet;
Import javax. swing. JButton;
Import javax. swing. JLabel;
Import javax. swing. JOptionPane;
Import javax. swing. JPanel;

Public class Test extends JApplet {
Private static final long serialVersionUID = 1L;
JButton btnStone = new JButton ("Stone ");
JButton btnForfex = new JButton ("Scissors ");
JButton btnCloth = new JButton ("cloth ");
JLabel jl = new JLabel ();
JPanel jpl = new JPanel ();
Public void init (){
Jpl. add (jl, BorderLayout. NORTH );
Jpl. add (btnStone, BorderLayout. SOUTH );
Jpl. add (btnForfex, BorderLayout. SOUTH );
Jpl. add (btnCloth, BorderLayout. SOUTH );
This. getContentPane (). add (jpl );

BtnForfex. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
DoSFC ();
If (doSFC () = 1 ){
Jl. setText ("Stone ");
JOptionPane. showMessageDialog (null, "flat ");
}
If (doSFC () = 2 ){
Jl. setText ("Scissors ");
JOptionPane. showMessageDialog (null, "you win ");
}
If (doSFC () = 3 ){
Jl. setText ("cloth ");
JOptionPane. showMessageDialog (null, "You lost ");
}
}
});
BtnStone. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
DoSFC ();
If (doSFC () = 1 ){
Jl. setText ("Stone ");
JOptionPane. showMessageDialog (null, "you win ");
}
If (doSFC () = 2 ){
Jl. setText ("Scissors &... remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.