豆机 java_Java 7.21 游戏:豆机(C++&Java)

本文介绍了如何使用C++和Java实现豆机游戏。游戏中,用户输入球的数量和槽位数,程序通过随机算法模拟球的下落路径并展示。C++和Java代码分别展示了运行、显示路径和机器状态的方法。
摘要由CSDN通过智能技术生成

9297f3b9edc4a0d9aebb8e972a36bf00.png

5f58fa6ba2eeee3fdce622887c759ed4.png

PS:

难点在于,随机之后的分隔,理解就很容易了

注意:槽的奇偶情况

C++:

#include

#include

#include

using namespace std;

class Machine {

public:

Machine(int,int);

void run();

void show_machine();

void show_road();

private:

int ball;

int slot;

int *count;

string *road;

};

Machine::Machine(int b, int s) : ball(b),slot(s) {

count = new int[s];

road = new string[b];

for (int i = 0; i < slot; i++)

count[i] = 0;

}

void Machine::run() {

double mid;

srand((unsigned int)time(0)); //随机

for (int i = 0; i < ball; i++) {

mid = ((double)slot - 1) / 2; //取中间值(小技巧:这里刚好使得slot奇偶都符合要求)

for (int j = 0; j < slot - 1; j++) {

if (rand() % 2) { //右移

mid += 0.5;

road[i] += 'R';

}

else { //左移

mid -= 0.5;

road[i] += 'L';

}

}

count[(int)mid]++; //该列count++

}

}

void Machine::show_road() { //显示每个球的路径

for (int i = 0; i < ball; i++)

cout << road[i] << endl;

}

void Machine::show_machine() { //显示机器

int flag;

string str;

for (int i = ball; i > 0; i--) { //模拟二维数组,当count[j]超过i,证明此行此列存在值

flag = 0;

str.clear();

for (int j = 0; j < slot; j++) {

if (count[j] >= i) {

str += "O";

flag = 1;

}

else

str+=" ";

}

if (flag) //使用str以及flag为了去除多余的空行

cout << str << endl;

}

}

int main() {

int ball, slot;

cout << "Enter the number of balls to drop : ";

cin >> ball;

cout << "Enter the number of slots in the bean machine : ";

cin >> slot;

cout << endl;

Machine My(ball, slot);

My.run();

My.show_road();

cout << endl;

My.show_machine();

return 0;

}

Java:

import java.util.Scanner;

public class BeanMachine {

int ball,slot;

int[] slots;

String []road;

public BeanMachine(int b,int s) {

ball=b;slot=s;

slots=new int[s];

road=new String[b];

}

public void run(){

double mid;

for(int i=0;i

mid=((double)slot-1)/2;

road[i]=new String();

for(int j=0;j

if(Math.random()>0.5){

mid+=0.5;

road[i]+="R";

}

else{

mid-=0.5;

road[i]+="L";

}

}

slots[(int)mid]++;

}

}

public void ShowRoad(){

for (int i = 0; i < ball; i++)

System.out.println(road[i]);

}

public void ShowMachine(){

String str;

int flag;

for (int i = ball; i > 0; i--) { //模拟二维数组,当count[j]超过i,证明此行此列存在值

flag = 0;

str="";

for (int j = 0; j < slot; j++) {

if (slots[j] >= i) {

str += "O";

flag = 1;

}

else

str+=" ";

}

if (flag==1) //使用str以及flag为了去除多余的空行

System.out.println(str);

}

}

public static void main(String[] args) {

int ball,solt;

Scanner Input=new Scanner(System.in);

System.out.print("Enter the number of balls to drop : ");

ball=Input.nextInt();

System.out.print("Enter the number of slots in the bean machine : ");

solt=Input.nextInt();

BeanMachine My=new BeanMachine(ball,solt);

My.run();

My.ShowRoad();

My.ShowMachine();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值