The answer rock-paper-scissors coding process 0912

The answer rock-paper-scissors coding process 0912

effect

Insert picture description here

Download brain map

https://cloud.189.cn/t/VJzu6jRvQ7Rb

Insert picture description here

01Create classes based on brain maps

Need to have classes

  • Player
  • Computer
  • Trial, referee

Create class

Insert picture description here

Three classes

A test file

Write the attributes and methods of the player class

Player class

public class Player {
    
    
    // 名字
    String name;
    // 得分
    int score;

    /**
     * 出拳方法
     */
    public void punches() {
    
    
        System.out.println("玩家出了一个拳");
    }
}

have a test

public class test {
    
    
    public static void main(String[] args) {
    
    
        // 测试开始

        // 玩家对象
        Player zs = new Player();
        zs.punches();
    }
}

result

玩家出了一个拳

Write attributes and methods of computer classes

The code is not much different from the code of the player class, so copy it directly

public class Computer {
    
    
    // 名字
    String name;
    // 得分
    int score;

    /**
     * 出拳方法
     */
    public void punches() {
    
    
        System.out.println("计算机出了一个拳");
    }
}

test

public class test {
    
    
    public static void main(String[] args) {
    
    
        // 测试开始

        // 玩家对象
        Player zs = new Player();
        zs.punches();

        // 计算机对象
        Computer pc = new Computer();
        pc.punches();
    }
}

result

玩家出了一个拳
计算机出了一个拳

Referee

definition

public class Referee {
    
    

    /**
     * 开始游戏
     * 用途:控制游戏流程
     */
    public void start() {
    
    
        System.out.println("让游戏开始吧");
    }
}

test

public class test {
    
    
    public static void main(String[] args) {
    
    
        // 测试开始

        // 玩家对象
        Player zs = new Player();
        zs.punches();

        // 计算机对象
        Computer pc = new Computer();
        pc.punches();

        // caipan对象
        Referee cp = new Referee();
        cp.start();
    }
}

result

玩家出了一个拳
计算机出了一个拳
让游戏开始吧

Source code 01

https://cloud.189.cn/t/7VRnyaMvMvAj

Video 01

https://www.ixigua.com/6871813006473298440?logTag=1kWy5BjWbX2BTRb5vH5d2

02 Realizing the start method of refereeing

Brain map
Insert picture description here

Clear the messy code in the test class

Only retain the instantiation of the judgment and the call of the start method

Test whether the results can be output normally

Write the execution process in the start method

Write the placeholder code first

/**
 * 裁判类
 */
public class Referee {
    
    

    /**
     * 开始游戏
     * 用途:控制游戏流程
     */
    public void start() {
    
    
        // 初始化选手
        System.out.println("玩家对象登场");
        System.out.println("电脑对象登场");

        // 获取出拳
        System.out.println("玩家出拳");
        System.out.println("电脑出拳");

        // 计数器变化
        System.out.println("计数器变化");

        // 胜负判断
        System.out.println("胜负判断");

        // 是否要继续
        System.out.println("要继续吗?");
    }
}

effect

玩家对象登场
电脑对象登场
玩家出拳
电脑出拳
计数器变化
胜负判断
要继续吗?

Video 02

https://www.ixigua.com/6871834964221133326?logTag=DP0I_im3_7KDCTTaWt3OD

03 Realize the punch between the player and the computer-the player

Player punches

    /**
     * 出拳方法
     */
    public String punches() {
    
    
        System.out.println("请出拳:1-剪刀,2-石头,3-布(输入相应数字):");
        String quan = ipt.next();
        System.out.println("你出拳" + quan);
        return quan;
    }

The player enters through the keyboard and punches

The quan variable receives keyboard input

String type

return the result of this entry

have a test

Data can be received normally

Insert picture description here

However the demand is

Insert picture description here

Enter 2 to prompt the stone

So there is still a lack of printing operation from number to string

Consider using branch statements to complete

Video 03

04 Realize the punch between the player and the computer-computer

The computer's method of punching

The computer uses random numbers to punch

Randomly out 1 2 3 three data

Write a random small function first

Test whether 1 2 3 data generation can be achieved

Insert picture description here

Generate a number in the computer punch method

    public int punches() {
    
    
        Random random = new Random();
        int randNum = random.nextInt(3) + 1;
        return randNum;
    }

Correct the data type of the player's punch

The computer outputs an integer

The player gives a string

Data types should be unified

So turn the return value of the player's punch into an integer return

Video 04

https://www.ixigua.com/6871846165554725390?logTag=-GFlBc25qOnbCxvX8jHd3

05 Number to string

Thinking, how to output here?

If the punch is 2, it outputs rock, if the punch is 1, it outputs scissors

Can consider it:

Multiple branch

Simple and rude writing


        // 需要,把1转换成剪刀,2转换成石头,3转换成布进行输出
        // 使用一个粗旷的方法来实现
        // 玩家出拳的转换
        if (player1Quan == 1) {
    
    
            System.out.println("你出拳:剪刀");
        } else if (player1Quan == 2) {
    
    
            System.out.println("你出拳:石头");
        } else if (player1Quan == 3) {
    
    
            System.out.println("你出拳:布");
        }
        // 电脑出拳的转换
        if (computer1Quan == 1) {
    
    
            System.out.println("电脑出拳:剪刀");
        } else if (computer1Quan == 2) {
    
    
            System.out.println("电脑出拳:石头");
        } else if (computer1Quan == 3) {
    
    
            System.out.println("电脑出拳:布");
        }
        System.out.println("虽然功能实现了,但是代码太罗嗦了,可以优化");

