佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 5140|回复: 153

Asignment新的小问题。。。

[复制链接]
发表于 30-9-2008 04:46 PM | 显示全部楼层 |阅读模式
Asignment是要做出snake n ladder的游戏。可是我遇上了几个问题:

1) 要如何让PLAYER 1 和 PLAYER 2 交替丢dice。
2) 当玩家超过100的时候,玩家倒退时,是否要为每个可能性作IF?
   

    if (step==95)
    {
        if (dice ==6)
         step=99
    }
    else if (step==96)
    {
        if (dice ==6)
        step=98
        if (dice==5)
        step=99
    }
    else if (step==97)
    {
        if (dice ==6)
        step=97
        else if (dice==5)
        step=98
        if (dice==4)
        step=99
    }
    .......

3)我用rand()%7 加一个 if (dice==0) continue; 来做dice,虽然能够randomly给我1到6的号码
,可是每次RUN,我得到的号码都是跟着3,4,6,5.......酱走。

我的code:
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#define m 50
void Line()
{
cout<<"-------------------------------------------------------------------------------\n";
}
void main()
{
char dice;
int A, n, number, next;
int player[m];
Line();
cout<<"\t\tWelcome to Snake and Ladder Game!!\n\n";
cout<<"Introduction: if a player reach a point where there is a ladder, he/she will \nraise to a higher location.\n";
cout<<"\nOn the other hand, if the player reach a spot where there is a snake over there,the player will drop to a lower location.\n";
Line();
cout<<"\nNow please enter the numbers of players:\n";
cin>>n;
cout<<"\nSo there will be "<<n<<" player(s) in the game.\n";
cout<<"Good Luck and Have Fun!!!\n\n";
cout<<"(All players start at 0)\n\n\n";
for (int x=0; x<n; x++)
{
player[x]=0;
while (player[x]<=100)
{
cout<<"Press 'T' to throw a dice!\n";
cin>>dice;
if (dice=='T')
{
A=rand()%7;
if (A==0)
continue;
else if (A==1)
{
cout<<"\n   \n";
cout<<" . \n";
cout<<"   \n";
cout<<"\n\nYou get 1!!\n";
}
else if (A==2)
{
cout<<"\n . \n";
cout<<"   \n";
cout<<" . \n";
cout<<"\n\nYou get 2!!\n";
}
else if (A==3)
{
cout<<"\n.  \n";
cout<<" . \n";
cout<<"  .\n";
cout<<"\n\nYou get 3!!\n";
}
else if (A==4)
{
cout<<"\n. .\n";
cout<<"   \n";
cout<<". .\n";
cout<<"\n\nYou get 4!!\n";
}
else if (A==5)
{
cout<<"\n. .\n";
cout<<" . \n";
cout<<". .\n";
cout<<"\n\nYou get 5!!\n";
}
else if (A==6)
{
cout<<"\n. .\n";
cout<<". .\n";
cout<<". .\n";
cout<<"\n\nYou get 6!!\n";
}
player[x]+=A;
number=x+1;
cout<<"Player "<<number<<" had reached "<<player[x]<<"\n";
cout<<"\n";
}
}
if (x==n-1)
continue;
next=number+1;
cout<<"\n\nPlayer "<<next<<"'s turn!!!\n";
}
}
以上问题已解决。


新问题如下:
我的code:
do
            {
            cout<<"Press 't' to throw a dice!\nPress 'q' to quit game.\n";
            cin>>dice;
            if (dice !='q'||'t')
            {
            Line();
            cout<<"\nINVALID CHOICE.\n\n";
            Line();
            }
            }
            while (dice!='q'||'t');

我想要的是每当user key-in q 和 t 以外的东西的时候, 用looping来让user回到前面,再key-in过。可是我的code得到的result是即使我type什么东西,output总会有INVALID CHOICE 然后一直loop 下去。。。。
是哪里出了问题呢?

[ 本帖最后由 Wongkokchoy 于 5-10-2008 04:54 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 30-9-2008 05:02 PM | 显示全部楼层
2) 当玩家超过100的时候,玩家倒退时,是否要为每个可能性作IF?

不如这样,

A = step + dice

if ( A > 100)
{
       step =100 - ( A - 100 );

}

不懂是不是你要的。
错了不要shoot我。
回复

使用道具 举报

 楼主| 发表于 30-9-2008 09:49 PM | 显示全部楼层
原帖由 exhellsing 于 30-9-2008 05:02 PM 发表
2) 当玩家超过100的时候,玩家倒退时,是否要为每个可能性作IF?

不如这样,

A = step + dice

if ( A > 100)
{
       step =100 - ( A - 100 );

}

不懂是不是你要的。
错了不要shoot我。




你的方法不错,我一定会用。
主要的问题在于要怎么让player 1 和 player 2交替丢 dice。


我想过要这样做:

n=no. of player
while (step[x]>=100)
{
for (int x=0; x>n; x++)
{
step[x]=0;
A=rand()&7;
step[x]+=A;
}
}

