佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

搜索
楼主: Wongkokchoy

Asignment新的小问题。。。

[复制链接]
发表于 8-10-2008 04:40 PM | 显示全部楼层

回复 100# Wongkokchoy 的帖子

if (player[x]==100)                      // exits loops when one of the players reach 100
            {
            winner (number, name);           <-------------------------------------
            break;
            }

改成

if (player[x]==100)                      // exits loops when one of the players reach 100
            {
            winner (number, name[x]);           <-------------------------------------
            break;
            }

还有
  1. void winner(int number,char name )
  2. {
  3. cin.get();
  4. cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
  5. cout << "\n"<<name<<" Player  [" <<number<< "] win!!!!\n\nCongratulation!!!\n\n"<<endl;
  6. cout<<"----------------------------------WINNER---------------------------------------\n";
  7. }
复制代码

改成
  1. void winner(int number,char name[] )
  2. {
  3. cin.get();
  4. cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
  5. cout << "\n"<<name<<" Player  [" <<number<< "] win!!!!\n\nCongratulation!!!\n\n"<<endl;
  6. cout<<"----------------------------------WINNER---------------------------------------\n";
  7. }
复制代码

[ 本帖最后由 onlylonly 于 8-10-2008 04:43 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 8-10-2008 04:48 PM | 显示全部楼层
我改了, 可是还是不能。。。。
error 的地方还是一样呢。
回复

使用道具 举报

发表于 8-10-2008 04:52 PM | 显示全部楼层

回复 102# Wongkokchoy 的帖子

你将code贴上来, 我帮你看看
回复

使用道具 举报

 楼主| 发表于 8-10-2008 04:57 PM | 显示全部楼层
好的。

  1. #include <iostream.h>             // header file
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <ctype.h>

  6. void Line();                     // function phototypes
  7. void Ladder(int);
  8. void Snake(int);
  9. void wall();
  10. void winner(int, char);

  11. void main()
  12. {
  13.     char dice, *name[100];                                      // declaration of variable
  14.     int A, n, number, next, x, reverse, *player;
  15.     Line();                                         // function call
  16.     cout<<"\t\tWelcome to Snake and Ladder Game!!\n\n";            // instuction
  17.     cout<<"Introduction: if a player reach a point where there is a ladder, he/she will \nraise to a higher location.\n";
  18.     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\n";
  19.     cout<<"The 1st player who reach 100 first will be the WINNER!!!!!!\n";
  20.     Line();                                        // function call

  21.     do                                                // do-while loop to avoid user to key in negative value or zero
  22.     {
  23.     cout<<"\nPlease enter the numbers of players:\n";
  24.     cin>>n;
  25.     if (n<=0)
  26.     cout<<"INVALID number of player!\n";
  27.     }
  28.     while (n<=0);
  29.     player=new int [n];             // declare array "player"
  30.     name[100]=new char [n];
  31.     for (int x=0; x<n; x++)
  32.     {
  33.     cout<<"Please enter player "<<(x+1)<<"'s name.\n";
  34.     cin.getline (name[x], 100);
  35.     }
  36.     for (int x=0; x<n; x++)
  37.     {
  38.     cout<<"Player "<<(x+1)<<"'s name is "<<name[x]<<".\n";
  39.     }
  40.     cout<<"\nSo there will be "<<n<<" player(s) in the game.\n";
  41.     cout<<"There are ladders at 4, 9, 19, 21, 28, 51, 71. 80 \n(80 will directly send you to the vitory!!)\n";
  42.     cout<<"Snakes located at 17, 54, 62, 64, 87, 93, 96, 98.\n\n";
  43.     cout<<"(All players start at 0)\n\n\n";
  44.     cout<<"Good Luck and Have Fun!!!\n\n";
  45.     Line();                      // funciton call
  46.     for (x = 0; x < n; x++)    // sets all cell values in the array player to 0
  47.     player[x] = 0;
  48.     do
  49.     {
  50.         for (x=0; x<n; x++)    // save players' position into array "player"
  51.         {
  52.             number=x+1;
  53.             cout<<"\n\n\n\n\n\nPlayer "<<(x+1)<<" is now at position "<<player[x]<<".\n";    // tell players their position and snake and ladders' location
  54.             cout<<"Ladders (4, 9, 19, 21, 28, 51, 71. 80)\n";
  55.             cout<<"Snakes  (17, 54, 62, 64, 87, 93, 96, 98)\n\n";
  56.             srand(time(0));            // seeds the random sequence generated by rand() later

  57.             do                         // a do-while loop to ensure that the choice entered by users is either t or q
  58.             {
  59.             cout<<"Press 'T' to throw a dice!\nPress 'Q' to quit game.\n";
  60.             cin>>dice;
  61.             if (toupper(dice) !='Q' && toupper(dice) !='T')     // toupper is used so that it is exercuate without
  62.                                                                // considering the upper and lower case of the choice
  63.             {
  64.             Line();                   // funciton call
  65.             cout<<"\nINVALID CHOICE.\n\n";
  66.             Line();                   // funciton call
  67.             }
  68.             }
  69.             while (toupper(dice)!='Q'&& toupper(dice)!='T');
  70.             if (toupper(dice)=='Q')
  71.             {
  72.             cout<<"\n\n\nUser had quit the game.";
  73.             cin.get();                // exits from the loop when user key in 'q' or 'Q'
  74.             break;
  75.             }
  76.             else if (toupper(dice)=='T')
  77.             {
  78.                 A=(rand()%6)+1;     // randomly generate a number from 1 to 6
  79.                  if (A==1)
  80.                 cout<<"\n   \n . \n   \n\n\nPlayer "<<number<<" gets 1!!\n";   // displays to tell users the number on the 'dice'
  81.                 else if (A==2)
  82.                  cout<<"\n . \n   \n . \n\n\nPlayer "<<number<<" gets 2!!\n";
  83.                 else if (A==3)
  84.                 cout<<"\n.  \n . \n  .\n\n\nPlayer "<<number<<" gets 3!!\n";
  85.                 else if (A==4)
  86.                 cout<<"\n. .\n   \n. .\n\n\nPlayer "<<number<<" gets 4!!\n";
  87.                 else if (A==5)
  88.                 cout<<"\n. .\n . \n. .\n\n\nPlayer "<<number<<" gets 5!!\n";
  89.                 else if (A==6)
  90.                 cout<<"\n. .\n. .\n. .\n\n\nPlayer "<<number<<" gets 6!!\n";
  91.                 player[x]+=A;

  92.                 if( player[x] > 100 )     // when users' position is over 100, user will reverse to their respective position
  93.                 {
  94.                 reverse = player[x]-100;
  95.                 wall();           // funciton call
  96.                 cin.get();
  97.                 cout<<"Player "<<number<<" had reached "<<player[x]<<"!\n\n";
  98.                 cin.get();
  99.                 player[x] = 100 - ( player[x] - 100 );
  100.                 cout<<" Ops!! Player"<<number<<" knocks the wall!\nPlayer "<<number<<" will reverse "<<reverse<<" steps......\n\n";
  101.                 wall();           // funciton call
  102.                 cout<<"Player "<<number<<" is now at position "<<player[x]<<"!\n\n";
  103.                 cin.get();

  104.                 }

  105.                 switch (player[x])   // place the location of snakes and ladders
  106.                 {
  107.                 case 4:
  108.                 Ladder(number);     // funciton call
  109.                 player[x]=14;
  110.                   break;
  111.                      case 9:
  112.                       Ladder(number);     // funciton call
  113.                 player[x]=31;
  114.                 break;
  115.                   case 19:
  116.                   Ladder(number);    // funciton call
  117.                   player[x]=38;
  118.                 break;
  119.                    case 21:
  120.                     Ladder(number);    // funciton call
  121.                 player[x]=42;
  122.                  break;
  123.                     case 28:
  124.                  Ladder(number);    // funciton call
  125.                  player[x]=84;
  126.                 break;
  127.                     case 51:
  128.                     Ladder(number);    // funciton call
  129.                    player[x]=67;
  130.                 break;
  131.                     case 71:
  132.                  Ladder(number);    // funciton call
  133.                   player[x]=91;
  134.                 break;
  135.                   case 80:
  136.                   Ladder(number);   // funciton call
  137.                   player[x]=100;
  138.                 break;
  139.                   case 17:
  140.                   Snake(number);    // funciton call
  141.                   player[x]=7;
  142.                 break;
  143.                   case 62:
  144.                   Snake(number);     // funciton call
  145.                   player[x]=19;
  146.                 break;
  147.                   case 54:
  148.                   Snake(number);     // funciton call
  149.                   player[x]=34;
  150.                 break;
  151.                   case 64:
  152.                   Snake(number);     // funciton call
  153.                   player[x]=60;
  154.                 break;
  155.                   case 87:
  156.                   Snake(number);     // funciton call
  157.                    player[x]=24;
  158.                 break;
  159.                  case 93:
  160.                  Snake(number);      // funciton call
  161.                    player[x]=73;
  162.                 break;
  163.                   case 95:
  164.                   Snake(number);      // funciton call
  165.                   player[x]=75;
  166.                 break;
  167.                  case 98:
  168.                   Snake(number);     // funciton call
  169.                   player[x]=79;
  170.                 break;
  171.                   }
  172.                 cin.get();
  173.                 cout<<"Player "<<number<<" had reached "<<player[x]<<"!\n\n";    // displays usres' position
  174.                 cin.get();
  175.                 next = x+2;
  176.                }
  177.             if (player[x]==100)                      // exits loops when one of the players reach 100
  178.             {
  179.             winner (number, name[x]);
  180.             break;
  181.             }
  182.         if (next==n+1)
  183.                next=1;
  184.                cout<<"\n\n\n\nPlayer "<<next<<"'s turn!!!\n\n\n\n\n\n";
  185.         }
  186.     if (toupper(dice)=='Q')
  187.     break;
  188.   }
  189. while(player[x]!=100);                 // keep the looping untill a player reach 100
  190. }                                  // end of main funciton


  191. // functions definition


  192. void Line()
  193. {
  194. cout<<"-------------------------------------------------------------------------------\n";
  195. }

  196. void Ladder(int number)
  197. {
  198. cin.get();
  199. cout<<"\n\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH_LADDER_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n\n";
  200. cout<<"\nLook!! There is a ladder over there!\n";
  201. cin.get();
  202. cout<<"Player "<<number<<" climb up the ladder.\n";
  203. }

  204. void Snake(int number)
  205. {
  206. cin.get();
  207. cout<<"\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SNAKE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
  208. cout<<"\nWoot!! Player "<<number<<" meets an anaconda!!!!\n";
  209. cin.get();
  210. cout<<"In order to escape from the snake, player "<<number<<" had run away.\n";
  211. }

  212. void wall()
  213. {
  214. cout<<"\n\n`````````````````````````````````WALL``````````````````````````````````````````\n\n";
  215. }

  216. void winner(int number,char name[] )
  217. {
  218. cin.get();
  219. cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
  220. cout << "\n"<<name<<" Player  [" <<number<< "] win!!!!\n\nCongratulation!!!\n\n"<<endl;
  221. cout<<"----------------------------------WINNER---------------------------------------\n";
  222. }
复制代码
回复

使用道具 举报

发表于 8-10-2008 05:02 PM | 显示全部楼层
你忘记修改 function phototype

#include <iostream.h>             // header file
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctype.h>

void Line();                     // function phototypes
void Ladder(int);
void Snake(int);
void wall();
void winner(int, char);


改成
#include <iostream.h>             // header file
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctype.h>

void Line();                     // function phototypes
void Ladder(int);
void Snake(int);
void wall();
void winner(int, char[]);
回复

使用道具 举报

 楼主| 发表于 8-10-2008 05:12 PM | 显示全部楼层
哈哈,对啊, 忘了要跟function phototype 一起改。
回复

使用道具 举报

Follow Us
 楼主| 发表于 8-10-2008 05:17 PM | 显示全部楼层
是没有error了,可是run到key-in名字那边就stop了,

Thread stopped.
Fault: access violation at 0x407c01:write of address 0X18
回复

使用道具 举报

 楼主| 发表于 8-10-2008 05:39 PM | 显示全部楼层
最新的code:
[code]
#include <iostream.h>           
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctype.h>

void Line();                  
void Ladder(int);
void Snake(int);
void wall();
void winner(int, char[]);
bool check_number( string input );

void main()
{
    char dice, *name[100];                                    
    int A, n, number, next, input, x, reverse, *player;
    Line();                                         // function call
    cout<<"\t\tWelcome to Snake and Ladder Game!!\n\n";            // instuction
    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 wherethere is a snake over there,the player will drop to a lowerlocation.\n\n";
    cout<<"The 1st player who reach 100 first will be the WINNER!!!!!!\n";
    Line();                                        // function call

    do                                                // do-while loop to avoid user to key in negative value or zero
    {
    cout<<"\nPlease enter the numbers of players:\n";
    cin>>input;
    if( check_number( input ) == false)
    cout << "INVALID , INPUT NOT NUMBER";
    else
    n = atoi(input.c_str()); // convert string to integer, store to n
    if (n<=0)
    cout<<"INVALID number of player!\n";
    }
    while (n<=0);
    player=new int [n];             // declare array "player"
    name[100]=new char [n];
    for (int x=0; x<n; x++)
    {
    cout<<"Please enter player "<<(x+1)<<"'s name.\n";
    cin.getline (name[x], 100);
    }
    for (int x=0; x<n; x++)
    {
    cout<<"Player "<<(x+1)<<"'s name is "<<name[x]<<".\n";
    }
    cout<<"\nSo there will be "<<n<<" player(s) in the game.\n";
    cout<<"There are ladders at 4, 9, 19, 21, 28, 51, 71. 80 \n(80 will directly send you to the vitory!!)\n";
    cout<<"Snakes located at 17, 54, 62, 64, 87, 93, 96, 98.\n\n";
    cout<<"(All players start at 0)\n\n\n";
    cout<<"Good Luck and Have Fun!!!\n\n";
    Line();                      // funciton call
    for (x = 0; x < n; x++)    // sets all cell values in the array player to 0
    player[x] = 0;
    do
    {
        for (x=0; x<n; x++)    // save players' position into array "player"
        {
            number=x+1;
           cout<<"\n\n\n\n\n\nPlayer "<<(x+1)<<" isnow at position "<<player[x]<<".\n";    // tell playerstheir position and snake and ladders' location
            cout<<"Ladders (4, 9, 19, 21, 28, 51, 71. 80)\n";
            cout<<"Snakes  (17, 54, 62, 64, 87, 93, 96, 98)\n\n";
            srand(time(0));            // seeds the random sequence generated by rand() later

            do                         // a do-while loop to ensure that the choice entered by users is either t or q
            {
            cout<<"Press 'T' to throw a dice!\nPress 'Q' to quit game.\n";
            cin>>dice;
            if (toupper(dice) !='Q' && toupper(dice) !='T')     // toupper is used so that it is exercuate without
                                                               // considering the upper and lower case of the choice
            {
            Line();                   // funciton call
            cout<<"\nINVALID CHOICE.\n\n";
            Line();                   // funciton call
            }
            }
            while (toupper(dice)!='Q'&& toupper(dice)!='T');
            if (toupper(dice)=='Q')
            {
            cout<<"\n\n\nUser had quit the game.";
            cin.get();                // exits from the loop when user key in 'q' or 'Q'
            break;
            }
            else if (toupper(dice)=='T')
            {
                A=(rand()%6)+1;     // randomly generate a number from 1 to 6
                 if (A==1)
               cout<<"\n   \n . \n   \n\n\nPlayer"<<number<<" gets 1!!\n";   // displays to tell users thenumber on the 'dice'
                else if (A==2)
                 cout<<"\n . \n   \n . \n\n\nPlayer "<<number<<" gets 2!!\n";
                else if (A==3)
                cout<<"\n.  \n . \n  .\n\n\nPlayer "<<number<<" gets 3!!\n";
                else if (A==4)
                cout<<"\n. .\n   \n. .\n\n\nPlayer "<<number<<" gets 4!!\n";
                else if (A==5)
                cout<<"\n. .\n . \n. .\n\n\nPlayer "<<number<<" gets 5!!\n";
                else if (A==6)
                cout<<"\n. .\n. .\n. .\n\n\nPlayer "<<number<<" gets 6!!\n";
                player[x]+=A;

                if( player[x] > 100 )     // when users' position is over 100, user will reverse to their respective position
                {
                reverse = player[x]-100;
                wall();           // funciton call
                cin.get();
                cout<<"Player "<<number<<" had reached "<<player[x]<<"!\n\n";
                cin.get();
                player[x] = 100 - ( player[x] - 100 );
               cout<<" Ops!! Player"<<number<<" knocksthe wall!\nPlayer "<<number<<" will reverse"<<reverse<<" steps......\n\n";
                wall();           // funciton call
                cout<<"Player "<<number<<" is now at position "<<player[x]<<"!\n\n";
                cin.get();

                }

                switch (player[x])   // place the location of snakes and ladders
                {
                case 4:
                Ladder(number);     // funciton call
                player[x]=14;
                  break;
                     case 9:
                      Ladder(number);     // funciton call
                player[x]=31;
                break;
                  case 19:
                  Ladder(number);    // funciton call
                  player[x]=38;
                break;
                   case 21:
                    Ladder(number);   
                player[x]=42;
                 break;
                    case 28:
                 Ladder(number);   
                 player[x]=84;
                break;
                    case 51:
                    Ladder(number);    // funciton call
                   player[x]=67;
                break;
                    case 71:
                 Ladder(number);   
                  player[x]=91;
                break;
                  case 80:
                  Ladder(number);  
                  player[x]=100;
                break;
                  case 17:
                  Snake(number);  
                  player[x]=7;
                break;
                  case 62:
                  Snake(number);   
                  player[x]=19;
                break;
                  case 54:
                  Snake(number);     
                  player[x]=34;
                break;
                  case 64:
                  Snake(number);    l
                  player[x]=60;
                break;
                  case 87:
                  Snake(number);   
                   player[x]=24;
                break;
                 case 93:
                 Snake(number);   
                   player[x]=73;
                break;
                  case 95:
                  Snake(number);   
                  player[x]=75;
                break;
                 case 98:
                  Snake(number);   
                  player[x]=79;
                break;
                  }
                cin.get();
               cout<<"Player "<<number<<" had reached"<<player[x]<<"!\n\n";    // displays usres' position
                cin.get();
                next = x+2;
               }
            if (player[x]==100)                      // exits loops when one of the players reach 100
            {
            winner (number, name[x]);
            break;
            }
        if (next==n+1)
               next=1;
               cout<<"\n\n\n\nPlayer "<<next<<"'s turn!!!\n\n\n\n\n\n";
        }
    if (toupper(dice)=='Q')
    break;
  }
while(player[x]!=100);                 
}                                





void Line()
{
cout<<"-------------------------------------------------------------------------------\n";
}

void Ladder(int number)
{
cin.get();
cout<<"\n\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH_LADDER_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n\n";
cout<<"\nLook!! There is a ladder over there!\n";
cin.get();
cout<<"Player "<<number<<" climb up the ladder.\n";
}

void Snake(int number)
{
cin.get();
cout<<"\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SNAKE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
cout<<"\nWoot!! Player "<<number<<" meets an anaconda!!!!\n";
cin.get();
cout<<"In order to escape from the snake, player "<<number<<" had run away.\n";
}

void wall()
{
cout<<"\n\n`````````````````````````````````WALL``````````````````````````````````````````\n\n";
}

void winner(int number,char name[] )
{
cin.get();
cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
cout << "\n"<<name<<" Player  [" <<number<< "] win!!!!\n\nCongratulation!!!\n\n"<<endl;
cout<<"----------------------------------WINNER---------------------------------------\n";
}

bool check_number( string input )
{
    int i;

    for( i = 0; i < input.length(); i++ )
        if( isdigit( input ) == false )
            return false;

    return true;
}
[\code]

[ 本帖最后由 Wongkokchoy 于 8-10-2008 05:43 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 8-10-2008 05:46 PM | 显示全部楼层
名字的问题还没解决,
string也有error......
回复

使用道具 举报

发表于 8-10-2008 05:53 PM | 显示全部楼层

回复 108# Wongkokchoy 的帖子

你的 dynamic multidimentional array declaration 错了, 不可以这样写的。 要这样


void main()
{
    char dice, **name;        
        int i;



    player=new int [n];             // declare array "player"
    name[100]=new char [n];
    for (int x=0; x<n; x++)

改成

    player=new int [n];             // declare array "player"

    name = new char* [n];

    for(i = 0; i < n ;i++)
        name = new char[100];


    for (int x=0; x<n; x++)

这里在加入

    do                                                // do-while loop to avoid user to key in negative value or zero
    {
    cout<<"\nPlease enter the numbers of players:\n";
    cin>>n;
    cin.get(); // 加入这行
    if (n<=0)
    cout<<"INVALID number of player!\n";
    }
回复

使用道具 举报

发表于 8-10-2008 05:54 PM | 显示全部楼层
这个code是这样
  1. #include <iostream.h>             // header file
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <ctype.h>

  6. void Line();                     // function phototypes
  7. void Ladder(int);
  8. void Snake(int);
  9. void wall();
  10. void winner(int, char[]);

  11. int main()
  12. {
  13.     char dice, **name;                                      // declaration of variable
  14.     int A, n, number, next, x, reverse, *player, i;
  15.     Line();                                         // function call
  16.     cout<<"\t\tWelcome to Snake and Ladder Game!!\n\n";            // instuction
  17.     cout<<"Introduction: if a player reach a point where there is a ladder, he/she will \nraise to a higher location.\n";
  18.     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\n";
  19.     cout<<"The 1st player who reach 100 first will be the WINNER!!!!!!\n";
  20.     Line();                                        // function call

  21.     do                                                // do-while loop to avoid user to key in negative value or zero
  22.     {
  23.     cout<<"\nPlease enter the numbers of players:\n";
  24.     cin>>n;
  25.     cin.get();
  26.     if (n<=0)
  27.     cout<<"INVALID number of player!\n";
  28.     }
  29.     while (n<=0);
  30.     player=new int [n];             // declare array "player"
  31.    
  32.     name = new char* [n];

  33.     for(i = 0; i < n ;i++)
  34.         name[i] = new char[100];

  35.     for (int x=0; x<n; x++)
  36.     {
  37.     cout<<"Please enter player "<<(x+1)<<"'s name.\n";
  38.     cin.getline (name[x], 100);
  39.     //cin.get();
  40.     }
  41.     for (int x=0; x<n; x++)
  42.     {
  43.     cout<<"Player "<<(x+1)<<"'s name is "<<name[x]<<".\n";
  44.     }
  45.     cout<<"\nSo there will be "<<n<<" player(s) in the game.\n";
  46.     cout<<"There are ladders at 4, 9, 19, 21, 28, 51, 71. 80 \n(80 will directly send you to the vitory!!)\n";
  47.     cout<<"Snakes located at 17, 54, 62, 64, 87, 93, 96, 98.\n\n";
  48.     cout<<"(All players start at 0)\n\n\n";
  49.     cout<<"Good Luck and Have Fun!!!\n\n";
  50.     Line();                      // funciton call
  51.     for (x = 0; x < n; x++)    // sets all cell values in the array player to 0
  52.     player[x] = 0;
  53.     do
  54.     {
  55.         for (x=0; x<n; x++)    // save players' position into array "player"
  56.         {
  57.             number=x+1;
  58.             cout<<"\n\n\n\n\n\nPlayer "<<(x+1)<<" is now at position "<<player[x]<<".\n";    // tell players their position and snake and ladders' location
  59.             cout<<"Ladders (4, 9, 19, 21, 28, 51, 71. 80)\n";
  60.             cout<<"Snakes  (17, 54, 62, 64, 87, 93, 96, 98)\n\n";
  61.             srand(time(0));            // seeds the random sequence generated by rand() later

  62.             do                         // a do-while loop to ensure that the choice entered by users is either t or q
  63.             {
  64.             cout<<"Press 'T' to throw a dice!\nPress 'Q' to quit game.\n";
  65.             cin>>dice;
  66.             if (toupper(dice) !='Q' && toupper(dice) !='T')     // toupper is used so that it is exercuate without
  67.                                                                // considering the upper and lower case of the choice
  68.             {
  69.             Line();                   // funciton call
  70.             cout<<"\nINVALID CHOICE.\n\n";
  71.             Line();                   // funciton call
  72.             }
  73.             }
  74.             while (toupper(dice)!='Q'&& toupper(dice)!='T');
  75.             if (toupper(dice)=='Q')
  76.             {
  77.             cout<<"\n\n\nUser had quit the game.";
  78.             cin.get();                // exits from the loop when user key in 'q' or 'Q'
  79.             break;
  80.             }
  81.             else if (toupper(dice)=='T')
  82.             {
  83.                 A=(rand()%6)+1;     // randomly generate a number from 1 to 6
  84.                  if (A==1)
  85.                 cout<<"\n   \n . \n   \n\n\nPlayer "<<number<<" gets 1!!\n";   // displays to tell users the number on the 'dice'
  86.                 else if (A==2)
  87.                  cout<<"\n . \n   \n . \n\n\nPlayer "<<number<<" gets 2!!\n";
  88.                 else if (A==3)
  89.                 cout<<"\n.  \n . \n  .\n\n\nPlayer "<<number<<" gets 3!!\n";
  90.                 else if (A==4)
  91.                 cout<<"\n. .\n   \n. .\n\n\nPlayer "<<number<<" gets 4!!\n";
  92.                 else if (A==5)
  93.                 cout<<"\n. .\n . \n. .\n\n\nPlayer "<<number<<" gets 5!!\n";
  94.                 else if (A==6)
  95.                 cout<<"\n. .\n. .\n. .\n\n\nPlayer "<<number<<" gets 6!!\n";
  96.                 player[x]+=A;

  97.                 if( player[x] > 100 )     // when users' position is over 100, user will reverse to their respective position
  98.                 {
  99.                 reverse = player[x]-100;
  100.                 wall();           // funciton call
  101.                 cin.get();
  102.                 cout<<"Player "<<number<<" had reached "<<player[x]<<"!\n\n";
  103.                 cin.get();
  104.                 player[x] = 100 - ( player[x] - 100 );
  105.                 cout<<" Ops!! Player"<<number<<" knocks the wall!\nPlayer "<<number<<" will reverse "<<reverse<<" steps......\n\n";
  106.                 wall();           // funciton call
  107.                 cout<<"Player "<<number<<" is now at position "<<player[x]<<"!\n\n";
  108.                 cin.get();

  109.                 }

  110.                 switch (player[x])   // place the location of snakes and ladders
  111.                 {
  112.                 case 4:
  113.                 Ladder(number);     // funciton call
  114.                 player[x]=14;
  115.                   break;
  116.                      case 9:
  117.                       Ladder(number);     // funciton call
  118.                 player[x]=31;
  119.                 break;
  120.                   case 19:
  121.                   Ladder(number);    // funciton call
  122.                   player[x]=38;
  123.                 break;
  124.                    case 21:
  125.                     Ladder(number);    // funciton call
  126.                 player[x]=42;
  127.                  break;
  128.                     case 28:
  129.                  Ladder(number);    // funciton call
  130.                  player[x]=84;
  131.                 break;
  132.                     case 51:
  133.                     Ladder(number);    // funciton call
  134.                    player[x]=67;
  135.                 break;
  136.                     case 71:
  137.                  Ladder(number);    // funciton call
  138.                   player[x]=91;
  139.                 break;
  140.                   case 80:
  141.                   Ladder(number);   // funciton call
  142.                   player[x]=100;
  143.                 break;
  144.                   case 17:
  145.                   Snake(number);    // funciton call
  146.                   player[x]=7;
  147.                 break;
  148.                   case 62:
  149.                   Snake(number);     // funciton call
  150.                   player[x]=19;
  151.                 break;
  152.                   case 54:
  153.                   Snake(number);     // funciton call
  154.                   player[x]=34;
  155.                 break;
  156.                   case 64:
  157.                   Snake(number);     // funciton call
  158.                   player[x]=60;
  159.                 break;
  160.                   case 87:
  161.                   Snake(number);     // funciton call
  162.                    player[x]=24;
  163.                 break;
  164.                  case 93:
  165.                  Snake(number);      // funciton call
  166.                    player[x]=73;
  167.                 break;
  168.                   case 95:
  169.                   Snake(number);      // funciton call
  170.                   player[x]=75;
  171.                 break;
  172.                  case 98:
  173.                   Snake(number);     // funciton call
  174.                   player[x]=79;
  175.                 break;
  176.                   }
  177.                 cin.get();
  178.                 cout<<"Player "<<number<<" had reached "<<player[x]<<"!\n\n";    // displays usres' position
  179.                 cin.get();
  180.                 next = x+2;
  181.                }
  182.             if (player[x]==100)                      // exits loops when one of the players reach 100
  183.             {
  184.             winner (number, name[x]);
  185.             break;
  186.             }
  187.         if (next==n+1)
  188.                next=1;
  189.                cout<<"\n\n\n\nPlayer "<<next<<"'s turn!!!\n\n\n\n\n\n";
  190.         }
  191.     if (toupper(dice)=='Q')
  192.     break;
  193.   }
  194. while(player[x]!=100);                 // keep the looping untill a player reach 100
  195. }                                  // end of main funciton


  196. // functions definition


  197. void Line()
  198. {
  199. cout<<"-------------------------------------------------------------------------------\n";
  200. }

  201. void Ladder(int number)
  202. {
  203. cin.get();
  204. cout<<"\n\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH_LADDER_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n\n";
  205. cout<<"\nLook!! There is a ladder over there!\n";
  206. cin.get();
  207. cout<<"Player "<<number<<" climb up the ladder.\n";
  208. }

  209. void Snake(int number)
  210. {
  211. cin.get();
  212. cout<<"\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SNAKE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
  213. cout<<"\nWoot!! Player "<<number<<" meets an anaconda!!!!\n";
  214. cin.get();
  215. cout<<"In order to escape from the snake, player "<<number<<" had run away.\n";
  216. }

  217. void wall()
  218. {
  219. cout<<"\n\n`````````````````````````````````WALL``````````````````````````````````````````\n\n";
  220. }

  221. void winner(int number,char name[] )
  222. {
  223. cin.get();
  224. cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
  225. cout << "\n" << name <<" Player  [" <<number<< "] win!!!!\n\nCongratulation!!!\n\n"<<endl;
  226. cout<<"----------------------------------WINNER---------------------------------------\n";
  227. }
复制代码
回复

使用道具 举报

发表于 8-10-2008 06:04 PM | 显示全部楼层

回复 108# Wongkokchoy 的帖子

跟着你最新的code修改的, 没错的话, 你应该是忘了 #include <string>,我没加下去
  1. #include <iostream.h>           
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <ctype.h>


  6. void Line();                  
  7. void Ladder(int);
  8. void Snake(int);
  9. void wall();
  10. void winner(int, char[]);
  11. bool check_number( string input );

  12. int main()
  13. {
  14.     char dice, **name;                                    
  15.     int A, n, number, next,  x, reverse, *player, i;
  16.     string input;

  17.     Line();                                         // function call
  18.     cout<<"\t\tWelcome to Snake and Ladder Game!!\n\n";            // instuction
  19.     cout<<"Introduction: if a player reach a point where there is a ladder, he/she will \nraise to a higher location.\n";
  20.    cout<<"\nOn the other hand, if the player reach a spot wherethere is a snake over there,the player will drop to a lowerlocation.\n\n";
  21.     cout<<"The 1st player who reach 100 first will be the WINNER!!!!!!\n";
  22.     Line();                                        // function call

  23.     do                                                // do-while loop to avoid user to key in negative value or zero
  24.     {
  25.     cout<<"\nPlease enter the numbers of players:\n";
  26.     cin>>input;
  27.     if( check_number( input ) == false)
  28.     cout << "INVALID , INPUT NOT NUMBER";
  29.     else
  30.     n = atoi(input.c_str()); // convert string to integer, store to n
  31.     if (n<=0)
  32.     cout<<"INVALID number of player!\n";
  33.     }
  34.     while (n<=0);
  35.     player=new int [n];             // declare array "player"

  36.     name = new char* [n];

  37.     for( i = 0; i < n; i++)
  38.         name[i] = new char [100];

  39.     for (int x=0; x<n; x++)
  40.     {
  41.     cout<<"Please enter player "<<(x+1)<<"'s name.\n";
  42.     cin.getline (name[x], 100);
  43.     }
  44.     for (int x=0; x<n; x++)
  45.     {
  46.     cout<<"Player "<<(x+1)<<"'s name is "<<name[x]<<".\n";
  47.     }
  48.     cout<<"\nSo there will be "<<n<<" player(s) in the game.\n";
  49.     cout<<"There are ladders at 4, 9, 19, 21, 28, 51, 71. 80 \n(80 will directly send you to the vitory!!)\n";
  50.     cout<<"Snakes located at 17, 54, 62, 64, 87, 93, 96, 98.\n\n";
  51.     cout<<"(All players start at 0)\n\n\n";
  52.     cout<<"Good Luck and Have Fun!!!\n\n";
  53.     Line();                      // funciton call
  54.     for (x = 0; x < n; x++)    // sets all cell values in the array player to 0
  55.     player[x] = 0;
  56.     do
  57.     {
  58.         for (x=0; x<n; x++)    // save players' position into array "player"
  59.         {
  60.             number=x+1;
  61.            cout<<"\n\n\n\n\n\nPlayer "<<(x+1)<<" isnow at position "<<player[x]<<".\n";    // tell playerstheir position and snake and ladders' location
  62.             cout<<"Ladders (4, 9, 19, 21, 28, 51, 71. 80)\n";
  63.             cout<<"Snakes  (17, 54, 62, 64, 87, 93, 96, 98)\n\n";
  64.             srand(time(0));            // seeds the random sequence generated by rand() later

  65.             do                         // a do-while loop to ensure that the choice entered by users is either t or q
  66.             {
  67.             cout<<"Press 'T' to throw a dice!\nPress 'Q' to quit game.\n";
  68.             cin>>dice;
  69.             if (toupper(dice) !='Q' && toupper(dice) !='T')     // toupper is used so that it is exercuate without
  70.                                                                // considering the upper and lower case of the choice
  71.             {
  72.             Line();                   // funciton call
  73.             cout<<"\nINVALID CHOICE.\n\n";
  74.             Line();                   // funciton call
  75.             }
  76.             }
  77.             while (toupper(dice)!='Q'&& toupper(dice)!='T');
  78.             if (toupper(dice)=='Q')
  79.             {
  80.             cout<<"\n\n\nUser had quit the game.";
  81.             cin.get();                // exits from the loop when user key in 'q' or 'Q'
  82.             break;
  83.             }
  84.             else if (toupper(dice)=='T')
  85.             {
  86.                 A=(rand()%6)+1;     // randomly generate a number from 1 to 6
  87.                  if (A==1)
  88.                cout<<"\n   \n . \n   \n\n\nPlayer"<<number<<" gets 1!!\n";   // displays to tell users thenumber on the 'dice'
  89.                 else if (A==2)
  90.                  cout<<"\n . \n   \n . \n\n\nPlayer "<<number<<" gets 2!!\n";
  91.                 else if (A==3)
  92.                 cout<<"\n.  \n . \n  .\n\n\nPlayer "<<number<<" gets 3!!\n";
  93.                 else if (A==4)
  94.                 cout<<"\n. .\n   \n. .\n\n\nPlayer "<<number<<" gets 4!!\n";
  95.                 else if (A==5)
  96.                 cout<<"\n. .\n . \n. .\n\n\nPlayer "<<number<<" gets 5!!\n";
  97.                 else if (A==6)
  98.                 cout<<"\n. .\n. .\n. .\n\n\nPlayer "<<number<<" gets 6!!\n";
  99.                 player[x]+=A;

  100.                 if( player[x] > 100 )     // when users' position is over 100, user will reverse to their respective position
  101.                 {
  102.                 reverse = player[x]-100;
  103.                 wall();           // funciton call
  104.                 cin.get();
  105.                 cout<<"Player "<<number<<" had reached "<<player[x]<<"!\n\n";
  106.                 cin.get();
  107.                 player[x] = 100 - ( player[x] - 100 );
  108.                cout<<" Ops!! Player"<<number<<" knocksthe wall!\nPlayer "<<number<<" will reverse"<<reverse<<" steps......\n\n";
  109.                 wall();           // funciton call
  110.                 cout<<"Player "<<number<<" is now at position "<<player[x]<<"!\n\n";
  111.                 cin.get();

  112.                 }

  113.                 switch (player[x])   // place the location of snakes and ladders
  114.                 {
  115.                 case 4:
  116.                 Ladder(number);     // funciton call
  117.                 player[x]=14;
  118.                   break;
  119.                      case 9:
  120.                       Ladder(number);     // funciton call
  121.                 player[x]=31;
  122.                 break;
  123.                   case 19:
  124.                   Ladder(number);    // funciton call
  125.                   player[x]=38;
  126.                 break;
  127.                    case 21:
  128.                     Ladder(number);   
  129.                 player[x]=42;
  130.                  break;
  131.                     case 28:
  132.                  Ladder(number);   
  133.                  player[x]=84;
  134.                 break;
  135.                     case 51:
  136.                     Ladder(number);    // funciton call
  137.                    player[x]=67;
  138.                 break;
  139.                     case 71:
  140.                  Ladder(number);   
  141.                   player[x]=91;
  142.                 break;
  143.                   case 80:
  144.                   Ladder(number);  
  145.                   player[x]=100;
  146.                 break;
  147.                   case 17:
  148.                   Snake(number);  
  149.                   player[x]=7;
  150.                 break;
  151.                   case 62:
  152.                   Snake(number);   
  153.                   player[x]=19;
  154.                 break;
  155.                   case 54:
  156.                   Snake(number);     
  157.                   player[x]=34;
  158.                 break;
  159.                   case 64:
  160.                   Snake(number);   
  161.                   player[x]=60;
  162.                 break;
  163.                   case 87:
  164.                   Snake(number);   
  165.                    player[x]=24;
  166.                 break;
  167.                  case 93:
  168.                  Snake(number);   
  169.                    player[x]=73;
  170.                 break;
  171.                   case 95:
  172.                   Snake(number);   
  173.                   player[x]=75;
  174.                 break;
  175.                  case 98:
  176.                   Snake(number);   
  177.                   player[x]=79;
  178.                 break;
  179.                   }
  180.                 cin.get();
  181.                cout<<"Player "<<number<<" had reached"<<player[x]<<"!\n\n";    // displays usres' position
  182.                 cin.get();
  183.                 next = x+2;
  184.                }
  185.             if (player[x]==100)                      // exits loops when one of the players reach 100
  186.             {
  187.             winner (number, name[x]);
  188.             break;
  189.             }
  190.         if (next==n+1)
  191.                next=1;
  192.                cout<<"\n\n\n\nPlayer "<<next<<"'s turn!!!\n\n\n\n\n\n";
  193.         }
  194.     if (toupper(dice)=='Q')
  195.     break;
  196.   }
  197. while(player[x]!=100);                 
  198. }                                





  199. void Line()
  200. {
  201. cout<<"-------------------------------------------------------------------------------\n";
  202. }

  203. void Ladder(int number)
  204. {
  205. cin.get();
  206. cout<<"\n\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH_LADDER_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n\n";
  207. cout<<"\nLook!! There is a ladder over there!\n";
  208. cin.get();
  209. cout<<"Player "<<number<<" climb up the ladder.\n";
  210. }

  211. void Snake(int number)
  212. {
  213. cin.get();
  214. cout<<"\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SNAKE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
  215. cout<<"\nWoot!! Player "<<number<<" meets an anaconda!!!!\n";
  216. cin.get();
  217. cout<<"In order to escape from the snake, player "<<number<<" had run away.\n";
  218. }

  219. void wall()
  220. {
  221. cout<<"\n\n`````````````````````````````````WALL``````````````````````````````````````````\n\n";
  222. }

  223. void winner(int number,char name[] )
  224. {
  225. cin.get();
  226. cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
  227. cout << "\n"<<name<<" Player  [" <<number<< "] win!!!!\n\nCongratulation!!!\n\n"<<endl;
  228. cout<<"----------------------------------WINNER---------------------------------------\n";
  229. }

  230. bool check_number( string input )
  231. {
  232.     int i;

  233.     for( i = 0; i < input.length(); i++ )
  234.         if( isdigit( input[i] ) == false )
  235.             return false;

  236.     return true;
  237. }
复制代码
回复

使用道具 举报

 楼主| 发表于 8-10-2008 06:10 PM | 显示全部楼层
那个string迟一些再说, 现在我能够run到key-in 名字那边了, 可是没得key-in player 1

他直接跳过player 2了。


还有就是到了winner那边的时候, display不到player的名字。。。。。

[ 本帖最后由 Wongkokchoy 于 8-10-2008 06:44 PM 编辑 ]
回复

使用道具 举报

发表于 8-10-2008 07:54 PM | 显示全部楼层
cout<<"The 1st player who reach 100 first will be the WINNER!!!!!!\n";
    Line();                                        // function call

    do                                                // do-while loop to avoid user to key in negative value or zero
    {
    cout<<"\nPlease enter the numbers of players:\n";
    cin>>input;
    cin.get(); // 加入这行
回复

使用道具 举报

 楼主| 发表于 8-10-2008 11:00 PM | 显示全部楼层
原来是cin.get();搞到酱的问题出来

那个check value 的我暂时放在一边,问了lecturer才打算要不要放下去。

谢谢onlylonly大大。 你的考试考得怎样?
回复

使用道具 举报

 楼主| 发表于 9-10-2008 09:58 AM | 显示全部楼层
再来一个问题:
请问我要如何才能把一个array的value copy去另一个array?

比如array是price[x] 和 model[x]

是不是model[x]=price[x]?
回复

使用道具 举报


ADVERTISEMENT

发表于 9-10-2008 12:26 PM | 显示全部楼层

回复 116# Wongkokchoy 的帖子

用for loop

将 a copy 去 b,
  1. int a[]= {3,2,1};
  2. int b[3];
  3. int i;

  4. for( i = 0; i < sizeof(a)/sizeof(int); i++)
  5.      b[i] = a[i];
复制代码
或是直接
  1. for( i = 0; i < 3; i++)
  2.      b[i] = a[i];
复制代码
[/code]

[ 本帖最后由 onlylonly 于 9-10-2008 12:28 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 9-10-2008 12:37 PM | 显示全部楼层
#include <iostream.h>

void main()
{
int c, a[10], b[10];

for (int x=0; x<10; x++)
{
c=x;
c+=2;
a[x]=c;
b[x]=a[x];
cout<<"c="<<c<<"\t";
}

for (int x=0; x<10; x++)
{
cout<<"a="<<a[x]<<"\t";
cout<<"b="<<b[x]<<"\t";
cin.get();
}
}

我试着做了, 可是cout a[x] 和 b[x]的时候只有 c=2的value被display出来,其他的不见了。
回复

使用道具 举报

 楼主| 发表于 9-10-2008 12:41 PM | 显示全部楼层
哎呀,cin,.get 在作怪。 还没睡醒醒。。。。
回复

使用道具 举报

发表于 9-10-2008 01:11 PM | 显示全部楼层

回复 119# Wongkokchoy 的帖子

cin.get() 我放下去是为了清除 newline character, 也就是enter key。 其余的时候是不必的。

在c 里面可以 scanf( "< space >%d", &variable);
但是在c++ 里面好像没有这样的 ( 应该是有,不过我还不懂, 说以我就用cin.get()

--------------

没错的话可以用 cin.ignore() , 不过不太会用, 找天要好好去研究他

[ 本帖最后由 onlylonly 于 9-10-2008 01:24 PM 编辑 ]
回复

使用道具 举报

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

本版积分规则

 

ADVERTISEMENT


本周最热论坛帖子本周最热论坛帖子

ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


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

GMT+8, 11-2-2026 09:16 AM , Processed in 0.749742 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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