|
|
发表于 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;
}
还有- 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";
- }
复制代码
改成
- 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";
- }
复制代码
[ 本帖最后由 onlylonly 于 8-10-2008 04:43 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

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

楼主 |
发表于 8-10-2008 04:57 PM
|
显示全部楼层
好的。
- #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);
- void main()
- {
- char dice, *name[100]; // declaration of variable
- int A, n, number, next, 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 where there is a snake over there,the player will drop to a lower location.\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>>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)<<" is now at position "<<player[x]<<".\n"; // tell players their 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 the number 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<<" knocks the 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); // funciton call
- player[x]=42;
- break;
- case 28:
- Ladder(number); // funciton call
- player[x]=84;
- break;
- case 51:
- Ladder(number); // funciton call
- player[x]=67;
- break;
- case 71:
- Ladder(number); // funciton call
- player[x]=91;
- break;
- case 80:
- Ladder(number); // funciton call
- player[x]=100;
- break;
- case 17:
- Snake(number); // funciton call
- player[x]=7;
- break;
- case 62:
- Snake(number); // funciton call
- player[x]=19;
- break;
- case 54:
- Snake(number); // funciton call
- player[x]=34;
- break;
- case 64:
- Snake(number); // funciton call
- player[x]=60;
- break;
- case 87:
- Snake(number); // funciton call
- player[x]=24;
- break;
- case 93:
- Snake(number); // funciton call
- player[x]=73;
- break;
- case 95:
- Snake(number); // funciton call
- player[x]=75;
- break;
- case 98:
- Snake(number); // funciton call
- 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); // keep the looping untill a player reach 100
- } // end of main funciton
- // functions definition
- 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";
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 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 一起改。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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是这样- #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[]);
- int main()
- {
- char dice, **name; // declaration of variable
- int A, n, number, next, x, reverse, *player, i;
- 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 where there is a snake over there,the player will drop to a lower location.\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>>n;
- cin.get();
- if (n<=0)
- cout<<"INVALID number of player!\n";
- }
- while (n<=0);
- player=new int [n]; // declare array "player"
-
- name = new char* [n];
- for(i = 0; i < n ;i++)
- name[i] = new char[100];
- for (int x=0; x<n; x++)
- {
- cout<<"Please enter player "<<(x+1)<<"'s name.\n";
- cin.getline (name[x], 100);
- //cin.get();
- }
- 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)<<" is now at position "<<player[x]<<".\n"; // tell players their 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 the number 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<<" knocks the 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); // funciton call
- player[x]=42;
- break;
- case 28:
- Ladder(number); // funciton call
- player[x]=84;
- break;
- case 51:
- Ladder(number); // funciton call
- player[x]=67;
- break;
- case 71:
- Ladder(number); // funciton call
- player[x]=91;
- break;
- case 80:
- Ladder(number); // funciton call
- player[x]=100;
- break;
- case 17:
- Snake(number); // funciton call
- player[x]=7;
- break;
- case 62:
- Snake(number); // funciton call
- player[x]=19;
- break;
- case 54:
- Snake(number); // funciton call
- player[x]=34;
- break;
- case 64:
- Snake(number); // funciton call
- player[x]=60;
- break;
- case 87:
- Snake(number); // funciton call
- player[x]=24;
- break;
- case 93:
- Snake(number); // funciton call
- player[x]=73;
- break;
- case 95:
- Snake(number); // funciton call
- player[x]=75;
- break;
- case 98:
- Snake(number); // funciton call
- 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); // keep the looping untill a player reach 100
- } // end of main funciton
- // functions definition
- 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";
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 8-10-2008 06:04 PM
|
显示全部楼层
回复 108# Wongkokchoy 的帖子
跟着你最新的code修改的, 没错的话, 你应该是忘了 #include <string>,我没加下去- #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 );
- int main()
- {
- char dice, **name;
- int A, n, number, next, x, reverse, *player, i;
- string input;
- 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 = new char* [n];
- for( i = 0; i < n; i++)
- name[i] = new char [100];
- 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);
- 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[i] ) == false )
- return false;
- return true;
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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]? |
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 12:26 PM
|
显示全部楼层
回复 116# Wongkokchoy 的帖子
用for loop
将 a copy 去 b,- int a[]= {3,2,1};
- int b[3];
- int i;
- for( i = 0; i < sizeof(a)/sizeof(int); i++)
- b[i] = a[i];
复制代码 或是直接- for( i = 0; i < 3; i++)
- 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 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|