大概是这样,可是我的while loop那边有问题。。。。
有那位能够帮帮我
回复

使用道具 举报

发表于 30-9-2008 10:05 PM | 显示全部楼层
原帖由 Wongkokchoy 于 30-9-2008 09:49 PM 发表




你的方法不错,我一定会用。
主要的问题在于要怎么让player 1 和 player 2交替丢 dice。


我想过要这样做:

n=no. of player
while (step[x]>=100)
{
for (int x=0; x>n; x++)
{
step[x]=0;
A ...


什么问题?
回复

使用道具 举报

发表于 30-9-2008 10:06 PM | 显示全部楼层
原帖由 Wongkokchoy 于 30-9-2008 04:46 PM 发表
3)我用rand()%7 加一个 if (dice==0) continue; 来做dice,虽然能够randomly给我1到6的号码
,可是每次RUN,我得到的号码都是跟着3,4,6,5.......酱走。
...


你觉得老是出3,4,5,6 罢了
事实上他真是random 的,没问题!
回复

使用道具 举报

 楼主| 发表于 30-9-2008 10:12 PM | 显示全部楼层
原帖由 winmxaa 于 30-9-2008 10:05 PM 发表


什么问题?


我的while loop 的condition 有问题。。。
回复

使用道具 举报

Follow Us
 楼主| 发表于 30-9-2008 10:16 PM | 显示全部楼层
原帖由 winmxaa 于 30-9-2008 10:06 PM 发表


你觉得老是出3,4,5,6 罢了
事实上他真是random 的,没问题!


可是哦,我第一次run:player 1 - 3.4.6.5,......
第二次run. player 1 -3.4.6.5....

lecturer 要求我们交上一份拿来test的data。如果交上3 个data全部都一样的话,要怎么交。。。。
回复

使用道具 举报

发表于 30-9-2008 10:45 PM | 显示全部楼层
Q1。 playerA和 playerB 交替

method playerA
//throw dice
//current location + dice number
//if exist 100, blah blah
//if playerA != 100, playerB()

playerB一样
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 30-9-2008 10:57 PM | 显示全部楼层
原帖由 晨天 于 30-9-2008 10:45 PM 发表
Q1。 playerA和 playerB 交替

method playerA
//throw dice
//current location + dice number
//if exist 100, blah blah
//if playerA != 100, playerB()

playerB一样



USM 用的是borland C++,我不是很明白你的意思,可以解释一下吗?
回复

使用道具 举报

发表于 30-9-2008 11:16 PM | 显示全部楼层
原帖由 Wongkokchoy 于 30-9-2008 10:57 PM 发表



USM 用的是borland C++,我不是很明白你的意思,可以解释一下吗?


pseudocode来的
回复

使用道具 举报

 楼主| 发表于 30-9-2008 11:20 PM | 显示全部楼层
原帖由 晨天 于 30-9-2008 10:45 PM 发表
Q1。 playerA和 playerB 交替

method playerA
//throw dice
//current location + dice number
//if exist 100, blah blah
//if playerA != 100, playerB()

playerB一样



method playerA是什么?playerB()又是什么?

不需要用到for和while吗?
回复

使用道具 举报

发表于 30-9-2008 11:38 PM | 显示全部楼层
看来你的老师还没有教你method

这是c/c++的一个feature
我也不大会解释, 你老师一定会迟点教的
  1. rand()%7;
  2. if (A==0)
  3. continue;
复制代码
不如写 (rand()%6 + 1)
  1. else if (A==1)
  2. {
  3. cout<<"\n   \n";
  4. cout<<" . \n";
  5. cout<<"   \n";
  6. cout<<"\n\nYou get 1!!\n";
  7. }
  8. else if (A==2)
  9. {
  10. cout<<"\n . \n";
  11. cout<<"   \n";
  12. cout<<" . \n";
  13. cout<<"\n\nYou get 2!!\n";
  14. }
  15. else if (A==3)
  16. {
  17. cout<<"\n.  \n";
  18. cout<<" . \n";
  19. cout<<"  .\n";
  20. cout<<"\n\nYou get 3!!\n";
  21. }
  22. else if (A==4)
  23. {
  24. cout<<"\n. .\n";
  25. cout<<"   \n";
  26. cout<<". .\n";
  27. cout<<"\n\nYou get 4!!\n";
  28. }
  29. else if (A==5)
  30. {
  31. cout<<"\n. .\n";
  32. cout<<" . \n";
  33. cout<<". .\n";
  34. cout<<"\n\nYou get 5!!\n";
  35. }
  36. else if (A==6)
  37. {
  38. cout<<"\n. .\n";
  39. cout<<". .\n";
  40. cout<<". .\n";
  41. cout<<"\n\nYou get 6!!\n";
  42. }
复制代码
你不觉得
cout<<"\n. .\n";
cout<<". .\n";
cout<<". .\n";
cout<<"\n\nYou get *!!\n";

一直重复吗???
你可以省略他, merge them
回复

使用道具 举报

 楼主| 发表于 30-9-2008 11:52 PM | 显示全部楼层
原帖由 晨天 于 30-9-2008 11:38 PM 发表
看来你的老师还没有教你method

这是c/c++的一个feature
我也不大会解释, 你老师一定会迟点教的rand()%7;
if (A==0)
continue;不如写 (rand()%6 + 1)else if (A==1)
{
cout



lecturer 已经教完syllibus了,我们学的就那么多。。。。。
那些点点点只是拿来装饰罢了
你说的我都会改进的。现在重点在于要如何用/不用for和 while 来让player 1 n player 2 交替丢dice。。。。
而且我的program 又不能只针对2个player 而已,所以array是免不了的了。

谁能救我 。。。。。。



要睡了,我明天再来。无论如何,谢谢大家!

[ 本帖最后由 Wongkokchoy 于 30-9-2008 11:54 PM 编辑 ]
回复

使用道具 举报

发表于 1-10-2008 12:04 AM | 显示全部楼层
原帖由 Wongkokchoy 于 30-9-2008 10:16 PM 发表


可是哦,我第一次run:player 1 - 3.4.6.5,......
第二次run. player 1 -3.4.6.5....

lecturer 要求我们交上一份拿来test的data。如果交上3 个data全部都一样的话,要怎么交。。。。


Vector v1 = new Vector();
        Vector v2 = new Vector();
        Vector v3 = new Vector();
        Vector v4 = new Vector();
        Vector v5 = new Vector();
        Vector v6 = new Vector();
        int randomNumber = 0;
        for(int i=0;i<6000;i++){
            randomNumber = (int)(Math.random()*6)+1;
            if(randomNumber==1){
                v1.add(0);
            }
            else if(randomNumber==2){
                v2.add(0);
            }
            else if(randomNumber==3){
                v3.add(0);
            }
            else if(randomNumber==4){
                v4.add(0);
            }
            else if(randomNumber==5){
                v5.add(0);
            }
            else if(randomNumber==6){
                v6.add(0);
            }
        }
        
        System.out.println("number 1 ="+v1.size());
        System.out.println("number 2 ="+v2.size());
        System.out.println("number 3 ="+v3.size());
        System.out.println("number 4 ="+v4.size());
        System.out.println("number 5 ="+v5.size());
        System.out.println("number 6 ="+v6.size());

number 1 =1025
number 2 =990
number 3 =1005
number 4 =1005
number 5 =986
number 6 =989

自己改去c++
回复

使用道具 举报

发表于 1-10-2008 02:07 AM | 显示全部楼层

回复 7# Wongkokchoy 的帖子

用 srand, 更换 rand 的 seed。

你的game 有没有 蛇 与 梯子 的? 还是一直是前进罢了?

如果只是前进的话就简单。

老师要求用 structure还是 OOP?

[ 本帖最后由 onlylonly 于 1-10-2008 10:20 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 1-10-2008 08:59 AM | 显示全部楼层
原帖由 winmxaa 于 1-10-2008 12:04 AM 发表


Vector v1 = new Vector();
        Vector v2 = new Vector();
        Vector v3 = new Vector();
        Vector v4 = new Vector();
        Vector v5 = new Vector();
        Vector v6 = new Vecto ...



老兄你的code是什么version的,我看不明白。。。
或许我的朋友能够了解它。。。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 1-10-2008 09:03 AM | 显示全部楼层
原帖由 onlylonly 于 1-10-2008 02:07 AM 发表
用 srand, 更换 rand 的 seed。

你的game 有没有 蛇 与 梯子 的? 还是一直是前进罢了?

如果只是前进的话就简单。

老师要求用 sequential 还是 OOP?



我不懂什么是sequential 什么是 OOP。。。。
我学的programming只是非常basic的。。。我会的只是Borland C++ 的 if, switch, for,while, function 和 array罢了。

至于蛇和梯子,我打算用if来做,这样可以吗?
回复

使用道具 举报

发表于 1-10-2008 10:23 AM | 显示全部楼层

回复 17# Wongkokchoy 的帖子

抱歉是 structural 。。 手快打错。

这样的话应该是structural programming了。

蛇和梯子可以用case。

以dynamic array 来记录用户的位置, 在根据位子来判断是否上梯子或是蛇。
回复

使用道具 举报

发表于 1-10-2008 10:24 AM | 显示全部楼层

回复 16# Wongkokchoy 的帖子

那位大大写的是java。 用处是显示 random 的次数。
回复

使用道具 举报

 楼主| 发表于 1-10-2008 11:24 AM | 显示全部楼层
这个program的大纲我已经抓到了,只是不懂要如何让player 交替丢dice。。。。。。

for (int x=0; x<n; x++)
{
player[x]=0;
while (player[x]<=100)
{
cout<<"Press 'T' to throw a dice!\n";
cin>>dice;

我的code只能够在player 1 到达100后,才轮到player 2。
可是我做不到player 1丢dice, 然后player 2 丢, 然后再到player 1 丢dice。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 23-12-2025 07:53 PM , Processed in 0.327062 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表