编了一个拿金币的小游戏 请大家多多指教
游戏规则是这样的:桌上有一堆金币(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 编辑 ]