| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2054 人关注过本帖
标题:编了一个拿金币的小游戏 请大家多多指教
只看楼主 加入收藏
pauli
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-2-2
收藏
 问题点数:0 回复次数:15 
编了一个拿金币的小游戏 请大家多多指教
游戏规则是这样的:桌上有一堆金币(30-40枚),玩家和电脑先后从桌上取走金币,最多可以取走3枚,最少取1枚,不能不取,通过猜金币的正反来决定先后顺序,最后桌上留下的最后一枚金币轮到谁取,谁就输了。

PS 我是初学者,接触JAVA才两个星期,请大家指教。

import java.util.Random;
import java.util.Scanner;

public class Playgames
{
    static int coinscope, result;
    static int coinNum;
    static String side;
    public static String selection;
    static int i;
    static int c;
    static int d;
    static int e;
    static int f;
    static int g = 0;
   
    public static void main(String[] args)   
    {   

        Scanner input = new Scanner(System.in);
        
        coinNum = new Random().nextInt(10)+30;
        System.out.println("\nThe number of coins is " + coinNum);
        c = coinNum;
        
        System.out.println("\nPlease choose the side of the coin:(1.head or 2.tail) ");
        int Num = input.nextInt();
        
        while(Num>2 | Num<1)
        {
            System.out.println("\nInvaild selection, please choose 1 or 2");
            Num = input.nextInt();
        }
            
            if(Num == 1)
            {
                selection="head";
            }
            else if(Num == 2)
            {
                selection="tail";
            }
            System.out.println("\nThe side you chose is " + selection);
            
            int RandomNum = new Random().nextInt(2)+1;
        
            if(RandomNum == 1)
            {
                side="head";
            }
            else if(RandomNum == 2)
            {
                side="tail";
            }
            System.out.print("\nThe side of coin is " + side);
            
            if(selection==side)
            {
                result = 1;
            }
            else
            {
                result =2;
            }
                        
            if(coinNum<=33&&coinNum>29)
            {
                i=8;
            }
            else if(coinNum<=37&&coinNum>33)
            {
                i=9;
            }
            else
            {
                i=10;
            }
            
        switch (result)
            {   
                case 1:   
                    
                    System.out.print("\n\nYou have chosen the right side, please take the coins");
                    
                    while(c != 1)
                    {               
                        g++;
                        System.out.println("\nRound "+g);
                        System.out.println("\nPlease take coins (1-3): ");
                        d = input.nextInt();
                        
                        while(d>3|d<0)
                        {
                            System.out.println("\nYou can not take more than 3 coins or less than 1 coin, please take coins again.");
                            System.out.println("Please take coins (1-3): ");
                            d = input.nextInt();
                        }
                        
                        c-=d;
                        System.out.println("You took " + d + "coins, there are " + c + "coins left");
                        e = 4*i - 3;
                        if(c<e&&e>1)
                        {
                            i-=2;
                            f = c - e;
                            c-=f;
                            System.out.println("The user took " + f + " coins, there are "+ c +" coins left.");
                        }
                        else if(c>e&&e>1)
                        {
                                f = c - e;
                                c-=f;
                                i--;                        
                                System.out.println("The user took " + f + " coins, there are "+ c +" coins left.");
                        }
                        else if(c==e&&e>4)
                        {
                            f = new Random().nextInt(3)+1;
                            c-=f;
                            i--;
                            System.out.println("The user took " + f + " coins, there are "+ c +" coins left.");
                        }
                        else if(c<=4&&c>1)
                        {
                            f=c-1;
                            g++;
                            c=1;
                            System.out.println("The user took " + f + " coins, there are "+ c +" coins left.");
                        }
                        else if(c==1)
                        {
                            System.out.println("\n1 coin left for user to take.\nCongratulations, you win.");
                            return;
                        }
                    }
                    System.out.println("\nYou lose, there is only 1 coin left for you to take.");
                    break;
               
                case 2:
                    
                    System.out.print("\nSorry, wrong side, please wait...");
                    
                    while(c != 1)
                    {               
                        
                        e = 4*i - 3;
                        

                        if(c<e&&e>1)
                        {   
                            i-=2;
                            f = c - e;
                            c-=f;
                           
                            g++;
                            System.out.println("\nRound "+g);
                            System.out.println("The user took " + f + " coins, there are "+ c +" coins left.");
                                                        
                            System.out.println("\nPlease take coins (1-3): ");
                            d = input.nextInt();
                            c-=d;
                            System.out.println("You took " + d + "coins, there are " + c + "coins left");
                           
                            while(d>3|d<0)
                            {
                                System.out.println("\nYou can not take more than 3 coins or less than 1 coin, please take coins again.");
                                System.out.println("Please take coins (1-3): ");
                                d = input.nextInt();
                            }
                        }
                        else if(c>e&&e>1)
                        {   
                            f = c - e;
                            c-=f;
                            i--;

                            g++;
                            System.out.println("\nRound "+g);
                            System.out.println("The user took " + f + " coins, there are "+ c +" coins left.");
                                                        
                            System.out.println("\nPlease take coins (1-3): ");
                            d = input.nextInt();
                            c-=d;
                            System.out.println("You took " + d + "coins, there are " + c + "coins left");
                           
                            while(d>3|d<0)
                            {
                                System.out.println("\nYou can not take more than 3 coins or less than 1 coin, please take coins again.");
                                System.out.println("Please take coins (1-3): ");
                                d = input.nextInt();
                            }
                        }
                        else if(c==33|c==37|c==e&&e>4)
                        {
                            f = new Random().nextInt(3)+1;
                            c-=f;
                            i--;
                            g++;
                            System.out.println("\nRound "+g);
                            System.out.println("\nThe user took " + f + " coins, there are "+ c +" coins left.");
                            c-=d;
                            System.out.println("\nPlease take coins (1-3): ");
                            d = input.nextInt();
                            while(d>3|d<0)
                            {
                                System.out.println("\nYou can not take more than 3 coins or less than 1 coin, please take coins again.");
                                System.out.println("Please take coins (1-3): ");
                                d = input.nextInt();
                            }
   
                        }
                        else if(c<=4&&c>1)
                        {
                            f=c-1;
                            c=1;
                            g++;
                            System.out.println("\nRound "+g);
                            System.out.println("The user took " + f + " coins, there are "+ c +" coins left.");
                            System.out.println("\n You lose, there is only 1 coin left for you to take.");
                            return;
                    }
                    System.out.println("\n1 coin left for user to take.\nCongratulations, you win.");
                    break;
            }
    }
}


[ 本帖最后由 pauli 于 2010-2-4 17:59 编辑 ]
搜索更多相关主题的帖子: 金币 指教 小游戏 
2010-02-04 17:55
fgh1987168
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-2-14
收藏
得分:0 
试试看哦~~
2010-02-17 23:04
nicole1998
Rank: 1
等 级:新手上路
威 望:1
帖 子:29
专家分:5
注 册:2007-5-21
收藏
得分:0 
1:整体看main函数写的有点太长了,可以尝试使用不同的function(),这样读起来更简明一些
2:没有方法的类是没有意义的
3:构造函数是必需的
4:数据类型权限问题
5:封装问题
2010-03-04 03:26
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:0 
感觉楼主以前学习过C/C++,JAVA也可以用面向过程编程,不过面向过程的程序很难扩展和维护,建议用面向对象方法来做

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2010-03-04 08:33
llooppzhang
Rank: 7Rank: 7Rank: 7
来 自:江苏
等 级:黑侠
威 望:5
帖 子:308
专家分:518
注 册:2009-10-18
收藏
得分:0 
是的,代码有点长了
2010-03-06 16:04
vecomwa
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:63
专家分:138
注 册:2009-9-1
收藏
得分:0 
import java.util.Random;
import java.util.Scanner;


public class YellowBoy {
    int or = 0;
    Random ran = new Random();
    Scanner input = new Scanner(System.in);
    int jinbi =30+ran.nextInt(11);//随机生成一个在30至40之间的数
   