Video 05

https://www.ixigua.com/6871849136673522183?logTag=Zf5bwygPjJUJlZ8W5p4cS

06 Winner Judgment

Insert picture description here

Use branches to handle three situations

  • Tie situation
  • Victory
  • The other is failure

Video 06

https://www.ixigua.com/6871853339307409927?logTag=m7mTVIFUSjvOZ85QnXhoK

07 The game loops

Insert picture description here
After a game, ask if the next game

If the user enters y, continue the game

do{
    
    反复的游戏}while(键盘输入是否是y);

Video 07

https://www.ixigua.com/6871856414227169799?logTag=fBvij9VaaSASNdbjoKzKj

08 Adjust display

  • Counter change
  • Player fixed name
  • Choose computer name
  • Show player vs computer before punching
  • do...while loop to while loop
  • Scoring processing

Code

import java.util.Scanner;

/**
 * 裁判类
 */
public class Referee {
    
    

    /**
     * 开始游戏
     * 用途:控制游戏流程
     */
    public void start() {
    
    
        // scanner
        Scanner ipt = new Scanner(System.in);
        // 初始化选手
        Player player1 = new Player();
        // 玩家名字默认为王子
        player1.name = "王子";

        // 初始化电脑
        Computer computer1 = new Computer();
        // 给电脑起名字
        System.out.println("请选择对方角色(1 刘备,2 孙权,3 曹操)");
        int nameNum = ipt.nextInt();
        String computer1Name = "";
        switch (nameNum) {
    
    
            case 1:
                computer1Name = "刘备";
                break;
            case 2:
                computer1Name = "孙权";
                break;
            case 3:
                computer1Name = "曹操";
                break;
        }
        computer1.name = computer1Name;


        // scanner对象
        // 是否继续
        String is_continue;
        // 计数器
        int counter = 0;

        // 宣布对战
        System.out.println(player1.name + "  VS  " + computer1.name + "  对战");

        // 游戏循环:出拳,评判,是否再玩,再出拳,再评判,再问是否玩,do...while


        // do...while 转while....
        // 是否要继续
        System.out.println("要继续吗?y-继续");
        is_continue = ipt.next();

        while ("y".equals(is_continue)) {
    
    
            // 获取出拳,需要有返回值,用于提供裁判进行比较
            int player1Quan = player1.punches();
            int computer1Quan = computer1.punches();

            // 需要,把1转换成剪刀,2转换成石头,3转换成布进行输出
            // 使用一个粗旷的方法来实现
            // 玩家出拳的转换
            if (player1Quan == 1) {
    
    
                System.out.println("你出拳:剪刀");
            } else if (player1Quan == 2) {
    
    
                System.out.println("你出拳:石头");
            } else if (player1Quan == 3) {
    
    
                System.out.println("你出拳:布");
            }
            // 电脑出拳的转换
            if (computer1Quan == 1) {
    
    
                System.out.println("电脑出拳:剪刀");
            } else if (computer1Quan == 2) {
    
    
                System.out.println("电脑出拳:石头");
            } else if (computer1Quan == 3) {
    
    
                System.out.println("电脑出拳:布");
            }

            // 计数器变化
            counter++;

            // 胜负判断
            if (computer1Quan == player1Quan) {
    
    
                // 平局情况
                System.out.println("天哪,平局了");

            } else if (player1Quan == 2 && computer1Quan == 1 || player1Quan == 1 && computer1Quan == 3 || player1Quan == 3 && computer1Quan == 2) {
    
    
                // 胜利情况
                System.out.println("玩家胜利,电脑失败");
                player1.score += 1;
            } else {
    
    
                // 失败情况
                System.out.println("电脑胜利,玩家失败");
                computer1.score += 1;
            }

            // 是否要继续
            System.out.println("要继续吗?y-继续");
            is_continue = ipt.next();

            // 换行
            System.out.println();
            System.out.println();

        }
        ;

        System.out.println("一共比赛的次数:" + counter);
        System.out.println("玩家得分:" + player1.score);
        System.out.println("电脑得分:" + computer1.score);
    }
}

Video 08

https://www.ixigua.com/6871861896690172424?logTag=0CaNWCd2OZz_j0uU9iiSk

Source file 08

https://cloud.189.cn/t/iYfI3iyQzi63

Description

This method is not optimal

In order to facilitate understanding, it is very detailed

When the coding process is thoroughly understood

Can be written in a better way

summary

This project mainly exercises how to enrich a complex project

Seeing such a troublesome project, the brain may be very big

But you have to learn to analyze, then disassemble, and then fill in the blanks.

No matter how difficult things are, you can divide and conquer

Have encountered structural design errors during the coding process

For example, the structure of the while loop should be used, and the result is the do...while... structure

But does not affect

We only need to adjust the code nearby

Guess you like

Origin blog.csdn.net/ifubing/article/details/108545389