Java收集金币_编了一个拿金币的小游戏 请大家多多指教

编了一个拿金币的小游戏 请大家多多指教

游戏规则是这样的:桌上有一堆金币(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(c1)

{

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(c1)

{

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 编辑]

搜索更多相关的解决方案:

金币  指教  小游戏

----------------解决方案--------------------------------------------------------

试试看哦~~

----------------解决方案--------------------------------------------------------

1:整体看main函数写的有点太长了,可以尝试使用不同的function(),这样读起来更简明一些

2:没有方法的类是没有意义的

3:构造函数是必需的

4:数据类型权限问题

5:封装问题

----------------解决方案--------------------------------------------------------

感觉楼主以前学习过C/C++,JAVA也可以用面向过程编程,不过面向过程的程序很难扩展和维护,建议用面向对象方法来做

----------------解决方案--------------------------------------------------------

是的,代码有点长了

----------------解决方案--------------------------------------------------------

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 - i;

System.out.println("剩余金币数:"+jinbi);

System.out.println("========================================");

}else {

System.out.println("输入错误,请重新输入!");

System.out.println("========================================");

premove();

}

}

}

----------------解决方案--------------------------------------------------------

不错啊~~~~~

----------------解决方案--------------------------------------------------------

闲着没事干  把这个游戏用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后缀的文件这是为什么呢 ???压缩一下吧!!!)

----------------解决方案--------------------------------------------------------

玩了一下,为什么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!!!

----------------解决方案--------------------------------------------------------

以下是引用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

高手啊 !!!!

----------------解决方案--------------------------------------------------------

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值