    public static void main(String args[]) {
        new YellowBoy().start();
    }
   
    public void start() {
        System.out.println("========================================");
        System.out.println("本次金币数为:"+jinbi);
        System.out.println("========================================");
        System.out.println("是否现在开始抛金币:");
        System.out.println("选择 1:是      选择2:否");
        System.out.println("请输入1或者2");
        System.out.println("========================================");
        int c = 5;
        or = input.nextInt();
        if(or==1) {
            c = select();
            while(jinbi > 0) {
                if(c%2==0) {
                    if(jinbi==1) {
                        System.out.println("恭喜,你赢入了!");
                        jinbi = -1;
                    }
                    else cremove();
                }else {
                    if(jinbi==1) {
                        System.out.println("不要放弃,继续努力!");
                        jinbi = -1;
                    }
                    else premove();
                }
                System.out.println("========================================");
                c++;
            }
        }else{
            System.out.println("游戏结束!");
        }
        
    }
   
    public int select() {//抛金币选择正反面
        int i = ran.nextInt(2);
        if(i==0) {
            System.out.println("正面,电脑先!");
            System.out.println("========================================");
            return i;
        }else {
            System.out.println("反面,玩家先!");
            System.out.println("========================================");
            return i;
        }
    }
   
    public void cremove() {
        int i = ran.nextInt(3)+1;
        if(jinbi == 3) {
            i = 2;
        }
        if(jinbi == 2) {
            i = 1;
        }
        jinbi = jinbi - i;
        System.out.println("计算机拿取金币:"+i);
        System.out.println("剩余金币数:"+jinbi);
        System.out.println("========================================");
    }
   
    public void premove() {
        System.out.println("选择你要拿取的金币数(1~3枚)");
        int i = input.nextInt();
        if(i<4 && i>0 && i<jinbi) {
            jinbi = jinbi - i;
            System.out.println("剩余金币数:"+jinbi);
            System.out.println("========================================");
        }else {
            System.out.println("输入错误,请重新输入!");
            System.out.println("========================================");
            premove();
        }
    }
}
2010-03-06 21:45
kkllkk22
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-3-13
收藏
得分:0 
不错啊~~~~~
2010-03-13 12:24
dadongzicool
Rank: 6Rank: 6
等 级:贵宾
威 望:11
帖 子:209
专家分:474
注 册:2009-3-20
收藏
得分:0 
闲着没事干  把这个游戏用SWING写了一下
package cn.dadongzicool.coin.entities;

import java.util.Random;

public class Coin {
   
    private int coinNumber;
    private int inputNumber;
    private int takeNumber;
   
    Random random;
   
    //public void getCoinSide(){
        //System.out.println("Coin's getCoinSide");
    //}
   
    public void setCoinNum(){
        random = new Random();
        coinNumber = random.nextInt(10)+30;
    }
   
    public int getCoinNum(){
        System.out.println("Coin's getCoinNum");
        return coinNumber;
    }
   
    public boolean takeCoin(int takeCoinNumber){
        System.out.println("Coin's takeCoin");
        if(takeCoinNumber <= coinNumber){
            coinNumber -=takeCoinNumber;
            return true;
        }
        else
            return false;
    }
   
    public boolean getWinner(){
        System.out.println("Coin's getWinner");
        if(coinNumber == 1){
            return true;
        }
        return false;
    }

}
**********************************************************************
package cn.dadongzicool.coin.game;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import cn.dadongzicool.coin.view.GamePanel;

public class Game {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        GamePanel gamePanel = new GamePanel();
        JFrame frame = new JFrame("Coin Game");
        
        GamePanel.showTextArea.append("****************************************\n"
                +"作者:dadongzicool\n"
                +"QQ:405734751\n"
                +"****************************************\n");
        
        frame.setSize(405, 405);
        frame.setLocation(100, 100);
        
        frame.add(gamePanel,BorderLayout.CENTER);
        frame.setVisible(true);
        frame.validate();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}
**************************************************************************
package cn.dadongzicool.coin.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import cn.dadongzicool.coin.controller.Controller;
import cn.dadongzicool.coin.entities.Coin;
import cn.dadongzicool.coin.view.GamePanel;

public class ButtonListener implements ActionListener{
   

    Coin coin= new Coin();
    Controller controller = new Controller(coin);
   
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == GamePanel.buttonEnter){
            controller.chargeOfEnter();
        }
        if(e.getSource() == GamePanel.buttonStart){
            controller.newGame();
            GamePanel.showTextArea.append("Threr are  "+coin.getCoinNum()+"  coins!!\n" +
                    "Please enter a number between 1 to 3!!!\n");
        }
    }
}
*************************************************************************************
package cn.dadongzicool.coin.view;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import cn.dadongzicool.coin.listener.ButtonListener;



public class GamePanel extends JPanel{
   
    //JPanel panel;
    public static JTextArea showTextArea;
    public static JTextField inputTextField;
    public static JButton buttonEnter;
    public static JButton buttonStart;
    public static JScrollPane pane;
   
    ButtonListener buttonListener;

    public GamePanel(){
        init();
    }

    public void init(){
        setLayout(null);
        //panel = new JPanel();
        showTextArea = new JTextArea();
        inputTextField = new JTextField(3);
        buttonEnter = new JButton("ENTER");
        buttonStart = new JButton("START");
        
        pane = new JScrollPane(showTextArea);
        
        buttonListener = new ButtonListener();
        
        setSize(400,400);
        add(pane);
        add(inputTextField);
        add(buttonEnter);
        add(buttonStart);
               
        pane.setBounds(10, 10, 380, 300);
        inputTextField.setBounds(10, 330, 100, 30);
        buttonEnter.setBounds(150, 330, 100, 35);
        buttonStart.setBounds(250, 330, 100, 35);
        
        buttonEnter.addActionListener(buttonListener);
        buttonStart.addActionListener(buttonListener);
        
    }
   
}
**************************************************************************
package cn.dadongzicool.coin.controller;

import java.util.Random;

import cn.dadongzicool.coin.entities.Coin;
import cn.dadongzicool.coin.view.GamePanel;

public class Controller {
   
    Coin coin;

    Random random = new Random();
   
    int inputNumber;

    public Controller(Coin coin){
        this.coin = coin;
    }
   
    public void newGame(){
        coin.setCoinNum();
    }
   
    public void chargeOfEnter(){
        if(!coin.getWinner()){
            try{
            inputNumber = Integer.parseInt(GamePanel.inputTextField.getText().trim());
            }
            catch(Exception ee){
                GamePanel.showTextArea.append("\n"+ee+"\n");
            }
            if(inputNumber <=0 || inputNumber > 3){
                GamePanel.showTextArea.append("Please enter a number between 1 to 3 !!!\n");
            }
            else{
                if(coin.takeCoin(inputNumber)){
                    GamePanel.showTextArea.append("user take  "+inputNumber+"  coins!\n" +
                            "There are  "+coin.getCoinNum()+"  left!\n");
                    if(coin.getWinner()){
                        GamePanel.showTextArea.append("Winner:user!!!\n");
                    }
            int computerTakeCoinNum = 0;
            if(coin.getCoinNum() > 4) {
                computerTakeCoinNum = random.nextInt(3)+1;
            }
            else {
                computerTakeCoinNum = coin.getCoinNum()-1;
                if(computerTakeCoinNum ==0 ){
                    computerTakeCoinNum++;
                }
            }
            if(!coin.getWinner()){
                coin.takeCoin(computerTakeCoinNum);
                GamePanel.showTextArea.append("computer take  "+computerTakeCoinNum+"  coins!\n" +
                        "There are  "+coin.getCoinNum()+"  left!\n");
                if(coin.getWinner()){
                    GamePanel.showTextArea.append("Winner:computer!!!\n");
                }
            }
        }
        else{
            GamePanel.showTextArea.append("Sorry,there is only  "+coin.getCoinNum()+"  left!\n" +
                    "Please enter a number which is lower than  "+coin.getCoinNum()+"  !\n");
        }
        }
    }
    }

}
等写完了 突然发现忘记了一个事情,没有写抛硬币选择谁先开始的代码,
有兴趣的可以帮我补上!!!

jar包:(不允许上传jar后缀的文件这是为什么呢 ???压缩一下吧!!!)
CoinGame.rar (5.54 KB)

本人出售软件专业毕业设计(包括论文,代码)题目众多,价格从优联系方式:QQ
1635008514(寻找高校代理)
2010-03-13 17:01
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:0 
玩了一下,为什么computer可以取-1 coin的?有作弊嫌疑
Threr are  32  coins!!
Please enter a number between 1 to 3!!!
user take  2  coins!
There are  30  left!
computer take  1  coins!
There are  29  left!
user take  2  coins!
There are  27  left!
computer take  3  coins!
There are  24  left!
user take  1  coins!
There are  23  left!
computer take  3  coins!
There are  20  left!
user take  3  coins!
There are  17  left!
computer take  1  coins!
There are  16  left!
user take  2  coins!
There are  14  left!
computer take  3  coins!
There are  11  left!
user take  1  coins!
There are  10  left!
computer take  1  coins!
There are  9  left!
user take  3  coins!
There are  6  left!
computer take  3  coins!
There are  3  left!
user take  3  coins!
There are  0  left!
computer take  -1  coins!
There are  1  left!
Winner:computer!!!

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2010-03-13 17:08
dadongzicool
Rank: 6Rank: 6
等 级:贵宾
威 望:11
帖 子:209
专家分:474
注 册:2009-3-20
收藏
得分:0 
以下是引用lampeter123在2010-3-13 17:08:29的发言:

玩了一下,为什么computer可以取-1 coin的?有作弊嫌疑
Threr are  32  coins!!
Please enter a number between 1 to 3!!!
user take  2  coins!
There are  30  left!
computer take  1  coins!
There are  29  lef ...
失误  我测试了N遍 居然没测试出这个BUG

高手啊 !!!!

本人出售软件专业毕业设计(包括论文,代码)题目众多,价格从优联系方式:QQ
1635008514(寻找高校代理)
2010-03-13 17:18
快速回复:编了一个拿金币的小游戏 请大家多多指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038420 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved